From ff1eb37f9e96eb6735e87c6591f04dfff3d36393 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 4 Mar 2024 09:57:09 -0700 Subject: [PATCH] fix mock api client Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 4 +- pkg/cmd/attestation/api/client_test.go | 23 ++---- .../attestation/api/mock_apiClient_test.go | 77 ++++++++----------- 3 files changed, 38 insertions(+), 66 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 26d5d83c1..bfcc47a36 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -78,7 +78,7 @@ func (c *LiveClient) getAttestations(url, name, digest string, limit int) ([]*At var err error // if no attestation or less than limit, then keep fetching for url != "" && len(attestations) < limit { - url, err = c.api.RESTWithNext(c.host, http.MethodGet, url, nil, resp) + url, err = c.api.RESTWithNext(c.host, http.MethodGet, url, nil, &resp) if err != nil { return nil, err } @@ -86,7 +86,7 @@ func (c *LiveClient) getAttestations(url, name, digest string, limit int) ([]*At attestations = append(attestations, resp.Attestations...) } - if len(resp.Attestations) == 0 { + if len(attestations) == 0 { return nil, newErrNoAttestations(name, digest) } diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 662f72f99..b3691bb52 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -26,19 +26,6 @@ func NewClientWithMockGHClient() Client { } } -func NewClientWithMockGHClientWithNextPage() Client { - fetcher := mockDataGenerator{ - NumAttestations: 5, - } - - return &LiveClient{ - api: mockAPIClient{ - OnREST: fetcher.OnRESTSuccess, - OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage, - }, - } -} - func TestGetURL(t *testing.T) { c := LiveClient{} @@ -62,14 +49,14 @@ func TestGetByDigest(t *testing.T) { attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit) require.NoError(t, err) - assert.Equal(t, len(attestations), 5) + assert.Equal(t, 5, len(attestations)) bundle := (attestations)[0].Bundle assert.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle+json;version=0.1") attestations, err = c.GetByOwnerAndDigest(testOwner, testDigest, DefaultLimit) require.NoError(t, err) - assert.Equal(t, len(attestations), 5) + assert.Equal(t, 5, len(attestations)) bundle = (attestations)[0].Bundle assert.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle+json;version=0.1") } @@ -82,7 +69,7 @@ func TestGetByDigestGreaterThanLimit(t *testing.T) { attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, limit) require.NoError(t, err) - assert.Equal(t, len(attestations), 3) + assert.Equal(t, 3, len(attestations)) bundle := (attestations)[0].Bundle assert.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle+json;version=0.1") @@ -95,7 +82,7 @@ func TestGetByDigestGreaterThanLimit(t *testing.T) { } func TestGetByDigestWithNextPage(t *testing.T) { - c := NewClientWithMockGHClientWithNextPage() + c := NewClientWithMockGHClient() attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit) require.NoError(t, err) @@ -112,7 +99,7 @@ func TestGetByDigestWithNextPage(t *testing.T) { } func TestGetByDigestGreaterThanLimitWithNextPage(t *testing.T) { - c := NewClientWithMockGHClientWithNextPage() + c := NewClientWithMockGHClient() limit := 7 // The method should return five results when the limit is not set diff --git a/pkg/cmd/attestation/api/mock_apiClient_test.go b/pkg/cmd/attestation/api/mock_apiClient_test.go index 08f557f05..7cffd4a80 100644 --- a/pkg/cmd/attestation/api/mock_apiClient_test.go +++ b/pkg/cmd/attestation/api/mock_apiClient_test.go @@ -1,6 +1,7 @@ package api import ( + "encoding/json" "errors" "fmt" "io" @@ -46,25 +47,20 @@ func (m mockDataGenerator) OnRESTSuccessHelper(hostname, method, p string, body atts[j] = &att } - var resp AttestationsResponse - resp.Attestations = atts - - data = resp + resp := AttestationsResponse{ + Attestations: atts, + } // // Convert the attestations to JSON - // jsonResponse, err := json.Marshal(resp) - // if err != nil { - // return err - // } + b, err := json.Marshal(resp) + if err != nil { + return err + } - // // Create a buffer containing the JSON response - // responseReader := bytes.NewBuffer(jsonResponse) - - // linkHeader := "" - // if hasNext { - // // Create a link header with the next page - // linkHeader = fmt.Sprintf("<%s&after=2>; rel=\"next\"", p) - // } + err = json.Unmarshal(b, &data) + if err != nil { + return err + } return nil } @@ -76,38 +72,27 @@ func (m mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p strin atts[j] = &att } - var resp AttestationsResponse - resp.Attestations = atts - - data = resp - - // // Convert the attestations to JSON - // jsonResponse, err := json.Marshal(resp) - // if err != nil { - // return err - // } - - // // Create a buffer containing the JSON response - // responseReader := bytes.NewBuffer(jsonResponse) - - // b, err := io.ReadAll(resp.Body) - // if err != nil { - // return "", err - // } - - // err = json.Unmarshal(b, &data) - // if err != nil { - // return "", err - // } - - - linkHeader := "" - if hasNext { - // Create a link header with the next page - linkHeader = fmt.Sprintf("<%s&after=2>; rel=\"next\"", p) + resp := AttestationsResponse{ + Attestations: atts, } - return linkHeader, nil + // // Convert the attestations to JSON + b, err := json.Marshal(resp) + if err != nil { + return "", err + } + + err = json.Unmarshal(b, &data) + if err != nil { + return "", err + } + + if hasNext { + // return a link header with the next page + return fmt.Sprintf("<%s&after=2>; rel=\"next\"", p), nil + } + + return "", nil } func (m mockDataGenerator) OnRESTNoAttestations(hostname, method, p string, body io.Reader, data interface{}) error {