-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies_test.go
86 lines (79 loc) · 2.08 KB
/
cookies_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
package kivikd
import (
"testing"
"github.com/go-kivik/kivikd/v4/authdb"
)
type validateTest struct {
Name string
Cookie string
User *authdb.UserContext
Valid bool
Err string
}
func TestValidateCookie(t *testing.T) {
s := &Service{}
if err := s.loadConf(); err != nil {
t.Fatal(err)
}
tests := []validateTest{
{
Name: "Valid", Cookie: "YWRtaW46NThDNTQzN0Y6OnE2cBAuoQKvVBHF2l4PIqKHqDM", Valid: true,
User: &authdb.UserContext{Name: "admin", Salt: "foo bar baz"},
},
{
Name: "WrongSalt", Cookie: "YWRtaW46NThDNTQzN0Y697rnaWCa_rarAm25wbOg3Gm3mqc", Valid: false,
User: &authdb.UserContext{Name: "admin", Salt: "123"},
},
}
for _, test := range tests {
func(test validateTest) {
t.Run(test.Name, func(t *testing.T) {
valid, err := s.ValidateCookie(test.User, test.Cookie)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != test.Err {
t.Errorf("Unexpected error.\nExpected: %s\n Actual: %s\n", test.Err, errMsg)
}
if valid != test.Valid {
t.Errorf("Unexpected result. Expected %t, Actual %t", test.Valid, valid)
}
})
}(test)
}
}
type tokenTest struct {
Name string
Salt string
Created int64
Expected string
Err string
}
func TestCreateAuthToken(t *testing.T) {
s := &Service{}
if err := s.loadConf(); err != nil {
t.Fatal(err)
}
tests := []tokenTest{
{Name: "admin", Salt: "foo bar baz", Created: 1489322879, Expected: "YWRtaW46NThDNTQzN0Y6OnE2cBAuoQKvVBHF2l4PIqKHqDM"},
{Name: "bob", Salt: "0123456789abc", Created: 1489322879, Expected: "Ym9iOjU4QzU0MzdGOihHwWRLS2vekOgsRrH1cEVrk6za"},
}
for _, test := range tests {
func(test tokenTest) {
t.Run(test.Name, func(t *testing.T) {
result, err := s.CreateAuthToken(test.Name, test.Salt, test.Created)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != test.Err {
t.Errorf("Unexpected error. Expected '%s', got '%s'", test.Err, errMsg)
}
if result != test.Expected {
t.Errorf("Expected: %s\n Actual: %s\n", test.Expected, result)
}
})
}(test)
}
}