Skip to content

Commit

Permalink
test(remote): scanner options
Browse files Browse the repository at this point in the history
improve coverage for scanner options
  • Loading branch information
sundowndev committed Feb 15, 2024
1 parent 880a5a7 commit 7bec7f8
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func runScan(opts *ScanCmdOptions) {
remoteLibrary := remote.NewLibrary(f)
remote.InitScanners(remoteLibrary)

result, errs := remoteLibrary.Scan(num)
// Scanner options are currently not used in CLI
result, errs := remoteLibrary.Scan(num, remote.ScannerOptions{})

err = output.GetOutput(output.Console, color.Output).Write(result, errs)
if err != nil {
Expand Down
84 changes: 82 additions & 2 deletions lib/remote/googlecse_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestGoogleCSEScanner_Scan_Success(t *testing.T) {
testcases := []struct {
name string
number *number.Number
opts ScannerOptions
expected map[string]interface{}
wantErrors map[string]error
mocks func()
Expand Down Expand Up @@ -89,6 +90,75 @@ func TestGoogleCSEScanner_Scan_Success(t *testing.T) {
})
},
},
{
name: "test with options and no results",
number: test.NewFakeUSNumber(),
opts: ScannerOptions{
"cx": "custom_cx",
"api_key": "secret",
},
expected: map[string]interface{}{
"googlecse": GoogleCSEScannerResponse{
Homepage: "https://cse.google.com/cse?cx=custom_cx",
ResultCount: 0,
TotalResultCount: 0,
TotalRequestCount: 2,
Items: nil,
},
},
wantErrors: map[string]error{},
mocks: func() {
gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "custom_cx").
// TODO: ensure that custom api key is used
// MatchHeader("Authorization", "secret").
// TODO: the matcher below doesn't work for some reason
//MatchParam("q", "intext:\"14152229670\" OR intext:\"+14152229670\" OR intext:\"4152229670\" OR intext:\"(415) 222-9670\"").
MatchParam("start", "0").
Reply(200).
JSON(&customsearch.Search{
ServerResponse: googleapi.ServerResponse{
Header: http.Header{},
HTTPStatusCode: 200,
},
SearchInformation: &customsearch.SearchSearchInformation{
FormattedSearchTime: "0",
FormattedTotalResults: "0",
SearchTime: 0,
TotalResults: "0",
ForceSendFields: nil,
NullFields: nil,
},
Items: []*customsearch.Result{},
})

gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "custom_cx").
// TODO: ensure that custom api key is used
// MatchHeader("Authorization", "secret").
// TODO: the matcher below doesn't work for some reason
//MatchParam("q", "(ext:doc OR ext:docx OR ext:odt OR ext:pdf OR ext:rtf OR ext:sxw OR ext:psw OR ext:ppt OR ext:pptx OR ext:pps OR ext:csv OR ext:txt OR ext:xls) intext:\"14152229670\" OR intext:\"+14152229670\" OR intext:\"4152229670\" OR intext:\"(415)+222-9670\"").
MatchParam("start", "0").
Reply(200).
JSON(&customsearch.Search{
ServerResponse: googleapi.ServerResponse{
Header: http.Header{},
HTTPStatusCode: 200,
},
SearchInformation: &customsearch.SearchSearchInformation{
FormattedSearchTime: "0",
FormattedTotalResults: "0",
SearchTime: 0,
TotalResults: "0",
ForceSendFields: nil,
NullFields: nil,
},
Items: []*customsearch.Result{},
})
},
},
{
name: "test with results",
number: test.NewFakeUSNumber(),
Expand Down Expand Up @@ -239,11 +309,11 @@ func TestGoogleCSEScanner_Scan_Success(t *testing.T) {
remote := NewLibrary(filter.NewEngine())
remote.AddScanner(scanner)

if scanner.DryRun(*tt.number, ScannerOptions{}) != nil {
if scanner.DryRun(*tt.number, tt.opts) != nil {
t.Fatal("DryRun() should return nil")
}

got, errs := remote.Scan(tt.number)
got, errs := remote.Scan(tt.number, tt.opts)
if len(tt.wantErrors) > 0 {
assert.Equal(t, tt.wantErrors, errs)
} else {
Expand All @@ -263,6 +333,16 @@ func TestGoogleCSEScanner_DryRun(t *testing.T) {
assert.Nil(t, scanner.DryRun(*test.NewFakeUSNumber(), ScannerOptions{}))
}

func TestGoogleCSEScanner_DryRunWithOptions(t *testing.T) {
errStr := "search engine ID and/or API key is not defined"

scanner := NewGoogleCSEScanner(&http.Client{})
assert.Nil(t, scanner.DryRun(*test.NewFakeUSNumber(), ScannerOptions{"cx": "test", "api_key": "secret"}))
assert.EqualError(t, scanner.DryRun(*test.NewFakeUSNumber(), ScannerOptions{"cx": "", "api_key": ""}), errStr)
assert.EqualError(t, scanner.DryRun(*test.NewFakeUSNumber(), ScannerOptions{"cx": "test"}), errStr)
assert.EqualError(t, scanner.DryRun(*test.NewFakeUSNumber(), ScannerOptions{"api_key": "test"}), errStr)
}

func TestGoogleCSEScanner_DryRun_Error(t *testing.T) {
scanner := NewGoogleCSEScanner(&http.Client{})
assert.EqualError(t, scanner.DryRun(*test.NewFakeUSNumber(), ScannerOptions{}), "search engine ID and/or API key is not defined")
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/googlesearch_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestGoogleSearchScanner(t *testing.T) {
t.Fatal("DryRun() should return nil")
}

got, errs := lib.Scan(tt.number)
got, errs := lib.Scan(tt.number, remote.ScannerOptions{})
if len(tt.wantErrors) > 0 {
assert.Equal(t, tt.wantErrors, errs)
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/remote/local_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func TestLocalScanner(t *testing.T) {
remote := NewLibrary(filter.NewEngine())
remote.AddScanner(scanner)

if scanner.DryRun(*tt.number, map[string]interface{}{}) != nil {
if scanner.DryRun(*tt.number, ScannerOptions{}) != nil {
t.Fatal("DryRun() should return nil")
}

got, errs := remote.Scan(tt.number)
got, errs := remote.Scan(tt.number, ScannerOptions{})
if len(tt.wantErrors) > 0 {
assert.Equal(t, tt.wantErrors, errs)
} else {
Expand Down
42 changes: 41 additions & 1 deletion lib/remote/numverify_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestNumverifyScanner(t *testing.T) {
testcases := []struct {
name string
number *number.Number
opts remote.ScannerOptions
mocks func(s *mocks.NumverifySupplier)
expected map[string]interface{}
wantErrors map[string]error
Expand Down Expand Up @@ -91,6 +92,45 @@ func TestNumverifyScanner(t *testing.T) {
expected: map[string]interface{}{},
wantErrors: map[string]error{},
},
{
name: "should run with options defined",
opts: remote.ScannerOptions{
"api_key": "secret",
},
number: func() *number.Number {
n, _ := number.NewNumber("15556661212")
return n
}(),
mocks: func(s *mocks.NumverifySupplier) {
s.On("Validate", "15556661212", "secret").Return(&suppliers.NumverifyValidateResponse{
Valid: true,
Number: "test",
LocalFormat: "test",
InternationalFormat: "test",
CountryPrefix: "test",
CountryCode: "test",
CountryName: "test",
Location: "test",
Carrier: "test",
LineType: "test",
}, nil).Once()
},
expected: map[string]interface{}{
"numverify": remote.NumverifyScannerResponse{
Valid: true,
Number: "test",
LocalFormat: "test",
InternationalFormat: "test",
CountryPrefix: "test",
CountryCode: "test",
CountryName: "test",
Location: "test",
Carrier: "test",
LineType: "test",
},
},
wantErrors: map[string]error{},
},
}

for _, tt := range testcases {
Expand All @@ -102,7 +142,7 @@ func TestNumverifyScanner(t *testing.T) {
lib := remote.NewLibrary(filter.NewEngine())
lib.AddScanner(scanner)

got, errs := lib.Scan(tt.number)
got, errs := lib.Scan(tt.number, tt.opts)
if len(tt.wantErrors) > 0 {
assert.Equal(t, tt.wantErrors, errs)
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/ovh_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestOVHScanner(t *testing.T) {
lib := remote.NewLibrary(filter.NewEngine())
lib.AddScanner(scanner)

got, errs := lib.Scan(tt.number)
got, errs := lib.Scan(tt.number, remote.ScannerOptions{})
if len(tt.wantErrors) > 0 {
assert.Equal(t, tt.wantErrors, errs)
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (r *Library) addError(k string, err error) {
r.errors[k] = err
}

func (r *Library) Scan(n *number.Number) (map[string]interface{}, map[string]error) {
func (r *Library) Scan(n *number.Number, opts ScannerOptions) (map[string]interface{}, map[string]error) {
var wg sync.WaitGroup

for _, s := range r.scanners {
Expand All @@ -69,15 +69,15 @@ func (r *Library) Scan(n *number.Number) (map[string]interface{}, map[string]err
}
}()

if err := s.DryRun(*n, make(ScannerOptions)); err != nil {
if err := s.DryRun(*n, opts); err != nil {
logrus.
WithField("scanner", s.Name()).
WithField("reason", err.Error()).
Debug("Scanner was ignored because it should not run")
return
}

data, err := s.Run(*n, make(ScannerOptions))
data, err := s.Run(*n, opts)
if err != nil {
r.addError(s.Name(), err)
return
Expand Down
10 changes: 5 additions & 5 deletions lib/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestRemoteLibrary_SuccessScan(t *testing.T) {
lib.AddScanner(fakeScanner)
lib.AddScanner(fakeScanner2)

result, errs := lib.Scan(num)
result, errs := lib.Scan(num, remote.ScannerOptions{})
assert.Equal(t, expected, result)
assert.Equal(t, map[string]error{}, errs)

Expand All @@ -65,7 +65,7 @@ func TestRemoteLibrary_FailedScan(t *testing.T) {

lib.AddScanner(fakeScanner)

result, errs := lib.Scan(num)
result, errs := lib.Scan(num, remote.ScannerOptions{})
assert.Equal(t, map[string]interface{}{}, result)
assert.Equal(t, map[string]error{"fake": dummyError}, errs)

Expand All @@ -86,7 +86,7 @@ func TestRemoteLibrary_EmptyScan(t *testing.T) {

lib.AddScanner(fakeScanner)

result, errs := lib.Scan(num)
result, errs := lib.Scan(num, remote.ScannerOptions{})
assert.Equal(t, map[string]interface{}{}, result)
assert.Equal(t, map[string]error{}, errs)

Expand All @@ -108,7 +108,7 @@ func TestRemoteLibrary_PanicRun(t *testing.T) {

lib.AddScanner(fakeScanner)

result, errs := lib.Scan(num)
result, errs := lib.Scan(num, remote.ScannerOptions{})
assert.Equal(t, map[string]interface{}{}, result)
assert.Equal(t, map[string]error{"fake": errors.New("panic occurred while running scan, see debug logs")}, errs)

Expand All @@ -129,7 +129,7 @@ func TestRemoteLibrary_PanicDryRun(t *testing.T) {

lib.AddScanner(fakeScanner)

result, errs := lib.Scan(num)
result, errs := lib.Scan(num, remote.ScannerOptions{})
assert.Equal(t, map[string]interface{}{}, result)
assert.Equal(t, map[string]error{"fake": errors.New("panic occurred while running scan, see debug logs")}, errs)

Expand Down

0 comments on commit 7bec7f8

Please sign in to comment.