From 5dc20844d7d03438cdb0eb01f46c612ba6d22fb9 Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Fri, 18 Aug 2023 23:08:49 +0800 Subject: [PATCH 1/3] Add test for router error handling --- internal/router/errors_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 internal/router/errors_test.go diff --git a/internal/router/errors_test.go b/internal/router/errors_test.go new file mode 100644 index 0000000..1529b2f --- /dev/null +++ b/internal/router/errors_test.go @@ -0,0 +1,22 @@ +package router + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +// func handleAPIError(handler func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc { + +func TestHandleAPIError(t *testing.T) { + mockController := func(w http.ResponseWriter, r *http.Request) error { + return nil + } + t.Run("Should return a http.HandlerFunc", func(t *testing.T) { + var handler interface{} = handleAPIError(mockController) + assert.NotNil(t, handler, "Should not be nil") + _, ok := handler.(http.HandlerFunc) + assert.True(t, ok, "Should be a http.HandlerFunc") + }) +} From 80f757ff700cb25565807cf6d4ff504919c9589e Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Fri, 18 Aug 2023 23:09:03 +0800 Subject: [PATCH 2/3] Add tests for permissions validation --- internal/permissions/validation_test.go | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 internal/permissions/validation_test.go diff --git a/internal/permissions/validation_test.go b/internal/permissions/validation_test.go new file mode 100644 index 0000000..d034961 --- /dev/null +++ b/internal/permissions/validation_test.go @@ -0,0 +1,77 @@ +package permissions + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +type test_alwaysTrue struct { + PermissionGroup +} + +func (t test_alwaysTrue) IsAuthorized(r *http.Request) bool { + return true +} + +type test_alwaysFalse struct { + PermissionGroup +} + +func (t test_alwaysFalse) IsAuthorized(r *http.Request) bool { + return false +} + +func TestValidateAllOf(t *testing.T) { + t.Run("should return true in the trivial case without any permissions", func(t *testing.T) { + groups := []PermissionGroup{} + allOf := AllOf{Groups: groups} + assert.True(t, allOf.IsAuthorized(nil)) + }) + t.Run("should return true if all groups return true", func(t *testing.T) { + groups := []PermissionGroup{ + test_alwaysTrue{}, + test_alwaysTrue{}, + test_alwaysTrue{}, + } + allOf := AllOf{Groups: groups} + assert.True(t, allOf.IsAuthorized(nil)) + }) + t.Run("should return false if any group returns false", func(t *testing.T) { + groups := []PermissionGroup{ + test_alwaysTrue{}, + test_alwaysFalse{}, + test_alwaysTrue{}, + } + allOf := AllOf{Groups: groups} + assert.False(t, allOf.IsAuthorized(nil)) + }) +} + +func TestValidateAnyOf(t *testing.T) { + t.Run("should return true in the trivial case without any permissions", func(t *testing.T) { + groups := []PermissionGroup{} + anyOf := AnyOf{Groups: groups} + assert.True(t, anyOf.IsAuthorized(nil)) + }) + t.Run("should return false if all groups return false", func(t *testing.T) { + groups := []PermissionGroup{ + test_alwaysFalse{}, + test_alwaysFalse{}, + test_alwaysFalse{}, + } + anyOf := AnyOf{Groups: groups} + assert.False(t, anyOf.IsAuthorized(nil)) + }) + t.Run("should return true if any group returns true", func(t *testing.T) { + groups := []PermissionGroup{ + test_alwaysFalse{}, + test_alwaysTrue{}, + test_alwaysFalse{}, + } + anyOf := AnyOf{Groups: groups} + assert.True(t, anyOf.IsAuthorized(nil)) + }) + +} From 0b18858bdc9442e1e3fcf769c277c03d73f9717f Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Wed, 23 Aug 2023 14:38:54 +0800 Subject: [PATCH 3/3] Remove commented code --- internal/router/errors_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/router/errors_test.go b/internal/router/errors_test.go index 1529b2f..35178df 100644 --- a/internal/router/errors_test.go +++ b/internal/router/errors_test.go @@ -7,8 +7,6 @@ import ( "github.com/stretchr/testify/assert" ) -// func handleAPIError(handler func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc { - func TestHandleAPIError(t *testing.T) { mockController := func(w http.ResponseWriter, r *http.Request) error { return nil