-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenopts_test.go
94 lines (88 loc) · 2.07 KB
/
tokenopts_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
package prototokens
import (
"fmt"
"testing"
"time"
tokenpb "github.com/lusis/prototokens/proto/gen/go/prototokens/v1"
"github.com/stretchr/testify/require"
)
func failingtokenopt() TokenOpt {
return func(pt *tokenpb.ProtoToken) error {
return fmt.Errorf("snarf!")
}
}
func TestTokenOpts(t *testing.T) {
testCases := map[string]struct {
err bool
opts []TokenOpt
}{
"all-opts": {
opts: []TokenOpt{
WithID(t.Name()),
WithUsages(tokenpb.TokenUsages_TOKEN_USAGES_HUMAN, tokenpb.TokenUsages_TOKEN_USAGES_EXCHANGE),
WithSID(t.Name() + "_sid"),
WithVendor([]byte("vendor data")),
},
},
"invalid-id": {
err: true,
opts: []TokenOpt{
WithID(""),
},
},
"invalid-sid": {
err: true,
opts: []TokenOpt{
WithSID(""),
},
},
"invalid-usages": {
err: true,
opts: []TokenOpt{
WithUsages(),
},
},
"invalid-vendor": {
err: true,
opts: []TokenOpt{
WithVendor(nil),
},
},
"custom-option": {
err: true,
opts: []TokenOpt{
failingtokenopt(),
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
pt, err := New(5*time.Minute, tc.opts...)
if tc.err {
require.Error(t, err, "should error")
require.Nil(t, pt, "token should be nil")
} else {
require.NoError(t, err, "should not error")
require.NotNil(t, pt, "token should not be nil")
}
})
}
}
func TestTokenOptsOverwrite(t *testing.T) {
testCases := map[string]TokenOpt{
"id": WithID(t.Name()),
"sid": WithSID(t.Name()),
"usages": WithUsages(tokenpb.TokenUsages_TOKEN_USAGES_HUMAN),
"vendor": WithVendor([]byte(t.Name())),
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
// bascially we attempt to pass in the same param twice which should trigger an overwrite error
// we don't care if it's the same or not, we shouldn't allow it
pt, err := New(5*time.Minute, tc, tc)
require.ErrorIs(t, err, ErrOverwrite, "should be an overwrite error")
require.Contains(t, err.Error(), n, "field should be in error message")
require.Nil(t, pt, "token should be nil")
})
}
}