-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity_test.go
More file actions
295 lines (279 loc) · 9.21 KB
/
Copy pathsecurity_test.go
File metadata and controls
295 lines (279 loc) · 9.21 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package stdocs
import (
"net/http"
"strings"
"testing"
)
func TestWithBearerAuth_RegistersScheme(t *testing.T) {
m := New(WithTitle("T"), WithBearerAuth("bearerAuth", "JWT"))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "bearerAuth").(map[string]any)
if scheme["type"] != "http" {
t.Errorf("type = %v", scheme["type"])
}
if scheme["scheme"] != "bearer" {
t.Errorf("scheme = %v", scheme["scheme"])
}
if scheme["bearerFormat"] != "JWT" {
t.Errorf("bearerFormat = %v", scheme["bearerFormat"])
}
}
func TestWithBearerAuth_NoFormat(t *testing.T) {
m := New(WithTitle("T"), WithBearerAuth("bearerAuth", ""))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "bearerAuth").(map[string]any)
if _, has := scheme["bearerFormat"]; has {
t.Errorf("bearerFormat should be absent when empty, got %v", scheme["bearerFormat"])
}
}
func TestWithBasicAuth(t *testing.T) {
m := New(WithTitle("T"), WithBasicAuth("basicAuth"))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "basicAuth").(map[string]any)
if scheme["scheme"] != "basic" {
t.Errorf("scheme = %v", scheme["scheme"])
}
}
func TestWithAPIKeyAuth_Header(t *testing.T) {
m := New(WithTitle("T"), WithAPIKeyAuth("apiKeyAuth", "header", "X-API-Key"))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "apiKeyAuth").(map[string]any)
if scheme["type"] != "apiKey" {
t.Errorf("type = %v", scheme["type"])
}
if scheme["in"] != "header" {
t.Errorf("in = %v", scheme["in"])
}
if scheme["name"] != "X-API-Key" {
t.Errorf("name = %v", scheme["name"])
}
}
func TestWithAPIKeyAuth_Query(t *testing.T) {
m := New(WithTitle("T"), WithAPIKeyAuth("k", "query", "api_key"))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "k").(map[string]any)
if scheme["in"] != "query" {
t.Errorf("in = %v", scheme["in"])
}
}
func TestWithOAuth2Auth(t *testing.T) {
m := New(WithTitle("T"), WithOAuth2Auth("oauth2", OAuthFlows{
AuthorizationCode: &OAuthFlow{
AuthorizationURL: "https://example.com/auth",
TokenURL: "https://example.com/token",
Scopes: map[string]string{
"read": "Read access",
"write": "Write access",
},
},
}))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "oauth2").(map[string]any)
if scheme["type"] != "oauth2" {
t.Errorf("type = %v", scheme["type"])
}
flows := jget(t, scheme, "flows", "authorizationCode").(map[string]any)
if flows["authorizationUrl"] != "https://example.com/auth" {
t.Errorf("authorizationUrl = %v", flows["authorizationUrl"])
}
if flows["tokenUrl"] != "https://example.com/token" {
t.Errorf("tokenUrl = %v", flows["tokenUrl"])
}
scopes := jget(t, flows, "scopes").(map[string]any)
if scopes["read"] != "Read access" {
t.Errorf("scopes[read] = %v", scopes["read"])
}
}
func TestWithSecurity_PerRoute(t *testing.T) {
m := New(WithTitle("T"), WithBearerAuth("bearerAuth", "JWT"))
m.HandleFunc("GET /public", func(w http.ResponseWriter, r *http.Request) {},
WithNoSecurity(),
)
m.HandleFunc("GET /private", func(w http.ResponseWriter, r *http.Request) {},
WithSecurity("bearerAuth"),
)
b, _ := m.JSON()
doc := jx(t, b)
// /public has an empty `security: []` array (opt-out).
pubOp := jget(t, doc, "paths", "/public", "get").(map[string]any)
secPub, has := pubOp["security"]
if !has {
t.Errorf("/public should have a security key (empty array), got nothing")
}
arrPub, ok := secPub.([]any)
if !ok || len(arrPub) != 0 {
t.Errorf("/public.security = %v, want empty array (opt-out)", secPub)
}
// /private has the bearer requirement.
privOp := jget(t, doc, "paths", "/private", "get").(map[string]any)
sec := jget(t, privOp, "security").([]any)
if len(sec) != 1 {
t.Fatalf("len(security) = %d, want 1", len(sec))
}
entry := sec[0].(map[string]any)
if _, has := entry["bearerAuth"]; !has {
t.Errorf("entry = %v, want bearerAuth", entry)
}
}
func TestWithSecurity_OAuthScopes(t *testing.T) {
m := New(WithTitle("T"), WithOAuth2Auth("oauth2", OAuthFlows{
ClientCredentials: &OAuthFlow{
TokenURL: "https://example.com/token",
Scopes: map[string]string{"read": "Read"},
},
}))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {},
WithSecurity("oauth2", "read", "write"),
)
b, _ := m.JSON()
doc := jx(t, b)
sec := jget(t, doc, "paths", "/x", "get", "security").([]any)
entry := sec[0].(map[string]any)
scopes := entry["oauth2"].([]any)
if len(scopes) != 2 {
t.Errorf("scopes = %v, want [read write]", scopes)
}
}
func TestWithGlobalSecurity(t *testing.T) {
m := New(
WithTitle("T"),
WithBearerAuth("bearerAuth", "JWT"),
WithGlobalSecurity("bearerAuth"),
)
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
// Top-level security should be present.
sec := jget(t, doc, "security").([]any)
if len(sec) != 1 {
t.Fatalf("top-level security = %d, want 1", len(sec))
}
entry := sec[0].(map[string]any)
if _, has := entry["bearerAuth"]; !has {
t.Errorf("entry = %v", entry)
}
}
func TestWithGlobalSecurity_OverriddenByRoute(t *testing.T) {
m := New(
WithTitle("T"),
WithBearerAuth("bearerAuth", "JWT"),
WithGlobalSecurity("bearerAuth"),
)
m.HandleFunc("GET /public", func(w http.ResponseWriter, r *http.Request) {},
WithNoSecurity(),
)
b, _ := m.JSON()
doc := jx(t, b)
pubOp := jget(t, doc, "paths", "/public", "get").(map[string]any)
// WithNoSecurity emits an empty `security: []` array on the
// operation, overriding the globally-applied bearerAuth. The
// presence of the key (with an empty array) is what does the
// override; a missing key would inherit.
sec, has := pubOp["security"]
if !has {
t.Fatalf("/public should have a security key, got nothing")
}
arr, ok := sec.([]any)
if !ok {
t.Fatalf("/public.security = %T, want []any", sec)
}
if len(arr) != 0 {
t.Errorf("/public.security = %v, want empty array (opt-out)", arr)
}
}
func TestWithSecurityScheme_Custom(t *testing.T) {
m := New(WithTitle("T"), WithSecurityScheme("customAuth", SecurityScheme{
Type: "apiKey",
In: "cookie",
Name: "session",
Description: "Session cookie",
}))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
scheme := jget(t, doc, "components", "securitySchemes", "customAuth").(map[string]any)
if scheme["description"] != "Session cookie" {
t.Errorf("description = %v", scheme["description"])
}
if scheme["in"] != "cookie" {
t.Errorf("in = %v", scheme["in"])
}
}
// TestWithSecurity_UnregisteredScheme guards the new validation:
// a security requirement that references a scheme name not
// registered in components.securitySchemes is an invalid spec
// and JSON() should return an error.
func TestWithSecurity_UnregisteredScheme(t *testing.T) {
m := New(WithTitle("T"))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {},
WithSecurity("missingScheme"),
)
_, err := m.JSON()
if err == nil {
t.Fatal("expected error for unregistered security scheme, got nil")
}
if !strings.Contains(err.Error(), "missingScheme") {
t.Errorf("error = %q, expected it to mention the missing scheme name", err)
}
}
// TestWithSecurity_RegisteredScheme passes validation: the scheme
// is in components.securitySchemes, so JSON() succeeds.
func TestWithSecurity_RegisteredScheme(t *testing.T) {
m := New(WithTitle("T"), WithBearerAuth("bearerAuth", "JWT"))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {},
WithSecurity("bearerAuth"),
)
_, err := m.JSON()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestWithSecurity_MultipleSchemes(t *testing.T) {
m := New(
WithTitle("T"),
WithBearerAuth("bearerAuth", ""),
WithAPIKeyAuth("apiKeyAuth", "header", "X-Key"),
)
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {},
// Operation accepts EITHER auth.
WithSecurity("bearerAuth"),
WithSecurity("apiKeyAuth"),
)
b, _ := m.JSON()
doc := jx(t, b)
sec := jget(t, doc, "paths", "/x", "get", "security").([]any)
if len(sec) != 2 {
t.Errorf("security entries = %d, want 2", len(sec))
}
// Both schemes should be in components.
comps := jget(t, doc, "components", "securitySchemes").(map[string]any)
if _, ok := comps["bearerAuth"]; !ok {
t.Errorf("bearerAuth missing from components")
}
if _, ok := comps["apiKeyAuth"]; !ok {
t.Errorf("apiKeyAuth missing from components")
}
}
func TestWithSecurity_EmptyNameIgnored(t *testing.T) {
// Should not panic or add an empty-named requirement.
m := New(WithTitle("T"), WithBearerAuth("bearerAuth", ""))
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {},
WithSecurity(""),
)
_, err := m.JSON()
if err != nil {
t.Fatal(err)
}
}