-
Notifications
You must be signed in to change notification settings - Fork 47
/
snyk_utils_test.go
103 lines (75 loc) · 2.5 KB
/
snyk_utils_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
package main
import (
"encoding/json"
"testing"
"github.com/michael-go/go-jsn/jsn"
"github.com/nsf/jsondiff"
"github.com/stretchr/testify/assert"
)
// Test MakeSnykRequest function
//makeSnykAPIRequest(verb string, endpointURL string, snykToken string, body []byte) []byte
func TestMakeSnykRequestFunc(t *testing.T) {
// should use the verb, the token, the url, and the body if POST
// return the body we send to check it works as expected
dummyBody := `{ test: test }`
dummyBodyMarshalled, _ := json.Marshal(dummyBody)
expectedTestURL := "/v1/org/123/project/123"
expectedGETMethod := "GET"
expectedPOSTMethod := "POST"
expectedToken := "123"
expectedPOSTResponse :=
`{
"body": "InsgdGVzdDogdGVzdCB9Ig==",
"method": "POST",
"token": "token 123",
"url": "/v1/org/123/project/123"
}`
expectedGETResponse :=
`{
"body": "",
"method": "GET",
"token": "token 123",
"url": "/v1/org/123/project/123"
}`
assert := assert.New(t)
// TESTING POST
server := HTTPResponseStubAndMirrorRequest(expectedTestURL, expectedPOSTMethod, expectedToken)
defer server.Close()
cD := debug{}
cD.setDebug(false)
response, err := makeSnykAPIRequest(expectedPOSTMethod, server.URL+expectedTestURL, expectedToken, dummyBodyMarshalled, cD)
if err != nil {
panic(err)
}
var unmarshalledResp map[string]interface{}
json.Unmarshal(response, &unmarshalledResp)
jsonResponse, _ := jsn.NewJson(unmarshalledResp)
assert.Equal(expectedPOSTResponse, jsonResponse.Pretty())
// TESTING GET
server = HTTPResponseStubAndMirrorRequest(expectedTestURL, expectedGETMethod, expectedToken)
defer server.Close()
response, err = makeSnykAPIRequest(expectedGETMethod, server.URL+expectedTestURL, expectedToken, nil, cD)
if err != nil {
panic(err)
}
json.Unmarshal(response, &unmarshalledResp)
jsonResponse, _ = jsn.NewJson(unmarshalledResp)
assert.Equal(expectedGETResponse, jsonResponse.Pretty())
return
}
func TestRESTPaginationFunc(t *testing.T) {
assert := assert.New(t)
cD := debug{}
cD.setDebug(false)
server := HTTPResponseRestPagination()
defer server.Close()
response, err := makeSnykAPIRequest_REST("GET", server.URL, "/rest/orgs/xyz-paging/projects?version=2022-07-08~beta&status=active", "123", nil, cD)
if err != nil {
panic(err)
}
marshalledResp, _ := json.Marshal(response)
fixture := readFixture("./fixtures/paginated_projects.json")
opts := jsondiff.DefaultConsoleOptions()
comparison, _ := jsondiff.Compare(fixture, marshalledResp, &opts)
assert.Equal("FullMatch", comparison.String())
}