-
Notifications
You must be signed in to change notification settings - Fork 5
/
remove_elements_test.go
81 lines (60 loc) · 4.08 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
package vervet_test
import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/snyk/vervet/v5"
"github.com/snyk/vervet/v5/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["/examples/hello-world"], qt.Not(qt.IsNil))
c.Assert(doc.Paths["/examples/hello-world/{id}"], qt.Not(qt.IsNil))
c.Assert(doc.Paths["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-request-id"], qt.Not(qt.IsNil))
c.Assert(doc.Paths["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-version-served"], qt.Not(qt.IsNil))
c.Assert(doc.Paths["/orgs/{org_id}/projects/{project_id}"].Delete.Parameters, qt.HasLen, 4)
c.Assert(doc.Paths["/orgs/{org_id}/projects/{project_id}"].Delete.Parameters[3].Value.Name, qt.Equals, "x-private-matter")
c.Assert(doc.Paths["/orgs/{orgId}/projects"].ExtensionProps.Extensions["x-snyk-api-resource"], qt.Not(qt.IsNil))
c.Assert(doc.ExtensionProps.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["/examples/hello-world"], qt.IsNil)
c.Assert(doc.Paths["/examples/hello-world/{id}"], qt.IsNil)
c.Assert(doc.Paths["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-request-id"], qt.IsNil) // now removed
c.Assert(doc.Paths["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-version-served"], qt.Not(qt.IsNil)) // still there
c.Assert(doc.Paths["/orgs/{org_id}/projects/{project_id}"].Delete.Parameters, qt.HasLen, 3) // x-private-matter removed
c.Assert(doc.Paths["/orgs/{orgId}/projects"].ExtensionProps.Extensions["x-snyk-api-resource"], qt.IsNil) // now removed
c.Assert(doc.ExtensionProps.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["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-request-id"], qt.Not(qt.IsNil))
c.Assert(doc.Paths["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-version-served"], qt.Not(qt.IsNil))
c.Assert(doc.Paths["/orgs/{org_id}/projects/{project_id}"].Delete.Parameters, qt.HasLen, 4)
c.Assert(doc.Paths["/orgs/{org_id}/projects/{project_id}"].Delete.Parameters[3].Value.Name, qt.Equals, "x-private-matter")
c.Assert(doc.Paths["/orgs/{orgId}/projects"].ExtensionProps.Extensions["x-snyk-api-resource"], qt.Not(qt.IsNil))
c.Assert(doc.ExtensionProps.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["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-request-id"], qt.Not(qt.IsNil)) // still there
c.Assert(doc.Paths["/orgs/{orgId}/projects"].Get.Responses["200"].Value.Headers["snyk-version-served"], qt.IsNil) // now removed
c.Assert(doc.Paths["/orgs/{org_id}/projects/{project_id}"].Delete.Parameters, qt.HasLen, 3) // x-private-matter removed
c.Assert(doc.Paths["/orgs/{orgId}/projects"].ExtensionProps.Extensions["x-snyk-api-resource"], qt.IsNil) // now removed
c.Assert(doc.ExtensionProps.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil)) // still there
}