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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
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"
"github.com/xlgmokha/x/pkg/x"
"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"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/internal/stub"
)
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 := x.New[*domain.Sparkle](domain.WithText("@tanuki for helping me"))
store := db.NewRepository[*domain.Sparkle]()
store.Save(t.Context(), sparkle)
mux := http.NewServeMux()
controller := New(store, stub.AllowWith(t, "user:*", "read", "sparkle:*"))
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 authenticated", func(t *testing.T) {
currentUser := x.New[*domain.User](domain.WithID[*domain.User](domain.ID("1")))
repository := db.NewRepository[*domain.Sparkle]()
t.Run("when the user is authorized", func(t *testing.T) {
mux := http.NewServeMux()
controller := New(repository, stub.AllowWith(t, "user:1", "create", "sparkle:*"))
controller.MountTo(mux)
t.Run("saves a new sparkle", func(t *testing.T) {
sparkle := x.New[*domain.Sparkle](domain.WithText("@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) {
currentUser := x.New[*domain.User](domain.WithID[*domain.User](domain.ID("1")))
sparkle := x.New[*domain.Sparkle](domain.WithText("@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),
)
mux.ServeHTTP(&FailingResponseWriter{T: t, ResponseRecorder: response}, request)
})
})
t.Run("when the user is not authorized", func(t *testing.T) {
t.Skip()
mux := http.NewServeMux()
controller := New(repository, stub.Deny())
controller.MountTo(mux)
t.Run("returns an error", func(t *testing.T) {
sparkle := x.New[*domain.Sparkle](domain.WithText("@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.StatusForbidden, response.Code)
})
})
})
})
}
|