"""Tests para whois_lookup.""" import os import sys sys.path.insert(0, os.path.dirname(__file__)) import whois_lookup as wl from whois_lookup import whois_lookup def _rdap_sample() -> dict: return { "ldhName": "organic-machine.com", "status": ["client transfer prohibited"], "events": [ {"eventAction": "registration", "eventDate": "2020-01-15T10:00:00Z"}, {"eventAction": "expiration", "eventDate": "2027-01-15T10:00:00Z"}, {"eventAction": "last changed", "eventDate": "2026-01-10T08:30:00Z"}, ], "nameservers": [ {"ldhName": "ns1.example.net"}, {"ldhName": "NS2.EXAMPLE.NET"}, ], "entities": [ { "handle": "REG-123", "roles": ["registrar"], "vcardArray": [ "vcard", [ ["version", {}, "text", "4.0"], ["fn", {}, "text", "Example Registrar Inc."], ], ], }, {"handle": "REGISTRANT-9", "roles": ["registrant"]}, ], } def test_normaliza_respuesta_rdap(monkeypatch): """Extrae registrar, fechas, nameservers, status y entities.""" monkeypatch.setattr(wl, "http_get_json", lambda url, timeout=15.0: _rdap_sample()) result = whois_lookup("organic-machine.com") assert result["found"] is True assert result["registrar"] == "Example Registrar Inc." assert result["creation_date"] == "2020-01-15T10:00:00Z" assert result["expiration_date"] == "2027-01-15T10:00:00Z" assert result["last_changed"] == "2026-01-10T08:30:00Z" assert result["nameservers"] == ["ns1.example.net", "ns2.example.net"] assert result["status"] == ["client transfer prohibited"] assert {"handle": "REGISTRANT-9", "roles": ["registrant"]} in result["entities"] assert result["raw"]["ldhName"] == "organic-machine.com" def test_dominio_no_encontrado_404(monkeypatch): """Un HTTP 404 de http_get_json devuelve {'found': False}.""" def fake(url, timeout=15.0): raise RuntimeError("http_get_json: HTTP 404 at 'rdap.org' — not found") monkeypatch.setattr(wl, "http_get_json", fake) result = whois_lookup("nope-no-existe-xyz.invalid") assert result == {"found": False} def test_otro_error_http_se_propaga(monkeypatch): """Un error HTTP distinto de 404 se propaga como RuntimeError.""" def fake(url, timeout=15.0): raise RuntimeError("http_get_json: HTTP 500 at 'rdap.org' — boom") monkeypatch.setattr(wl, "http_get_json", fake) try: whois_lookup("organic-machine.com") assert False, "deberia haberse propagado el error 500" except RuntimeError as e: assert "HTTP 500" in str(e) def test_sin_registrar_ni_fechas(monkeypatch): """RDAP minimo: campos opcionales quedan None / listas vacias.""" monkeypatch.setattr( wl, "http_get_json", lambda url, timeout=15.0: {"ldhName": "x.com"} ) result = whois_lookup("x.com") assert result["found"] is True assert result["registrar"] is None assert result["creation_date"] is None assert result["nameservers"] == [] assert result["status"] == [] assert result["entities"] == [] def test_dominio_vacio_lanza_error(): """Dominio vacio lanza RuntimeError.""" try: whois_lookup("") assert False, "deberia haber lanzado RuntimeError" except RuntimeError: pass