-
Notifications
You must be signed in to change notification settings - Fork 5
/
remove_elements_test.go
122 lines (101 loc) · 4.18 KB
/
remove_elements_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
120
121
122
package vervet_test
import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/snyk/vervet/v8"
"github.com/snyk/vervet/v8/testdata"
)
func TestRemoveElementsExact(t *testing.T) {
c := qt.New(t)
doc, err := vervet.NewDocumentFile(testdata.Path("output/2021-08-20~experimental/spec.yaml"))
c.Assert(err, qt.IsNil)
// Establish that the OpenAPI document has these expected features
c.Assert(doc.Paths.Value("/examples/hello-world"), qt.Not(qt.IsNil))
c.Assert(doc.Paths.Value("/examples/hello-world/{id}"), qt.Not(qt.IsNil))
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-request-id"],
qt.Not(qt.IsNil),
)
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-version-served"],
qt.Not(qt.IsNil),
)
c.Assert(doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").Delete.Parameters, qt.HasLen, 4)
c.Assert(
doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").Delete.Parameters[3].Value.Name,
qt.Equals,
"x-private-matter",
)
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.Not(qt.IsNil))
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil))
// Remove some of them
err = vervet.RemoveElements(doc.T, vervet.ExcludePatterns{
ExtensionPatterns: []string{"x-snyk-api-releases", "x-snyk-api-resource"},
HeaderPatterns: []string{"snyk-request-id", "x-private-matter"},
Paths: []string{"/examples/hello-world", "/examples/hello-world/{id}"},
})
c.Assert(err, qt.IsNil)
// Assert their removal
c.Assert(doc.Paths.Value("/examples/hello-world"), qt.IsNil)
c.Assert(doc.Paths.Value("/examples/hello-world/{id}"), qt.IsNil)
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-request-id"],
qt.IsNil,
) // now removed
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-version-served"],
qt.Not(qt.IsNil),
) // still there
c.Assert(doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").
Delete.Parameters, qt.HasLen, 3) // x-private-matter removed
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.IsNil) // now removed
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil)) // still there
}
func TestRemoveElementsRegex(t *testing.T) {
c := qt.New(t)
doc, err := vervet.NewDocumentFile(testdata.Path("output/2021-08-20~experimental/spec.yaml"))
c.Assert(err, qt.IsNil)
// Establish that the OpenAPI document has these expected features
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-request-id"],
qt.Not(qt.IsNil),
)
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-version-served"],
qt.Not(qt.IsNil),
)
c.Assert(
doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").Delete.Parameters,
qt.HasLen,
4,
)
c.Assert(
doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").Delete.Parameters[3].Value.Name,
qt.Equals,
"x-private-matter",
)
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.Not(qt.IsNil))
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil))
// Remove some of them
err = vervet.RemoveElements(doc.T, vervet.ExcludePatterns{
ExtensionPatterns: []string{"x-snyk-api-r.*"},
HeaderPatterns: []string{"snyk-version-.*", "x-private-.*"},
})
c.Assert(err, qt.IsNil)
// Assert their removal
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-request-id"],
qt.Not(qt.IsNil),
) // still there
c.Assert(
doc.Paths.Value("/orgs/{orgId}/projects").Get.Responses.Status(200).Value.Headers["snyk-version-served"],
qt.IsNil,
) // now removed
c.Assert(
doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").Delete.Parameters,
qt.HasLen,
3,
) // x-private-matter removed
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.IsNil) // now removed
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil)) // still there
}