Skip to content

Commit

Permalink
Merge pull request #984 from sundowndev/fix/ovh
Browse files Browse the repository at this point in the history
Limit OVH scan to specific country codes
  • Loading branch information
sundowndev committed Jan 15, 2022
2 parents 2764197 + e002774 commit 07bbc95
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/remote/ovh_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (s *ovhScanner) ShouldRun() bool {
}

func (s *ovhScanner) Scan(n *number.Number) (interface{}, error) {
if !s.isSupported(n.CountryCode) {
return nil, nil
}

res, err := s.client.Search(*n)
if err != nil {
return nil, err
Expand All @@ -46,3 +50,18 @@ func (s *ovhScanner) Scan(n *number.Number) (interface{}, error) {

return data, nil
}

func (s *ovhScanner) supportedCountryCodes() []int32 {
// See https://api.ovh.com/console/#/telephony/number/detailedZones#GET
return []int32{33, 32, 44, 34, 41}
}

func (s *ovhScanner) isSupported(code int32) bool {
supported := false
for _, c := range s.supportedCountryCodes() {
if code == c {
supported = true
}
}
return supported
}
12 changes: 11 additions & 1 deletion lib/remote/ovh_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestOVHScanner(t *testing.T) {
dummyError := errors.New("dummy")

dummyNumber, _ := number.NewNumber("15556661212")
dummyNumber, _ := number.NewNumber("33365174444")

testcases := []struct {
name string
Expand Down Expand Up @@ -51,6 +51,16 @@ func TestOVHScanner(t *testing.T) {
"ovh": dummyError,
},
},
{
name: "country not supported",
number: func() *number.Number {
num, _ := number.NewNumber("15556661212")
return num
}(),
mocks: func(s *mocks.OVHSupplier) {},
expected: map[string]interface{}{},
wantErrors: map[string]error{},
},
}

for _, tt := range testcases {
Expand Down
4 changes: 3 additions & 1 deletion lib/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (r *Library) Scan(n *number.Number) (map[string]interface{}, map[string]err
errors[name] = err
continue
}
allData[name] = data
if data != nil {
allData[name] = data
}
}
return allData, errors
}
4 changes: 4 additions & 0 deletions lib/remote/suppliers/ovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewOVHSupplier() *OVHSupplier {
func (s *OVHSupplier) Search(num number.Number) (*OVHScannerResponse, error) {
countryCode := strings.ToLower(num.Country)

if countryCode == "" {
return nil, fmt.Errorf("country code +%d wasn't recognized", num.CountryCode)
}

// Build the request
response, err := http.Get(fmt.Sprintf("https://api.ovh.com/1.0/telephony/number/detailedZones?country=%s", countryCode))
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions lib/remote/suppliers/ovh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,15 @@ func TestOVHSupplierError(t *testing.T) {
Err: dummyError,
}, err)
}

func TestOVHSupplierCountryCodeError(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution

num, _ := number.NewNumber("15556661212")

s := NewOVHSupplier()

got, err := s.Search(*num)
assert.Nil(t, got)
assert.EqualError(t, err, "country code +1 wasn't recognized")
}

0 comments on commit 07bbc95

Please sign in to comment.