diff --git a/occupi-backend/pkg/database/database_helpers.go b/occupi-backend/pkg/database/database_helpers.go index 5d86312e..8a255d6e 100644 --- a/occupi-backend/pkg/database/database_helpers.go +++ b/occupi-backend/pkg/database/database_helpers.go @@ -17,13 +17,36 @@ func IsLocationInRange(locations []models.Location, unrecognizedLogger *ipinfo.C } for _, loc := range locations { + // Skip if loc.Location is empty + if loc.Location == "" { + continue + } + coords1 := strings.Split(loc.Location, ",") - lat1, _ := strconv.ParseFloat(coords1[0], 64) - lon1, _ := strconv.ParseFloat(coords1[1], 64) + // Skip if coords1 does not contain exactly 2 elements (latitude and longitude) + if len(coords1) != 2 { + continue + } + + lat1, err1 := strconv.ParseFloat(coords1[0], 64) + lon1, err2 := strconv.ParseFloat(coords1[1], 64) + // Skip if parsing latitude or longitude fails + if err1 != nil || err2 != nil { + continue + } coords2 := strings.Split(unrecognizedLogger.Location, ",") - lat2, _ := strconv.ParseFloat(coords2[0], 64) - lon2, _ := strconv.ParseFloat(coords2[1], 64) + // Skip if coords2 does not contain exactly 2 elements (latitude and longitude) + if len(coords2) != 2 { + continue + } + + lat2, err3 := strconv.ParseFloat(coords2[0], 64) + lon2, err4 := strconv.ParseFloat(coords2[1], 64) + // Skip if parsing latitude or longitude fails + if err3 != nil || err4 != nil { + continue + } loc1 := haversine.Coord{Lat: lat1, Lon: lon1} loc2 := haversine.Coord{Lat: lat2, Lon: lon2} diff --git a/occupi-backend/tests/database_test.go b/occupi-backend/tests/database_test.go index d48a8003..7d0aec58 100644 --- a/occupi-backend/tests/database_test.go +++ b/occupi-backend/tests/database_test.go @@ -4789,6 +4789,46 @@ func TestIsLocationInRange(t *testing.T) { unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles expected: true, }, + { + name: "Empty Location String", + locations: []models.Location{ + {City: "CityF", Region: "RegionF", Country: "CountryF", Location: ""}, // Empty location + }, + unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles + expected: false, // Should skip and return false since there's no valid location within range + }, + { + name: "Invalid Location Format (empty string) for Unrecognized Logger", + locations: []models.Location{ + {City: "CityG", Region: "RegionG", Country: "CountryG", Location: "40.7128,-74.0060"}, // New York + }, + unrecognizedLogger: &ipinfo.Core{Location: ""}, // Empty location + expected: false, // Should skip and return false since the location format is invalid + }, + { + name: "Invalid Location Format (single coordinate)", + locations: []models.Location{ + {City: "CityG", Region: "RegionG", Country: "CountryG", Location: "40.7128"}, // Incomplete location + }, + unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles + expected: false, // Should skip and return false since the location format is invalid + }, + { + name: "Invalid Location Format (non-numeric coordinates)", + locations: []models.Location{ + {City: "CityH", Region: "RegionH", Country: "CountryH", Location: "abc,xyz"}, // Non-numeric location + }, + unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles + expected: false, // Should skip and return false since the location format is invalid + }, + { + name: "Invalid Location Format (non-numeric latitude) for Unrecognized Logger", + locations: []models.Location{ + {City: "CityI", Region: "RegionI", Country: "CountryI", Location: "40.7128,-74.0060"}, // New York + }, + unrecognizedLogger: &ipinfo.Core{Location: "abc,-118"}, // Non-numeric latitude + expected: false, // Should skip and return false since the location format is invalid + }, } for _, tt := range tests {