-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
194 lines (181 loc) · 5.56 KB
/
main_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func Test_UserRegistration(t *testing.T) {
scenarios := []struct {
name string
method string
formData string
expectedStatus int
expectedBody string
}{
{
name: "Method not allowed",
method: http.MethodGet,
formData: "",
expectedStatus: 405,
expectedBody: "Method not allowed\n",
},
{
name: "Invalid username or password",
method: http.MethodPost,
formData: "username=ab&password=1234567",
expectedStatus: 406,
expectedBody: "Invalid username and/or password\n",
},
{
name: "Successful registration",
method: http.MethodPost,
formData: "username=abcd&password=12345678",
expectedStatus: 200,
expectedBody: "User registered successfully",
},
}
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
req := httptest.NewRequest(scenario.method, "/register", strings.NewReader(scenario.formData))
if scenario.method == http.MethodPost {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
rr := httptest.NewRecorder()
register(rr, req)
assert.Equal(t, scenario.expectedStatus, rr.Code)
assert.Equal(t, scenario.expectedBody, rr.Body.String())
})
}
}
func Test_UserLogin(t *testing.T) {
scenarios := []struct {
name string
method string
formData string
registerBefore bool
expectedStatusCode int
expectedBody string
}{
{
name: "Method not allowed",
method: http.MethodGet,
formData: "",
registerBefore: false,
expectedStatusCode: 405,
expectedBody: "Method not allowed\n",
},
{
name: "Invalid credentials",
method: http.MethodPost,
formData: "username=doesnot&password=exist",
registerBefore: false,
expectedStatusCode: 401,
expectedBody: "Invalid username or password\n",
},
{
name: "Successful login",
method: http.MethodPost,
formData: "username=validuser&password=validpassword",
registerBefore: true,
expectedStatusCode: 200,
expectedBody: "User logged in successfully",
},
}
initialiseJwks()
registerUser()
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
request := httptest.NewRequest(scenario.method, "/login", strings.NewReader(scenario.formData))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
login(rr, request)
assert.Equal(t, scenario.expectedStatusCode, rr.Code)
assert.Equal(t, scenario.expectedBody, rr.Body.String())
})
}
}
func Test_UserAccessToProtectedResource(t *testing.T) {
scenarios := []struct {
name string
method string
loginBefore bool
expectedStatusCode int
expectedBody string
}{
{
name: "Method not allowed",
method: http.MethodGet,
loginBefore: true,
expectedStatusCode: 405,
expectedBody: "Method not allowed\n",
},
{
name: "Unauthorized",
method: http.MethodPost,
expectedStatusCode: 401,
loginBefore: false,
expectedBody: "Unauthorized\n",
},
{
name: "Authorized",
method: http.MethodPost,
expectedStatusCode: 200,
loginBefore: true,
expectedBody: "Welcome, validuser!",
},
}
initialiseJwks()
registerUser()
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
jwks, _ := jwkSet.JSONPublic(r.Context())
_, _ = w.Write(jwks)
}))
defer testServer.Close()
_ = os.Setenv("JWKS_BASE_URL", testServer.URL)
log.Infof("JWKS_BASE_URL: %s ", testServer.URL)
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
request := httptest.NewRequest(scenario.method, "/protected", nil)
if scenario.loginBefore {
authHeader, csrfToken := loginUser(t)
request.Header.Set(authorizationHeader, authHeader)
request.Header.Set("X-CSRF-Token", csrfToken)
}
rr := httptest.NewRecorder()
withAuth(protected)(rr, request)
assert.Equal(t, scenario.expectedStatusCode, rr.Code)
assert.Equal(t, scenario.expectedBody, rr.Body.String())
})
}
}
func registerUser() {
log.Debugf("Registering test user...")
request := httptest.NewRequest(http.MethodPost, "/register", strings.NewReader("username=validuser&password=validpassword"))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
register(rr, request)
}
func loginUser(t *testing.T) (string, string) {
log.Debugf("Logging in test user...")
request := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader("username=validuser&password=validpassword"))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
login(rr, request)
assert.Equal(t, 200, rr.Code)
authHeader := rr.Header().Get(authorizationHeader)
csrfToken := getCookieValue(CsrfToken, rr)
log.Debugf("Retrieved csrf_token [%v] and authorization header [%v] from request", csrfToken, authHeader)
return authHeader, csrfToken
}
func getCookieValue(cookieName string, rr *httptest.ResponseRecorder) string {
for _, cookie := range rr.Result().Cookies() {
if cookie.Name == cookieName {
return cookie.Value
}
}
return ""
}