Files
hearth/script/wowmine-stickers/importer/tests/test_validation.py
T
2026-11-27 01:40:30 +00:00

87 lines
2.9 KiB
Python

"""Client catalog compatibility and player-controlled input rejection."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from pydantic import JsonValue, ValidationError
from models import Catalog, ImportFailureError
from tests.conftest import TOKEN, Reply, api_server, encoded, routes_for
from tests.test_import import run_cli
if TYPE_CHECKING:
from pathlib import Path
@pytest.mark.parametrize(
"pack",
[
"https://evil.test/addstickers/FixturePack",
"https://t.me@evil.test/addstickers/FixturePack",
"https://user@t.me/addstickers/FixturePack",
"//t.me/addstickers/FixturePack",
"https://t.me/addstickers/FixturePack?host=evil.test",
"FixturePack/../secret",
"FixturePack\n",
],
)
def test_player_pack_input_cannot_select_fetch_origin(
tmp_path: Path, pack: str
) -> None:
"""Reject player-controlled origins before making any fixture request."""
received: list[str] = []
with api_server({}, received) as origin:
result = run_cli(tmp_path, origin, pack)
assert result.returncode == 1
assert received == []
assert not (tmp_path / "cache" / "catalog.json").exists()
@pytest.mark.parametrize("route", ["getStickerSet", "static.webp"])
def test_redirect_never_sends_token_to_destination(
tmp_path: Path,
conversion_assets: dict[str, bytes],
route: str,
) -> None:
"""Never forward credential-bearing API or file redirects."""
received: list[str] = []
with api_server({}, received) as destination:
routes = routes_for({"static.webp": conversion_assets["static.webp"]})
routes[route] = Reply(b"", 302, destination + "/" + TOKEN)
with api_server(routes) as origin:
result = run_cli(tmp_path, origin)
assert result.returncode == 1
assert received == []
@pytest.mark.parametrize(
("field", "value"),
[
("frames", 0),
("frames", 61),
("id", "A" * 64),
("emoji", "😀" * 33),
],
)
def test_catalog_rejects_values_client_cannot_use(field: str, value: str | int) -> None:
"""Reject sticker values outside the client's catalog contract."""
sticker: JsonValue = {"id": "a" * 64, "emoji": "", "frames": 1, field: value}
with pytest.raises((ValueError, UnicodeError, ImportFailureError)):
_ = Catalog.model_validate_json(
encoded(
{
"version": 1,
"packs": [{"id": "Pack", "title": "Title", "stickers": [sticker]}],
}
)
)
@pytest.mark.parametrize("version", [True, 0, 2, "1"])
def test_catalog_version_requires_integer_one(*, version: bool | int | str) -> None:
"""Reject booleans, other versions and string coercion for catalog version."""
with pytest.raises(ValidationError):
_ = Catalog.model_validate_json(encoded({"version": version, "packs": []}))