mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-07-25 22:31:41 +00:00
120 lines
3.8 KiB
Python
120 lines
3.8 KiB
Python
"""Unit tests for FragmentClient — initialization, validation, and cookie parsing."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from pyfragment import FragmentClient
|
|
from pyfragment.types import ConfigurationError, CookieError
|
|
from tests.shared import VALID_API_KEY, VALID_COOKIES, VALID_SEED
|
|
|
|
# Client init tests
|
|
|
|
|
|
def test_valid_init() -> None:
|
|
client = FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=VALID_COOKIES)
|
|
assert client.seed == VALID_SEED.strip()
|
|
assert client.api_key == VALID_API_KEY
|
|
assert client.wallet_version == "V5R1"
|
|
|
|
|
|
# Wallet version tests
|
|
|
|
|
|
def test_wallet_version_v4r2() -> None:
|
|
client = FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=VALID_COOKIES, wallet_version="V4R2")
|
|
assert client.wallet_version == "V4R2"
|
|
|
|
|
|
def test_wallet_version_is_case_insensitive() -> None:
|
|
client = FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=VALID_COOKIES, wallet_version="v5r1")
|
|
assert client.wallet_version == "V5R1"
|
|
|
|
|
|
def test_unsupported_wallet_version_raises() -> None:
|
|
with pytest.raises(ConfigurationError):
|
|
FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=VALID_COOKIES, wallet_version="V3R2")
|
|
|
|
|
|
# Seed and mnemonic validation tests
|
|
|
|
|
|
def test_missing_seed_raises() -> None:
|
|
with pytest.raises(ConfigurationError):
|
|
FragmentClient(seed="", api_key=VALID_API_KEY, cookies=VALID_COOKIES)
|
|
|
|
|
|
def test_whitespace_only_seed_raises() -> None:
|
|
with pytest.raises(ConfigurationError):
|
|
FragmentClient(seed=" ", api_key=VALID_API_KEY, cookies=VALID_COOKIES)
|
|
|
|
|
|
def test_invalid_mnemonic_length_raises() -> None:
|
|
bad_seed = " ".join(["word"] * 23)
|
|
with pytest.raises(ConfigurationError):
|
|
FragmentClient(seed=bad_seed, api_key=VALID_API_KEY, cookies=VALID_COOKIES)
|
|
|
|
|
|
def test_valid_mnemonic_lengths() -> None:
|
|
for length in (12, 18, 24):
|
|
seed = " ".join(["abandon"] * (length - 1) + ["about"])
|
|
client = FragmentClient(seed=seed, api_key=VALID_API_KEY, cookies=VALID_COOKIES)
|
|
assert len(client.seed.split()) == length
|
|
|
|
|
|
# API key validation tests
|
|
|
|
|
|
def test_missing_api_key_raises() -> None:
|
|
with pytest.raises(ConfigurationError):
|
|
FragmentClient(seed=VALID_SEED, api_key="", cookies=VALID_COOKIES)
|
|
|
|
|
|
def test_short_api_key_raises() -> None:
|
|
with pytest.raises(ConfigurationError):
|
|
FragmentClient(seed=VALID_SEED, api_key="A" * 42, cookies=VALID_COOKIES)
|
|
|
|
|
|
# Cookie validation tests
|
|
|
|
|
|
def test_cookies_as_json_string() -> None:
|
|
client = FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=json.dumps(VALID_COOKIES))
|
|
assert client.cookies == VALID_COOKIES
|
|
|
|
|
|
def test_invalid_cookies_json_raises() -> None:
|
|
with pytest.raises(CookieError):
|
|
FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies="{not valid json}")
|
|
|
|
|
|
def test_missing_cookie_key_raises() -> None:
|
|
with pytest.raises(CookieError):
|
|
FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies={"stel_ssid": "x"})
|
|
|
|
|
|
def test_empty_cookie_value_raises() -> None:
|
|
bad = {**VALID_COOKIES, "stel_token": ""}
|
|
with pytest.raises(CookieError):
|
|
FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=bad)
|
|
|
|
|
|
def test_whitespace_cookie_value_raises() -> None:
|
|
bad = {**VALID_COOKIES, "stel_ton_token": " "}
|
|
with pytest.raises(CookieError):
|
|
FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=bad)
|
|
|
|
|
|
def test_repr() -> None:
|
|
client = FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=VALID_COOKIES)
|
|
r = repr(client)
|
|
assert "FragmentClient" in r
|
|
assert "V5R1" in r
|
|
assert "4 keys" in r
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_context_manager() -> None:
|
|
async with FragmentClient(seed=VALID_SEED, api_key=VALID_API_KEY, cookies=VALID_COOKIES) as client:
|
|
assert isinstance(client, FragmentClient)
|