From f57f8271c3f9cb5d8faf1ad5cfa813cfd3c57369 Mon Sep 17 00:00:00 2001 From: bohd4nx Date: Thu, 5 Mar 2026 04:18:55 +0200 Subject: [PATCH] test: add pytest test files (were missing from previous commit) --- tests/001_test_decode.py | 34 ++++++++++++++++++++++++++++++++++ tests/002_test_hash.py | 16 ++++++++++++++++ tests/__init__.py | 0 tests/conftest.py | 13 +++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 tests/001_test_decode.py create mode 100644 tests/002_test_hash.py create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py diff --git a/tests/001_test_decode.py b/tests/001_test_decode.py new file mode 100644 index 0000000..58acd10 --- /dev/null +++ b/tests/001_test_decode.py @@ -0,0 +1,34 @@ +"""Tests for clean_decode() — BOC-encoded Fragment payloads decode to +human-readable UTF-8 with the Telegram label and Ref# intact.""" +import re + +import pytest + +from app.utils.decoder import clean_decode + +PAYLOADS = [ + pytest.param( + "te6ccgEBAgEALwABTgAAAAAxMDAwMDAwIFRlbGVncmFtIFN0YXJzIAoKUmVmI1RQb01wegEABkM3ZQ", + id="stars", + ), + pytest.param( + "te6ccgEBAgEANAABTgAAAABUZWxlZ3JhbSBQcmVtaXVtIGZvciAxIHllYXIgCgpSZWYjcgEAEE9OQnM2cmNt", + id="premium", + ), + pytest.param( + "te6ccgEBAgEAMAABTgAAAABUZWxlZ3JhbSBhY2NvdW50IHRvcCB1cCAKClJlZiNrMXpDRQEACFkxd3g", + id="topup", + ), +] + + +@pytest.mark.parametrize("payload", PAYLOADS) +def test_payload(payload: str) -> None: + result = clean_decode(payload) + assert "Telegram" in result + assert re.search(r"Ref#[A-Za-z0-9]+", result), f"no Ref# in {result!r}" + assert all(ord(c) <= 127 for c in result), f"non-ASCII chars in {result!r}" + + +def test_empty_input_returns_string() -> None: + assert isinstance(clean_decode(""), str) diff --git a/tests/002_test_hash.py b/tests/002_test_hash.py new file mode 100644 index 0000000..e7a3a46 --- /dev/null +++ b/tests/002_test_hash.py @@ -0,0 +1,16 @@ +"""Tests for get_fragment_hash() — fetches a valid lowercase hex hash +from the fragment.com/stars/buy page source.""" +import re + +import pytest + +from app.core.constants import BASE_HEADERS, STARS_PAGE +from app.utils.hash import get_fragment_hash + + +@pytest.mark.asyncio +async def test_hash_is_valid_hex(cookies: dict) -> None: + result = await get_fragment_hash(cookies, BASE_HEADERS, STARS_PAGE) + assert isinstance(result, str) + assert len(result) >= 10, f"hash too short: {result!r}" + assert re.fullmatch(r"[a-f0-9]+", result), f"not a hex string: {result!r}" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..5fd734c --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,13 @@ +import pytest + +from app.core.cookies import load_cookies +from app.core.exceptions import CookiesError + + +@pytest.fixture +def cookies(): + """Load Fragment cookies, skip the test if they are unavailable.""" + try: + return load_cookies() + except CookiesError as exc: + pytest.skip(f"Cookies unavailable — {exc}")