-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
onetimeauth_test.go
82 lines (67 loc) · 1.49 KB
/
onetimeauth_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
package onetimeauth
import (
"testing"
"time"
"github.com/golang-jwt/jwt/v4"
)
const (
testUserID = "tester"
invalidToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTQ3OTY4ODEsImlhdCI6MTYxNDc5Njg4MCwiaXNzIjoic2hpbnB1cnUgdi5URVNUSU5HX0JVSUxEIiwibmJmIjoxNjE0Nzk2ODgwLCJzdWIiOiJ0ZXN0ZXIifQ.na7K3mAszvtMN9x1VfIEv_QU5ZKWHJUSPONPKABFbCI"
)
var testOptions = &Options{
Issuer: "test issuer",
Lifetime: time.Second,
SigningKeyLength: 128,
TokenKeyLength: 32,
SigningMethod: jwt.SigningMethodHS256,
}
func TestNew(t *testing.T) {
_, err := New(nil)
if err != nil {
t.Error(err)
}
_, err = New(new(Options))
if err != nil {
t.Error(err)
}
_, err = New(testOptions)
if err != nil {
t.Error(err)
}
}
func TestGetKey(t *testing.T) {
a, err := New(testOptions)
if err != nil {
t.Error(err)
}
_, err = a.GetKey(testUserID)
if err != nil {
t.Error(err)
}
}
func TestValidateKey(t *testing.T) {
a, err := New(testOptions)
if err != nil {
t.Error(err)
}
_, err = a.ValidateKey(invalidToken)
if err == nil {
t.Error("invalid token passed falsely")
}
token, err := a.GetKey(testUserID)
if err != nil {
t.Error(err)
}
userID, err := a.ValidateKey(token)
if err != nil {
t.Error(err)
}
if userID != testUserID {
t.Error("user id missmatch")
}
time.Sleep(2 * time.Second)
_, err = a.ValidateKey(token)
if err == nil {
t.Error("key did not expire")
}
}