add more mock api client options

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-04 10:45:44 -07:00
parent b8a570fcfa
commit b1fbfdd228
3 changed files with 18 additions and 122 deletions

View file

@ -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

View file

@ -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

View file

@ -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)
}