diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-25 11:08:58 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-25 11:08:58 -0600 |
| commit | 2b1e14690ea6426a67c0faaaddcfb8aa7360dce7 (patch) | |
| tree | 7f764225e3e3a26bbd7532e72ab99a54e465be92 /app/middleware/require_user_test.go | |
| parent | 0053db0d265af313dd281db5cf1e73236cde30c6 (diff) | |
refactor: move db and mountable to app
Diffstat (limited to 'app/middleware/require_user_test.go')
| -rw-r--r-- | app/middleware/require_user_test.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/middleware/require_user_test.go b/app/middleware/require_user_test.go new file mode 100644 index 0000000..68b9911 --- /dev/null +++ b/app/middleware/require_user_test.go @@ -0,0 +1,43 @@ +package middleware + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" +) + +func TestRequireUser(t *testing.T) { + middleware := RequireUser(http.StatusFound, "/login") + + t.Run("when a user is not logged in", func(t *testing.T) { + t.Run("redirects to the homepage", func(t *testing.T) { + r, w := test.RequestResponse("GET", "/example") + + server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Fail(t, "unexpected call to handler") + })) + server.ServeHTTP(w, r) + + require.Equal(t, http.StatusFound, w.Code) + assert.Equal(t, "/login", w.Header().Get("Location")) + }) + }) + + t.Run("when a user is logged in", func(t *testing.T) { + t.Run("forwards the request", func(t *testing.T) { + r, w := test.RequestResponse("GET", "/example", test.WithContextKeyValue(t.Context(), key.CurrentUser, &domain.User{})) + + server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusTeapot) + })) + server.ServeHTTP(w, r) + + require.Equal(t, http.StatusTeapot, w.Code) + }) + }) +} |
