-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmsg_server_test.go
130 lines (122 loc) · 3.04 KB
/
msg_server_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
package sqrl_test
import (
"testing"
sqrl "github.com/RaniSputnik/sqrl-go"
"github.com/stretchr/testify/assert"
)
func TestServerMsgEncode(t *testing.T) {
t.Run("EncodesValidServerMsg", func(t *testing.T) {
testCases := []struct {
Input sqrl.ServerMsg
Expect string
}{
{
Input: sqrl.ServerMsg{
Ver: []string{sqrl.V1},
Nut: "bRW-IegCUhGmcz9yvTtDKA",
Tif: sqrl.TIF(5),
Qry: "/sqrl?nut=bRW-IegCUhGmcz9yvTtDKA",
},
Expect: "dmVyPTENCm51dD1iUlctSWVnQ1VoR21jejl5dlR0REtBDQp0aWY9NQ0KcXJ5PS9zcXJsP251dD1iUlctSWVnQ1VoR21jejl5dlR0REtBDQo",
},
{
Input: sqrl.ServerMsg{
Ver: []string{sqrl.V1},
Nut: "foo",
Tif: sqrl.TIF(5),
Qry: "/sqrl?nut=foo",
URL: "https://sqrl.example.com?123456789",
},
Expect: "dmVyPTENCm51dD1mb28NCnRpZj01DQpxcnk9L3Nxcmw_bnV0PWZvbw0KdXJsPWh0dHBzOi8vc3FybC5leGFtcGxlLmNvbT8xMjM0NTY3ODkNCg",
},
}
for _, test := range testCases {
got, err := test.Input.Encode()
assert.NoError(t, err)
assert.Equal(t, test.Expect, got)
}
})
}
func TestServerMsgParse(t *testing.T) {
t.Run("ReturnsValidServerMsg", func(t *testing.T) {
testCases := []struct {
Input string
Expect sqrl.ServerMsg
}{
{
Input: "dmVyPTENCm51dD1iUlctSWVnQ1VoR21jejl5dlR0REtBDQp0aWY9NQ0KcXJ5PS9zcXJsP251dD1iUlctSWVnQ1VoR21jejl5dlR0REtBDQo",
Expect: sqrl.ServerMsg{
Ver: []string{sqrl.V1},
Nut: "bRW-IegCUhGmcz9yvTtDKA",
Tif: 5,
Qry: "/sqrl?nut=bRW-IegCUhGmcz9yvTtDKA",
},
},
{
Input: "dmVyPTENCm51dD1RTFlOd1N2TEZMZWd3RTlVMUZySG5BDQp0aWY9NA0KcXJ5PS9zcXJsP251dD1RTFlOd1N2TEZMZWd3RTlVMUZySG5BDQpzaW49MA0K",
Expect: sqrl.ServerMsg{
Ver: []string{sqrl.V1},
Nut: "QLYNwSvLFLegwE9U1FrHnA",
Tif: 4,
Qry: "/sqrl?nut=QLYNwSvLFLegwE9U1FrHnA",
// TODO: Sin: 0,
},
},
{
Input: "dmVyPTENCm51dD1mb28NCnRpZj01DQpxcnk9L3Nxcmw_bnV0PWZvbw0KdXJsPWh0dHBzOi8vc3FybC5leGFtcGxlLmNvbT8xMjM0NTY3ODkNCg",
Expect: sqrl.ServerMsg{
Ver: []string{sqrl.V1},
Nut: "foo",
Tif: 5,
Qry: "/sqrl?nut=foo",
URL: "https://sqrl.example.com?123456789",
},
},
}
for _, test := range testCases {
got, err := sqrl.ParseServer(test.Input)
assert.NoError(t, err)
if assert.NotNil(t, got) {
assert.Equal(t, test.Expect, *got)
}
}
})
}
func TestServerMsgIs(t *testing.T) {
testCases := []struct {
Input sqrl.TIF
Test sqrl.TIF
Expect bool
}{
{
Input: sqrl.TIFIPMatch,
Test: sqrl.TIFIPMatch,
Expect: true,
},
{
Input: sqrl.TIFClientFailure | sqrl.TIFCommandFailed,
Test: sqrl.TIFCommandFailed,
Expect: true,
},
{
Input: sqrl.TIFBadIDAssociation | sqrl.TIFIPMatch,
Test: sqrl.TIFIPMatch,
Expect: true,
},
{
Input: sqrl.TIFFunctionNotSupported,
Test: sqrl.TIFIPMatch,
Expect: false,
},
{
Input: sqrl.TIF(0),
Test: sqrl.TIFSQRLDisabled,
Expect: false,
},
}
for _, testCase := range testCases {
serverMsg := sqrl.ServerMsg{Tif: testCase.Input}
got := serverMsg.Is(testCase.Test)
assert.Equal(t, testCase.Expect, got)
}
}