forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scopes_test.go
129 lines (100 loc) · 3 KB
/
scopes_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
package keycloak
import (
"context"
"net/http"
"testing"
)
func createScope(t *testing.T, k *Keycloak, realm, clientID, name string) *Scope {
t.Helper()
scope := &Scope{
Name: String(name),
}
scope, _, err := k.Scopes.Create(context.Background(), realm, clientID, scope)
if err != nil {
t.Errorf("Scopes.Create returned error: %v", err)
}
return scope
}
func TestScopesService_Create(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
scope := &Scope{
Name: String("scope_read"),
}
scope, res, err := k.Scopes.Create(context.Background(), realm, clientID, scope)
if err != nil {
t.Errorf("Scopes.Create returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
if *scope.Name != "scope_read" {
t.Errorf("got: %s, want: %s", *scope.Name, "scope_read")
}
}
func TestScopesService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
createScope(t, k, realm, clientID, "read")
createScope(t, k, realm, clientID, "write")
scopes, res, err := k.Scopes.List(context.Background(), realm, clientID)
if err != nil {
t.Errorf("Scopes.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if len(scopes) != 2 {
t.Errorf("got: %d, want: %d", len(scopes), 2)
}
}
func TestScopesService_Get(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
scope := createScope(t, k, realm, clientID, "read")
scope, res, err := k.Scopes.Get(context.Background(), realm, clientID, *scope.ID)
if err != nil {
t.Errorf("Scopes.Get returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *scope.Name != "read" {
t.Errorf("got: %s, want: %s", *scope.Name, "read")
}
}
func TestScopesService_Delete(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
scope := createScope(t, k, realm, clientID, "read")
res, err := k.Scopes.Delete(context.Background(), realm, clientID, *scope.ID)
if err != nil {
t.Errorf("Scopes.Delete returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestScopesService_Update(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
scope := createScope(t, k, realm, clientID, "read")
scope.Name = String("updated")
res, err := k.Scopes.Update(context.Background(), realm, clientID, scope)
if err != nil {
t.Errorf("Scopes.Update returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}