-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosin_test.go
531 lines (502 loc) · 11.1 KB
/
osin_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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package sqlite
import (
"database/sql"
"path/filepath"
"testing"
"time"
"github.com/carlmjohnson/be"
"github.com/go-ap/errors"
"github.com/openshift/osin"
)
type initFn func(db *sql.DB) error
func initializeOsinDb(t *testing.T, fns ...initFn) repo {
dbPath := filepath.Join(t.TempDir(), "storage.sqlite")
r := repo{path: dbPath, logFn: t.Logf, errFn: t.Errorf}
be.NilErr(t, bootstrapOsin(r))
be.NilErr(t, r.Open())
for _, fn := range fns {
be.NilErr(t, fn(r.conn))
}
return r
}
func Test_repo_Clone(t *testing.T) {
s := initializeOsinDb(t)
got := s.Clone()
cloned, ok := got.(*repo)
be.True(t, ok)
s.logFn = nil
s.errFn = nil
cloned.logFn = nil
cloned.errFn = nil
be.DeepEqual[*repo](t, &s, cloned)
}
func Test_repo_CreateClient(t *testing.T) {
tests := []struct {
name string
init []initFn
arg osin.Client
err error
}{
{
name: "empty",
arg: nil,
err: nilClientErr,
},
{
name: "default client",
arg: &osin.DefaultClient{Id: "test", Secret: "test", RedirectUri: "/"},
},
{
name: "default client with user data",
arg: &osin.DefaultClient{Id: "test", Secret: "test", RedirectUri: "/", UserData: "https://example.com"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.CreateClient(tt.arg)
checkErrorsEqual(t, tt.err, err)
if tt.arg == nil {
return
}
err = s.Open()
be.NilErr(t, err)
sel := "SELECT code, secret, redirect_uri, extra from clients where code=?;"
res, err := s.conn.Query(sel, tt.arg.GetId())
be.NilErr(t, err)
for res.Next() {
var code string
var secret string
var redir string
var extra []byte
err := res.Scan(&code, &secret, &redir, &extra)
be.NilErr(t, err)
be.Equal(t, tt.arg.GetId(), code)
be.Equal(t, tt.arg.GetSecret(), secret)
be.Equal(t, tt.arg.GetRedirectUri(), redir)
if tt.arg.GetUserData() != nil {
ud, err := assertToBytes(tt.arg.GetUserData())
be.NilErr(t, err)
be.DeepEqual(t, ud, extra)
}
}
})
}
}
func Test_repo_GetClient(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
want osin.Client
err error
}{
{
name: "missing",
arg: "missing",
err: errClientNotFound(nil),
},
{
name: "found",
init: []initFn{
func(db *sql.DB) error {
_, err := db.Exec(createClient, "found", "secret", "redirURI", any("extra123"))
return err
},
},
arg: "found",
want: &osin.DefaultClient{
Id: "found",
Secret: "secret",
RedirectUri: "redirURI",
UserData: any("extra123"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
got, err := s.GetClient(tt.arg)
checkErrorsEqual(t, tt.err, err)
if tt.want != nil {
be.DeepEqual(t, tt.want, got)
}
})
}
}
func Test_repo_ListClients(t *testing.T) {
tests := []struct {
name string
init []initFn
want []osin.Client
err error
}{
{
name: "missing",
want: []osin.Client{},
},
{
name: "found",
init: []initFn{
func(db *sql.DB) error {
_, err := db.Exec(createClient, "found", "secret", "redirURI", any("extra123"))
return err
},
},
want: []osin.Client{
&osin.DefaultClient{
Id: "found",
Secret: "secret",
RedirectUri: "redirURI",
UserData: any("extra123"),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
got, err := s.ListClients()
checkErrorsEqual(t, tt.err, err)
be.DeepEqual(t, tt.want, got)
})
}
}
var hellTimeStr = "2666-06-06 06:06:06"
var hellTime, _ = time.Parse("2006-01-02 15:04:05", hellTimeStr)
func Test_repo_LoadAccess(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
want *osin.AccessData
err error
}{
{
name: "empty",
arg: "",
want: nil,
err: errors.Newf("Empty access code"),
},
{
name: "find one",
arg: "one",
init: []initFn{
func(db *sql.DB) error {
db.Exec(createClient, "client", "secret", "redir", "extra123")
db.Exec(saveAuthorize, "client", "auth", "666", "scop", "redir", "state", hellTimeStr, "extra123")
_, err := db.Exec(saveAccess, "client", "auth", nil, "one", "ref", "666", "redir", "scope", hellTimeStr, "extra")
return err
},
},
want: &osin.AccessData{
Client: &osin.DefaultClient{
Id: "client",
Secret: "secret",
RedirectUri: "redir",
UserData: "extra123",
},
AuthorizeData: &osin.AuthorizeData{
Client: &osin.DefaultClient{
Id: "client",
Secret: "secret",
RedirectUri: "redir",
UserData: "extra123",
},
Code: "auth",
ExpiresIn: 666,
Scope: "scop",
RedirectUri: "redir",
State: "state",
CreatedAt: hellTime,
UserData: "extra123",
},
AccessToken: "one",
RefreshToken: "ref",
ExpiresIn: 666,
Scope: "scope",
RedirectUri: "redir",
CreatedAt: hellTime,
UserData: "extra",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
got, err := s.LoadAccess(tt.arg)
checkErrorsEqual(t, tt.err, err)
be.DeepEqual(t, tt.want, got)
})
}
}
func Test_repo_LoadAuthorize(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
want *osin.AuthorizeData
err error
}{
{
name: "empty",
arg: "",
want: nil,
err: errors.Newf("Empty authorize code"),
},
{
name: "find one",
arg: "one",
init: []initFn{
func(db *sql.DB) error {
db.Exec(createClient, "client", "secret", "redir", "extra123")
_, err := db.Exec(saveAuthorize, "client", "one", "666", "scop", "redir", "state", hellTimeStr, "extra123")
return err
},
},
want: &osin.AuthorizeData{
Client: &osin.DefaultClient{
Id: "client",
Secret: "secret",
RedirectUri: "redir",
UserData: "extra123",
},
Code: "one",
ExpiresIn: 666,
Scope: "scop",
RedirectUri: "redir",
State: "state",
CreatedAt: hellTime,
UserData: "extra123",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
got, err := s.LoadAuthorize(tt.arg)
checkErrorsEqual(t, tt.err, err)
be.DeepEqual(t, tt.want, got)
})
}
}
func Test_repo_LoadRefresh(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
want *osin.AccessData
err error
}{
{
name: "empty",
arg: "",
want: nil,
err: errors.Newf("Empty refresh code"),
},
{
name: "find refresh",
arg: "ref1",
init: []initFn{
func(db *sql.DB) error {
db.Exec(createClient, "client", "secret", "redir", "extra123")
db.Exec(saveAuthorize, "client", "auth", "666", "scop", "redir", "state", hellTimeStr, "extra123")
db.Exec(saveAccess, "client", "auth", nil, "one", "ref", "666", "redir", "scope", hellTimeStr, "extra")
_, err := db.Exec(saveRefresh, "ref1", "one")
return err
},
},
want: &osin.AccessData{
Client: &osin.DefaultClient{
Id: "client",
Secret: "secret",
RedirectUri: "redir",
UserData: "extra123",
},
AuthorizeData: &osin.AuthorizeData{
Client: &osin.DefaultClient{
Id: "client",
Secret: "secret",
RedirectUri: "redir",
UserData: "extra123",
},
Code: "auth",
ExpiresIn: 666,
Scope: "scop",
RedirectUri: "redir",
State: "state",
CreatedAt: hellTime,
UserData: "extra123",
},
AccessToken: "one",
RefreshToken: "ref",
ExpiresIn: 666,
Scope: "scope",
RedirectUri: "redir",
CreatedAt: hellTime,
UserData: "extra",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
got, err := s.LoadRefresh(tt.arg)
checkErrorsEqual(t, tt.err, err)
be.DeepEqual(t, tt.want, got)
})
}
}
func Test_repo_openOsin(t *testing.T) {
tests := []struct {
name string
init []initFn
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.Open()
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_RemoveAccess(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.RemoveAccess(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_RemoveAuthorize(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.RemoveAuthorize(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_RemoveClient(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.RemoveClient(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_RemoveRefresh(t *testing.T) {
tests := []struct {
name string
init []initFn
arg string
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.RemoveRefresh(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_SaveAccess(t *testing.T) {
tests := []struct {
name string
init []initFn
arg *osin.AccessData
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.SaveAccess(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_SaveAuthorize(t *testing.T) {
tests := []struct {
name string
init []initFn
arg *osin.AuthorizeData
err error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.SaveAuthorize(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}
func Test_repo_UpdateClient(t *testing.T) {
tests := []struct {
name string
init []initFn
arg osin.Client
err error
}{
{
name: "empty",
err: nilClientErr,
},
{
name: "basic",
init: []initFn{
func(db *sql.DB) error {
_, err := db.Exec(createClient, "found", "secret", "redirURI", any("extra123"))
return err
},
},
arg: &osin.DefaultClient{
Id: "found",
Secret: "secret",
RedirectUri: "redirURI",
UserData: any("extra123"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := initializeOsinDb(t, tt.init...)
err := s.UpdateClient(tt.arg)
checkErrorsEqual(t, tt.err, err)
})
}
}