forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies_test.go
153 lines (124 loc) · 3.78 KB
/
policies_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package keycloak
import (
"context"
"net/http"
"testing"
)
func createUserPolicy(t *testing.T, k *Keycloak, realm, clientID, userID string) *UserPolicy {
policy := &UserPolicy{
Policy: Policy{
Type: String("user"),
Logic: String(LogicPositive),
DecisionStrategy: String(DecisionStrategyUnanimous),
Name: String("policy"),
},
Users: []string{userID},
}
policy, _, err := k.Policies.CreateUserPolicy(context.Background(), realm, clientID, policy)
if err != nil {
t.Errorf("Policies.CreateUserPolicy returned error: %v", err)
}
return policy
}
func TestPoliciesService_CreateUserPolicy(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
userID := createUser(t, k, realm, "john")
policy := &UserPolicy{
Policy: Policy{
Type: String("user"),
Logic: String(LogicPositive),
DecisionStrategy: String(DecisionStrategyUnanimous),
Name: String("policy"),
},
Users: []string{userID},
}
policy, res, err := k.Policies.CreateUserPolicy(context.Background(), realm, clientID, policy)
if err != nil {
t.Errorf("Policies.CreateUserPolicy returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
if *policy.Name != "policy" {
t.Errorf("got: %s, want: %s", *policy.Name, "policy")
}
}
func TestPoliciesService_CreateRolePolicy(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
createRealmRole(t, k, realm, "role")
role, res, err := k.RealmRoles.GetByName(context.Background(), realm, "role")
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
policy := &RolePolicy{
Policy: Policy{
Type: String("role"),
Logic: String(LogicPositive),
DecisionStrategy: String(DecisionStrategyUnanimous),
Name: String("policy"),
},
Roles: []*RoleDefinition{{
ID: role.ID,
}},
}
policy, res, err = k.Policies.CreateRolePolicy(context.Background(), realm, clientID, policy)
if err != nil {
t.Errorf("Policies.CreateRolePolicy returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
if *policy.Name != "policy" {
t.Errorf("got: %s, want: %s", *policy.Name, "policy")
}
}
func TestPoliciesService_CreateGroupPolicy(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
groupID := createGroup(t, k, realm, "group")
policy := &GroupPolicy{
Policy: Policy{
Type: String("role"),
Logic: String(LogicPositive),
DecisionStrategy: String(DecisionStrategyUnanimous),
Name: String("policy"),
},
Groups: []*GroupDefinition{{
ID: &groupID,
}},
}
policy, res, err := k.Policies.CreateGroupPolicy(context.Background(), realm, clientID, policy)
if err != nil {
t.Errorf("Policies.CreateGroupPolicy returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
if *policy.Name != "policy" {
t.Errorf("got: %s, want: %s", *policy.Name, "policy")
}
}
func TestPoliciesService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
roles, res, err := k.Policies.List(context.Background(), realm, clientID)
if err != nil {
t.Errorf("Policies.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if len(roles) != 1 {
t.Errorf("got: %d, want: %d", len(roles), 1)
}
}