SIMNet/tests/test_auth_client.py

84 lines
3.7 KiB
Python

from typing import TYPE_CHECKING
import pytest
import pytest_asyncio
from simnet.client.components.auth import AuthClient
from simnet.utils.enums import Region
from simnet.utils.player import recognize_genshin_server, recognize_genshin_game_biz
if TYPE_CHECKING:
from simnet.client.cookies import Cookies
@pytest_asyncio.fixture
async def auth_client(account_id: int, region: "Region", cookies: "Cookies"):
async with AuthClient(
cookies=cookies,
account_id=account_id,
region=region,
) as client_instance:
yield client_instance
@pytest.mark.asyncio
class TestAuthClient:
@staticmethod
async def test_get_hk4e_token_by_cookie_token(auth_client: "AuthClient", genshin_player_id: int):
if genshin_player_id is None:
pytest.skip("Test case test_get_hk4e_token_by_cookie_token skipped: No genshin player id set.")
game_biz = recognize_genshin_game_biz(genshin_player_id)
region = recognize_genshin_server(genshin_player_id)
await auth_client.get_hk4e_token_by_cookie_token(game_biz, region, player_id=genshin_player_id)
hk4e_token = auth_client.client.cookies.get("e_hk4e_token")
assert hk4e_token is not None
@staticmethod
async def test_get_stoken_by_login_ticket(auth_client: "AuthClient", login_ticket: str, account_id: int):
if auth_client.region != Region.CHINESE:
pytest.skip(
"Test case test_get_stoken_by_login_ticket skipped:"
"This method is only available for the Chinese region."
)
if login_ticket is None:
pytest.skip("Test case test_get_stoken_by_login_ticket skipped: Parameter login_ticket is None")
stoken = await auth_client.get_stoken_by_login_ticket(login_ticket, account_id)
assert stoken is not None
@staticmethod
async def test_get_cookie_token_by_stoken(auth_client: "AuthClient", stoken: str, account_id: int):
if auth_client.region != Region.CHINESE:
pytest.skip(
"Test case test_get_cookie_token_by_stoken skipped:"
"This method is only available for the Chinese region."
)
if stoken is None:
pytest.skip("Test case test_get_cookie_token_by_stoken skipped: Parameter stoken is None")
cookie_token = await auth_client.get_cookie_token_by_stoken(stoken, account_id)
assert cookie_token is not None
@staticmethod
async def test_get_ltoken_by_stoken(auth_client: "AuthClient", stoken: str, account_id: int):
if auth_client.region != Region.CHINESE:
pytest.skip(
"Test case test_get_ltoken_by_stoken skipped:This method is only available for the Chinese region."
)
if stoken is None:
pytest.skip("Test case test_get_ltoken_by_stoken skipped: Parameter stoken is None")
ltoken = await auth_client.get_ltoken_by_stoken(stoken, account_id)
assert ltoken is not None
@staticmethod
async def test_get_authkey_by_stoken(auth_client: "AuthClient", stoken: str, genshin_player_id: int):
if auth_client.region != Region.CHINESE:
pytest.skip(
"Test case test_get_authkey_by_stoken skipped:This method is only available for the Chinese region."
)
if stoken is None:
pytest.skip("Test case test_get_authkey_by_stoken skipped: Parameter stoken is None")
auth_client.player_id = genshin_player_id
game_biz = recognize_genshin_game_biz(genshin_player_id)
region = recognize_genshin_server(genshin_player_id)
authkey = await auth_client.get_authkey_by_stoken("webview_gacha", game_biz, region)
assert authkey is not None