From b41270bb4747887af773c418be83e4c9a0bd3463 Mon Sep 17 00:00:00 2001 From: Gareth Rushgrove Date: Sat, 13 May 2023 12:51:25 +0100 Subject: [PATCH] Start adding tests for the Snyk module --- lib/snyk/self.go | 4 ---- lib/snyk/self_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 lib/snyk/self_test.go diff --git a/lib/snyk/self.go b/lib/snyk/self.go index 6765a8a..d9569fe 100644 --- a/lib/snyk/self.go +++ b/lib/snyk/self.go @@ -15,11 +15,7 @@ const experimentalVersion = "2023-04-28~experimental" type selfDocument struct { Data struct { Attributes users.User `json:"attributes,omitempty"` - ID string `json:"id,omitempty"` - Type string `json:"type,omitempty"` } - Jsonapi users.JsonApi `json:"jsonapi,omitempty"` - Links users.Links `json:"links,omitempty"` } func getSnykOrg(auth *securityprovider.SecurityProviderApiKey) (*uuid.UUID, error) { diff --git a/lib/snyk/self_test.go b/lib/snyk/self_test.go new file mode 100644 index 0000000..a34dbf5 --- /dev/null +++ b/lib/snyk/self_test.go @@ -0,0 +1,38 @@ +package snyk + +import ( + "net/http" + "testing" + + "github.com/deepmap/oapi-codegen/pkg/securityprovider" + "github.com/google/uuid" + "github.com/jarcoal/httpmock" + "github.com/stretchr/testify/assert" +) + +func TestGetSnykOrg(t *testing.T) { + expectedOrg := uuid.New() + auth, _ := securityprovider.NewSecurityProviderApiKey("header", "name", "value") + + httpmock.Activate() + defer httpmock.DeactivateAndReset() + httpmock.RegisterResponder("GET", "https://api.snyk.io/rest/self", + func(req *http.Request) (*http.Response, error) { + jsonBody := `{ + "data": { + "attributes": { + "default_org_context": "` + expectedOrg.String() + `" + } + } + }` + resp := httpmock.NewStringResponse(200, jsonBody) + resp.Header.Set("Content-Type", "application/json") + return resp, nil + }, + ) + + actualOrg, err := getSnykOrg(auth) + assert.NoError(t, err) + assert.Equal(t, expectedOrg, *actualOrg) +} +