From a86fab3cdf02184a14fad16fbb400204bd968020 Mon Sep 17 00:00:00 2001 From: ahmed Date: Mon, 29 Jan 2024 12:48:10 -0500 Subject: [PATCH] mocking out verified case for aha + adding inactive account test --- pkg/detectors/aha/aha_test.go | 42 ++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/pkg/detectors/aha/aha_test.go b/pkg/detectors/aha/aha_test.go index d76b696a55f8..1e66303bcba8 100644 --- a/pkg/detectors/aha/aha_test.go +++ b/pkg/detectors/aha/aha_test.go @@ -12,6 +12,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" + "gopkg.in/h2non/gock.v1" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" @@ -28,26 +29,33 @@ func TestAha_FromChunk(t *testing.T) { secret := testSecrets.MustGetField("AHA_SECRET") inactiveSecret := testSecrets.MustGetField("AHA_INACTIVE") + interceptClient := common.SaneHttpClient() + type args struct { ctx context.Context data []byte verify bool } tests := []struct { - name string - s Scanner - args args - want []detectors.Result - wantErr bool + name string + s Scanner + args args + mockSetup func() + want []detectors.Result + wantErr bool }{ { name: "found, verified", - s: Scanner{}, + s: Scanner{client: interceptClient}, args: args{ ctx: context.Background(), data: []byte(fmt.Sprintf("You can find a aha secret %s within %s but verified", secret, domain)), verify: true, }, + mockSetup: func() { + gock.InterceptClient(interceptClient) + gock.New(domain).Reply(200) + }, want: []detectors.Result{ { DetectorType: detectorspb.DetectorType_Aha, @@ -56,6 +64,24 @@ func TestAha_FromChunk(t *testing.T) { }, wantErr: false, }, + { + name: "found, unverified error due to inactive account", + s: Scanner{}, + args: args{ + ctx: context.Background(), + data: []byte(fmt.Sprintf("You can find a aha secret %s within %s but verified", secret, domain)), + verify: true, + }, + want: func() []detectors.Result { + r := detectors.Result{ + DetectorType: detectorspb.DetectorType_Aha, + Verified: false, + } + r.SetVerificationError(fmt.Errorf("unexpected HTTP response status 403")) + return []detectors.Result{r} + }(), + wantErr: false, + }, { name: "found, real secrets, verification error due to timeout", s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)}, @@ -122,6 +148,10 @@ func TestAha_FromChunk(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if tt.mockSetup != nil { + tt.mockSetup() + defer gock.Off() + } got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) if (err != nil) != tt.wantErr { t.Errorf("Aha.FromData() error = %v, wantErr %v", err, tt.wantErr)