From a0370a78ece12988a1c161d08950ae0a67a965f6 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Thu, 20 Jan 2022 21:11:09 +0100 Subject: [PATCH] Fix assertion usage and use randomly generated token in auth middleware tests --- auth/authorization_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/auth/authorization_test.go b/auth/authorization_test.go index a8fc6c8..7f8d45e 100644 --- a/auth/authorization_test.go +++ b/auth/authorization_test.go @@ -5,17 +5,20 @@ import ( "net/http/httptest" "testing" + "github.com/bxcodec/faker/v3" "github.com/stretchr/testify/assert" ) func TestAuthorizationMiddleware(t *testing.T) { + token := faker.Word() + t.Run("AuthorizationMiddleware returns 403 response when authorization header is incorrect", func(t *testing.T) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) middleware := AuthenticationMiddleware{ - Secret: "foo", + Secret: token, } middlewareHandler := middleware.Middleware(handler) @@ -25,7 +28,7 @@ func TestAuthorizationMiddleware(t *testing.T) { middlewareHandler.ServeHTTP(responseRecorder, request) - assert.Equal(t, responseRecorder.Code, 403) + assert.Equal(t, 403, responseRecorder.Code) }) t.Run("AuthorizationMiddleware continues when authorization header is correct", func(t *testing.T) { @@ -34,17 +37,17 @@ func TestAuthorizationMiddleware(t *testing.T) { }) middleware := AuthenticationMiddleware{ - Secret: "foo", + Secret: token, } middlewareHandler := middleware.Middleware(handler) request, _ := http.NewRequest("GET", "/", nil) - request.Header.Set("Authorization", "foo") + request.Header.Set("Authorization", token) responseRecorder := httptest.NewRecorder() middlewareHandler.ServeHTTP(responseRecorder, request) - assert.Equal(t, responseRecorder.Code, 200) + assert.Equal(t, 200, responseRecorder.Code) }) }