-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_test.go
More file actions
60 lines (50 loc) · 1.18 KB
/
token_test.go
File metadata and controls
60 lines (50 loc) · 1.18 KB
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
package auth_test
import (
"testing"
"github.com/flick-web/auth"
)
func TestPasswordChecking(t *testing.T) {
lm := &auth.LoginManager{BcryptCost: 10}
hash, err := lm.GetHash("testpassword")
if err != nil {
t.Error(err)
}
valid := lm.CheckPassword("testpassword", hash)
if !valid {
t.Error("Should have been valid")
}
valid = lm.CheckPassword("testpasswor", hash)
if valid {
t.Error("Should not have been valid")
}
valid = lm.CheckPassword("estpassword", hash)
if valid {
t.Error("Should not have been valid")
}
valid = lm.CheckPassword("", hash)
if valid {
t.Error("Should not have been valid")
}
}
func TestJWTs(t *testing.T) {
signer := auth.NewTokenSigner("dispatch", []byte("GcWik@!FN2s@xZK#rXh&FkLM9b^dGLQs"))
token, err := signer.CreateToken("testuser")
if err != nil {
t.Error(err)
}
claims, err := signer.ParseToken(token)
if err != nil {
t.Error(err)
}
if claims.Subject != "testuser" {
t.Errorf("Incorrect sub: %s\n", claims.Subject)
}
if claims.Issuer != "dispatch" {
t.Errorf("Incorrect issuer: %s\n", claims.Issuer)
}
token = "badstring" + token
claims, err = signer.ParseToken(token)
if err == nil {
t.Error("Expected error")
}
}