-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
142 lines (105 loc) · 4.32 KB
/
middleware_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package auth
import (
"net/http"
"net/http/httptest"
"testing"
)
const validTokenWithSYSTEMRole = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTE4MDAwMTYsImlhdCI6MTU2MjQxMTIzMiwiaXNzIjoiQXV0aFNlcnZpY2UiLCJzdWIiOiJhdXRoLXNlcnZpY2UiLCJuYW1lIjoiYXV0aC1zZXJ2aWNlIiwidXNlcm5hbWUiOiJhdXRoLXNlcnZpY2UiLCJhdXRob3JpdGllcyI6W3sicm9sZSI6IlNZU1RFTSJ9XX0.v7I5-Zf4UASLgW4RRyIiihwE6cHnEXLwhWJxMLgLjf5GnbIKTqMnDNqhxE1gd0liNkM7_mcxNm63rbCDwIHhmw"
func TestAuthenticatedMiddlewareForExpiredToken(t *testing.T) {
middleware := create("privatesigningpassowrd")
nextHandler := &nextHandler{}
req, rr := newRequestResponseEmulation(t)
req.AddCookie(&http.Cookie{Name: "JWT", Value: expiredToken})
middleware.IsAuthenticated(nextHandler).ServeHTTP(rr, req)
if nextHandler.Visited {
t.Error("Next handler should not have been called for expired JWT token")
}
if rr.Code != http.StatusUnauthorized {
t.Error("Status should be unauthorized")
}
}
func TestAuthenticatedMiddlewareForValidToken(t *testing.T) {
middleware := create("privatesigningpassowrd")
nextHandler := &nextHandler{}
req, rr := newRequestResponseEmulation(t)
req.AddCookie(&http.Cookie{Name: "JWT", Value: tokenValidUntil2099})
middleware.IsAuthenticated(nextHandler).ServeHTTP(rr, req)
if !nextHandler.Visited {
t.Error("Next handler should have been called for valid JWT token")
}
if rr.Code == http.StatusUnauthorized {
t.Error("Status should not be unauthorized")
}
}
func TestHasAnyRoleMiddlewareForValidTokenButWithoutRole(t *testing.T) {
middleware := create("privatesigningpassowrd")
nextHandler := &nextHandler{}
req, rr := newRequestResponseEmulation(t)
req.AddCookie(&http.Cookie{Name: "JWT", Value: tokenValidUntil2099})
middleware.HasAnyRole("SYSTEM")(nextHandler).ServeHTTP(rr, req)
if nextHandler.Visited {
t.Error("Next handler should not have been called for valid JWT without SYSTEM role")
}
if rr.Code != http.StatusUnauthorized {
t.Error("Status should be unauthorized since user has no role")
}
}
func TestHasAnyRoleMiddlewareForValidTokenAndSYSTEMRole(t *testing.T) {
middleware := create("privateKey")
nextHandler := &nextHandler{}
req, rr := newRequestResponseEmulation(t)
req.AddCookie(&http.Cookie{Name: "JWT", Value: validTokenWithSYSTEMRole})
middleware.HasAnyRole("SYSTEM")(nextHandler).ServeHTTP(rr, req)
if !nextHandler.Visited {
t.Error("Next handler should have been called for valid JWT token with SYSTEM role")
}
if rr.Code == http.StatusUnauthorized {
t.Error("Status should not be unauthorized")
}
}
func TestHasAnyRoleMiddlewareForValidTokenButDifferentRoleThanExpected(t *testing.T) {
middleware := create("privateKey")
nextHandler := &nextHandler{}
req, rr := newRequestResponseEmulation(t)
req.AddCookie(&http.Cookie{Name: "JWT", Value: validTokenWithSYSTEMRole})
middleware.HasAnyRole("USER")(nextHandler).ServeHTTP(rr, req)
if nextHandler.Visited {
t.Error("Next handler should not have been called for valid JWT token with other not wanted role")
}
if rr.Code != http.StatusUnauthorized {
t.Error("Status should be unauthorized")
}
}
func TestUnauthorizedForInvalidToken(t *testing.T) {
middleware := create("privateKey")
nextHandler := &nextHandler{}
req, rr := newRequestResponseEmulation(t)
req.AddCookie(&http.Cookie{Name: "JWT", Value: expiredToken})
middleware.HasAnyRole("USER")(nextHandler).ServeHTTP(rr, req)
if nextHandler.Visited {
t.Error("Next handler should not have been called for valid JWT token with other not wanted role")
}
if rr.Code != http.StatusUnauthorized {
t.Error("Status should be unauthorized")
}
}
// ----- test helpers ----------------
func newRequestResponseEmulation(t *testing.T) (*http.Request, *httptest.ResponseRecorder) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
return req, rr
}
type nextHandler struct {
Visited bool
}
func (handler *nextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler.Visited = true
}
func create(key string) Middleware {
return New(Config{
JWTPrivateKey: []byte(key),
})
}