-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
51 lines (36 loc) · 1.09 KB
/
auth_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
package main
import (
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
)
func TestAuthVerify_WithValidToken(t *testing.T) {
signingKey := []byte("secret")
token, _ := authNew(signingKey, "testuser", time.Hour)
user, err := authVerify(signingKey, token)
assert.NoError(t, err)
assert.Equal(t, "testuser", user)
}
func TestAuthVerify_WithInvalidToken(t *testing.T) {
signingKey := []byte("secret")
wrongKey := []byte("wrongsecret")
token, _ := authNew(signingKey, "testuser", time.Hour)
_, err := authVerify(wrongKey, token)
assert.Error(t, err)
}
func TestAuthVerify_WithExpiredToken(t *testing.T) {
signingKey := []byte("secret")
token, _ := authNew(signingKey, "testuser", -time.Hour)
_, err := authVerify(signingKey, token)
assert.Error(t, err)
}
func TestAuthNew_WithValidCredentials(t *testing.T) {
signingKey := []byte("secret")
token, err := authNew(signingKey, "testuser", time.Hour)
assert.NoError(t, err)
_, err = jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return signingKey, nil
})
assert.NoError(t, err)
}