-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_describable_test.go
More file actions
233 lines (199 loc) · 5.85 KB
/
api_describable_test.go
File metadata and controls
233 lines (199 loc) · 5.85 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
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/gin-gonic/gin"
api "dappco.re/go/api"
)
type describableSpecGroup struct {
name string
basePath string
descs []api.RouteDescription
}
func (g *describableSpecGroup) Name() string { return g.name }
func (g *describableSpecGroup) BasePath() string { return g.basePath }
func (g *describableSpecGroup) RegisterRoutes(rg *gin.RouterGroup) {}
func (g *describableSpecGroup) Describe() []api.RouteDescription { return g.descs }
type describableHandler struct {
desc api.RouteDescription
operationID string
tags []string
summary string
longDescription string
}
func (h *describableHandler) Describe() api.RouteDescription {
if h == nil {
return api.RouteDescription{}
}
return h.desc
}
func (h *describableHandler) OperationID() string {
if h == nil {
return ""
}
return h.operationID
}
func (h *describableHandler) Tags() []string {
if h == nil {
return nil
}
return h.tags
}
func (h *describableHandler) Summary() string {
if h == nil {
return ""
}
return h.summary
}
func (h *describableHandler) Description() string {
if h == nil {
return ""
}
return h.longDescription
}
func buildDescribableOperation(t *testing.T, group api.RouteGroup, path, method string) map[string]any {
t.Helper()
builder := &api.SpecBuilder{
Title: "Test",
Version: "1.0.0",
}
data, err := builder.Build([]api.RouteGroup{group})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var spec map[string]any
if err := json.Unmarshal(data, &spec); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
paths := spec["paths"].(map[string]any)
pathItem, ok := paths[path].(map[string]any)
if !ok {
t.Fatalf("expected path %q in spec", path)
}
operation, ok := pathItem[method].(map[string]any)
if !ok {
t.Fatalf("expected %s operation on %q", method, path)
}
return operation
}
func TestDescribable_Good_HandlerMetadataFlowsToOpenAPI(t *testing.T) {
handler := &describableHandler{
desc: api.RouteDescription{
StatusCode: http.StatusCreated,
RequestBody: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{"type": "string"},
},
},
Response: map[string]any{
"type": "object",
"properties": map[string]any{
"id": map[string]any{"type": "string"},
},
},
},
operationID: "widgets_create",
tags: []string{"widgets", "catalog"},
summary: "Create widget",
longDescription: "Creates a widget and returns the stored record.",
}
group := &describableSpecGroup{
name: "widgets",
basePath: "/api/widgets",
descs: []api.RouteDescription{
{
Method: http.MethodPost,
Path: "/",
Handler: handler,
},
},
}
operation := buildDescribableOperation(t, group, "/api/widgets", "post")
if got := operation["operationId"]; got != "widgets_create" {
t.Fatalf("expected handler operationId, got %v", got)
}
if got := operation["summary"]; got != "Create widget" {
t.Fatalf("expected handler summary, got %v", got)
}
if got := operation["description"]; got != "Creates a widget and returns the stored record." {
t.Fatalf("expected handler description, got %v", got)
}
tags, ok := operation["tags"].([]any)
if !ok {
t.Fatalf("expected tags array, got %T", operation["tags"])
}
if len(tags) != 2 || tags[0] != "widgets" || tags[1] != "catalog" {
t.Fatalf("expected handler tags, got %v", tags)
}
requestBody := operation["requestBody"].(map[string]any)
content := requestBody["content"].(map[string]any)
schema := content["application/json"].(map[string]any)["schema"].(map[string]any)
properties := schema["properties"].(map[string]any)
if _, ok := properties["name"]; !ok {
t.Fatal("expected request body schema from handler Describe")
}
responses := operation["responses"].(map[string]any)
if _, ok := responses["201"]; !ok {
t.Fatal("expected status code from handler Describe")
}
}
func TestDescribable_Bad_MissingHandlerMetadataFallsBackSafely(t *testing.T) {
group := &describableSpecGroup{
name: "widgets",
basePath: "/api/widgets",
descs: []api.RouteDescription{
{
Method: http.MethodGet,
Path: "/status",
Summary: "Widget status",
Description: "Returns widget availability.",
Tags: []string{"status"},
Handler: &describableHandler{},
},
},
}
operation := buildDescribableOperation(t, group, "/api/widgets/status", "get")
if got := operation["operationId"]; got != "get_api_widgets_status" {
t.Fatalf("expected generated operationId fallback, got %v", got)
}
if got := operation["summary"]; got != "Widget status" {
t.Fatalf("expected route summary fallback, got %v", got)
}
if got := operation["description"]; got != "Returns widget availability." {
t.Fatalf("expected route description fallback, got %v", got)
}
tags, ok := operation["tags"].([]any)
if !ok {
t.Fatalf("expected tags array, got %T", operation["tags"])
}
if len(tags) != 1 || tags[0] != "status" {
t.Fatalf("expected route tag fallback, got %v", tags)
}
}
func TestDescribable_Ugly_NilHandlerIsIgnored(t *testing.T) {
group := &describableSpecGroup{
name: "widgets",
basePath: "/api/widgets",
descs: []api.RouteDescription{
{
Method: http.MethodGet,
Path: "/status",
Handler: (*describableHandler)(nil),
},
},
}
operation := buildDescribableOperation(t, group, "/api/widgets/status", "get")
if got := operation["operationId"]; got != "get_api_widgets_status" {
t.Fatalf("expected generated operationId with nil handler, got %v", got)
}
tags, ok := operation["tags"].([]any)
if !ok {
t.Fatalf("expected tags array, got %T", operation["tags"])
}
if len(tags) != 1 || tags[0] != "widgets" {
t.Fatalf("expected group-name tag fallback, got %v", tags)
}
}