Skip to content

Commit

Permalink
test: googlecse scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Sep 4, 2022
1 parent 385adf5 commit 36a87c9
Showing 1 changed file with 223 additions and 46 deletions.
269 changes: 223 additions & 46 deletions lib/remote/googlecse_scanner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package remote

import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
"github.com/sundowndev/phoneinfoga/v2/lib/number"
Expand All @@ -13,12 +14,13 @@ import (
"testing"
)

func TestGoogleCSEScanner(t *testing.T) {
func TestGoogleCSEScanner_Scan_Success(t *testing.T) {
testcases := []struct {
name string
number *number.Number
expected map[string]interface{}
wantErrors map[string]error
mocks func()
}{
{
name: "test with no results",
Expand All @@ -33,6 +35,187 @@ func TestGoogleCSEScanner(t *testing.T) {
},
},
wantErrors: map[string]error{},
mocks: func() {
gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "fake_search_engine_id").
// 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", "fake_search_engine_id").
// 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(),
expected: map[string]interface{}{
"googlecse": GoogleCSEScannerResponse{
Homepage: "https://cse.google.com/cse?cx=fake_search_engine_id",
ResultCount: 2,
TotalResultCount: 2,
TotalRequestCount: 2,
Items: []ResultItem{
{
Title: "Result 1",
URL: "https://result1.com",
},
{
Title: "Result 2",
URL: "https://result2.com",
},
},
},
},
wantErrors: map[string]error{},
mocks: func() {
gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "fake_search_engine_id").
// 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: "2",
SearchTime: 0,
TotalResults: "2",
ForceSendFields: nil,
NullFields: nil,
},
Items: []*customsearch.Result{
{
Title: "Result 1",
Link: "https://result1.com",
},
{
Title: "Result 2",
Link: "https://result2.com",
},
},
})

gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "fake_search_engine_id").
// 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 rate limit error",
number: test.NewFakeUSNumber(),
expected: map[string]interface{}{},
wantErrors: map[string]error{
"googlecse": errors.New("rate limit exceeded, see https://developers.google.com/custom-search/v1/overview#pricing"),
},
mocks: func() {
gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "fake_search_engine_id").
MatchParam("start", "0").
Reply(429).
JSON(&googleapi.Error{
Code: 429,
Message: "rate limit exceeded",
Details: nil,
Body: "rate limit exceeded",
Header: http.Header{},
Errors: []googleapi.ErrorItem{},
})
},
},
{
name: "test with basic error",
number: test.NewFakeUSNumber(),
expected: map[string]interface{}{},
wantErrors: map[string]error{
"googlecse": &googleapi.Error{
Code: 403,
Message: "",
Details: nil,
Body: "{\"code\":403,\"message\":\"dummy error\",\"details\":null,\"Body\":\"dummy error\",\"Header\":{},\"Errors\":[]}\n",
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Errors: nil,
},
},
mocks: func() {
gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "fake_search_engine_id").
MatchParam("start", "0").
Reply(403).
JSON(&googleapi.Error{
Code: 403,
Message: "dummy error",
Details: nil,
Body: "dummy error",
Header: http.Header{},
Errors: []googleapi.ErrorItem{},
})
},
},
}

Expand All @@ -42,51 +225,7 @@ func TestGoogleCSEScanner(t *testing.T) {
_ = os.Setenv("GOOGLE_API_KEY", "fake_api_key")
defer os.Clearenv()

gock.New("https://customsearch.googleapis.com").
Get("/customsearch/v1").
MatchParam("cx", "fake_search_engine_id").
// 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", "fake_search_engine_id").
// 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{},
})
tt.mocks()
defer gock.Off() // Flush pending mocks after test execution

scanner := NewGoogleCSEScanner(&http.Client{})
Expand All @@ -107,3 +246,41 @@ func TestGoogleCSEScanner(t *testing.T) {
})
}
}

func TestGoogleCSEScanner_ShouldRun(t *testing.T) {
scanner := NewGoogleCSEScanner(&http.Client{})
remote := NewLibrary(filter.NewEngine())
remote.AddScanner(scanner)

assert.False(t, scanner.ShouldRun(*test.NewFakeUSNumber()))
}

func TestGoogleCSEScanner_MaxResults(t *testing.T) {
testcases := []struct {
value string
expected int64
}{
{
value: "",
expected: 10,
},
{
value: "20",
expected: 20,
},
{
value: "120",
expected: 100,
},
}

defer os.Clearenv()

for _, tt := range testcases {
_ = os.Setenv("GOOGLECSE_MAX_RESULTS", tt.value)

scanner := NewGoogleCSEScanner(nil)

assert.Equal(t, tt.expected, scanner.(*googleCSEScanner).MaxResults)
}
}

0 comments on commit 36a87c9

Please sign in to comment.