103 lines
3.0 KiB
Python
103 lines
3.0 KiB
Python
"""Тесты deep-link входа в админку через Telegram."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from common.repositories import admin_login_tokens_repo
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_tokens(monkeypatch: pytest.MonkeyPatch) -> dict[str, Any]:
|
|
state: dict[str, Any] = {"confirmed": None}
|
|
|
|
async def create_token() -> str:
|
|
return "test-token"
|
|
|
|
async def consume_token(token: str) -> int | None:
|
|
return state["confirmed"]
|
|
|
|
monkeypatch.setattr(admin_login_tokens_repo, "create_token", create_token)
|
|
monkeypatch.setattr(admin_login_tokens_repo, "consume_token", consume_token)
|
|
return state
|
|
|
|
|
|
def _client(client_factory, settings_factory) -> TestClient:
|
|
return client_factory(
|
|
authenticated=False,
|
|
follow_redirects=False,
|
|
settings=settings_factory(
|
|
bot_username="debot_test_bot",
|
|
admin_public_url="https://debot-test.mpakosrv.ru",
|
|
),
|
|
)
|
|
|
|
|
|
def test_login_page_has_deeplink(fake_tokens: dict[str, Any], client_factory, settings_factory) -> None:
|
|
response = _client(client_factory, settings_factory).get("/login")
|
|
|
|
assert response.status_code == 200
|
|
assert "Открыть Telegram" in response.text
|
|
assert "/auth/deeplink/start" in response.text
|
|
|
|
|
|
def test_deeplink_start_redirects_to_telegram(
|
|
fake_tokens: dict[str, Any],
|
|
client_factory,
|
|
settings_factory,
|
|
) -> None:
|
|
client = _client(client_factory, settings_factory)
|
|
|
|
response = client.get("/auth/deeplink/start")
|
|
|
|
assert response.status_code == 303
|
|
assert response.headers["location"] == "https://t.me/debot_test_bot?start=login_test-token"
|
|
assert "debot_login_token" in response.headers["set-cookie"]
|
|
|
|
|
|
def test_deeplink_status_without_cookie(fake_tokens: dict[str, Any], client_factory, settings_factory) -> None:
|
|
response = _client(client_factory, settings_factory).get("/auth/deeplink/status")
|
|
|
|
assert response.json() == {"status": "none"}
|
|
|
|
|
|
def test_deeplink_status_pending(fake_tokens: dict[str, Any], client_factory, settings_factory) -> None:
|
|
client = _client(client_factory, settings_factory)
|
|
client.get("/auth/deeplink/start")
|
|
|
|
response = client.get("/auth/deeplink/status")
|
|
|
|
assert response.json() == {"status": "pending"}
|
|
|
|
|
|
def test_deeplink_status_confirms_session(
|
|
fake_tokens: dict[str, Any],
|
|
client_factory,
|
|
settings_factory,
|
|
) -> None:
|
|
fake_tokens["confirmed"] = 1
|
|
client = _client(client_factory, settings_factory)
|
|
client.get("/auth/deeplink/start")
|
|
|
|
response = client.get("/auth/deeplink/status")
|
|
|
|
assert response.json() == {"status": "ok"}
|
|
assert client.get("/").status_code == 200
|
|
|
|
|
|
def test_deeplink_status_denied_for_unknown_admin(
|
|
fake_tokens: dict[str, Any],
|
|
client_factory,
|
|
settings_factory,
|
|
) -> None:
|
|
fake_tokens["confirmed"] = 2
|
|
client = _client(client_factory, settings_factory)
|
|
client.get("/auth/deeplink/start")
|
|
|
|
response = client.get("/auth/deeplink/status")
|
|
|
|
assert response.json() == {"status": "denied"}
|