Skip to content

Commit

Permalink
fix: pact tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Jun 5, 2024
1 parent 9b2c692 commit 9617679
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 9 deletions.
12 changes: 3 additions & 9 deletions internal/deepcode/client_pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ func TestSnykCodeClientPact(t *testing.T) {
setupPact(t)

t.Run("Create bundle", func(t *testing.T) {
files := make(map[string]string)
files[path1] = util.Hash([]byte(content))

pact.AddInteraction().Given("New bundle").UponReceiving("Create bundle").WithCompleteRequest(consumer.Request{
Method: "POST",
Path: matchers.String("/bundle"),
Headers: getPutPostHeaderMatcher(),
Body: getPutPostBodyMatcher(),
}).WithCompleteResponse(consumer.Response{
Status: 200,
Headers: matchers.MapMatcher{
Expand All @@ -70,8 +72,6 @@ func TestSnykCodeClientPact(t *testing.T) {

test := func(config consumer.MockServerConfig) error {
client := getDeepCodeClient(t, getLocalMockserver(config))
files := make(map[string]string)
files[path1] = util.Hash([]byte(content))
bundleHash, missingFiles, err := client.CreateBundle(context.Background(), files)

if err != nil {
Expand Down Expand Up @@ -99,7 +99,6 @@ func TestSnykCodeClientPact(t *testing.T) {
Method: "POST",
Path: matchers.String("/bundle"),
Headers: getPutPostHeaderMatcher(),
Body: getPutPostBodyMatcher(),
}).WithCompleteResponse(consumer.Response{
Status: 401,
Headers: matchers.MapMatcher{
Expand Down Expand Up @@ -137,7 +136,6 @@ func TestSnykCodeClientPact(t *testing.T) {
Method: "PUT",
Path: matchers.Term("/bundle/"+bundleHash, "/bundle/[A-Fa-f0-9]{64}"),
Headers: getPutPostHeaderMatcher(),
Body: getPutPostBodyMatcher(),
}).WithCompleteResponse(consumer.Response{
Status: 200,
Headers: matchers.MapMatcher{
Expand Down Expand Up @@ -252,10 +250,6 @@ func getPutPostHeaderMatcher() matchers.MapMatcher {
}
}

func getPutPostBodyMatcher() matchers.Matcher {
return matchers.Like(make([]byte, 1))
}

func getSnykRequestIdMatcher() matchers.Matcher {
return matchers.Regex("fc763eba-0905-41c5-a27f-3934ab26786c", uuidMatcher)
}
Expand Down
133 changes: 133 additions & 0 deletions internal/deepcode/pacts/code-client-go-snykcodeapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,139 @@
},
"status": 200
}
},
{
"description": "Extend bundle",
"providerState": "Existing bundle",
"request": {
"headers": {
"Content-Encoding": "gzip",
"Content-Type": "application/octet-stream",
"snyk-org-name": "00000000-0000-0000-0000-000000000023",
"snyk-request-id": "fc763eba-0905-41c5-a27f-3934ab26786c"
},
"matchingRules": {
"$.header['snyk-org-name']": {
"match": "regex",
"regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
},
"$.header['snyk-request-id']": {
"match": "regex",
"regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
},
"$.path": {
"match": "regex",
"regex": "/bundle/[A-Fa-f0-9]{64}"
}
},
"method": "PUT",
"path": "/bundle/faa6b7161c14f933ef4ca79a18ad9283eab362d5e6d3a977125eb95b37c377d8"
},
"response": {
"body": {
"bundleHash": "string",
"missingFiles": [
"string"
]
},
"headers": {
"Content-Type": "application/json"
},
"matchingRules": {
"$.body.bundleHash": {
"match": "type"
},
"$.body.missingFiles": {
"match": "type",
"min": 1
},
"$.body.missingFiles[*]": {
"match": "type"
}
},
"status": 200
}
},
{
"description": "Create bundle",
"providerState": "New bundle",
"request": {
"headers": {
"Content-Encoding": "gzip",
"Content-Type": "application/octet-stream",
"snyk-org-name": "00000000-0000-0000-0000-000000000023",
"snyk-request-id": "fc763eba-0905-41c5-a27f-3934ab26786c"
},
"matchingRules": {
"$.header['snyk-org-name']": {
"match": "regex",
"regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
},
"$.header['snyk-request-id']": {
"match": "regex",
"regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
}
},
"method": "POST",
"path": "/bundle"
},
"response": {
"body": {
"bundleHash": "string",
"missingFiles": [
"string"
]
},
"headers": {
"Content-Type": "application/json"
},
"matchingRules": {
"$.body.bundleHash": {
"match": "type"
},
"$.body.missingFiles": {
"match": "type",
"min": 1
},
"$.body.missingFiles[*]": {
"match": "type"
}
},
"status": 200
}
},
{
"description": "Create bundle",
"providerState": "New bundle and invalid token",
"request": {
"headers": {
"Content-Encoding": "gzip",
"Content-Type": "application/octet-stream",
"snyk-org-name": "00000000-0000-0000-0000-000000000023",
"snyk-request-id": "fc763eba-0905-41c5-a27f-3934ab26786c"
},
"matchingRules": {
"$.header['snyk-org-name']": {
"match": "regex",
"regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
},
"$.header['snyk-request-id']": {
"match": "regex",
"regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
}
},
"method": "POST",
"path": "/bundle"
},
"response": {
"body": {
"message": "Invalid auth token provided"
},
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"status": 401
}
}
],
"metadata": {
Expand Down

0 comments on commit 9617679

Please sign in to comment.