LOT 1 - Optimizare script build -Instalare mono comanda
This commit is contained in:
parent
5380c3fc63
commit
42ff22bf85
127 changed files with 16163 additions and 532 deletions
104
ai_platform/modules/domain_check/api/tests/test_whois_dates.py
Normal file
104
ai_platform/modules/domain_check/api/tests/test_whois_dates.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
"""
|
||||
Regression tests for WHOIS date handling.
|
||||
|
||||
Root cause of the historical HTTP 500 on `.ro` domains (e.g. exemplu.ro):
|
||||
python-whois returns ROTLD dates as raw strings, and downstream code did
|
||||
`datetime - str`, raising TypeError. These tests lock in the fix.
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.whois_service import normalize_whois_date, _as_list, WhoisService
|
||||
from app.services.risk_scorer import RiskScorer, _coerce_datetime
|
||||
|
||||
|
||||
# --- normalize_whois_date -------------------------------------------------
|
||||
|
||||
@pytest.mark.parametrize("value", [
|
||||
None,
|
||||
"",
|
||||
" ",
|
||||
"none",
|
||||
"N/A",
|
||||
])
|
||||
def test_normalize_returns_none_for_empty(value):
|
||||
assert normalize_whois_date(value) is None
|
||||
|
||||
|
||||
def test_normalize_passthrough_datetime():
|
||||
dt = datetime(2020, 1, 2, 3, 4, 5)
|
||||
assert normalize_whois_date(dt) == dt
|
||||
|
||||
|
||||
def test_normalize_iso_string():
|
||||
assert normalize_whois_date("2020-01-02T03:04:05Z") == datetime(2020, 1, 2, 3, 4, 5)
|
||||
|
||||
|
||||
def test_normalize_rotld_style_string():
|
||||
# ROTLD / free-form formats that broke datetime.fromisoformat
|
||||
assert normalize_whois_date("2015-03-22") == datetime(2015, 3, 22)
|
||||
assert normalize_whois_date("22.03.2015").year == 2015
|
||||
assert normalize_whois_date("Before 2012-08-10") is not None # fuzzy
|
||||
|
||||
|
||||
def test_normalize_list_takes_first_valid():
|
||||
out = normalize_whois_date([None, "2019-05-06", datetime(2021, 1, 1)])
|
||||
assert out == datetime(2019, 5, 6)
|
||||
|
||||
|
||||
def test_normalize_garbage_returns_none():
|
||||
assert normalize_whois_date("not a date at all !!") is None
|
||||
|
||||
|
||||
def test_as_list_normalizes():
|
||||
assert _as_list(None) == []
|
||||
assert _as_list("a") == ["a"]
|
||||
assert _as_list(["a", None, "", "b"]) == ["a", "b"]
|
||||
|
||||
|
||||
# --- the actual crash site: risk scorer must never raise on bad dates -----
|
||||
|
||||
def test_score_domain_age_does_not_raise_on_string():
|
||||
"""The exact regression: a raw string creation_date must not 500."""
|
||||
scorer = RiskScorer()
|
||||
score, details = scorer._score_domain_age({'creation_date': '2015-03-22'})
|
||||
assert isinstance(score, int)
|
||||
assert details['details']['age_days'] is not None
|
||||
|
||||
|
||||
def test_score_domain_age_does_not_raise_on_garbage():
|
||||
scorer = RiskScorer()
|
||||
score, details = scorer._score_domain_age({'creation_date': 'totally-not-a-date'})
|
||||
# Garbage coerces to None -> "unable to determine" path, never an exception.
|
||||
assert score == 50
|
||||
assert details['details']['age_days'] is None
|
||||
|
||||
|
||||
def test_score_domain_age_missing():
|
||||
scorer = RiskScorer()
|
||||
score, details = scorer._score_domain_age({})
|
||||
assert score == 50
|
||||
|
||||
|
||||
def test_coerce_datetime():
|
||||
assert _coerce_datetime(None) is None
|
||||
assert _coerce_datetime("2020-01-01") == datetime(2020, 1, 1)
|
||||
assert _coerce_datetime(datetime(2020, 1, 1)) == datetime(2020, 1, 1)
|
||||
assert _coerce_datetime("garbage") is None
|
||||
|
||||
|
||||
# --- is_registered signal -------------------------------------------------
|
||||
|
||||
def test_is_registered_true_when_creation_date():
|
||||
assert WhoisService._is_registered({'creation_date': datetime(2020, 1, 1)}) is True
|
||||
|
||||
|
||||
def test_is_registered_true_when_registrar():
|
||||
assert WhoisService._is_registered({'registrar': 'ROTLD'}) is True
|
||||
|
||||
|
||||
def test_is_registered_none_when_sparse():
|
||||
# No positive signal -> inconclusive (None), DNS decides downstream.
|
||||
assert WhoisService._is_registered({'creation_date': None, 'registrar': None,
|
||||
'name_servers': [], 'status': []}) is None
|
||||
Loading…
Add table
Add a link
Reference in a new issue