From b1fbfdd228c14b9cd066da090776b465a5a7958f Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 4 Mar 2024 10:45:44 -0700 Subject: [PATCH] add more mock api client options Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client_test.go | 20 ++- .../attestation/api/mock_apiClient_test.go | 4 + .../attestation/verification/sigstore_test.go | 116 ------------------ 3 files changed, 18 insertions(+), 122 deletions(-) delete mode 100644 pkg/cmd/attestation/verification/sigstore_test.go diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 705121778..0ab5528f6 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -13,14 +13,22 @@ const ( testDigest = "sha256:12313213" ) -func NewClientWithMockGHClient() Client { +func NewClientWithMockGHClient(hasNextPage bool) Client { fetcher := mockDataGenerator{ NumAttestations: 5, } + if hasNextPage { + return &LiveClient{ + api: mockAPIClient{ + OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage, + }, + } + } + return &LiveClient{ api: mockAPIClient{ - OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage, + OnRESTWithNext: fetcher.OnRESTSuccess, }, } } @@ -44,7 +52,7 @@ func TestGetURL(t *testing.T) { } func TestGetByDigest(t *testing.T) { - c := NewClientWithMockGHClient() + c := NewClientWithMockGHClient(false) attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit) require.NoError(t, err) @@ -61,7 +69,7 @@ func TestGetByDigest(t *testing.T) { } func TestGetByDigestGreaterThanLimit(t *testing.T) { - c := NewClientWithMockGHClient() + c := NewClientWithMockGHClient(false) limit := 3 // The method should return five results when the limit is not set @@ -81,7 +89,7 @@ func TestGetByDigestGreaterThanLimit(t *testing.T) { } func TestGetByDigestWithNextPage(t *testing.T) { - c := NewClientWithMockGHClient() + c := NewClientWithMockGHClient(true) attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit) require.NoError(t, err) @@ -98,7 +106,7 @@ func TestGetByDigestWithNextPage(t *testing.T) { } func TestGetByDigestGreaterThanLimitWithNextPage(t *testing.T) { - c := NewClientWithMockGHClient() + c := NewClientWithMockGHClient(true) 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 043ef4a36..0f04fe8f5 100644 --- a/pkg/cmd/attestation/api/mock_apiClient_test.go +++ b/pkg/cmd/attestation/api/mock_apiClient_test.go @@ -20,6 +20,10 @@ type mockDataGenerator struct { NumAttestations int } +func (m mockDataGenerator) OnRESTSuccess(hostname, method, p string, body io.Reader, data interface{}) (string, error) { + return m.OnRESTWithNextSuccessHelper(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 diff --git a/pkg/cmd/attestation/verification/sigstore_test.go b/pkg/cmd/attestation/verification/sigstore_test.go deleted file mode 100644 index aa9e2235b..000000000 --- a/pkg/cmd/attestation/verification/sigstore_test.go +++ /dev/null @@ -1,116 +0,0 @@ -package verification - -import ( - "testing" - - "github.com/cli/cli/v2/pkg/cmd/attestation/api" - "github.com/cli/cli/v2/pkg/cmd/attestation/artifact" - "github.com/cli/cli/v2/pkg/cmd/attestation/artifact/oci" - "github.com/cli/cli/v2/pkg/cmd/attestation/logger" - "github.com/cli/cli/v2/pkg/cmd/attestation/test" - - "github.com/sigstore/sigstore-go/pkg/verify" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -var ( - artifactPath = test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz") - bundlePath = test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0-bundle.json") - ociClient = oci.NewMockClient() -) - -func TestVerify_PublicGoodSuccess(t *testing.T) { - res := test.SuppressAndRestoreOutput() - defer res() - - artifact, err := artifact.NewDigestedArtifact(ociClient, artifactPath, "sha512") - require.NoError(t, err) - - c := FetchAttestationsConfig{ - APIClient: api.NewTestClient(), - BundlePath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0-bundle.json"), - Digest: artifact.DigestWithAlg(), - Owner: "sigstore", - } - - attestations, err := GetAttestations(c) - require.NoError(t, err) - - config := SigstoreConfig{ - Logger: logger.NewDefaultLogger(), - NoPublicGood: false, - } - - v, err := NewSigstoreVerifier(config, verify.PolicyBuilder{}) - require.NoError(t, err) - - resp := v.Verify(attestations) - assert.Nil(t, resp.Error) - - verifyResults := resp.VerifyResults - assert.Len(t, verifyResults, 1) - - result := verifyResults[0] - assert.NotNil(t, result.VerificationResult) - assert.Equal(t, attestations[0], result.Attestation) -} - -func TestVerify_PublicGoodFail_WithNoPublicGoodFlag(t *testing.T) { - res := test.SuppressAndRestoreOutput() - defer res() - - artifact, err := artifact.NewDigestedArtifact(ociClient, artifactPath, "sha512") - require.NoError(t, err) - - c := FetchAttestationsConfig{ - BundlePath: bundlePath, - Digest: artifact.Digest(), - Owner: "sigstore", - } - - attestations, err := GetAttestations(c) - require.NoError(t, err) - - config := SigstoreConfig{ - Logger: logger.NewDefaultLogger(), - NoPublicGood: true, - } - - v, err := NewSigstoreVerifier(config, verify.PolicyBuilder{}) - require.NoError(t, err) - - resp := v.Verify(attestations) - assert.Nil(t, resp.VerifyResults) - assert.ErrorContains(t, resp.Error, "verifying with issuer \"sigstore.dev\"") -} - -func TestVerify_Failure(t *testing.T) { - res := test.SuppressAndRestoreOutput() - defer res() - - artifact, err := artifact.NewDigestedArtifact(ociClient, artifactPath, "sha512") - require.NoError(t, err) - - c := FetchAttestationsConfig{ - BundlePath: bundlePath, - Digest: artifact.Digest(), - Owner: "sigstore", - } - - attestations, err := GetAttestations(c) - require.NoError(t, err) - - config := SigstoreConfig{ - Logger: logger.NewDefaultLogger(), - NoPublicGood: true, - } - - v, err := NewSigstoreVerifier(config, verify.PolicyBuilder{}) - require.NoError(t, err) - - resp := v.Verify(attestations) - assert.NotNil(t, resp.Error) - assert.Nil(t, resp.VerifyResults) -}