-
Notifications
You must be signed in to change notification settings - Fork 208
/
user_test.go
111 lines (89 loc) · 2.08 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
package authboss
import "testing"
func TestOAuth2PIDs(t *testing.T) {
t.Parallel()
provider := "provider"
uid := "uid"
pid := MakeOAuth2PID(provider, uid)
if pid != "oauth2;;provider;;uid" {
t.Error("pid was wrong:", pid)
}
gotProvider, gotUID := ParseOAuth2PIDP(pid)
if gotUID != uid {
t.Error("uid was wrong:", gotUID)
}
if gotProvider != provider {
t.Error("provider was wrong:", gotProvider)
}
notEnoughSegments, didntStartWithOAuth2 := false, false
func() {
defer func() {
if r := recover(); r != nil {
notEnoughSegments = true
}
}()
_, _ = ParseOAuth2PIDP("nope")
}()
if !notEnoughSegments {
t.Error("expected a panic when there's not enough segments")
}
func() {
defer func() {
if r := recover(); r != nil {
didntStartWithOAuth2 = true
}
}()
_, _ = ParseOAuth2PIDP("notoauth2;;but;;restisgood")
}()
if !didntStartWithOAuth2 {
t.Error("expected a panic when the pid doesn't start with oauth2")
}
}
type testAssertionFailUser struct{}
func (testAssertionFailUser) GetPID() string { return "" }
func (testAssertionFailUser) PutPID(string) {}
func TestUserAssertions(t *testing.T) {
t.Parallel()
u := &mockUser{}
fu := testAssertionFailUser{}
paniced := false
func() {
defer func() {
if r := recover(); r != nil {
paniced = true
}
}()
MustBeAuthable(u)
MustBeConfirmable(u)
MustBeLockable(u)
MustBeOAuthable(u)
MustBeRecoverable(u)
}()
if paniced {
t.Error("The mock user should have included all interfaces and should not panic")
}
didPanic := func(f func()) (paniced bool) {
defer func() {
if r := recover(); r != nil {
paniced = true
}
}()
f()
return paniced
}
if !didPanic(func() { MustBeAuthable(fu) }) {
t.Error("should have panic'd")
}
if !didPanic(func() { MustBeConfirmable(fu) }) {
t.Error("should have panic'd")
}
if !didPanic(func() { MustBeLockable(fu) }) {
t.Error("should have panic'd")
}
if !didPanic(func() { MustBeOAuthable(fu) }) {
t.Error("should have panic'd")
}
if !didPanic(func() { MustBeRecoverable(fu) }) {
t.Error("should have panic'd")
}
}