Skip to content

Commit

Permalink
feat: initial report analytics contract testing [HEAD-879] (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cata authored Nov 10, 2023
1 parent 0f0fc7e commit 54d54e3
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
99 changes: 99 additions & 0 deletions infrastructure/analytics/pact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package analytics

import (
"encoding/json"
"fmt"
"log"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/stretchr/testify/assert"

"github.com/snyk/snyk-ls/internal/testutil"
)

func TestAnalyticsProviderPact(t *testing.T) {
testutil.NotOnWindows(t, "we don't have a pact cli")
c := testutil.UnitTest(t)

pact := &dsl.Pact{
Consumer: "snyk-ls",
Provider: "AnalyticsProvider",
LogDir: "logs",
PactDir: "./pacts",
LogLevel: "DEBUG",
}

defer pact.Teardown()

pact.Setup(true)
base := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
orgUUID := "54125374-3f93-402e-b693-e0724794d71f"

var test = func() (err error) {
//prepare
c.SetToken("token")
c.SetOrganization(orgUUID)
c.UpdateApiEndpoints(base)
c.SetAnalyticsEnabled(true)
expectedBody := getExpectedBodyRequest()
bodyBytes, err := json.Marshal(expectedBody)
assert.NoError(t, err)

// invoke function under test
err = SendAnalyticsToAPI(c, bodyBytes)
assert.NoError(t, err)

return nil
}

pact.
AddInteraction().
Given("Analytics data is ready").
UponReceiving("A request to create analytics data").
WithRequest(dsl.Request{
Method: "POST",
Path: dsl.String("/rest/api/orgs/" + orgUUID + "/analytics"),
Body: getExpectedBodyRequest(),
}).
WillRespondWith(dsl.Response{
Status: 201,
Headers: dsl.MapMatcher{"Content-Type": dsl.Term("application/json", `^application\/json$`)},
Body: map[string]interface{}{},
})

// Verify runs the current test case against a Mock Service.
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
}
}

func getExpectedBodyRequest() map[string]interface{} {
return map[string]interface{}{
"data": map[string]interface{}{
"type": "analytics",
"attributes": map[string]interface{}{
"deviceId": "unique-uuid",
"application": "snyk-cli",
"application_version": "1.1233.0",
"os": "Windows",
"arch": "AMD64",
"integration_name": "IntelliJ",
"integration_version": "2.5.5",
"integration_environment": "IntelliJ Ultimate",
"integration_environment_version": "2023.3",
"event_type": "Scan done",
"status": "Succeeded",
"scan_type": "Snyk Open Source",
"unique_issue_count": map[string]interface{}{
"critical": 15,
"high": 10,
"medium": 1,
"low": 2,
},
"duration_ms": "1000",
"timestamp_finished": "2023-09-01T12:00:00Z",
},
},
}
}
64 changes: 64 additions & 0 deletions infrastructure/analytics/pacts/snyk-ls-analyticsprovider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"consumer": {
"name": "snyk-ls"
},
"provider": {
"name": "AnalyticsProvider"
},
"interactions": [
{
"description": "A request to create analytics data",
"providerState": "Analytics data is ready",
"request": {
"method": "POST",
"path": "/rest/api/orgs/54125374-3f93-402e-b693-e0724794d71f/analytics",
"body": {
"data": {
"attributes": {
"application": "snyk-cli",
"application_version": "1.1233.0",
"arch": "AMD64",
"deviceId": "unique-uuid",
"duration_ms": "1000",
"event_type": "Scan done",
"integration_environment": "IntelliJ Ultimate",
"integration_environment_version": "2023.3",
"integration_name": "IntelliJ",
"integration_version": "2.5.5",
"os": "Windows",
"scan_type": "Snyk Open Source",
"status": "Succeeded",
"timestamp_finished": "2023-09-01T12:00:00Z",
"unique_issue_count": {
"critical": 15,
"high": 10,
"low": 2,
"medium": 1
}
},
"type": "analytics"
}
}
},
"response": {
"status": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {
},
"matchingRules": {
"$.headers.Content-Type": {
"match": "regex",
"regex": "^application\\/json$"
}
}
}
}
],
"metadata": {
"pactSpecification": {
"version": "2.0.0"
}
}
}

0 comments on commit 54d54e3

Please sign in to comment.