forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources_test.go
133 lines (99 loc) · 3.02 KB
/
resources_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
package keycloak
import (
"context"
"net/http"
"testing"
)
// create a new resource.
func createResource(t *testing.T, k *Keycloak, realm, clientID, name string) *Resource {
t.Helper()
resource := &Resource{
Name: String(name),
DisplayName: String(name),
}
created, _, err := k.Resources.Create(context.Background(), realm, clientID, resource)
if err != nil {
t.Errorf("Resources.Create returned error: %v", err)
}
return created
}
func TestResourcesService_Create(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
ctx := context.Background()
resource := &Resource{
Name: String("resource"),
DisplayName: String("resource"),
}
created, res, err := k.Resources.Create(ctx, realm, clientID, resource)
if err != nil {
t.Errorf("Clients.CreateResource returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
if *created.Name != "resource" {
t.Errorf("got: %s, want: %s", *resource.Name, "Default Resource")
}
}
func TestResourcesService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
resources, res, err := k.Resources.List(context.Background(), realm, clientID)
if err != nil {
t.Errorf("Clients.ListResources returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if len(resources) != 1 {
t.Errorf("got: %d, want: %d", len(resources), 1)
}
}
func TestResourcesService_Get(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
// list all resources first
resources, res, err := k.Resources.List(context.Background(), realm, clientID)
if err != nil {
t.Errorf("Clients.ListResources returned error: %v", err)
}
resource, res, err := k.Resources.Get(context.Background(), realm, clientID, *resources[0].ID)
if err != nil {
t.Errorf("Clients.GetResource returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *resource.Name != "Default Resource" {
t.Errorf("got: %s, want: %s", *resource.Name, "Default Resource")
}
}
func TestResourcesService_Deletee(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
ctx := context.Background()
resource := &Resource{
Name: String("resource"),
DisplayName: String("resource"),
}
created, res, err := k.Resources.Create(ctx, realm, clientID, resource)
if err != nil {
t.Errorf("Clients.CreateResource returned error: %v", err)
}
res, err = k.Resources.Delete(ctx, realm, clientID, *created.ID)
if err != nil {
t.Errorf("Clients.DeleteResource returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}