-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
214 lines (194 loc) · 5.37 KB
/
client_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package client_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"gitlab.bytemark.co.uk/auth/client"
)
func withHandledClient(t *testing.T, h func(w http.ResponseWriter, r *http.Request), f func(client *client.Client)) {
ts := httptest.NewServer(http.HandlerFunc(h))
defer ts.Close()
client, err := client.New(ts.URL)
if err != nil {
t.Fatal(err)
}
f(client)
}
func withTestClient(t *testing.T, f func(client *client.Client)) {
withHandledClient(t, FixturesHandler, f)
}
func withSlowTestClient(t *testing.T, f func(client *client.Client)) {
withHandledClient(t, SlowHandler, f)
}
func TestNewRejectsNonHTTPchemes(t *testing.T) {
x, err := client.New("ftp://example.com")
if x != nil {
t.Error("did not expect a client to be returned")
}
if err == nil {
t.Fatal("expected an error to be returned")
}
if !strings.Contains(err.Error(), "scheme") {
t.Errorf("unexpected error: %s", err.Error())
}
}
func TestHandlesTrickyEndpointURLs(t *testing.T) {
t.Skip("TODO")
}
func cmpStringArrays(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func cmpSession(t *testing.T, a, b *client.SessionData) {
if a.Token != b.Token {
t.Errorf("unexpected Token %s", a.Token)
}
if a.Username != b.Username {
t.Errorf("unexpected Username %s", a.Token)
}
if !cmpStringArrays(a.Factors, b.Factors) {
t.Errorf("unexpected Factors %v", a.Factors)
}
if !cmpStringArrays(a.GroupMemberships, b.GroupMemberships) {
t.Errorf("unexpected GroupMemberships %v", a.Factors)
}
}
func TestReadSession(t *testing.T) {
withTestClient(t, func(c *client.Client) {
session, err := c.ReadSession(context.Background(), "good-session")
if err != nil {
t.Fatal(err)
}
cmpSession(t, session, fSessions["good-session"])
})
}
func TestReadSessionCancellation(t *testing.T) {
withSlowTestClient(t, func(c *client.Client) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
session, err := c.ReadSession(ctx, "good-session")
if session != nil {
t.Error("no session should be returned")
}
if err == nil {
t.Fatal("expected an error")
}
if !strings.Contains(err.Error(), "context canceled") {
t.Errorf("unexpected error: %v", err)
}
})
}
func TestCreateSession(t *testing.T) {
withTestClient(t, func(c *client.Client) {
session, err := c.CreateSession(context.Background(), fCreds["good-user"])
if err != nil {
t.Fatal(err)
}
cmpSession(t, session, fSessions["good-session"])
})
}
func TestCreateSessionCancellation(t *testing.T) {
withSlowTestClient(t, func(c *client.Client) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
session, err := c.CreateSession(ctx, fCreds["good-user"])
if session != nil {
t.Error("no session should be returned")
}
if err == nil {
t.Fatal("expected an error")
}
if !strings.Contains(err.Error(), "context canceled") {
t.Errorf("unexpected error: %v", err)
}
})
}
func TestCreateSessionWithBadCredentials(t *testing.T) {
withTestClient(t, func(c *client.Client) {
session, err := c.CreateSession(context.Background(), client.Credentials{"username": "bad-user", "password": "foo"})
if err == nil {
t.Error("expected an error")
}
if session != nil {
t.Error("no session should be returned")
}
})
}
func TestCreateSessionToken(t *testing.T) {
withTestClient(t, func(c *client.Client) {
token, err := c.CreateSessionToken(context.Background(), fCreds["good-user"])
if err != nil {
t.Fatal(err)
}
if token != fSessions["good-session"].Token {
t.Errorf("unexpected token %s", token)
}
})
}
func TestCreateSessionTokenCancellation(t *testing.T) {
withSlowTestClient(t, func(c *client.Client) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
token, err := c.CreateSessionToken(ctx, fCreds["good-user"])
if token != "" {
t.Error("no token should be returned")
}
if err == nil {
t.Fatal("expected an error")
}
if !strings.Contains(err.Error(), "context canceled") {
t.Errorf("unexpected error: %v", err)
}
})
}
func TestCreateSessionTokenWithBadCredentials(t *testing.T) {
withTestClient(t, func(c *client.Client) {
session, err := c.CreateSession(context.Background(), client.Credentials{"username": "bad-user", "password": "foo"})
if err == nil {
t.Error("expected an error")
}
if session != nil {
t.Error("no session should be returned")
}
})
}
func TestCreateImpersonatedSessionTokenWithGoodToken(t *testing.T) {
withTestClient(t, func(c *client.Client) {
token, err := c.CreateImpersonatedSessionToken(context.Background(), "good-session", "impersonated")
if err != nil {
t.Fatal(err)
}
if token != "impersonated-session" {
t.Errorf("unexpected token %s", token)
}
})
}
func TestCreateImpersonatedSessionTokenWithBadToken(t *testing.T) {
withTestClient(t, func(c *client.Client) {
token, err := c.CreateImpersonatedSessionToken(context.Background(), "bad-session", "impersonated")
if err == nil {
t.Error("expected an error")
}
if token != "" {
t.Error("no token should be returned")
}
})
}
func TestCreateImpersonatedSession(t *testing.T) {
withTestClient(t, func(c *client.Client) {
session, err := c.CreateImpersonatedSession(context.Background(), "good-session", "impersonated")
if err != nil {
t.Fatal(err)
}
cmpSession(t, session, fSessions["impersonated-session"])
})
}