gendesign/tradein-mvp/backend/tests/test_me_brand.py
lekss361 95bada3855 feat(tradein): brand-by-account + result IA redesign (distinct asking/sale prices)
- Resolve brand from account: /me returns brand (praktika/kopylov->praktika);
  useBrand uses ?brand= param OR me.brand (param wins) -> praktika auto-branded
  on login (no manual ?brand= needed).
- Result IA (Brusnika-style): single «Ожидаемая цена сделки» headline (lower,
  -X% vs asking, 5-12% note); two DISTINCT reference figures (asking объявления
  vs ДКП Росреестр) + price bars; sources row; 3 always-on explainer cards
  (asking!=real, ремонт по состоянию, скрытые расходы <=15%).
- Defer commercial buyback value (Brusnika p.4): OfferCard stub + TODO, no
  invented numbers (pending Praktika commercial terms).
- tsc clean, ruff clean, 35 backend tests (7 new /me-brand).

Refs #657
2026-05-30 09:38:09 +03:00

50 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Tests for #657 brand-by-account — UserScope.brand resolution.
Pure-function tests against app.core.auth: brand slug привязывается к аккаунту
(praktika/kopylov → "praktika"), остальные юзеры → None. get_user_scope
прокидывает brand в scope, который /me отдаёт фронту.
Без БД и сети: читается реальный auth/roles.yaml (ancestor-walk внутри репо).
"""
from __future__ import annotations
import os
import sys
from unittest.mock import MagicMock
# DATABASE_URL required by config before any app import (см. test_dadata.py).
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://test:test@localhost:5432/test")
# WeasyPrint stub — not installed без GTK (consistent с прочими тестами).
_wp_mock = MagicMock()
sys.modules.setdefault("weasyprint", _wp_mock)
import pytest # noqa: E402
from app.core.auth import get_brand_for_user, get_user_scope # noqa: E402
@pytest.mark.parametrize(
("username", "expected"),
[
("praktika", "praktika"),
("kopylov", "praktika"),
("admin", None),
("user1", None),
("unknown_user", None),
],
)
def test_get_brand_for_user(username: str, expected: str | None) -> None:
assert get_brand_for_user(username) == expected
def test_user_scope_carries_brand_for_praktika() -> None:
scope = get_user_scope("praktika")
assert scope["brand"] == "praktika"
assert scope["username"] == "praktika"
def test_user_scope_brand_none_for_generic_user() -> None:
scope = get_user_scope("user1")
assert scope["brand"] is None