-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjwt_test.go
61 lines (50 loc) · 1.31 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
package main
import (
"net/http"
"os"
"testing"
"github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateJWTtoken(t *testing.T) {
tokenString, err := createJWTtoken("login")
require.NoError(t, err)
token, err := parseJWTtoken(tokenString)
require.NoError(t, err)
assert.True(t, token.Valid)
assert.Equal(t, tokenString, token.Raw)
claims, ok := token.Claims.(jwt.MapClaims)
assert.True(t, ok)
assert.Equal(t, claims["login"], "login")
tokenString, err = createJWTtoken("fail")
require.NoError(t, err)
err = os.Setenv("JWT_KEY", "")
require.NoError(t, err)
_, err = parseJWTtoken(tokenString)
require.Error(t, err)
err = os.Setenv("JWT_KEY", "returnJWT_KEY")
require.NoError(t, err)
}
func TestGetJWTtokenFromCookies(t *testing.T) {
expectedToken, err := createJWTtoken("login")
require.Nil(t, err)
cookies := []*http.Cookie{
{
Name: "someCookie",
Value: "someValue",
},
{
Name: "jwt",
Value: expectedToken,
HttpOnly: true,
SameSite: 3,
},
}
actualToken, err := getJWTtokenFromCookies(cookies)
require.Nil(t, err)
assert.Equal(t, expectedToken, actualToken.Raw)
cookies = []*http.Cookie{}
_, err = getJWTtokenFromCookies(cookies)
require.EqualError(t, err, "jwt token was not found in cookies.")
}