forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realms_test.go
136 lines (104 loc) · 3.02 KB
/
realms_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
package keycloak
import (
"context"
"net/http"
"testing"
)
// create a new realm and delete it afterwards.
func createRealm(t *testing.T, k *Keycloak, name string) {
t.Helper()
realm := &Realm{
Enabled: Bool(true),
ID: String(name),
Realm: String(name),
}
ctx := context.Background()
// create a new realm
if _, err := k.Realms.Create(ctx, realm); err != nil {
t.Errorf("Realms.Create returned error: %v", err)
}
t.Cleanup(func() {
if _, err := k.Realms.Delete(ctx, name); err != nil {
t.Errorf("Realms.Delete returned error: %v", err)
}
})
}
func TestRealmsService_Create(t *testing.T) {
k := client(t)
name := "supernice"
ctx := context.Background()
realm := &Realm{
Enabled: Bool(true),
ID: String(name),
Realm: String(name),
}
res, err := k.Realms.Create(ctx, realm)
if err != nil {
t.Errorf("Realms.Create returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
if res.Header.Get("Location") != "http://localhost:8080/auth/admin/realms/supernice" {
t.Errorf("got: %s, want: %s", res.Header.Get("Location"), "http://localhost:8080/auth/admin/realms/supernice")
}
// manually clean up
if _, err := k.Realms.Delete(ctx, name); err != nil {
t.Errorf("Realms.Delete returned error: %v", err)
}
}
func TestRealmsService_List(t *testing.T) {
k := client(t)
createRealm(t, k, "first")
createRealm(t, k, "second")
realms, res, err := k.Realms.List(context.Background())
if err != nil {
t.Errorf("Realms.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
// it includes the master realm
if len(realms) != 3 {
t.Errorf("got: %d, want: %d", len(realms), 3)
}
}
func TestRealmsService_Get(t *testing.T) {
k := client(t)
createRealm(t, k, "first")
realm, res, err := k.Realms.Get(context.Background(), "first")
if err != nil {
t.Errorf("Realms.Get returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *realm.ID != "first" {
t.Errorf("got: %s, want: %s", *realm.ID, "first")
}
}
func TestRealmsService_Delete(t *testing.T) {
k := client(t)
createRealm(t, k, "first")
res, err := k.Realms.Delete(context.Background(), "first")
if err != nil {
t.Errorf("Realms.Delete returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestRealmsService_GetConfig(t *testing.T) {
k := client(t)
createRealm(t, k, "first")
config, res, err := k.Realms.GetConfig(context.Background(), "first")
if err != nil {
t.Errorf("Realms.GetConfig returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *config.Issuer != "http://localhost:8080/auth/realms/first" {
t.Errorf("got: %s, want: %s", *config.Issuer, "http://localhost:8080/auth/realms/first")
}
}