-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
106 lines (95 loc) · 3.73 KB
/
jwt_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
package jwt
import (
"strings"
"testing"
"time"
)
func TestBase64Encoding(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
bs64 := generator.EncodeBase64([]byte("gints means gaming hints!"))
expected := "Z2ludHMgbWVhbnMgZ2FtaW5nIGhpbnRzIQ=="
if strings.Compare(bs64, expected) != 0 {
t.Errorf("expected: %s\n\tgot: %s", expected, bs64)
}
}
func TestBase64Decoding(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
str := string(generator.DecodeBase64("Z2ludHMgbWVhbnMgZ2FtaW5nIGhpbnRzIQ=="))
expected := "gints means gaming hints!"
if strings.Compare(str, expected) != 0 {
t.Errorf("expected: %s\n\tgot: %s", expected, str)
}
}
func TestEncodeHMAC(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
hmac := generator.EncodeBase64(generator.EncodeHMAC("gints means gaming hints!"))
expected := "hAooNE4gke+aCxNdvTZgPZCecw6mUrvGQ5pIWg6gDhc="
if strings.Compare(hmac, expected) != 0 {
t.Errorf("expected: %s\n\tgot: %s", expected, hmac)
}
}
func TestEncodeJWT(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
hmac := generator.EncodeBase64(generator.EncodeJWT("gints", "gaming hints"))
expected := "0DY8xD+FPMDOW1cb3PW8ktpUwcgDFPauwxWnMWe42CI="
if strings.Compare(hmac, expected) != 0 {
t.Errorf("expected: %s\n\tgot: %s", expected, hmac)
}
}
func TestValidateJWT(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
jwt := "gints.gaming hinst." +
generator.EncodeBase64(generator.EncodeJWT("gints", "gaming hints"))
if generator.ValidateJWT(jwt) {
t.Errorf("validator can't validate jwt correctly")
}
}
func TestGenerateJWT(t *testing.T) {
t.Logf("I can't find a reasonable way to write a test for it.\n" +
"\tBut I'm sure that it's working correctly!")
}
func TestDecode(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
payload := generator.Decode(generator.GenerateJWT("[email protected]", false))
expectedEmail := "[email protected]"
expectedAdmin := false
expectedExp := time.Now().Unix() + 5*3600
if strings.Compare(payload.Email, expectedEmail) != 0 ||
payload.Admin != expectedAdmin || payload.Expire != expectedExp {
t.Errorf("expected %s, %t, %d", expectedEmail, expectedAdmin, expectedExp)
t.Errorf("got %s, %t, %d", payload.Email, payload.Admin, payload.Expire)
}
}
func TestCheckExpire(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
payload := generator.Decode(generator.GenerateJWT("[email protected]", false))
if generator.CheckExpire(payload.Expire) {
t.Errorf("jwt is expiring soon")
}
if !generator.CheckExpire(payload.Expire - 6*3600) {
t.Errorf("jwt is expiring late")
}
}
func TestCheckRelogin(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
payload := generator.Decode(generator.GenerateJWT("[email protected]", false))
if generator.CheckReLogin(payload.Expire - 3600*24*13) {
t.Errorf("jwt requests relogin soon")
}
if !generator.CheckReLogin(payload.Expire - 3600*24*15) {
t.Errorf("jwt requests relogin late")
}
}
func TestRenewJWT(t *testing.T) {
generator := NewJWTGenerator("1cfabccbf188251666dfa066a88864a7")
jwt := generator.GenerateJWT("[email protected]", false)
payload := generator.Decode(generator.RenewJWT(jwt))
expectedEmail := "[email protected]"
expectedAdmin := false
expectedExp := time.Now().Unix() + 5*3600
if strings.Compare(payload.Email, expectedEmail) != 0 ||
payload.Admin != expectedAdmin || payload.Expire != expectedExp {
t.Errorf("expected %s, %t, %d", expectedEmail, expectedAdmin, expectedExp)
t.Errorf("got %s, %t, %d", payload.Email, payload.Admin, payload.Expire)
}
}