veza/veza-backend-api/internal/services/geoip_service_test.go

60 lines
1.5 KiB
Go
Raw Normal View History

package services
import (
"os"
"testing"
"go.uber.org/zap"
)
func TestNewGeoIPServiceDisabled(t *testing.T) {
os.Unsetenv("GEOIP_DB_PATH")
logger := zap.NewNop()
svc := NewGeoIPService(logger)
if svc.IsEnabled() {
t.Error("expected GeoIP to be disabled when GEOIP_DB_PATH not set")
}
country, city := svc.Lookup("1.2.3.4")
if country != "" || city != "" {
t.Error("expected empty results when disabled")
}
}
func TestNewGeoIPServiceMissingDB(t *testing.T) {
os.Setenv("GEOIP_DB_PATH", "/nonexistent/path.mmdb")
defer os.Unsetenv("GEOIP_DB_PATH")
logger := zap.NewNop()
svc := NewGeoIPService(logger)
if svc.IsEnabled() {
t.Error("expected GeoIP to be disabled when DB file doesn't exist")
}
}
func TestGeoIPLookupPrivateIPs(t *testing.T) {
// Even if enabled, private IPs should return empty
svc := &GeoIPService{logger: zap.NewNop(), enabled: true}
tests := []string{"127.0.0.1", "::1", "192.168.1.1", "10.0.0.1", "0.0.0.0"}
for _, ip := range tests {
country, city := svc.Lookup(ip)
if country != "" || city != "" {
t.Errorf("expected empty for private IP %s, got country=%q city=%q", ip, country, city)
}
}
}
func TestGeoIPLookupInvalidIP(t *testing.T) {
svc := &GeoIPService{logger: zap.NewNop(), enabled: true}
country, city := svc.Lookup("not-an-ip")
if country != "" || city != "" {
t.Error("expected empty for invalid IP")
}
}
// TestGeoIPResolverInterface verifies GeoIPService satisfies GeoIPResolver.
func TestGeoIPResolverInterface(t *testing.T) {
var _ GeoIPResolver = &GeoIPService{}
}