summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard/controller_test.go
blob: 34725651bb64596cce985867908f9fc17d02dd50 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package dashboard

import (
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/xlgmokha/x/pkg/test"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
)

type FailingResponseWriter struct {
	*testing.T
	*httptest.ResponseRecorder
	headerWritten bool
}

func (f *FailingResponseWriter) WriteHeader(statusCode int) {
	require.False(f.T, f.headerWritten)
	f.headerWritten = true
	f.ResponseRecorder.WriteHeader(statusCode)
}

func (f *FailingResponseWriter) Write([]byte) (int, error) {
	return 0, errors.New("write failed")
}

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) {
			r, w := test.RequestResponse("GET", "/dashboard")

			mux.ServeHTTP(w, r)

			t.Run("redirects to the home page", func(t *testing.T) {
				require.Equal(t, http.StatusNotFound, w.Code)
			})
		})

		t.Run("when authenticated", func(t *testing.T) {
			ctx := cfg.CurrentUser.With(t.Context(), &domain.User{ID: domain.ID("1")})
			r, w := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx))
			mux.ServeHTTP(w, r)

			t.Run("renders a dashboard page", func(t *testing.T) {
				assert.Equal(t, http.StatusOK, w.Code)
				assert.Equal(t, "text/html", w.Header().Get("Content-Type"))
				assert.Contains(t, w.Body.String(), "<html")
			})
		})

		t.Run("prevents double WriteHeader when template rendering fails", func(t *testing.T) {
			ctx := cfg.CurrentUser.With(t.Context(), &domain.User{ID: domain.ID("1")})
			request, response := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx))

			controller.Show(&FailingResponseWriter{T: t, ResponseRecorder: response}, request)
		})
	})

	t.Run("GET /dashboard/nav", func(t *testing.T) {
		t.Run("when unauthenticated", func(t *testing.T) {
			r, w := test.RequestResponse("GET", "/dashboard/nav")

			mux.ServeHTTP(w, r)

			t.Run("renders the site header", func(t *testing.T) {
				assert.Equal(t, http.StatusOK, w.Code)
				assert.Equal(t, "text/html", w.Header().Get("Content-Type"))
				assert.Contains(t, w.Body.String(), "Login")
			})
		})

		t.Run("when authenticated", func(t *testing.T) {
			ctx := cfg.CurrentUser.With(t.Context(), &domain.User{
				ID:       domain.ID("1"),
				Username: "root",
			})
			r, w := test.RequestResponse("GET", "/dashboard/nav", test.WithContext(ctx))
			mux.ServeHTTP(w, r)

			t.Run("renders the site header", func(t *testing.T) {
				assert.Equal(t, http.StatusOK, w.Code)
				assert.Equal(t, "text/html", w.Header().Get("Content-Type"))
				assert.Contains(t, w.Body.String(), "root")
			})
		})
	})
}