9
9
"testing"
10
10
"time"
11
11
12
- "github.com/kylelemons/godebug/pretty"
12
+ "github.com/google/go-cmp/cmp"
13
+ "github.com/google/go-cmp/cmp/cmpopts"
13
14
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
14
15
15
16
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
@@ -32,11 +33,12 @@ func TestClickupPersonalToken_FromChunk(t *testing.T) {
32
33
verify bool
33
34
}
34
35
tests := []struct {
35
- name string
36
- s Scanner
37
- args args
38
- want []detectors.Result
39
- wantErr bool
36
+ name string
37
+ s Scanner
38
+ args args
39
+ want []detectors.Result
40
+ wantErr bool
41
+ wantVerificationErr bool
40
42
}{
41
43
{
42
44
name : "found, verified" ,
@@ -52,7 +54,8 @@ func TestClickupPersonalToken_FromChunk(t *testing.T) {
52
54
Verified : true ,
53
55
},
54
56
},
55
- wantErr : false ,
57
+ wantErr : false ,
58
+ wantVerificationErr : false ,
56
59
},
57
60
{
58
61
name : "found, unverified" ,
@@ -68,7 +71,8 @@ func TestClickupPersonalToken_FromChunk(t *testing.T) {
68
71
Verified : false ,
69
72
},
70
73
},
71
- wantErr : false ,
74
+ wantErr : false ,
75
+ wantVerificationErr : false ,
72
76
},
73
77
{
74
78
name : "not found" ,
@@ -78,14 +82,31 @@ func TestClickupPersonalToken_FromChunk(t *testing.T) {
78
82
data : []byte ("You cannot find the secret within" ),
79
83
verify : true ,
80
84
},
81
- want : nil ,
82
- wantErr : false ,
85
+ want : nil ,
86
+ wantErr : false ,
87
+ wantVerificationErr : false ,
88
+ },
89
+ {
90
+ name : "found verifiable secret, verification failed due to unexpected API response" ,
91
+ s : Scanner {client : common .ConstantResponseHttpClient (404 , "" )},
92
+ args : args {
93
+ ctx : context .Background (),
94
+ data : []byte (fmt .Sprintf ("You can find a clickuppersonaltoken secret %s within but not valid" , inactiveSecret )), // the secret would satisfy the regex but not pass validation
95
+ verify : true ,
96
+ },
97
+ want : []detectors.Result {
98
+ {
99
+ DetectorType : detectorspb .DetectorType_ClickupPersonalToken ,
100
+ Verified : false ,
101
+ },
102
+ },
103
+ wantErr : false ,
104
+ wantVerificationErr : true ,
83
105
},
84
106
}
85
107
for _ , tt := range tests {
86
108
t .Run (tt .name , func (t * testing.T ) {
87
- s := Scanner {}
88
- got , err := s .FromData (tt .args .ctx , tt .args .verify , tt .args .data )
109
+ got , err := tt .s .FromData (tt .args .ctx , tt .args .verify , tt .args .data )
89
110
if (err != nil ) != tt .wantErr {
90
111
t .Errorf ("ClickupPersonalToken.FromData() error = %v, wantErr %v" , err , tt .wantErr )
91
112
return
@@ -94,9 +115,15 @@ func TestClickupPersonalToken_FromChunk(t *testing.T) {
94
115
if len (got [i ].Raw ) == 0 {
95
116
t .Fatalf ("no raw secret present: \n %+v" , got [i ])
96
117
}
118
+
119
+ if (got [i ].VerificationError () != nil ) != tt .wantVerificationErr {
120
+ t .Fatalf ("wantVerificationError = %v, verification error = %v" , tt .wantVerificationErr , got [i ].VerificationError ())
121
+ }
97
122
got [i ].Raw = nil
98
123
}
99
- if diff := pretty .Compare (got , tt .want ); diff != "" {
124
+
125
+ ignoreOpts := cmpopts .IgnoreFields (detectors.Result {}, "Raw" , "verificationError" )
126
+ if diff := cmp .Diff (got , tt .want , ignoreOpts ); diff != "" {
100
127
t .Errorf ("ClickupPersonalToken.FromData() %s diff: (-got +want)\n %s" , tt .name , diff )
101
128
}
102
129
})
0 commit comments