Ajustes conforme relatorio de testes 2026-08-27
This commit is contained in:
@@ -402,7 +402,44 @@ class TimApiClient:
|
||||
|
||||
def contestar(self, payload: dict[str, Any]) -> Any:
|
||||
if self.mock:
|
||||
return self.fixture("contestacao_tool")
|
||||
# The mock must behave like the real provider for the current request.
|
||||
# Returning the whole static fixture leaked unrelated invoice items into
|
||||
# a one-item transaction and also exposed contradictory fixture fields.
|
||||
fixture = self.fixture("contestacao_tool")
|
||||
provider = fixture.get("body") if isinstance(fixture, dict) and isinstance(fixture.get("body"), dict) else fixture
|
||||
provider = dict(provider or {})
|
||||
requested = [x for x in (payload.get("items") or []) if isinstance(x, dict)]
|
||||
fixture_rows = [x for x in (provider.get("itemsResponse") or provider.get("items_response") or []) if isinstance(x, dict)]
|
||||
|
||||
def norm(value: Any) -> str:
|
||||
import unicodedata
|
||||
text = unicodedata.normalize("NFKD", str(value or "").casefold())
|
||||
text = "".join(ch for ch in text if not unicodedata.combining(ch))
|
||||
return " ".join("".join(ch if ch.isalnum() else " " for ch in text).split())
|
||||
|
||||
selected: list[dict[str, Any]] = []
|
||||
for req in requested:
|
||||
name = str(req.get("itemName") or req.get("item_name") or req.get("name") or "").strip()
|
||||
row = next((dict(x) for x in fixture_rows if norm(x.get("itemName") or x.get("item_name")) == norm(name)), None)
|
||||
if row is None:
|
||||
row = {
|
||||
"correctAccountStatus": "NAO_CRIAR",
|
||||
"itemName": name,
|
||||
"message": "Item não existe na fatura com o valor informado",
|
||||
"status": "NAO_INICIADA",
|
||||
}
|
||||
selected.append(row)
|
||||
if requested:
|
||||
provider["itemsResponse"] = selected
|
||||
provider["sr"] = str(payload.get("sr") or provider.get("sr") or "")
|
||||
# Keep useful provider response fields, but never return the fixture's
|
||||
# stale top-level normalized/result/protocol fields as if they came
|
||||
# from the remote API.
|
||||
if isinstance(fixture, dict):
|
||||
for key in ("barcode", "codigo_boleto", "contestation_id", "contestationId", "manualContaCertaIndicator"):
|
||||
if key in fixture and key not in provider:
|
||||
provider[key] = fixture[key]
|
||||
return provider
|
||||
data = dict(payload)
|
||||
data.setdefault("userId", self._env_first("TIM_CUSTOMER_CONTESTATION_USER_ID", ))
|
||||
data.setdefault("customerIdCurrent", data.get("customerId") or "")
|
||||
|
||||
Reference in New Issue
Block a user