-
Notifications
You must be signed in to change notification settings - Fork 14
/
groups_test.go
184 lines (141 loc) · 4.02 KB
/
groups_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package keycloak
import (
"context"
"net/http"
"strings"
"testing"
)
// create a new group.
func createGroup(t *testing.T, k *Keycloak, realm string, groupName string) string {
t.Helper()
group := &Group{
Name: String(groupName),
}
res, err := k.Groups.Create(context.Background(), realm, group)
if err != nil {
t.Errorf("Groups.Create returned error: %v", err)
}
parts := strings.Split(res.Header.Get("Location"), "/")
groupID := parts[len(parts)-1]
return groupID
}
func TestGroupsService_Create(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
ctx := context.Background()
group := &Group{
Name: String("mygroup"),
}
res, err := k.Groups.Create(ctx, realm, group)
if err != nil {
t.Errorf("Groups.Create returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
}
func TestGroupsService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
createGroup(t, k, realm, "group_a")
createGroup(t, k, realm, "group_b")
groups, res, err := k.Groups.List(context.Background(), realm)
if err != nil {
t.Errorf("Groups.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if len(groups) != 2 {
t.Errorf("got: %d, want: %d", len(groups), 2)
}
}
func TestGroupsService_Get(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
groupID := createGroup(t, k, realm, "group")
group, res, err := k.Groups.Get(context.Background(), realm, groupID)
if err != nil {
t.Errorf("Groups.Get returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *group.Name != "group" {
t.Errorf("got: %s, want: %s", *group.Name, "group")
}
}
func TestGroupsService_Delete(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
groupID := createGroup(t, k, realm, "group")
res, err := k.Groups.Delete(context.Background(), realm, groupID)
if err != nil {
t.Errorf("Groups.Delete returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestGroupsService_AddRealmRoles(t *testing.T) {
k := client(t)
realm := "first"
roleNames := [2]string{"role1", "role2"}
createRealm(t, k, realm)
for _, r := range roleNames {
createRealmRole(t, k, realm, r)
}
groupID := createGroup(t, k, realm, "group")
ctx := context.Background()
// get role in order to assign it
var roles []*Role
for _, r := range roleNames {
role, _, err := k.RealmRoles.GetByName(ctx, realm, r)
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
roles = append(roles, role)
}
res, err := k.Groups.AddRealmRoles(ctx, realm, groupID, roles)
if err != nil {
t.Errorf("Groups.AddRealmRoles returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestGroupsService_RemoveRealmRoles(t *testing.T) {
k := client(t)
realm := "first"
roleNames := [2]string{"role1", "role2"}
createRealm(t, k, realm)
for _, r := range roleNames {
createRealmRole(t, k, realm, r)
}
groupID := createGroup(t, k, realm, "group")
ctx := context.Background()
// get role in order to assign it
var roles []*Role
for _, r := range roleNames {
role, _, err := k.RealmRoles.GetByName(ctx, realm, r)
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
roles = append(roles, role)
}
_, err := k.Groups.AddRealmRoles(ctx, realm, groupID, roles)
if err != nil {
t.Errorf("Groups.AddRealmRoles returned error: %v", err)
}
res, err := k.Groups.RemoveRealmRoles(ctx, realm, groupID, roles)
if err != nil {
t.Errorf("Groups.RemoveRealmRoles returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}