-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
112 lines (87 loc) · 2.46 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
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
package main
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/golang-jwt/jwt"
)
func TestCreateJWTValid(t *testing.T) {
s := Session{
ID: "",
Name: "",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
jwt.MapClaims{
"username": s.Name,
"exp": time.Now().Add(time.Minute).Unix(),
})
tokenString, _ := token.SignedString([]byte(os.Getenv("SECRET_KEY") + randomSalt))
c := http.Cookie{
Name: "token",
Value: tokenString,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Expires: time.Now().Add(time.Minute), // 1 minute expire session removed
}
result := s.createToken()
// check returned cookie structure has no error
if result == nil {
t.Errorf("createToken method returned not nil: got %v want %v", result, "filled structure with created name and value.")
}
// check name and value is setup correct
if s.ID != result.Value && s.ID != "" {
t.Errorf("createToken method wrong session value setup: got %v want %v", s.ID, c.Value)
}
// check name and value is setup correct
if s.Name != result.Name && s.Name != "" {
t.Errorf("createToken method wrong session name setup: got %v want %v", s.Name, c.Name)
}
}
func TestDeleteJWTCorrect(t *testing.T) {
s := Session{
ID: "",
Name: "token",
AuthType: AuthTypeJWTToken,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
jwt.MapClaims{
"username": s.Name,
"exp": time.Now().Add(time.Minute).Unix(),
})
tokenString, _ := token.SignedString([]byte(os.Getenv("SECRET_KEY") + randomSalt))
c := http.Cookie{
Name: "token",
Value: tokenString,
MaxAge: -1,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Expires: time.Now(),
}
s.ID = c.Value
s.Name = c.Name
req, err := http.NewRequest(http.MethodGet, "/logout", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(http.NotFound)
handler(rr, req)
s.deleteToken(rr)
// check correct header setup
expected := ""
if header := rr.Result().Header.Get("Set-Cookie"); header != expected {
t.Errorf("handler returned wrong header set: got %v want %v", header, expected)
}
// check value is setup correct
if s.ID != "" {
t.Errorf("deleteToken method wrong session value setup: got %v want %v", s.ID, c.Value)
}
// check name is setup correct
if s.Name != "" {
t.Errorf("deleteToken method wrong session name setup: got %v want %v", s.Name, c.Name)
}
}