diff options
Diffstat (limited to 'app/controllers/dashboard')
| -rw-r--r-- | app/controllers/dashboard/controller.go | 20 | ||||
| -rw-r--r-- | app/controllers/dashboard/controller_test.go | 12 |
2 files changed, 30 insertions, 2 deletions
diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go index 060dbfa..cd1c44f 100644 --- a/app/controllers/dashboard/controller.go +++ b/app/controllers/dashboard/controller.go @@ -1,6 +1,14 @@ package dashboard -import "net/http" +import ( + "net/http" + + "github.com/xlgmokha/x/pkg/context" + "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain" +) + +var CurrentUserKey context.Key[*domain.User] = context.Key[*domain.User]("current_user") type Controller struct { } @@ -14,5 +22,13 @@ func (c *Controller) MountTo(mux *http.ServeMux) { } func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/", http.StatusFound) + currentUser := CurrentUserKey.From(r.Context()) + + if x.IsZero(currentUser) { + http.Redirect(w, r, "/", http.StatusFound) + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "text/html") } diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go index 0bfb3d6..ac9e10d 100644 --- a/app/controllers/dashboard/controller_test.go +++ b/app/controllers/dashboard/controller_test.go @@ -4,6 +4,7 @@ import ( "net/http" "testing" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" "gotest.tools/v3/assert" ) @@ -25,5 +26,16 @@ func TestController(t *testing.T) { assert.Equal(t, "/", location) }) }) + + t.Run("when authenticated", func(t *testing.T) { + t.Run("renders a dashboard page", func(t *testing.T) { + ctx := CurrentUserKey.With(t.Context(), &domain.User{}) + r, w := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx)) + + mux.ServeHTTP(w, r) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "text/html", w.Header().Get("Content-Type")) + }) + }) }) } |
