forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realmRoles_test.go
119 lines (90 loc) · 2.64 KB
/
realmRoles_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
package keycloak
import (
"context"
"net/http"
"testing"
)
// create a new realm role.
func createRealmRole(t *testing.T, k *Keycloak, realm string, name string) {
t.Helper()
role := &Role{
Name: String(name),
Description: String(name + " description"),
}
if _, err := k.RealmRoles.Create(context.Background(), realm, role); err != nil {
t.Errorf("RealmRoles.Create returned error: %v", err)
}
}
func TestRealmRolesService_Create(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
ctx := context.Background()
role := &Role{
Name: String("my name"),
Description: String("my description"),
}
res, err := k.RealmRoles.Create(ctx, realm, role)
if err != nil {
t.Errorf("RealmRoles.Create returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
}
func TestRealmRolesService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
createRealmRole(t, k, realm, "first")
createRealmRole(t, k, realm, "second")
roles, res, err := k.RealmRoles.List(context.Background(), realm, nil)
if err != nil {
t.Errorf("RealmRoles.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
// it includes the "offline_access" and "uma_authorization" roles
if len(roles) != 4 {
t.Errorf("got: %d, want: %d", len(roles), 4)
}
}
func TestRealmRolesService_GetByName(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
createRealmRole(t, k, realm, "first")
role, res, err := k.RealmRoles.GetByName(context.Background(), realm, "first")
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *role.Name != "first" {
t.Errorf("got: %s, want: %s", *role.Name, "first")
}
}
func TestRealmRolesService_GetByID(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
createRealmRole(t, k, realm, "first")
// get by name first to get id
role, res, err := k.RealmRoles.GetByName(context.Background(), realm, "first")
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
// now get by id
role, res, err = k.RealmRoles.GetByID(context.Background(), realm, *role.ID)
if err != nil {
t.Errorf("RealmRoles.GetByID returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *role.Name != "first" {
t.Errorf("got: %s, want: %s", *role.Name, "first")
}
}