|
| 1 | +import pytest |
| 2 | + |
| 3 | +from leakix.client import HostResult |
| 4 | +from leakix.domain import L9Subdomain |
| 5 | +from leakix.plugin import APIResult |
| 6 | + |
| 7 | + |
| 8 | +class TestAPIResultSerialization: |
| 9 | + def test_to_dict(self): |
| 10 | + data = {"name": "GrafanaOpenPlugin", "description": "Grafana open instances"} |
| 11 | + obj = APIResult.from_dict(data) |
| 12 | + result = obj.to_dict() |
| 13 | + assert result["name"] == "GrafanaOpenPlugin" |
| 14 | + assert result["description"] == "Grafana open instances" |
| 15 | + |
| 16 | + def test_round_trip(self): |
| 17 | + data = {"name": "MongoOpenPlugin", "description": "MongoDB open instances"} |
| 18 | + first = APIResult.from_dict(data).to_dict() |
| 19 | + second = APIResult.from_dict(first).to_dict() |
| 20 | + assert first == second |
| 21 | + |
| 22 | + @pytest.mark.parametrize( |
| 23 | + "data", |
| 24 | + [ |
| 25 | + {"name": "PluginA", "description": "Description A"}, |
| 26 | + {"name": "PluginB", "description": ""}, |
| 27 | + {"name": "", "description": "Empty name plugin"}, |
| 28 | + ], |
| 29 | + ids=["normal", "empty-description", "empty-name"], |
| 30 | + ) |
| 31 | + def test_round_trip_parametrized(self, data): |
| 32 | + first = APIResult.from_dict(data).to_dict() |
| 33 | + second = APIResult.from_dict(first).to_dict() |
| 34 | + assert first == second |
| 35 | + |
| 36 | + |
| 37 | +class TestL9SubdomainSerialization: |
| 38 | + def test_to_dict(self): |
| 39 | + data = { |
| 40 | + "subdomain": "api.example.com", |
| 41 | + "distinct_ips": 3, |
| 42 | + "last_seen": "2024-01-15T10:30:00+00:00", |
| 43 | + } |
| 44 | + obj = L9Subdomain.from_dict(data) |
| 45 | + result = obj.to_dict() |
| 46 | + assert result["subdomain"] == "api.example.com" |
| 47 | + assert result["distinct_ips"] == 3 |
| 48 | + assert "2024-01-15" in result["last_seen"] |
| 49 | + |
| 50 | + def test_round_trip(self): |
| 51 | + data = { |
| 52 | + "subdomain": "www.example.com", |
| 53 | + "distinct_ips": 1, |
| 54 | + "last_seen": "2024-06-20T12:00:00+00:00", |
| 55 | + } |
| 56 | + first = L9Subdomain.from_dict(data).to_dict() |
| 57 | + second = L9Subdomain.from_dict(first).to_dict() |
| 58 | + assert first == second |
| 59 | + |
| 60 | + @pytest.mark.parametrize( |
| 61 | + "data", |
| 62 | + [ |
| 63 | + { |
| 64 | + "subdomain": "mail.example.com", |
| 65 | + "distinct_ips": 0, |
| 66 | + "last_seen": "2024-01-01T00:00:00+00:00", |
| 67 | + }, |
| 68 | + { |
| 69 | + "subdomain": "a.b.c.example.com", |
| 70 | + "distinct_ips": 999, |
| 71 | + "last_seen": "2025-12-31T23:59:59+00:00", |
| 72 | + }, |
| 73 | + ], |
| 74 | + ids=["zero-ips", "deep-subdomain"], |
| 75 | + ) |
| 76 | + def test_round_trip_parametrized(self, data): |
| 77 | + first = L9Subdomain.from_dict(data).to_dict() |
| 78 | + second = L9Subdomain.from_dict(first).to_dict() |
| 79 | + assert first == second |
| 80 | + |
| 81 | + |
| 82 | +class TestHostResultSerialization: |
| 83 | + def test_to_dict_empty(self): |
| 84 | + data = {"Services": None, "Leaks": None} |
| 85 | + obj = HostResult.from_dict(data) |
| 86 | + result = obj.to_dict() |
| 87 | + assert isinstance(result, dict) |
| 88 | + |
| 89 | + def test_round_trip_empty(self): |
| 90 | + data = {"Services": None, "Leaks": None} |
| 91 | + first = HostResult.from_dict(data).to_dict() |
| 92 | + second = HostResult.from_dict(first).to_dict() |
| 93 | + assert first == second |
0 commit comments