forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups_test.go
111 lines (83 loc) · 2.26 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
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)
}
}