-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspec_test.go
119 lines (98 loc) · 2.5 KB
/
spec_test.go
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
package main
import (
"log"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var mockPath = map[string]*PathItem{
"/users/{id}": &PathItem{
Get: &Operation{
XTestSuites: []XTestSuite{
{
Description: "[POS] Should return success",
Request: XTestSuiteRequest{},
Response: XTestSuiteResponse{},
},
},
},
},
}
var mockServers = []*Server{
{
URL: "http://mock.com",
Description: "Local dev",
},
}
var mockSpec = Spec{
Paths: mockPath,
Servers: mockServers,
}
type mockTestProcessor struct{}
func (mockTestProcessor) processTestSuites(httpMethod, pathName string, xTestSuites []XTestSuite) []testSuiteReport {
testSuiteReports := []testSuiteReport{
{
PathName: "/users/{id}",
Operation: "GET",
Description: "[POS] Should return success",
Status: testResultPassed,
},
}
return testSuiteReports
}
func TestSpec(t *testing.T) {
report, err := execWithReporter(mockSpec, mockTestProcessor{})
assert.NoError(t, err)
assert.Equal(t, "/users/{id}", report.TestSuites[0].PathName)
assert.Equal(t, "GET", report.TestSuites[0].Operation)
assert.Equal(t, "[POS] Should return success", report.TestSuites[0].Description)
assert.Equal(t, testResultPassed, report.TestSuites[0].Status)
}
/*
These tests below are going to be used for testing openapi spec parsing
*/
func TestParseOpenApi_AllPositive(t *testing.T) {
fileBytes, err := os.ReadFile("test-data/openapi.yaml")
if err != nil {
log.Fatalf("Failed to read file, err: %v", err)
}
_, err = parseAndValidateSpec(fileBytes)
assert.NoError(t, err)
}
func TestParseOpenApi_MultipleServers(t *testing.T) {
fileBytes, err := os.ReadFile("test-data/openapi_multiple_servers.yaml")
if err != nil {
log.Fatalf("Failed to read file, err: %v", err)
}
_, err = parseAndValidateSpec(fileBytes)
assert.Error(t, err)
}
func TestParseOpenApi(t *testing.T) {
testSuites := []struct {
description string
filePath string
shouldFail bool
}{
{
description: "[POS] Vanilla test case",
filePath: "test-data/openapi.yaml",
shouldFail: false,
},
{
description: "[NEG] Should fail because of multiple servers",
filePath: "test-data/openapi_multiple_servers.yaml",
shouldFail: true,
},
}
for _, test := range testSuites {
t.Run(test.description, func(t *testing.T) {
fileBytes, _ := os.ReadFile(test.filePath)
_, err := parseAndValidateSpec(fileBytes)
if test.shouldFail {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}