-
Notifications
You must be signed in to change notification settings - Fork 64
/
user_test.go
375 lines (336 loc) · 10.3 KB
/
user_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package chef
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"testing"
"github.com/r3labs/diff"
)
var (
testUserJSON = "test/user.json"
testVerboseUserJSON = "test/verbose_user.json"
)
func TestUserFromJSONDecoder(t *testing.T) {
if file, err := os.Open(testUserJSON); err != nil {
t.Error("Unexpected error '", err, "' during os.Open on", testUserJSON)
} else {
dec := json.NewDecoder(file)
var g User
if err := dec.Decode(&g); err == io.EOF {
log.Fatal(g)
} else if err != nil {
log.Fatal(err)
}
}
}
func TestVerboseUserFromJSONDecoder(t *testing.T) {
if file, err := os.Open(testVerboseUserJSON); err != nil {
t.Error("Unexpected error '", err, "' during os.Open on", testVerboseUserJSON)
} else {
dec := json.NewDecoder(file)
var g User
if err := dec.Decode(&g); err == io.EOF {
log.Fatal(g)
} else if err != nil {
log.Fatal(err)
}
}
}
func TestUserslist(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{ "user_name1": "https://url/for/user_name1", "user_name2": "https://url/for/user_name2"}`)
}
})
// Test list
users, err := client.Users.List()
if err != nil {
t.Errorf("Users.List returned error: %v", err)
}
listWant := map[string]string{"user_name1": "https://url/for/user_name1", "user_name2": "https://url/for/user_name2"}
if !reflect.DeepEqual(users, listWant) {
t.Errorf("Users.List returned %+v, want %+v", users, listWant)
}
}
func TestVerboseUserslist(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{
"janechef": { "email": "[email protected]", "first_name": "jane", "last_name": "chef_user" },
"yaelsmith": { "email": "[email protected]", "first_name": "yael", "last_name": "smith" }
}`)
}
})
// Test list
users, err := client.Users.VerboseList()
if err != nil {
t.Errorf("Verbose Users.List returned error: %v", err)
}
jane := UserVerboseResult{Email: "[email protected]", FirstName: "jane", LastName: "chef_user"}
yael := UserVerboseResult{Email: "[email protected]", FirstName: "yael", LastName: "smith"}
listWant := map[string]UserVerboseResult{"janechef": jane, "yaelsmith": yael}
if !reflect.DeepEqual(users, listWant) {
t.Errorf("Verbose Users.List returned %+v, want %+v", users, listWant)
}
}
func TestUserCreate(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "POST":
fmt.Fprintf(w, `{
"uri": "https://chefserver/users/user_name1",
"chef_key": {
"name": "default",
"public_key": "-----BEGIN RSA PUBLIC KEY-----",
"expiration_date": "infinity",
"uri": "https://chefserver/users/user_name1/keys/default",
"private_key": "-----BEGIN RSA PRIVATE KEY-----"
}
}`)
}
})
// Create User
user := User{UserName: "user_name1", Email: "[email protected]", Password: "dummypass"}
userresult, err := client.Users.Create(user)
if err != nil {
t.Errorf("Users.Create returned error: %v", err)
}
Want := UserResult{Uri: "https://chefserver/users/user_name1",
ChefKey: ChefKey{
Name: "default",
PublicKey: "-----BEGIN RSA PUBLIC KEY-----",
ExpirationDate: "infinity",
Uri: "https://chefserver/users/user_name1/keys/default",
PrivateKey: "-----BEGIN RSA PRIVATE KEY-----",
},
}
if !reflect.DeepEqual(userresult, Want) {
t.Errorf("Users.Create returned %+v, want %+v", userresult, Want)
}
}
func TestUserGet(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{
"username": "user1",
"display_name": "User Name",
"email": "[email protected]",
"external_authentication_uid": "user1",
"first_name": "User",
"last_name": "Name",
"middle_name": "S",
"public_key": "-----BEGIN RSA PUBLIC KEY-----",
"recovery_authentication_enabled": true
}`)
}
})
// Get User
user, err := client.Users.Get("user1")
if err != nil {
t.Errorf("User.Get returned error: %v", err)
}
Want := User{UserName: "user1", DisplayName: "User Name", Email: "[email protected]", ExternalAuthenticationUid: "user1", FirstName: "User", LastName: "Name", MiddleName: "S", PublicKey: "-----BEGIN RSA PUBLIC KEY-----", RecoveryAuthenticationEnabled: true}
if !reflect.DeepEqual(user, Want) {
t.Errorf("Users.Get returned %+v, want %+v", user, Want)
}
}
func TestUserDelete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "DELETE":
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
}`)
}
})
// DELETE User
err := client.Users.Delete("user1")
if err != nil {
t.Errorf("User.Get returned error: %v", err)
}
}
func TestUserUpdate(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user_name1", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "PUT":
fmt.Fprintf(w, `{
"uri": "https://chefserver/users/user_name1"
}`)
}
})
// Update User
user := User{UserName: "user_name1", Email: "[email protected]", Password: "dummypass"}
userresult, err := client.Users.Update("user_name1", user)
if err != nil {
t.Errorf("Users.Update returned error: %v", err)
}
Want := UserResult{Uri: "https://chefserver/users/user_name1"}
if !reflect.DeepEqual(userresult, Want) {
t.Errorf("Users.Update returned %+v, want %+v", userresult, Want)
}
}
func TestListKeys(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1/keys", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `[
{
"name": "default",
"uri": "https://chefserver/users/user1/keys/default",
"expired": false
}
]`)
}
})
keyresult, err := client.Users.ListKeys("user1")
if err != nil {
t.Errorf("Users.ListKeys returned error: %v", err)
}
defaultItem := KeyItem{
Name: "default",
Uri: "https://chefserver/users/user1/keys/default",
Expired: false,
}
Want := []KeyItem{defaultItem}
if !reflect.DeepEqual(keyresult, Want) {
t.Errorf("Users.ListKeys returned %+v, want %+v", keyresult, Want)
}
}
func TestAddKey(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1/keys", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "POST":
fmt.Fprintf(w, `{
"name": "newkey",
"uri": "https://chefserver/users/user1/keys/newkey",
"expired": false
}`)
}
})
keyadd := AccessKey{
Name: "newkey",
PublicKey: "RSA KEY",
ExpirationDate: "infinity",
}
keyresult, err := client.Users.AddKey("user1", keyadd)
if err != nil {
t.Errorf("Users.AddKey returned error: %v", err)
}
Want := KeyItem{
Name: "newkey",
Uri: "https://chefserver/users/user1/keys/newkey",
Expired: false,
}
if !reflect.DeepEqual(keyresult, Want) {
t.Errorf("Users.AddKey returned %+v, want %+v", keyresult, Want)
}
}
func TestDeleteKey(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1/keys/newkey", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "DELETE":
fmt.Fprintf(w, `{
"name": "newkey",
"public_key": "RSA KEY",
"expiration_date": "infinity"
}`)
}
})
keyresult, err := client.Users.DeleteKey("user1", "newkey")
if err != nil {
t.Errorf("Users.DeleteKey returned error: %v", err)
}
Want := AccessKey{
Name: "newkey",
PublicKey: "RSA KEY",
ExpirationDate: "infinity",
}
if !reflect.DeepEqual(keyresult, Want) {
diff, _ := diff.Diff(keyresult, Want)
t.Errorf("Users.DeleteKey returned %+v, want %+v, differences %+v", keyresult, Want, diff)
}
}
func TestGetKey(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1/keys/newkey", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{
"name": "newkey",
"public_key": "RSA KEY",
"expiration_date": "infinity"
}`)
}
})
keyresult, err := client.Users.GetKey("user1", "newkey")
if err != nil {
t.Errorf("Users.GetKey returned error: %v", err)
}
Want := AccessKey{
Name: "newkey",
PublicKey: "RSA KEY",
ExpirationDate: "infinity",
}
if !reflect.DeepEqual(keyresult, Want) {
diff, _ := diff.Diff(keyresult, Want)
t.Errorf("Users.GetKey returned %+v, want %+v, differences %+v", keyresult, Want, diff)
}
}
func TestUpdateKey(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/user1/keys/newkey", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "PUT":
fmt.Fprintf(w, `{
"name": "newkey",
"public_key": "RSA NEW KEY",
"expiration_date": "infinity"
}`)
}
})
updKey := AccessKey{
Name: "newkey",
PublicKey: "RSA NEW KEY",
ExpirationDate: "infinity",
}
keyresult, err := client.Users.UpdateKey("user1", "newkey", updKey)
if err != nil {
t.Errorf("Users.UpdateKey returned error: %v", err)
}
Want := AccessKey{
Name: "newkey",
PublicKey: "RSA NEW KEY",
ExpirationDate: "infinity",
}
if !reflect.DeepEqual(keyresult, Want) {
diff, _ := diff.Diff(keyresult, Want)
t.Errorf("Users.UpdateKey returned %+v, want %+v, differences %+v", keyresult, Want, diff)
}
}