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
|
package sparkles
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xlgmokha/x/pkg/serde"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test"
)
func TestSparkles(t *testing.T) {
t.Run("GET /sparkles", func(t *testing.T) {
sparkle, _ := domain.NewSparkle("@tanuki for helping me")
store := db.NewRepository()
store.Save(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("saves a new sparkle", func(t *testing.T) {
repository := db.NewRepository()
mux := http.NewServeMux()
controller := New(repository)
controller.MountTo(mux)
sparkle, _ := domain.NewSparkle("@tanuki for reviewing my MR!")
request, response := test.RequestResponse(
"POST",
"/sparkles",
test.WithAcceptHeader(serde.JSON),
test.WithContentType(sparkle, serde.JSON),
)
mux.ServeHTTP(response, request)
require.Equal(t, http.StatusCreated, response.Code)
assert.Equal(t, 1, len(repository.All()))
})
})
}
|