summaryrefslogtreecommitdiff
path: root/app/controllers/sparkles/controller_test.go
blob: 37e1a827309643a016d9095ff9601a50246b022b (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package sparkles

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/xlgmokha/x/pkg/serde"
	"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/db"
	"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 TestSparkles(t *testing.T) {
	t.Run("GET /sparkles", func(t *testing.T) {
		sparkle, _ := domain.NewSparkle("@tanuki for helping me")
		store := db.NewRepository[*domain.Sparkle]()
		store.Save(t.Context(), sparkle)

		mux := http.NewServeMux()
		controller := New(store)
		controller.MountTo(mux)

		t.Run("returns JSON", func(t *testing.T) {
			request, response := test.RequestResponse(
				"GET",
				"/sparkles",
				test.WithAcceptHeader(serde.JSON),
			)

			mux.ServeHTTP(response, request)

			assert.Equal(t, http.StatusOK, response.Code)
			items, err := serde.FromJSON[[]*domain.Sparkle](response.Body)
			require.NoError(t, err)

			assert.Equal(t, 1, len(items))
			assert.Equal(t, "@tanuki", items[0].Sparklee)
			assert.Equal(t, "for helping me", items[0].Reason)
		})
	})

	t.Run("POST /sparkles", func(t *testing.T) {
		t.Run("when a user is logged in", func(t *testing.T) {
			currentUser := domain.NewUser(domain.WithID[*domain.User](domain.ID("1")))

			t.Run("saves a new sparkle", func(t *testing.T) {
				repository := db.NewRepository[*domain.Sparkle]()
				mux := http.NewServeMux()
				controller := New(repository)
				controller.MountTo(mux)

				sparkle, _ := domain.NewSparkle("@tanuki for reviewing my code!")
				request, response := test.RequestResponse(
					"POST",
					"/sparkles",
					test.WithAcceptHeader(serde.JSON),
					test.WithContentType(sparkle, serde.JSON),
					test.WithContextKeyValue(t.Context(), cfg.CurrentUser, currentUser),
				)

				mux.ServeHTTP(response, request)

				require.Equal(t, http.StatusCreated, response.Code)

				t.Run("returns a JSON representation of the sparkle", func(t *testing.T) {
					item, err := serde.FromJSON[*domain.Sparkle](response.Body)
					require.NoError(t, err)

					assert.NotEmpty(t, item.ID)
					assert.Equal(t, "@tanuki", item.Sparklee)
					assert.Equal(t, "for reviewing my code!", item.Reason)
				})

				t.Run("saves the sparkle to the db", func(t *testing.T) {
					assert.Equal(t, 1, len(repository.All(t.Context())))
					item := repository.All(t.Context())[0]

					assert.Equal(t, "@tanuki", item.Sparklee)
					assert.Equal(t, "for reviewing my code!", item.Reason)
					assert.Equal(t, currentUser, item.Author)
				})
			})

			t.Run("prevents double WriteHeader when serialization fails", func(t *testing.T) {
				repository := db.NewRepository[*domain.Sparkle]()
				controller := New(repository)

				currentUser := domain.NewUser(domain.WithID[*domain.User](domain.ID("1")))
				sparkle, _ := domain.NewSparkle("@user for testing")

				request, response := test.RequestResponse(
					"POST",
					"/sparkles",
					test.WithAcceptHeader(serde.JSON),
					test.WithContentType(sparkle, serde.JSON),
					test.WithContextKeyValue(t.Context(), cfg.CurrentUser, currentUser),
				)

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