-
Notifications
You must be signed in to change notification settings - Fork 1
/
token_test.go
141 lines (121 loc) · 3.19 KB
/
token_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
131
132
133
134
135
136
137
138
139
140
141
package apns
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"net/http"
"testing"
"time"
)
func TestTokenKeyNil(t *testing.T) {
token := NewToken(nil, "5JZB9P77A7", "SUPERTEEM1")
_, err := token.Generate()
if err != nil {
if err != ErrTokenKeyNil {
t.Errorf("err: %v; want: ErrTokenKeyNil", err)
}
} else {
t.Error("err must be not nil")
}
}
func TestTokenKeyInvalid(t *testing.T) {
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
t.Fatal(err)
}
token := NewToken(key, "5JZB9P77A7", "SUPERTEEM1")
_, err = token.Generate()
if err != nil {
if err != ErrJWTKeyNotECDSAP256 {
t.Errorf("err: %v; want: ErrJWTKeyNotECDSAP256", err)
}
} else {
t.Error("err must be not nil")
}
}
func TestTokenExpired(t *testing.T) {
token := &Token{}
if !token.Expired() {
t.Error("token must be expired")
}
token.IssuedAt = time.Now().Add(-(TokenRefreshInterval + 1) * time.Second).Unix()
if !token.Expired() {
t.Error("token must be expired")
}
token.RefreshInterval = 60
token.IssuedAt = time.Now().Add(-61 * time.Second).Unix()
if !token.Expired() {
t.Error("token must be expired")
}
}
func TestTokenNotExpired(t *testing.T) {
token := &Token{
IssuedAt: time.Now().Unix(),
}
if token.Expired() {
t.Error("token must be not expired")
}
token.IssuedAt = time.Now().Add(-(TokenRefreshInterval - 1) * time.Second).Unix()
if token.Expired() {
t.Error("token must be not expired")
}
token.RefreshInterval = 3600
token.IssuedAt = time.Now().Add(-3599 * time.Second).Unix()
if token.Expired() {
t.Error("token must be not expired")
}
}
func TestTokenGenerateIfExpired(t *testing.T) {
key, err := AuthKeyFromFile("testdata/AuthKey_5MDQ4KLTY7.p8")
if err != nil {
t.Fatal(err)
}
token := NewToken(key, "5JZB9P77A7", "SUPERTEEM1")
want, err := token.GenerateIfExpired()
if err != nil {
t.Fatal(err)
}
got, err := token.GenerateIfExpired()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("got: %v; want: %v", got, want)
}
}
func TestSetAuthorization(t *testing.T) {
key, err := AuthKeyFromFile("testdata/AuthKey_5MDQ4KLTY7.p8")
if err != nil {
t.Fatal(err)
}
token := NewToken(key, "5JZB9P77A7", "SUPERTEEM1")
h := make(http.Header)
token.SetAuthorization(h)
if h.Get("authorization") != "bearer "+token.Bearer {
t.Errorf("invalid authorization header: %v", h.Get("authorization"))
}
}
func TestSetAuthorizationWithInvalidKey(t *testing.T) {
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
t.Fatal(err)
}
token := NewToken(key, "5JZB9P77A7", "SUPERTEEM1")
h := make(http.Header)
err = token.SetAuthorization(h)
if err != nil {
if err != ErrJWTKeyNotECDSAP256 {
t.Errorf("err: %v; want: ErrJWTKeyNotECDSAP256", err)
}
} else {
t.Error("err must be not nil")
}
}
func TestSetBearer(t *testing.T) {
bearer := "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjVKWkI5UDc3QTcifQ.eyJpc3MiOiJTVVBFUlRFRU0xIiwiaWF0IjoxNjI5MDAwMDAwfQ.f9paE35q8NzQyrnJaDbYb1yBzBcj8_jHpeMe06y5OERf-0ERD70VZCS6dG-toWKSxeevYEARKMwoGliJtZ7atQ"
h := make(http.Header)
SetBearer(h, bearer)
if h.Get("authorization") != "bearer "+bearer {
t.Errorf("invalid authorization header: %v", h.Get("authorization"))
}
}