"""Skin Telegram login: non-ASCII hash is a mismatch (not a 500), cross-site Origin is refused.""" import ast import asyncio import hashlib import hmac import json import time from pathlib import Path from aiohttp import web from aiohttp.test_utils import make_mocked_request SRC = Path(__file__).resolve().parents[1] / "gateway" / "grok-web-skin.py" def _load(*names, **extra): tree = ast.parse(SRC.read_text(encoding="utf-8")) nodes = [n for n in tree.body if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and n.name in names] assert {n.name for n in nodes} == set(names) ns = {"time": time, "hashlib": hashlib, "hmac": hmac, "json": json, "web": web, "TG_AUTH_MAX_AGE": 86400, "_tg_bot_token": lambda: "123:abc", **extra} exec(compile(ast.Module(body=nodes, type_ignores=[]), str(SRC), "exec"), ns) return ns def test_non_ascii_hash_is_rejected_not_500(): ns = _load("_tg_check_hash") data = {"id": "1", "auth_date": str(int(time.time())), "hash": "ф" * 64} assert ns["_tg_check_hash"](data) is False def test_tg_login_refuses_cross_site_origin(): ns = _load("tg_login") req = make_mocked_request("POST", "/api/tg_widget_login", headers={"Origin": "https://evil.example"}) resp = asyncio.run(ns["tg_login"](req)) assert resp.status == 403 assert json.loads(resp.body)["error"] == "bad_origin"