| 1 |
package g8 |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
FirstTestProviderClientPermission = "permission-1" |
| 15 |
SecondTestProviderClientPermission = "permission-2" |
| 16 |
TestProviderClientToken = "client-token-from-provider" |
| 17 |
TestProviderClientData = "client-data-from-provider" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
mockClientProvider = NewClientProvider(func(token string) *Client { |
| 22 |
|
| 23 |
|
| 24 |
if token == TestProviderClientToken { |
| 25 |
return &Client{ |
| 26 |
Token: TestProviderClientToken, |
| 27 |
Data: TestProviderClientData, |
| 28 |
Permissions: []string{FirstTestProviderClientPermission, SecondTestProviderClientPermission}, |
| 29 |
} |
| 30 |
} |
| 31 |
return nil |
| 32 |
}) |
| 33 |
) |
| 34 |
|
| 35 |
type testHandler struct { |
| 36 |
} |
| 37 |
|
| 38 |
func (handler *testHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) { |
| 39 |
writer.WriteHeader(http.StatusOK) |
| 40 |
} |
| 41 |
|
| 42 |
func testHandlerFunc(writer http.ResponseWriter, _ *http.Request) { |
| 43 |
writer.WriteHeader(http.StatusOK) |
| 44 |
} |
| 45 |
|
| 46 |
func TestUsability(t *testing.T) { |
| 47 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 48 |
|
| 49 |
var handler http.Handler = &testHandler{} |
| 50 |
handlerFunc := func(writer http.ResponseWriter, request *http.Request) { |
| 51 |
writer.WriteHeader(http.StatusOK) |
| 52 |
} |
| 53 |
|
| 54 |
router := http.NewServeMux() |
| 55 |
router.Handle("/handle", handler) |
| 56 |
router.Handle("/handle-protected", gate.Protect(handler)) |
| 57 |
router.HandleFunc("/handlefunc", handlerFunc) |
| 58 |
router.HandleFunc("/handlefunc-protected", gate.ProtectFunc(handlerFunc)) |
| 59 |
} |
| 60 |
|
| 61 |
func TestNewGate(t *testing.T) { |
| 62 |
gate := NewGate(nil) |
| 63 |
if gate == nil { |
| 64 |
t.Error("gate should not be nil") |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestUnprotectedHandler(t *testing.T) { |
| 69 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 70 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 71 |
responseRecorder := httptest.NewRecorder() |
| 72 |
|
| 73 |
router := http.NewServeMux() |
| 74 |
router.Handle("/handle", &testHandler{}) |
| 75 |
router.ServeHTTP(responseRecorder, request) |
| 76 |
|
| 77 |
if responseRecorder.Code != http.StatusOK { |
| 78 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestGate_ProtectWithInvalidToken(t *testing.T) { |
| 83 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 84 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 85 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 86 |
responseRecorder := httptest.NewRecorder() |
| 87 |
|
| 88 |
router := http.NewServeMux() |
| 89 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 90 |
router.ServeHTTP(responseRecorder, request) |
| 91 |
|
| 92 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 93 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestGate_ProtectWithValidToken(t *testing.T) { |
| 98 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 99 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 100 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "good-token")) |
| 101 |
responseRecorder := httptest.NewRecorder() |
| 102 |
|
| 103 |
router := http.NewServeMux() |
| 104 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 105 |
router.ServeHTTP(responseRecorder, request) |
| 106 |
|
| 107 |
if responseRecorder.Code != http.StatusOK { |
| 108 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestGate_ProtectMultipleTimes(t *testing.T) { |
| 113 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 114 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 115 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "good-token")) |
| 116 |
badRequest, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 117 |
badRequest.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 118 |
|
| 119 |
router := http.NewServeMux() |
| 120 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 121 |
|
| 122 |
for i := 0; i < 100; i++ { |
| 123 |
responseRecorder := httptest.NewRecorder() |
| 124 |
router.ServeHTTP(responseRecorder, request) |
| 125 |
if responseRecorder.Code != http.StatusOK { |
| 126 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 127 |
} |
| 128 |
responseRecorder = httptest.NewRecorder() |
| 129 |
router.ServeHTTP(responseRecorder, badRequest) |
| 130 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 131 |
t.Errorf("%s %s should have returned %d, but returned %d instead", badRequest.Method, badRequest.URL, http.StatusOK, responseRecorder.Code) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestGate_ProtectWithValidTokenExposedThroughClientProvider(t *testing.T) { |
| 137 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClientProvider(mockClientProvider)) |
| 138 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 139 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", TestProviderClientToken)) |
| 140 |
responseRecorder := httptest.NewRecorder() |
| 141 |
|
| 142 |
router := http.NewServeMux() |
| 143 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 144 |
router.ServeHTTP(responseRecorder, request) |
| 145 |
|
| 146 |
if responseRecorder.Code != http.StatusOK { |
| 147 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestGate_ProtectWithValidTokenExposedThroughClientProviderWithCache(t *testing.T) { |
| 152 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClientProvider(mockClientProvider.WithCache(60*time.Minute, 70000))) |
| 153 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 154 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", TestProviderClientToken)) |
| 155 |
responseRecorder := httptest.NewRecorder() |
| 156 |
|
| 157 |
router := http.NewServeMux() |
| 158 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 159 |
router.ServeHTTP(responseRecorder, request) |
| 160 |
|
| 161 |
if responseRecorder.Code != http.StatusOK { |
| 162 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestGate_ProtectWithInvalidTokenWhenUsingClientProvider(t *testing.T) { |
| 167 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClientProvider(mockClientProvider)) |
| 168 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 169 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 170 |
responseRecorder := httptest.NewRecorder() |
| 171 |
|
| 172 |
router := http.NewServeMux() |
| 173 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 174 |
router.ServeHTTP(responseRecorder, request) |
| 175 |
|
| 176 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 177 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func TestGate_ProtectWithPermissionsWhenValidTokenAndSufficientPermissionsWhileUsingClientProvider(t *testing.T) { |
| 182 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClientProvider(mockClientProvider)) |
| 183 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 184 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", TestProviderClientToken)) |
| 185 |
responseRecorder := httptest.NewRecorder() |
| 186 |
|
| 187 |
router := http.NewServeMux() |
| 188 |
router.Handle("/handle", gate.ProtectWithPermissions(&testHandler{}, []string{SecondTestProviderClientPermission})) |
| 189 |
router.ServeHTTP(responseRecorder, request) |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if responseRecorder.Code != http.StatusOK { |
| 195 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestGate_ProtectWithPermissionsWhenValidTokenAndInsufficientPermissionsWhileUsingClientProvider(t *testing.T) { |
| 200 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClientProvider(mockClientProvider)) |
| 201 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 202 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", TestProviderClientToken)) |
| 203 |
responseRecorder := httptest.NewRecorder() |
| 204 |
|
| 205 |
router := http.NewServeMux() |
| 206 |
router.Handle("/handle", gate.ProtectWithPermissions(&testHandler{}, []string{"unrelated-permission"})) |
| 207 |
router.ServeHTTP(responseRecorder, request) |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 213 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func TestGate_ProtectWithPermissionsWhenClientHasSufficientPermissions(t *testing.T) { |
| 218 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("token").WithPermission("admin"))) |
| 219 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 220 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 221 |
responseRecorder := httptest.NewRecorder() |
| 222 |
|
| 223 |
router := http.NewServeMux() |
| 224 |
router.Handle("/handle", gate.ProtectWithPermissions(&testHandler{}, []string{"admin"})) |
| 225 |
router.ServeHTTP(responseRecorder, request) |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if responseRecorder.Code != http.StatusOK { |
| 230 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func TestGate_ProtectWithPermissionsWhenClientHasInsufficientPermissions(t *testing.T) { |
| 235 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClientWithPermissions("token", []string{"mod"}))) |
| 236 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 237 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 238 |
responseRecorder := httptest.NewRecorder() |
| 239 |
|
| 240 |
router := http.NewServeMux() |
| 241 |
router.Handle("/handle", gate.ProtectWithPermissions(&testHandler{}, []string{"admin"})) |
| 242 |
router.ServeHTTP(responseRecorder, request) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 247 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestGate_ProtectWithPermissions(t *testing.T) { |
| 252 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("mytoken").WithPermissions([]string{"create", "read", "update", "delete"}))) |
| 253 |
|
| 254 |
router := http.NewServeMux() |
| 255 |
router.Handle("/create", gate.ProtectWithPermissions(&testHandler{}, []string{"create"})) |
| 256 |
router.Handle("/read", gate.ProtectWithPermissions(&testHandler{}, []string{"read"})) |
| 257 |
router.Handle("/update", gate.ProtectWithPermissions(&testHandler{}, []string{"update"})) |
| 258 |
router.Handle("/delete", gate.ProtectWithPermissions(&testHandler{}, []string{"delete"})) |
| 259 |
router.Handle("/crud", gate.ProtectWithPermissions(&testHandler{}, []string{"create", "read", "update", "delete"})) |
| 260 |
router.Handle("/backup", gate.ProtectWithPermissions(&testHandler{}, []string{"read", "backup"})) |
| 261 |
|
| 262 |
checkRouterOutput := func(t *testing.T, router *http.ServeMux, url string, expectedResponseCode int) { |
| 263 |
t.Run(strings.TrimPrefix(url, "/"), func(t *testing.T) { |
| 264 |
request, _ := http.NewRequest("GET", url, http.NoBody) |
| 265 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "mytoken")) |
| 266 |
responseRecorder := httptest.NewRecorder() |
| 267 |
router.ServeHTTP(responseRecorder, request) |
| 268 |
if responseRecorder.Code != expectedResponseCode { |
| 269 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, expectedResponseCode, responseRecorder.Code) |
| 270 |
} |
| 271 |
}) |
| 272 |
} |
| 273 |
|
| 274 |
checkRouterOutput(t, router, "/create", http.StatusOK) |
| 275 |
checkRouterOutput(t, router, "/read", http.StatusOK) |
| 276 |
checkRouterOutput(t, router, "/update", http.StatusOK) |
| 277 |
checkRouterOutput(t, router, "/delete", http.StatusOK) |
| 278 |
checkRouterOutput(t, router, "/crud", http.StatusOK) |
| 279 |
checkRouterOutput(t, router, "/backup", http.StatusUnauthorized) |
| 280 |
} |
| 281 |
|
| 282 |
func TestGate_ProtectWithPermissionWhenClientHasSufficientPermissions(t *testing.T) { |
| 283 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("token").WithPermission("admin"))) |
| 284 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 285 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 286 |
responseRecorder := httptest.NewRecorder() |
| 287 |
|
| 288 |
router := http.NewServeMux() |
| 289 |
router.Handle("/handle", gate.ProtectWithPermission(&testHandler{}, "admin")) |
| 290 |
router.ServeHTTP(responseRecorder, request) |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if responseRecorder.Code != http.StatusOK { |
| 295 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
func TestGate_ProtectWithPermissionWhenClientHasInsufficientPermissions(t *testing.T) { |
| 300 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClientWithPermissions("token", []string{"mod"}))) |
| 301 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 302 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 303 |
responseRecorder := httptest.NewRecorder() |
| 304 |
|
| 305 |
router := http.NewServeMux() |
| 306 |
router.Handle("/handle", gate.ProtectWithPermission(&testHandler{}, "admin")) |
| 307 |
router.ServeHTTP(responseRecorder, request) |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 312 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
func TestGate_PermissionMiddlewareWhenClientHasSufficientPermissions(t *testing.T) { |
| 317 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("token").WithPermission("admin"))) |
| 318 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 319 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 320 |
responseRecorder := httptest.NewRecorder() |
| 321 |
|
| 322 |
router := http.NewServeMux() |
| 323 |
router.Handle("/handle", gate.PermissionMiddleware("admin")(&testHandler{})) |
| 324 |
router.ServeHTTP(responseRecorder, request) |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if responseRecorder.Code != http.StatusOK { |
| 329 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
func TestGate_PermissionMiddlewareWhenClientHasInsufficientPermissions(t *testing.T) { |
| 334 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClientWithPermissions("token", []string{"mod"}))) |
| 335 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 336 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 337 |
responseRecorder := httptest.NewRecorder() |
| 338 |
|
| 339 |
router := http.NewServeMux() |
| 340 |
router.Handle("/handle", gate.PermissionMiddleware("admin")(&testHandler{})) |
| 341 |
router.ServeHTTP(responseRecorder, request) |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 346 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestGate_ProtectFuncWithInvalidToken(t *testing.T) { |
| 351 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 352 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 353 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 354 |
responseRecorder := httptest.NewRecorder() |
| 355 |
|
| 356 |
router := http.NewServeMux() |
| 357 |
router.Handle("/handle", gate.ProtectFunc(testHandlerFunc)) |
| 358 |
router.ServeHTTP(responseRecorder, request) |
| 359 |
|
| 360 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 361 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
func TestGate_ProtectFuncWithValidToken(t *testing.T) { |
| 366 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 367 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 368 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "good-token")) |
| 369 |
responseRecorder := httptest.NewRecorder() |
| 370 |
|
| 371 |
router := http.NewServeMux() |
| 372 |
router.Handle("/handle", gate.ProtectFunc(testHandlerFunc)) |
| 373 |
router.ServeHTTP(responseRecorder, request) |
| 374 |
|
| 375 |
if responseRecorder.Code != http.StatusOK { |
| 376 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
func TestGate_ProtectFuncWithPermissionWhenClientHasSufficientPermissions(t *testing.T) { |
| 381 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("token").WithPermission("admin"))) |
| 382 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 383 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 384 |
responseRecorder := httptest.NewRecorder() |
| 385 |
|
| 386 |
router := http.NewServeMux() |
| 387 |
router.HandleFunc("/handle", gate.ProtectFuncWithPermission(testHandlerFunc, "admin")) |
| 388 |
router.ServeHTTP(responseRecorder, request) |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
if responseRecorder.Code != http.StatusOK { |
| 393 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
func TestGate_ProtectFuncWithPermissionWhenClientHasInsufficientPermissions(t *testing.T) { |
| 398 |
gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClientWithPermissions("token", []string{"mod"}))) |
| 399 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 400 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 401 |
responseRecorder := httptest.NewRecorder() |
| 402 |
|
| 403 |
router := http.NewServeMux() |
| 404 |
router.HandleFunc("/handle", gate.ProtectFuncWithPermission(testHandlerFunc, "admin")) |
| 405 |
router.ServeHTTP(responseRecorder, request) |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 410 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func TestGate_WithCustomUnauthorizedResponseBody(t *testing.T) { |
| 415 |
gate := New().WithAuthorizationService(NewAuthorizationService()).WithCustomUnauthorizedResponseBody([]byte("test")) |
| 416 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 417 |
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 418 |
responseRecorder := httptest.NewRecorder() |
| 419 |
|
| 420 |
router := http.NewServeMux() |
| 421 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 422 |
router.ServeHTTP(responseRecorder, request) |
| 423 |
|
| 424 |
if responseRecorder.Code != http.StatusUnauthorized { |
| 425 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 426 |
} |
| 427 |
if responseBody, _ := io.ReadAll(responseRecorder.Body); string(responseBody) != "test" { |
| 428 |
t.Errorf("%s %s should have returned %s, but returned %s instead", request.Method, request.URL, "test", string(responseBody)) |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
func TestGate_ProtectWithNoAuthorizationService(t *testing.T) { |
| 433 |
gate := New() |
| 434 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 435 |
responseRecorder := httptest.NewRecorder() |
| 436 |
|
| 437 |
router := http.NewServeMux() |
| 438 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 439 |
router.ServeHTTP(responseRecorder, request) |
| 440 |
|
| 441 |
if responseRecorder.Code != http.StatusOK { |
| 442 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
func TestGate_ProtectWithRateLimit(t *testing.T) { |
| 447 |
gate := New().WithRateLimit(2) |
| 448 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 449 |
router := http.NewServeMux() |
| 450 |
router.Handle("/handle", gate.Protect(&testHandler{})) |
| 451 |
|
| 452 |
responseRecorder := httptest.NewRecorder() |
| 453 |
router.ServeHTTP(responseRecorder, request) |
| 454 |
if responseRecorder.Code != http.StatusOK { |
| 455 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 456 |
} |
| 457 |
|
| 458 |
responseRecorder = httptest.NewRecorder() |
| 459 |
router.ServeHTTP(responseRecorder, request) |
| 460 |
if responseRecorder.Code != http.StatusOK { |
| 461 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 462 |
} |
| 463 |
|
| 464 |
responseRecorder = httptest.NewRecorder() |
| 465 |
router.ServeHTTP(responseRecorder, request) |
| 466 |
if responseRecorder.Code != http.StatusTooManyRequests { |
| 467 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusTooManyRequests, responseRecorder.Code) |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
time.Sleep(time.Second) |
| 472 |
|
| 473 |
responseRecorder = httptest.NewRecorder() |
| 474 |
router.ServeHTTP(responseRecorder, request) |
| 475 |
if responseRecorder.Code != http.StatusOK { |
| 476 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
func TestGate_WithCustomTokenExtractor(t *testing.T) { |
| 481 |
authorizationService := NewAuthorizationService().WithClientProvider(mockClientProvider) |
| 482 |
customTokenExtractorFunc := func(request *http.Request) string { |
| 483 |
sessionCookie, err := request.Cookie("session") |
| 484 |
if err != nil { |
| 485 |
return "" |
| 486 |
} |
| 487 |
return sessionCookie.Value |
| 488 |
} |
| 489 |
gate := New().WithAuthorizationService(authorizationService).WithCustomTokenExtractor(customTokenExtractorFunc) |
| 490 |
|
| 491 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 492 |
request.AddCookie(&http.Cookie{Name: "session", Value: TestProviderClientToken}) |
| 493 |
responseRecorder := httptest.NewRecorder() |
| 494 |
|
| 495 |
router := http.NewServeMux() |
| 496 |
router.Handle("/handle", gate.ProtectFunc(func(w http.ResponseWriter, r *http.Request) { |
| 497 |
if r.Context().Value(TokenContextKey) != TestProviderClientToken { |
| 498 |
t.Errorf("token should have been passed to the request context") |
| 499 |
} |
| 500 |
if r.Context().Value(DataContextKey) != TestProviderClientData { |
| 501 |
t.Errorf("data should have been passed to the request context") |
| 502 |
} |
| 503 |
w.WriteHeader(http.StatusOK) |
| 504 |
})) |
| 505 |
router.ServeHTTP(responseRecorder, request) |
| 506 |
|
| 507 |
if responseRecorder.Code != http.StatusOK { |
| 508 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
func TestGateWithCustomHeader(t *testing.T) { |
| 513 |
authorizationService := NewAuthorizationService().WithClientProvider(mockClientProvider) |
| 514 |
customTokenExtractorFunc := func(request *http.Request) string { |
| 515 |
return request.Header.Get("X-API-Token") |
| 516 |
} |
| 517 |
gate := New().WithAuthorizationService(authorizationService).WithCustomTokenExtractor(customTokenExtractorFunc) |
| 518 |
|
| 519 |
request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 520 |
request.Header.Add("X-API-Token", TestProviderClientToken) |
| 521 |
responseRecorder := httptest.NewRecorder() |
| 522 |
|
| 523 |
router := http.NewServeMux() |
| 524 |
router.Handle("/handle", gate.ProtectFunc(func(w http.ResponseWriter, r *http.Request) { |
| 525 |
if r.Context().Value(TokenContextKey) != TestProviderClientToken { |
| 526 |
t.Errorf("token should have been passed to the request context") |
| 527 |
} |
| 528 |
if r.Context().Value(DataContextKey) != TestProviderClientData { |
| 529 |
t.Errorf("data should have been passed to the request context") |
| 530 |
} |
| 531 |
w.WriteHeader(http.StatusOK) |
| 532 |
})) |
| 533 |
router.ServeHTTP(responseRecorder, request) |
| 534 |
|
| 535 |
if responseRecorder.Code != http.StatusOK { |
| 536 |
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 537 |
} |
| 538 |
} |