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