Skip to content

Commit

Permalink
mocking out verified case for aha + adding inactive account test
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1 committed Jan 29, 2024
1 parent 0c41ce5 commit a86fab3
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions pkg/detectors/aha/aha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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)},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a86fab3

Please sign in to comment.