-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_test.go
More file actions
487 lines (444 loc) · 14.9 KB
/
post_test.go
File metadata and controls
487 lines (444 loc) · 14.9 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package fiberoapi
import (
"fmt"
"io"
"net/http/httptest"
"strings"
"testing"
"github.com/gofiber/fiber/v2"
)
// Test structs pour POST
type PostUserInput struct {
Username string `json:"username" validate:"required,min=3,max=20,alphanum"`
Email string `json:"email" validate:"required,email"`
Age int `json:"age" validate:"required,min=13,max=120"`
Bio string `json:"bio" validate:"omitempty,max=500"`
}
type PostUserWithPathInput struct {
GroupID string `path:"groupId" validate:"required,uuid4"`
Username string `json:"username" validate:"required,min=3,max=20"`
Email string `json:"email" validate:"required,email"`
Role string `json:"role" validate:"required,oneof=member admin"`
}
type PostProductInput struct {
CategoryID string `path:"categoryId" validate:"required,uuid4"`
Name string `json:"name" validate:"required,min=2,max=100"`
Description string `json:"description" validate:"omitempty,max=1000"`
Price float64 `json:"price" validate:"required,min=0"`
Quantity int `json:"quantity" validate:"required,min=0"`
Tags []string `json:"tags" validate:"omitempty,dive,min=1,max=20"`
}
type PostOutput struct {
ID string `json:"id"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
type PostError struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
func TestPostOApi_SimplePost(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Test with simple POST (JSON body only)
Post(oapi, "/users", func(c *fiber.Ctx, input PostUserInput) (PostOutput, PostError) {
return PostOutput{
ID: "user_123",
Message: fmt.Sprintf("User %s created successfully", input.Username),
Data: map[string]interface{}{
"username": input.Username,
"email": input.Email,
"age": input.Age,
"bio": input.Bio,
},
}, PostError{}
}, OpenAPIOptions{
OperationID: "create-user",
Summary: "Create a new user",
Tags: []string{"users"},
})
// Test with valid data
body := `{"username":"john123","email":"john@example.com","age":25,"bio":"Hello world"}`
req := httptest.NewRequest("POST", "/users", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
respBody, _ := io.ReadAll(resp.Body)
respStr := string(respBody)
if !strings.Contains(respStr, `"id":"user_123"`) {
t.Errorf("Expected response to contain user ID, got %s", respStr)
}
if !strings.Contains(respStr, `"username":"john123"`) {
t.Errorf("Expected response to contain username, got %s", respStr)
}
}
func TestPostOApi_WithPathParams(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Test with POST + path parameters
Post(oapi, "/groups/:groupId/users", func(c *fiber.Ctx, input PostUserWithPathInput) (PostOutput, PostError) {
return PostOutput{
ID: "user_456",
Message: fmt.Sprintf("User %s added to group %s", input.Username, input.GroupID),
Data: map[string]interface{}{
"groupId": input.GroupID,
"username": input.Username,
"email": input.Email,
"role": input.Role,
},
}, PostError{}
}, OpenAPIOptions{
OperationID: "add-user-to-group",
Summary: "Add user to a group",
Tags: []string{"groups", "users"},
})
// Test with valid data
body := `{"username":"jane123","email":"jane@example.com","role":"member"}`
req := httptest.NewRequest("POST", "/groups/550e8400-e29b-41d4-a716-446655440000/users", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
respBody, _ := io.ReadAll(resp.Body)
respStr := string(respBody)
if !strings.Contains(respStr, `"groupId":"550e8400-e29b-41d4-a716-446655440000"`) {
t.Errorf("Expected response to contain group ID, got %s", respStr)
}
if !strings.Contains(respStr, `"username":"jane123"`) {
t.Errorf("Expected response to contain username, got %s", respStr)
}
}
func TestPostOApi_Validation(t *testing.T) {
app := fiber.New()
oapi := New(app)
Post(oapi, "/users", func(c *fiber.Ctx, input PostUserInput) (PostOutput, PostError) {
return PostOutput{
ID: "user_789",
Message: "User created",
}, PostError{}
}, OpenAPIOptions{
OperationID: "create-user-validation",
Summary: "Create user with validation",
})
tests := []struct {
name string
body string
expectedStatus int
shouldPass bool
errorContains string
}{
{
name: "Valid data",
body: `{"username":"alice123","email":"alice@example.com","age":28}`,
expectedStatus: 200,
shouldPass: true,
},
{
name: "Missing required field",
body: `{"email":"alice@example.com","age":28}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "required",
},
{
name: "Username too short",
body: `{"username":"al","email":"alice@example.com","age":28}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "min",
},
{
name: "Invalid email",
body: `{"username":"alice123","email":"not-an-email","age":28}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "email",
},
{
name: "Age too young",
body: `{"username":"alice123","email":"alice@example.com","age":10}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "min",
},
{
name: "Bio too long",
body: fmt.Sprintf(`{"username":"alice123","email":"alice@example.com","age":28,"bio":"%s"}`, strings.Repeat("a", 501)),
expectedStatus: 400,
shouldPass: false,
errorContains: "max",
},
{
name: "Invalid JSON",
body: `{"username":"alice123","email":"alice@example.com","age":28,}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "validation_error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("POST", "/users", strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
if tt.shouldPass {
if !strings.Contains(bodyStr, "User created") {
t.Errorf("Expected success message, got %s", bodyStr)
}
} else {
if !strings.Contains(bodyStr, "validation_error") {
t.Errorf("Expected validation error, got %s", bodyStr)
}
if tt.errorContains != "" && !strings.Contains(bodyStr, tt.errorContains) {
t.Errorf("Expected error to contain '%s', got %s", tt.errorContains, bodyStr)
}
}
})
}
}
func TestPostOApi_ComplexValidation(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Test with complex validation (arrays, nested validation)
Post(oapi, "/categories/:categoryId/products",
func(c *fiber.Ctx, input PostProductInput) (PostOutput, PostError) {
return PostOutput{
ID: "product_999",
Message: fmt.Sprintf("Product %s created in category %s", input.Name, input.CategoryID),
Data: map[string]interface{}{
"name": input.Name,
"price": input.Price,
"quantity": input.Quantity,
"tags": input.Tags,
"categoryId": input.CategoryID,
},
}, PostError{}
}, OpenAPIOptions{
OperationID: "create-product",
Summary: "Create a new product",
Tags: []string{"products"},
})
tests := []struct {
name string
url string
body string
expectedStatus int
shouldPass bool
errorContains string
}{
{
name: "Valid product",
url: "/categories/550e8400-e29b-41d4-a716-446655440000/products",
body: `{"name":"Laptop","description":"Gaming laptop","price":999.99,"quantity":10,"tags":["gaming","electronics"]}`,
expectedStatus: 200,
shouldPass: true,
},
{
name: "Invalid category UUID",
url: "/categories/invalid-uuid/products",
body: `{"name":"Laptop","price":999.99,"quantity":10}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "uuid4",
},
{
name: "Negative price",
url: "/categories/550e8400-e29b-41d4-a716-446655440000/products",
body: `{"name":"Laptop","price":-100,"quantity":10}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "min",
},
{
name: "Negative quantity",
url: "/categories/550e8400-e29b-41d4-a716-446655440000/products",
body: `{"name":"Laptop","price":999.99,"quantity":-1}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "min",
},
{
name: "Invalid tag (empty string)",
url: "/categories/550e8400-e29b-41d4-a716-446655440000/products",
body: `{"name":"Laptop","price":999.99,"quantity":10,"tags":["gaming",""]}`,
expectedStatus: 400,
shouldPass: false,
errorContains: "min",
},
{
name: "Tag too long",
url: "/categories/550e8400-e29b-41d4-a716-446655440000/products",
body: fmt.Sprintf(`{"name":"Laptop","price":999.99,"quantity":10,"tags":["%s"]}`, strings.Repeat("a", 21)),
expectedStatus: 400,
shouldPass: false,
errorContains: "max",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("POST", tt.url, strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
if tt.shouldPass {
if !strings.Contains(bodyStr, "Product") && !strings.Contains(bodyStr, "created") {
t.Errorf("Expected success message, got %s", bodyStr)
}
} else {
if !strings.Contains(bodyStr, "validation_error") {
t.Errorf("Expected validation error, got %s", bodyStr)
}
if tt.errorContains != "" && !strings.Contains(bodyStr, tt.errorContains) {
t.Errorf("Expected error to contain '%s', got %s", tt.errorContains, bodyStr)
}
}
})
}
}
func TestPostOApi_ErrorHandling(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Test with custom error handling
Post(oapi, "/users", func(c *fiber.Ctx, input PostUserInput) (PostOutput, PostError) {
if input.Username == "forbidden" {
return PostOutput{}, PostError{
StatusCode: 403,
Message: "Username is forbidden",
Details: "This username is not allowed",
}
}
if input.Email == "exists@example.com" {
return PostOutput{}, PostError{
StatusCode: 409,
Message: "Email already exists",
Details: "A user with this email already exists",
}
}
return PostOutput{
ID: "user_success",
Message: "User created successfully",
}, PostError{}
}, OpenAPIOptions{
OperationID: "create-user-with-errors",
Summary: "Create user with custom error handling",
})
// Test 1: Username forbidden
body1 := `{"username":"forbidden","email":"test@example.com","age":25}`
req1 := httptest.NewRequest("POST", "/users", strings.NewReader(body1))
req1.Header.Set("Content-Type", "application/json")
resp1, err := app.Test(req1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp1.StatusCode != 403 {
t.Errorf("Expected status 403, got %d", resp1.StatusCode)
}
body1Resp, _ := io.ReadAll(resp1.Body)
if !strings.Contains(string(body1Resp), "Username is forbidden") {
t.Errorf("Expected forbidden error message, got %s", string(body1Resp))
}
// Test 2: Email exists
body2 := `{"username":"john123","email":"exists@example.com","age":25}`
req2 := httptest.NewRequest("POST", "/users", strings.NewReader(body2))
req2.Header.Set("Content-Type", "application/json")
resp2, err := app.Test(req2)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp2.StatusCode != 409 {
t.Errorf("Expected status 409, got %d", resp2.StatusCode)
}
body2Resp, _ := io.ReadAll(resp2.Body)
if !strings.Contains(string(body2Resp), "Email already exists") {
t.Errorf("Expected email exists error message, got %s", string(body2Resp))
}
// Test 3: Success case
body3 := `{"username":"john123","email":"john@example.com","age":25}`
req3 := httptest.NewRequest("POST", "/users", strings.NewReader(body3))
req3.Header.Set("Content-Type", "application/json")
resp3, err := app.Test(req3)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp3.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp3.StatusCode)
}
body3Resp, _ := io.ReadAll(resp3.Body)
if !strings.Contains(string(body3Resp), "User created successfully") {
t.Errorf("Expected success message, got %s", string(body3Resp))
}
}
func TestPostOApi_OperationStorage(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Check that POST operations are properly stored
initialCount := len(oapi.operations)
Post(oapi, "/test", func(c *fiber.Ctx, input PostUserInput) (PostOutput, PostError) {
return PostOutput{Message: "test"}, PostError{}
}, OpenAPIOptions{
OperationID: "test-post-operation",
Summary: "Test POST operation",
Tags: []string{"test", "post"},
Description: "This is a test POST operation",
})
// Check that an operation was added
if len(oapi.operations) != initialCount+1 {
t.Errorf("Expected %d operations, got %d", initialCount+1, len(oapi.operations))
}
// Check the operation content
lastOp := oapi.operations[len(oapi.operations)-1]
if lastOp.Method != "POST" {
t.Errorf("Expected method POST, got %s", lastOp.Method)
}
if lastOp.Path != "/test" {
t.Errorf("Expected path /test, got %s", lastOp.Path)
}
if lastOp.Options.OperationID != "test-post-operation" {
t.Errorf("Expected operationId test-post-operation, got %s", lastOp.Options.OperationID)
}
if lastOp.Options.Summary != "Test POST operation" {
t.Errorf("Expected summary 'Test POST operation', got %s", lastOp.Options.Summary)
}
if lastOp.Options.Description != "This is a test POST operation" {
t.Errorf("Expected description 'This is a test POST operation', got %s", lastOp.Options.Description)
}
// Check the tags
expectedTags := []string{"test", "post"}
for _, expectedTag := range expectedTags {
found := false
for _, tag := range lastOp.Options.Tags {
if tag == expectedTag {
found = true
break
}
}
if !found {
t.Errorf("Expected to find '%s' in tags, got %v", expectedTag, lastOp.Options.Tags)
}
}
}