"""Профиль: «о себе» (bio), любимая фракция, аватар (загрузка/отдача/удаление), публичный профиль.""" from __future__ import annotations from fastapi.testclient import TestClient from tests.conftest import add_group_member, create_finished_match, csrf_headers, login # Минимальный «PNG»: достаточно сигнатуры — сервер не декодирует, только сниффит тип. PNG = b"\x89PNG\r\n\x1a\n" + b"\x00" * 64 def _use_tmp_uploads(monkeypatch, tmp_path) -> None: from app.core.config import settings monkeypatch.setattr(settings, "dev_upload_dir", str(tmp_path)) def _finished_match_for(client: TestClient, engine, me: dict) -> int: """Группа + второй игрок + одна завершённая партия (чтобы me попал в лидерборд).""" exps = [e["id"] for e in client.get("/api/expansions").json()] gid = client.post( "/api/groups", json={"name": "Группа", "expansion_ids": exps}, headers=csrf_headers(client) ).json()["id"] p2 = add_group_member(engine, gid, "Соперник") fids = [f["id"] for f in client.get(f"/api/groups/{gid}/factions").json()] create_finished_match( client, gid, [ {"user_id": me["id"], "faction_id": fids[0], "place": 1}, {"user_id": p2, "faction_id": fids[1], "place": 2}, ], ) return gid def test_update_bio(client: TestClient): login(client, "Игрок") r = client.patch("/api/users/me/profile", json={"bio": "Люблю орков"}, headers=csrf_headers(client)) assert r.status_code == 200, r.text assert r.json()["bio"] == "Люблю орков" # Слишком длинное «о себе» → 422. r = client.patch( "/api/users/me/profile", json={"bio": "x" * 501}, headers=csrf_headers(client) ) assert r.status_code == 422, r.text def test_avatar_upload_get_delete(client: TestClient, monkeypatch, tmp_path): _use_tmp_uploads(monkeypatch, tmp_path) me = login(client, "Аватарыч") # Загрузка валидного PNG → avatar_url выставлен. r = client.put( "/api/users/me/avatar", files={"file": ("a.png", PNG, "image/png")}, headers=csrf_headers(client), ) assert r.status_code == 200, r.text avatar_url = r.json()["avatar_url"] assert avatar_url and "/avatar?v=" in avatar_url # Отдача файла. g = client.get(f"/api/users/{me['id']}/avatar") assert g.status_code == 200, g.text assert g.content == PNG assert g.headers["content-type"] == "image/png" # Удаление → avatar_url пропадает, файл больше не отдаётся. r = client.delete("/api/users/me/avatar", headers=csrf_headers(client)) assert r.status_code == 200 and r.json()["avatar_url"] is None, r.text assert client.get(f"/api/users/{me['id']}/avatar").status_code == 404 def test_avatar_rejects_non_image_and_oversize(client: TestClient, monkeypatch, tmp_path): _use_tmp_uploads(monkeypatch, tmp_path) login(client, "Игрок") r = client.put( "/api/users/me/avatar", files={"file": ("x.txt", b"not an image", "text/plain")}, headers=csrf_headers(client), ) assert r.status_code == 422, r.text big = PNG + b"\x00" * (2 * 1024 * 1024 + 10) r = client.put( "/api/users/me/avatar", files={"file": ("big.png", big, "image/png")}, headers=csrf_headers(client), ) assert r.status_code == 422, r.text def test_public_profile(client: TestClient): me = login(client, "Публичный") client.patch("/api/users/me/profile", json={"bio": "Привет"}, headers=csrf_headers(client)) r = client.get(f"/api/users/{me['id']}/profile") assert r.status_code == 200, r.text body = r.json() assert body["nickname"] == "Публичный" assert body["bio"] == "Привет" assert body["avatar_url"] is None assert "overall" in body["stats"] assert client.get("/api/users/999999/profile").status_code == 404 def test_leaderboard_includes_avatar_url(client: TestClient, engine, monkeypatch, tmp_path): _use_tmp_uploads(monkeypatch, tmp_path) me = login(client, "Топчик") _finished_match_for(client, engine, me) def entry_for(uid: int) -> dict: board = client.get("/api/stats/leaderboard").json() return next(e for e in board["entries"] + board["provisional"] if e["user_id"] == uid) assert entry_for(me["id"])["avatar_url"] is None # без аватара — null client.put( "/api/users/me/avatar", files={"file": ("a.png", PNG, "image/png")}, headers=csrf_headers(client), ) assert "/avatar?v=" in entry_for(me["id"])["avatar_url"] # после загрузки — ссылка def test_main_faction_counts_random_games(client: TestClient, engine): """«Чаще всего играет на» — по всей истории, включая рандомные раздачи.""" me = login(client, "Фракционер") exps = [e["id"] for e in client.get("/api/expansions").json()] gid = client.post( "/api/groups", json={"name": "Группа", "expansion_ids": exps}, headers=csrf_headers(client) ).json()["id"] p2 = add_group_member(engine, gid, "Партнёр") fids = [f["id"] for f in client.get(f"/api/groups/{gid}/factions").json()] # Дважды играл фракцией A по рандому, один раз фракцией B — по своему выбору. for _ in range(2): create_finished_match( client, gid, [ {"user_id": me["id"], "faction_id": fids[0], "place": 1, "was_random": True}, {"user_id": p2, "faction_id": fids[1], "place": 2}, ], ) create_finished_match( client, gid, [ {"user_id": me["id"], "faction_id": fids[2], "place": 1}, {"user_id": p2, "faction_id": fids[1], "place": 2}, ], ) stats = client.get("/api/users/me/stats").json() # Условия выбора не важны: A сыграна чаще, пусть и по рандому. assert stats["main_faction"]["faction_id"] == fids[0] assert stats["main_faction"]["name_ru_prepositional"] # падежная форма проставлена # Любимая больше не выводится из истории — она задаётся игроком вручную. assert stats["favorite_faction"] is None def test_main_faction_present_for_random_only_player(client: TestClient, engine): """Игроку, который всегда играет рандомом, «чаще всего играет на» всё равно считается.""" me = login(client, "Рандомщик") exps = [e["id"] for e in client.get("/api/expansions").json()] gid = client.post( "/api/groups", json={"name": "Группа", "expansion_ids": exps}, headers=csrf_headers(client) ).json()["id"] p2 = add_group_member(engine, gid, "Оппонент") fids = [f["id"] for f in client.get(f"/api/groups/{gid}/factions").json()] create_finished_match( client, gid, [ {"user_id": me["id"], "faction_id": fids[0], "place": 1, "was_random": True}, {"user_id": p2, "faction_id": fids[1], "place": 2, "was_random": True}, ], ) stats = client.get("/api/users/me/stats").json() assert stats["main_faction"]["faction_id"] == fids[0] def test_favorite_faction_set_and_clear(client: TestClient): """Любимая фракция — личный выбор игрока: сохраняется и снимается.""" login(client, "Выбирающий") fid = client.get("/api/factions").json()[0]["id"] r = client.patch( "/api/users/me/profile", json={"favorite_faction_id": fid}, headers=csrf_headers(client) ) assert r.status_code == 200, r.text assert r.json()["favorite_faction_id"] == fid assert client.get("/api/users/me").json()["favorite_faction_id"] == fid # Партий нет вовсе, а любимая фракция всё равно показывается — она не из статистики. assert client.get("/api/users/me/stats").json()["favorite_faction"]["id"] == fid r = client.patch( "/api/users/me/profile", json={"favorite_faction_id": None}, headers=csrf_headers(client) ) assert r.status_code == 200 assert r.json()["favorite_faction_id"] is None assert client.get("/api/users/me/stats").json()["favorite_faction"] is None def test_favorite_faction_rejects_unknown(client: TestClient): login(client, "Фантазёр") r = client.patch( "/api/users/me/profile", json={"favorite_faction_id": 99999}, headers=csrf_headers(client) ) assert r.status_code == 422 def test_bio_update_keeps_favorite_faction(client: TestClient): """Частичный PATCH: правка «о себе» не должна обнулять выбранную фракцию.""" login(client, "Аккуратный") fid = client.get("/api/factions").json()[0]["id"] client.patch( "/api/users/me/profile", json={"favorite_faction_id": fid}, headers=csrf_headers(client) ) r = client.patch("/api/users/me/profile", json={"bio": "Только био"}, headers=csrf_headers(client)) assert r.status_code == 200 assert r.json()["bio"] == "Только био" assert r.json()["favorite_faction_id"] == fid def test_prepositional_dictionary_and_fallback(): """Падежная форма берётся из словаря; незнакомый код — именительный как есть.""" from app.services import faction_service assert faction_service.prepositional("orks", "Орки") == "Орках" assert ( faction_service.prepositional("astra_militarum", "Имперская гвардия") == "Имперской гвардии" ) # Фракция, заведённая админом мимо словаря, не роняет вывод. assert faction_service.prepositional("custom_xeno", "Ксеносы") == "Ксеносы"