summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard/controller_test.go
blob: ac9e10dc12fbf6190849285a94304001094cc464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package dashboard

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"
)

func TestController(t *testing.T) {
	mux := http.NewServeMux()
	controller := New()
	controller.MountTo(mux)

	t.Run("GET /dashboard", func(t *testing.T) {
		t.Run("when unauthenticated", func(t *testing.T) {
			t.Run("redirects to the home page", func(t *testing.T) {
				r, w := test.RequestResponse("GET", "/dashboard")

				mux.ServeHTTP(w, r)

				assert.Equal(t, http.StatusFound, w.Code)
				location := w.HeaderMap.Get("Location")
				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"))
			})
		})
	})
}