generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
listener_test.go
549 lines (511 loc) · 11.9 KB
/
listener_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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package listener
import (
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/webhook-schema"
"github.com/xmidt-org/wrp-listener/event"
)
type vador func(*assert.Assertions, *Listener)
type newTest struct {
description string
r webhook.Registration
noRegistration bool
noUrl bool
opt Option
opts []Option
check vador
checks []vador
expectedErr error
}
func vadorBody(assert *assert.Assertions, l *Listener) {
assert.NotNil(l.body)
}
func vadorAcceptedSecrets(ok ...string) vador {
return func(assert *assert.Assertions, l *Listener) {
if ok == nil {
ok = []string{}
}
assert.Equal(ok, l.acceptedSecrets)
}
}
var validWHR = webhook.Registration{
Duration: webhook.CustomDuration(5 * time.Minute),
}
// TestNew is focused on validating the input to New and not all the forms of
// options.
func TestNew(t *testing.T) {
tests := []newTest{
{
description: "empty is not ok",
expectedErr: ErrInput,
}, {
description: "no url fails",
r: validWHR,
noUrl: true,
expectedErr: ErrInput,
}, {
description: "nil registration fails",
noRegistration: true,
expectedErr: ErrInput,
}, {
description: "nearly empty is ok",
r: validWHR,
checks: []vador{
vadorBody,
vadorAcceptedSecrets(),
},
}, {
description: "nearly empty with an interval is ok",
opt: Interval(5 * time.Minute),
r: validWHR,
checks: []vador{
vadorBody,
vadorAcceptedSecrets(),
},
}, {
description: "nearly empty with an invalid interval",
opt: Interval(-5 * time.Minute),
expectedErr: ErrInput,
},
}
commonNewTest(t, tests)
}
func commonNewTest(t *testing.T, tests []newTest) {
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
r := tc.r
opts := make([]Option, 0, len(tc.opts)+1)
opts = append(opts, tc.opt)
opts = append(opts, tc.opts...)
url := "http://example.com"
if tc.noUrl {
url = ""
}
rPtr := &r
if tc.noRegistration {
rPtr = nil
}
got, err := New(url, rPtr, opts...)
if tc.expectedErr != nil {
assert.Nil(got)
assert.ErrorIs(err, tc.expectedErr)
return
}
require.NotNil(got)
require.NoError(err)
checks := make([]vador, 0, len(tc.checks)+1)
checks = append(checks, tc.check)
checks = append(checks, tc.checks...)
for _, c := range checks {
if c != nil {
c(assert, got)
}
}
})
}
}
func TestTokenize(t *testing.T) {
tests := []struct {
description string
input http.Request
opt Option
opts []Option
expected Token
expectedErr error
event *event.Tokenize
}{
{
description: "basic test",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"sha1=12345"},
},
},
opt: AcceptSHA1(),
expected: token{
alg: "sha1",
principal: "12345",
},
event: &event.Tokenize{
Header: webpaHeader,
Algorithms: []string{"none", "sha1"},
Algorithm: "sha1",
},
}, {
description: "basic test, using the alternate header",
input: http.Request{
Header: http.Header{
xmidtHeader: []string{"sha1=12345"},
},
},
opt: AcceptSHA1(),
expected: token{
alg: "sha1",
principal: "12345",
},
event: &event.Tokenize{
Header: xmidtHeader,
Algorithms: []string{"none", "sha1"},
Algorithm: "sha1",
},
}, {
description: "multiple auth possible, choose the best",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"sha1=12345"},
},
},
opts: []Option{
AcceptSHA1(),
AcceptNoHash(),
},
expected: token{
alg: "sha1",
principal: "12345",
},
event: &event.Tokenize{
Header: webpaHeader,
Algorithms: []string{"none", "sha1"},
Algorithm: "sha1",
},
}, {
description: "no header with that name",
opt: AcceptNoHash(),
expected: token{
alg: "none",
principal: "",
},
event: &event.Tokenize{
Algorithms: []string{"none"},
Algorithm: "none",
},
}, {
description: "empty header",
input: http.Request{
Header: http.Header{
webpaHeader: []string{" "},
},
},
opt: AcceptNoHash(),
expected: token{
alg: "none",
principal: "",
},
event: &event.Tokenize{
Header: webpaHeader,
Algorithms: []string{"none"},
Algorithm: "none",
},
}, {
description: "malformed header",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"foo=="},
},
},
expectedErr: ErrInvalidTokenHeader,
event: &event.Tokenize{
Header: webpaHeader,
Err: ErrInvalidHeaderFormat,
},
}, {
description: "malformed header 2",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"foo"},
},
},
expectedErr: ErrInvalidTokenHeader,
event: &event.Tokenize{
Header: webpaHeader,
Err: ErrInvalidHeaderFormat,
},
}, {
description: "empty value",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"foo= "},
},
},
expectedErr: ErrInvalidTokenHeader,
event: &event.Tokenize{
Header: webpaHeader,
Err: ErrInvalidHeaderFormat,
},
}, {
description: "empty key",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"=foo"},
},
},
expectedErr: ErrInvalidTokenHeader,
event: &event.Tokenize{
Header: webpaHeader,
Err: ErrInvalidHeaderFormat,
},
}, {
description: "no matching key",
input: http.Request{
Header: http.Header{
webpaHeader: []string{"sha256=12345"},
},
},
expectedErr: ErrAlgorithmNotFound,
event: &event.Tokenize{
Header: webpaHeader,
Algorithms: []string{"none", "sha256"},
Err: ErrAlgorithmNotFound,
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
opts := append(tc.opts, tc.opt)
whl, err := New(
"http://example.com",
&webhook.Registration{
Duration: webhook.CustomDuration(5 * time.Minute),
},
opts...,
)
require.NotNil(whl)
require.NoError(err)
if tc.event != nil {
got := whl.AddTokenizeEventListener(event.TokenizeFunc(
func(e event.Tokenize) {
assert.Equal(tc.event.Header, e.Header)
assert.Equal(tc.event.Algorithms, e.Algorithms)
assert.Equal(tc.event.Algorithm, e.Algorithm)
assert.ErrorIs(e.Err, tc.event.Err)
}))
require.NotNil(got)
}
in := tc.input
got, err := whl.Tokenize(&in)
if tc.expectedErr != nil {
assert.Nil(got)
assert.ErrorIs(err, tc.expectedErr)
return
}
assert.NoError(err)
require.NotNil(got)
assert.Equal(tc.expected.Type(), got.Type())
assert.Equal(tc.expected.Principal(), got.Principal())
})
}
}
func TestAuthorize(t *testing.T) {
tests := []struct {
description string
input http.Request
token Token
opt Option
opts []Option
expectedErr error
event *event.Authorize
}{
{
description: "basic test",
input: http.Request{
Body: io.NopCloser(strings.NewReader("foo")),
},
opts: []Option{
AcceptSHA1(),
AcceptedSecrets("123456"),
},
token: token{
alg: "sha1",
principal: "f76a55b14b2b3bd08116b4ee857dd6439b507317",
},
event: &event.Authorize{
Algorithm: "sha1",
},
}, {
description: "basic test, signature does not match",
input: http.Request{
Body: io.NopCloser(strings.NewReader("foo")),
},
opts: []Option{
AcceptSHA1(),
AcceptedSecrets("123456"),
},
token: token{
alg: "sha1",
principal: "0000",
},
event: &event.Authorize{
Algorithm: "sha1",
Err: ErrInvalidSignature,
},
expectedErr: ErrInvalidSignature,
}, {
description: "empty body is ok",
input: http.Request{
Body: io.NopCloser(strings.NewReader("")),
},
opts: []Option{
AcceptSHA1(),
AcceptedSecrets("123456"),
},
token: token{
alg: "sha1",
principal: "823688dafca7393d24c871a2da98a84d8732e927",
},
event: &event.Authorize{
Algorithm: "sha1",
},
}, {
description: "no body, is ok",
opts: []Option{
AcceptSHA1(),
AcceptedSecrets("123456"),
},
token: token{
alg: "sha1",
principal: "823688dafca7393d24c871a2da98a84d8732e927",
},
event: &event.Authorize{
Algorithm: "sha1",
},
}, {
description: "invalid principle",
token: token{
alg: "sha1",
principal: "f", // invalid because it needs to be 2 characters.
},
expectedErr: ErrInvalidSignature,
event: &event.Authorize{
Err: ErrInvalidSignature,
},
}, {
description: "nil token",
expectedErr: ErrNoToken,
}, {
description: "no matching hash",
input: http.Request{
Body: io.NopCloser(strings.NewReader("foo")),
},
token: token{
alg: "sha1",
principal: "f0",
},
expectedErr: ErrNotAcceptedHash,
event: &event.Authorize{
Algorithm: "sha1",
Err: ErrNotAcceptedHash,
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
opts := append(tc.opts, tc.opt)
whl, err := New(
"http://example.com",
&webhook.Registration{
Duration: webhook.CustomDuration(5 * time.Minute),
},
opts...,
)
if tc.event != nil {
got := whl.AddAuthorizeEventListener(event.AuthorizeFunc(
func(e event.Authorize) {
assert.Equal(tc.event.Algorithm, e.Algorithm)
assert.ErrorIs(e.Err, tc.event.Err)
}))
require.NotNil(got)
}
require.NotNil(whl)
require.NoError(err)
in := tc.input
err = whl.Authorize(&in, tc.token)
if tc.expectedErr != nil {
assert.ErrorIs(err, tc.expectedErr)
return
}
assert.NoError(err)
})
}
}
func TestListener_Accept(t *testing.T) {
tests := []struct {
description string
opt Option
opts []Option
expectBefore []string
secrets []string
}{
{
description: "simple test",
secrets: []string{"foo"},
}, {
description: "simple test accepting secrets",
opt: AcceptedSecrets("bar"),
expectBefore: []string{"bar"},
secrets: []string{"foo"},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
r := validWHR
opts := append(tc.opts, tc.opt)
l, err := New("http://example.com", &r, opts...)
require.NotNil(l)
require.NoError(err)
if tc.expectBefore == nil {
tc.expectBefore = []string{}
}
assert.Equal(tc.expectBefore, l.acceptedSecrets)
l.Accept(tc.secrets)
assert.Equal(tc.secrets, l.acceptedSecrets)
})
}
}
func TestListener_String(t *testing.T) {
tests := []struct {
description string
opt Option
opts []Option
str string
}{
{
description: "simple test",
str: "Listener(URL(http://example.com))",
}, {
description: "simple test",
opt: AcceptedSecrets("bar"),
str: "Listener(URL(http://example.com), AcceptedSecrets(***))",
}, {
description: "simple test",
opts: []Option{AcceptedSecrets("bar"), AcceptSHA1()},
str: "Listener(URL(http://example.com), AcceptedSecrets(***), AcceptSHA1())",
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
r := validWHR
opts := append(tc.opts, tc.opt)
l, err := New("http://example.com", &r, opts...)
require.NotNil(l)
require.NoError(err)
assert.Equal(tc.str, l.String())
})
}
}