From fe5d85e169ee6ba017c83e9261edc864c61b5d0d Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 4 Mar 2024 10:20:59 -0700 Subject: [PATCH] clean up mock api client Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 1 - pkg/cmd/attestation/api/client_test.go | 7 +- .../attestation/api/mock_apiClient_test.go | 76 +++---------------- 3 files changed, 14 insertions(+), 70 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index bfcc47a36..4a63f40a2 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -16,7 +16,6 @@ const ( ) type apiClient interface { - REST(hostname, method, p string, body io.Reader, data interface{}) error RESTWithNext(hostname, method, p string, body io.Reader, data interface{}) (string, error) } diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index b3691bb52..705121778 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -20,7 +20,6 @@ func NewClientWithMockGHClient() Client { return &LiveClient{ api: mockAPIClient{ - OnREST: fetcher.OnRESTSuccess, OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage, }, } @@ -125,8 +124,7 @@ func TestGetByDigest_NoAttestationsFound(t *testing.T) { c := LiveClient{ api: mockAPIClient{ - OnREST: fetcher.OnRESTNoAttestations, - OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage, + OnRESTWithNext: fetcher.OnRESTWithNextNoAttestations, }, } @@ -148,8 +146,7 @@ func TestGetByDigest_Error(t *testing.T) { c := LiveClient{ api: mockAPIClient{ - OnREST: fetcher.OnRESTError, - OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage, + OnRESTWithNext: fetcher.OnRESTWithNextError, }, } diff --git a/pkg/cmd/attestation/api/mock_apiClient_test.go b/pkg/cmd/attestation/api/mock_apiClient_test.go index 7cffd4a80..043ef4a36 100644 --- a/pkg/cmd/attestation/api/mock_apiClient_test.go +++ b/pkg/cmd/attestation/api/mock_apiClient_test.go @@ -9,14 +9,9 @@ import ( ) type mockAPIClient struct { - OnREST func(hostname, method, p string, body io.Reader, data interface{}) error OnRESTWithNext func(hostname, method, p string, body io.Reader, data interface{}) (string, error) } -func (m mockAPIClient) REST(hostname, method, p string, body io.Reader, data interface{}) error { - return m.OnREST(hostname, method, p, body, data) -} - func (m mockAPIClient) RESTWithNext(hostname, method, p string, body io.Reader, data interface{}) (string, error) { return m.OnRESTWithNext(hostname, method, p, body, data) } @@ -25,10 +20,6 @@ type mockDataGenerator struct { NumAttestations int } -func (m mockDataGenerator) OnRESTSuccess(hostname, method, p string, body io.Reader, data interface{}) error { - return m.OnRESTSuccessHelper(hostname, method, p, body, data, false) -} - func (m mockDataGenerator) OnRESTSuccessWithNextPage(hostname, method, p string, body io.Reader, data interface{}) (string, error) { // if path doesn't contain after, it means first time hitting the mock server // so return the first page and return the link header in the response @@ -40,31 +31,6 @@ func (m mockDataGenerator) OnRESTSuccessWithNextPage(hostname, method, p string, return m.OnRESTWithNextSuccessHelper(hostname, method, p, body, data, false) } -func (m mockDataGenerator) OnRESTSuccessHelper(hostname, method, p string, body io.Reader, data interface{}, hasNext bool) error { - atts := make([]*Attestation, m.NumAttestations) - for j := 0; j < m.NumAttestations; j++ { - att := makeTestAttestation() - atts[j] = &att - } - - resp := AttestationsResponse{ - Attestations: atts, - } - - // // 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 - } - - return nil -} - func (m mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p string, body io.Reader, data interface{}, hasNext bool) (string, error) { atts := make([]*Attestation, m.NumAttestations) for j := 0; j < m.NumAttestations; j++ { @@ -95,43 +61,25 @@ func (m mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p strin return "", nil } -func (m mockDataGenerator) OnRESTNoAttestations(hostname, method, p string, body io.Reader, data interface{}) error { - var resp AttestationsResponse - resp.Attestations = make([]*Attestation, 0) - - 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) - - return nil -} - func (m mockDataGenerator) OnRESTWithNextNoAttestations(hostname, method, p string, body io.Reader, data interface{}) (string, error) { - var resp AttestationsResponse - resp.Attestations = make([]*Attestation, 0) + resp := AttestationsResponse{ + Attestations: make([]*Attestation, 0), + } - data = resp + // // Convert the attestations to JSON + b, err := json.Marshal(resp) + if err != nil { + return "", err + } - // Convert the attestations to JSON - // data, err := json.Marshal(resp) - // if err != nil { - // return err - // } + err = json.Unmarshal(b, &data) + if err != nil { + return "", err + } return "", nil } -func (m mockDataGenerator) OnRESTError(hostname, method, p string, body io.Reader, data interface{}) error { - return errors.New("failed to get attestations") -} - func (m mockDataGenerator) OnRESTWithNextError(hostname, method, p string, body io.Reader, data interface{}) (string, error) { return "", errors.New("failed to get attestations") }