import pytest from server.utils import ( ensure_parent_dir, get_client_ip, get_mac_from_arp, load_config, normalize_mac, validate_imsi, validate_mac, validate_op_key, ) class _FakeRequest: def __init__(self, headers=None, remote_addr=None): self.headers = headers or {} self.remote_addr = remote_addr class TestClientResolution: def test_client_ip_prefers_forwarded(self): req = _FakeRequest(headers={"X-Forwarded-For": "203.0.113.7, 10.0.0.1"}, remote_addr="10.0.0.1") assert get_client_ip(req) == "203.0.113.7" def test_client_ip_falls_back_to_remote_addr(self): assert get_client_ip(_FakeRequest(remote_addr="192.168.4.10")) == "192.168.4.10" def test_client_ip_empty_when_unknown(self): assert get_client_ip(_FakeRequest()) == "" def test_mac_from_arp_match(self, tmp_path): arp = tmp_path / "arp" arp.write_text( "IP address HW type Flags HW address Mask Device\n" "192.168.4.10 0x1 0x2 AA:BB:CC:DD:EE:FF * wlan0\n" ) assert get_mac_from_arp("192.168.4.10", str(arp)) == "aa:bb:cc:dd:ee:ff" def test_mac_from_arp_no_match(self, tmp_path): arp = tmp_path / "arp" arp.write_text("IP address ...\n192.168.4.99 0x1 0x2 11:22:33:44:55:66 * wlan0\n") assert get_mac_from_arp("192.168.4.10", str(arp)) is None def test_mac_from_arp_missing_file(self): assert get_mac_from_arp("192.168.4.10", "/no/such/arp") is None class TestEnsureParentDir: def test_bare_filename_does_not_crash(self): # No directory component -> must be a no-op, not FileNotFoundError. ensure_parent_dir("subscribers.db") def test_creates_nested_dirs(self, tmp_path): target = tmp_path / "a" / "b" / "x.db" ensure_parent_dir(str(target)) assert (tmp_path / "a" / "b").is_dir() class TestMac: def test_valid(self): assert validate_mac("aa:bb:cc:dd:ee:ff") def test_invalid(self): assert not validate_mac("nope") assert not validate_mac("") def test_normalize_dashes_and_case(self): assert normalize_mac("AA-BB-CC-DD-EE-FF") == "aa:bb:cc:dd:ee:ff" def test_normalize_no_separator(self): assert normalize_mac("aabbccddeeff") == "aa:bb:cc:dd:ee:ff" class TestImsi: def test_valid(self): assert validate_imsi("302720000000001") def test_wrong_length(self): assert not validate_imsi("30272000000000") def test_non_digit(self): assert not validate_imsi("30272000000000x") class TestOpKey: def test_valid(self): assert validate_op_key("63bfa50ee6523365ff14c1f45f88737d") def test_wrong_length(self): assert not validate_op_key("abcd") def test_non_hex(self): assert not validate_op_key("zz" * 16) class TestLoadConfig: def _base(self): return ( "network: {mcc: '302', mnc: '720', op_key: '63bfa50ee6523365ff14c1f45f88737d'}\n" "server: {host: '0.0.0.0', port: 5000}\n" "database: {path: '/tmp/x.db'}\n" "rate_limiting: {window_hours: 1}\n" "profile: {profile_name: 'P', carrier_name: 'C'}\n" ) def test_valid(self, tmp_path): p = tmp_path / "c.yaml" p.write_text(self._base()) cfg = load_config(str(p)) assert cfg["network"]["mcc"] == "302" def test_missing_file(self): with pytest.raises(FileNotFoundError): load_config("/no/such/config.yaml") def test_empty_file(self, tmp_path): p = tmp_path / "empty.yaml" p.write_text("") with pytest.raises(ValueError, match="empty or not a mapping"): load_config(str(p)) def test_missing_section(self, tmp_path): p = tmp_path / "c.yaml" p.write_text("network: {op_key: '63bfa50ee6523365ff14c1f45f88737d'}\n") with pytest.raises(ValueError, match="Missing required config section"): load_config(str(p)) def test_bad_op_key(self, tmp_path): p = tmp_path / "c.yaml" p.write_text(self._base().replace("63bfa50ee6523365ff14c1f45f88737d", "abcd")) with pytest.raises(ValueError, match="op_key"): load_config(str(p))