-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoperations_test.go
More file actions
131 lines (119 loc) · 3.57 KB
/
operations_test.go
File metadata and controls
131 lines (119 loc) · 3.57 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
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package codescan
import (
"testing"
"github.com/go-openapi/testify/v2/require"
"github.com/go-openapi/spec"
"github.com/go-openapi/testify/v2/assert"
)
func TestOperationsExpression(t *testing.T) {
assert.RegexpT(t, rxOperation, "swagger:operation DELETE /orders/{id} deleteOrder")
assert.RegexpT(t, rxOperation, "swagger:operation GET /v1.2/something deleteOrder")
}
func TestOperationsParser(t *testing.T) {
sctx, err := newScanCtx(&Options{
Packages: []string{
"./goparsing/classification",
"./goparsing/classification/models",
"./goparsing/classification/operations",
"./goparsing/classification/operations_annotation",
},
WorkDir: "fixtures",
})
require.NoError(t, err)
var ops spec.Paths
for _, apiPath := range sctx.app.Operations {
prs := &operationsBuilder{
ctx: sctx,
path: apiPath,
operations: make(map[string]*spec.Operation),
}
require.NoError(t, prs.Build(&ops))
}
assert.Len(t, ops.Paths, 3)
po, ok := ops.Paths["/pets"]
assert.TrueT(t, ok)
require.NotNil(t, po.Get)
assertAnnotationOperation(t,
po.Get,
"getPet",
"",
"List all pets",
[]string{"pets"},
)
if po.Get != nil {
rsp, k := po.Get.Responses.StatusCodeResponses[200]
if assert.TrueT(t, k) {
assert.EqualT(t, "An paged array of pets", rsp.Description)
}
if assert.NotNil(t, po.Get.Responses.Default) {
assert.EqualT(t, "unexpected error", po.Get.Responses.Default.Description)
}
}
po, ok = ops.Paths["/pets/{id}"]
assert.TrueT(t, ok)
assert.NotNil(t, po.Put)
assertAnnotationOperation(t,
po.Put,
"updatePet",
"Updates the details for a pet.",
"Some long explanation,\nspanning over multipele lines,\nAKA the description.",
[]string{"pets"},
)
if po.Put != nil {
rsp, k := po.Put.Responses.StatusCodeResponses[400]
if assert.TrueT(t, k) {
assert.EqualT(t, "Invalid ID supplied", rsp.Description)
}
rsp, k = po.Put.Responses.StatusCodeResponses[404]
if assert.TrueT(t, k) {
assert.EqualT(t, "Pet not found", rsp.Description)
}
rsp, k = po.Put.Responses.StatusCodeResponses[405]
if assert.TrueT(t, k) {
assert.EqualT(t, "Validation exception", rsp.Description)
}
}
po, ok = ops.Paths["/v1/events"]
assert.TrueT(t, ok)
assert.NotNil(t, po.Get)
assertAnnotationOperation(t,
po.Get,
"getEvents",
"Events",
"Mitigation Events",
[]string{"Events"},
)
if po.Get != nil {
rsp, k := po.Get.Responses.StatusCodeResponses[200]
if assert.TrueT(t, k) {
assert.EqualT(t, "#/definitions/ListResponse", rsp.Schema.Ref.String())
assert.EqualT(t, "200", rsp.Description)
}
rsp, k = po.Get.Responses.StatusCodeResponses[400]
if assert.TrueT(t, k) {
assert.EqualT(t, "#/definitions/ErrorResponse", rsp.Schema.Ref.String())
assert.EqualT(t, "400", rsp.Description)
}
}
}
func assertAnnotationOperation(t *testing.T, op *spec.Operation, id, summary, description string, tags []string) {
assert.NotNil(t, op)
assert.EqualT(t, summary, op.Summary)
assert.EqualT(t, description, op.Description)
assert.EqualT(t, id, op.ID)
assert.Equal(t, tags, op.Tags)
assert.SliceContainsT(t, op.Consumes, "application/json")
assert.SliceContainsT(t, op.Consumes, "application/xml")
assert.SliceContainsT(t, op.Produces, "application/json")
assert.SliceContainsT(t, op.Produces, "application/xml")
assert.Len(t, op.Security, 1)
if len(op.Security) > 0 {
akv, ok := op.Security[0]["petstore_auth"]
assert.TrueT(t, ok)
// akv must be defined & not empty
assert.NotNil(t, akv)
assert.NotEmpty(t, akv)
}
}