From 6c9e77907f287d0610436df53064efc27d071aa7 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Fri, 21 Jan 2022 18:21:34 +0100 Subject: [PATCH 1/3] Fix flipped expected and actual parameters in main tests --- main_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 6f33974..1c0feeb 100644 --- a/main_test.go +++ b/main_test.go @@ -21,7 +21,7 @@ func TestIndexRoute(t *testing.T) { IndexHandler(responseRecorder, request) - assert.Equal(t, responseRecorder.Code, 200, "Response code should be 200") + assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200") assert.NotNil(t, responseRecorder.Body, "Response should contain body") }) } @@ -44,7 +44,7 @@ func TestEndpointRoute(t *testing.T) { router.ServeHTTP(responseRecorder, request) - assert.Equal(t, responseRecorder.Code, 200) + assert.Equal(t, 200, responseRecorder.Code) body, _ := json.Marshal(data) assert.JSONEq(t, string(body), responseRecorder.Body.String()) }) @@ -57,6 +57,6 @@ func TestEndpointRoute(t *testing.T) { router.ServeHTTP(responseRecorder, request) - assert.Equal(t, responseRecorder.Code, 404) + assert.Equal(t, 404, responseRecorder.Code) }) } From 6a1a5e6985d344d16efc66e75dc992c6c892b2f1 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Fri, 21 Jan 2022 18:42:22 +0100 Subject: [PATCH 2/3] Fix various formatting --- auth/authorization.go | 2 +- pipelines/executable_step.go | 3 ++- pipelines/executable_step_test.go | 3 ++- pipelines/pipeline.go | 5 +++-- pipelines/pipeline_test.go | 3 ++- pipelines/step_test.go | 3 ++- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/auth/authorization.go b/auth/authorization.go index 5048228..8d3a14d 100644 --- a/auth/authorization.go +++ b/auth/authorization.go @@ -23,6 +23,6 @@ func (middleware AuthenticationMiddleware) Middleware(next http.Handler) http.Ha func CreateAuthenticationMiddleware(secret string) AuthenticationMiddleware { return AuthenticationMiddleware{ - secret: secret, + secret, } } diff --git a/pipelines/executable_step.go b/pipelines/executable_step.go index 518b839..c0bfedb 100644 --- a/pipelines/executable_step.go +++ b/pipelines/executable_step.go @@ -3,8 +3,9 @@ package pipelines import ( "errors" "fmt" - "github.com/disintegration/imaging" "image" + + "github.com/disintegration/imaging" ) type IExecutableStep interface { diff --git a/pipelines/executable_step_test.go b/pipelines/executable_step_test.go index 4be7ae8..2445471 100644 --- a/pipelines/executable_step_test.go +++ b/pipelines/executable_step_test.go @@ -1,8 +1,9 @@ package pipelines import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeserializeOptionsResizeImage(t *testing.T) { diff --git a/pipelines/pipeline.go b/pipelines/pipeline.go index e9a783a..c5cc9b8 100644 --- a/pipelines/pipeline.go +++ b/pipelines/pipeline.go @@ -5,12 +5,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/disintegration/imaging" - "github.com/geplauder/lithium/storage" "io/fs" "log" "os" "path/filepath" + + "github.com/disintegration/imaging" + "github.com/geplauder/lithium/storage" ) // Pipelines diff --git a/pipelines/pipeline_test.go b/pipelines/pipeline_test.go index 4bee961..0b2a20c 100644 --- a/pipelines/pipeline_test.go +++ b/pipelines/pipeline_test.go @@ -1,12 +1,13 @@ package pipelines import ( - "github.com/geplauder/lithium/storage" "image" "os" "path/filepath" "testing" + "github.com/geplauder/lithium/storage" + "github.com/stretchr/testify/assert" ) diff --git a/pipelines/step_test.go b/pipelines/step_test.go index f731f5f..d7d4595 100644 --- a/pipelines/step_test.go +++ b/pipelines/step_test.go @@ -1,8 +1,9 @@ package pipelines import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestTranslateStep(t *testing.T) { From 76d15be087f910c1ed76e62398732d650eadcd39 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Fri, 21 Jan 2022 18:44:32 +0100 Subject: [PATCH 3/3] Add additional test for AuthenticationMiddleware that checks for Bearer prefix --- auth/authorization_test.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/auth/authorization_test.go b/auth/authorization_test.go index 139f416..36075ea 100644 --- a/auth/authorization_test.go +++ b/auth/authorization_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestAuthorizationMiddleware(t *testing.T) { +func TestAuthenticationMiddleware(t *testing.T) { token := faker.Word() middleware := CreateAuthenticationMiddleware(token) @@ -19,7 +19,7 @@ func TestAuthorizationMiddleware(t *testing.T) { middlewareHandler := middleware.Middleware(handler) - t.Run("AuthorizationMiddleware returns 403 response when authorization header is incorrect", func(t *testing.T) { + t.Run("AuthenticationMiddleware returns 403 response when authorization header is incorrect", func(t *testing.T) { request, _ := http.NewRequest("GET", "/", nil) responseRecorder := httptest.NewRecorder() @@ -28,7 +28,17 @@ func TestAuthorizationMiddleware(t *testing.T) { assert.Equal(t, 403, responseRecorder.Code) }) - t.Run("AuthorizationMiddleware continues when authorization header is correct", func(t *testing.T) { + t.Run("AuthenticationMiddleware returns 403 response when authorization header is missing Bearer prefix", func(t *testing.T) { + request, _ := http.NewRequest("GET", "/", nil) + request.Header.Set("Authorization", token) + responseRecorder := httptest.NewRecorder() + + middlewareHandler.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 403, responseRecorder.Code) + }) + + t.Run("AuthenticationMiddleware continues when authorization header is correct", func(t *testing.T) { request, _ := http.NewRequest("GET", "/", nil) request.Header.Set("Authorization", "Bearer "+token) responseRecorder := httptest.NewRecorder()