-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi_gate_test.go
More file actions
59 lines (54 loc) · 1.62 KB
/
openapi_gate_test.go
File metadata and controls
59 lines (54 loc) · 1.62 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
package arc
import (
"context"
"net/http"
"testing"
)
func TestValidateOpenAPIQualityPasses(t *testing.T) {
type in struct {
Name string `json:"name"`
}
type out struct {
ID int64 `json:"id"`
}
e := New()
e.SetOpenAPIServers([]map[string]any{{"url": "http://localhost:8080/api/v1", "description": "dev"}})
e.RegisterOpenAPISecurityScheme("BearerAuth", map[string]any{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT",
})
Handle(e, http.MethodPost, "/users", "users_create_qg", func(ctx context.Context, in *in) (*Response[out], error) {
return OK(out{ID: 1}), nil
}, WithTags("Users"), WithSecurity("BearerAuth"), WithRequestExamples(map[string]any{
"ok": map[string]any{"name": "alice"},
}))
issues := ValidateOpenAPIQuality(e.OpenAPISpec(), OpenAPIQualityGates{
RequireRootTags: true,
RequireServers: true,
RequireExamples: true,
RequiredSecuritySchemes: []string{"BearerAuth"},
})
if len(issues) != 0 {
t.Fatalf("expected no issues, got %v", issues)
}
}
func TestValidateOpenAPIQualityDetectsMissingFields(t *testing.T) {
type in struct{}
type out struct {
OK bool `json:"ok"`
}
e := New()
Handle(e, http.MethodGet, "/x", "x_get_qg", func(ctx context.Context, in *in) (*Response[out], error) {
return OK(out{OK: true}), nil
})
issues := ValidateOpenAPIQuality(e.OpenAPISpec(), OpenAPIQualityGates{
RequireRootTags: true,
RequireServers: true,
RequireExamples: true,
RequiredSecuritySchemes: []string{"BearerAuth"},
})
if len(issues) != 4 {
t.Fatalf("expected 4 issues, got %d: %v", len(issues), issues)
}
}