-
Notifications
You must be signed in to change notification settings - Fork 14
/
clients_test.go
157 lines (120 loc) · 3.67 KB
/
clients_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package keycloak
import (
"context"
"net/http"
"strings"
"testing"
)
// create a new client.
func createClient(t *testing.T, k *Keycloak, realm string, clientID string) string {
t.Helper()
client := &Client{
Enabled: Bool(true),
ClientID: String(clientID),
RedirectUris: []string{"http://localhost:4200/*"},
AuthorizationServicesEnabled: Bool(true),
ServiceAccountsEnabled: Bool(true),
PublicClient: Bool(false),
}
res, err := k.Clients.Create(context.Background(), realm, client)
if err != nil {
t.Errorf("Clients.Create returned error: %v", err)
}
parts := strings.Split(res.Header.Get("Location"), "/")
id := parts[len(parts)-1]
return id
}
func TestClientsService_Create(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
ctx := context.Background()
client := &Client{
Enabled: Bool(true),
ClientID: String("myclient"),
}
res, err := k.Clients.Create(ctx, realm, client)
if err != nil {
t.Errorf("Clients.Create returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
}
func TestClientsService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clients, res, err := k.Clients.List(context.Background(), realm)
if err != nil {
t.Errorf("Clients.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
// it includes the "account", "account-console", "admin-cli", "broker", "realm-management", "security-admin-console"
if len(clients) != 6 {
t.Errorf("got: %d, want: %d", len(clients), 6)
}
}
func TestClientsService_Get(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
client, res, err := k.Clients.Get(context.Background(), realm, clientID)
if err != nil {
t.Errorf("Clients.Get returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *client.ClientID != "client" {
t.Errorf("got: %s, want: %s", *client.ClientID, "client")
}
}
func TestClientsService_GetSecret(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
credential, res, err := k.Clients.GetSecret(context.Background(), realm, clientID)
if err != nil {
t.Errorf("Clients.Get returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *credential.Type != "secret" {
t.Errorf("got: %s, want: %s", *credential.Type, "secret")
}
}
func TestClientsService_CreateSecret(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
ctx := context.Background()
credential, res, err := k.Clients.GetSecret(ctx, realm, clientID)
if err != nil {
t.Errorf("Clients.Get returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
if *credential.Type != "secret" {
t.Errorf("got: %s, want: %s", *credential.Type, "secret")
}
// create a new secret
next, _, err := k.Clients.CreateSecret(ctx, realm, clientID)
if err != nil {
t.Errorf("Clients.CreateSecret returned error: %v", err)
}
// make sure it is a different one
if *next.Type != "secret" {
t.Errorf("got: %s, want: %s", *next.Type, "secret")
}
if credential.Value == next.Value {
t.Errorf("got: %t, want: %t", credential.Value == next.Value, credential.Value != next.Value)
}
}