1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
"""Tests for the client."""
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING, Any
import aiohttp
from aiohttp import ClientError
from aiohttp.hdrs import METH_GET
from aioresponses import CallbackResult, aioresponses
import pytest
from nyt_games import NYTGamesClient, NYTGamesConnectionError, NYTGamesError
from nyt_games.exceptions import NYTGamesAuthenticationError
from tests import load_fixture
from tests.const import HEADERS, MOCK_URL
if TYPE_CHECKING:
from syrupy import SnapshotAssertion
async def test_putting_in_own_session(
responses: aioresponses,
) -> None:
"""Test putting in own session."""
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
status=200,
body=load_fixture("latest.json"),
)
async with aiohttp.ClientSession() as session:
client = NYTGamesClient(session=session, token="abc")
await client.get_latest_stats()
assert client.session is not None
assert not client.session.closed
await client.close()
assert not client.session.closed
async def test_creating_own_session(
responses: aioresponses,
) -> None:
"""Test creating own session."""
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
status=200,
body=load_fixture("latest.json"),
)
client = NYTGamesClient(token="abc")
await client.get_latest_stats()
assert client.session is not None
assert not client.session.closed
await client.close()
assert client.session.closed
async def test_unexpected_server_response(
responses: aioresponses,
client: NYTGamesClient,
) -> None:
"""Test handling unexpected response."""
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
status=404,
headers={"Content-Type": "plain/text"},
body="Yes",
)
with pytest.raises(NYTGamesError):
await client.get_latest_stats()
async def test_unauthorized(
responses: aioresponses,
client: NYTGamesClient,
) -> None:
"""Test handling unauthorized response."""
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
status=403,
body='{"error": "forbidden"}',
)
with pytest.raises(NYTGamesAuthenticationError):
await client.get_latest_stats()
async def test_timeout(
responses: aioresponses,
) -> None:
"""Test request timeout."""
# Faking a timeout by sleeping
async def response_handler(_: str, **_kwargs: Any) -> CallbackResult:
"""Response handler for this test."""
await asyncio.sleep(2)
return CallbackResult(body="Goodmorning!")
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
callback=response_handler,
)
async with NYTGamesClient(request_timeout=1, token="abc") as client:
with pytest.raises(NYTGamesConnectionError):
await client.get_latest_stats()
async def test_client_error(
client: NYTGamesClient,
responses: aioresponses,
) -> None:
"""Test client error."""
async def response_handler(_: str, **_kwargs: Any) -> CallbackResult:
"""Response handler for this test."""
raise ClientError
responses.get(
f"{MOCK_URL}/measures/current",
callback=response_handler,
)
with pytest.raises(NYTGamesConnectionError):
await client.get_latest_stats()
@pytest.mark.parametrize(
"fixture", ["latest.json", "new_account.json", "no_spelling_bee.json"]
)
async def test_get_latest(
responses: aioresponses,
client: NYTGamesClient,
snapshot: SnapshotAssertion,
fixture: str,
) -> None:
"""Test status call."""
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
status=200,
body=load_fixture(fixture),
)
assert await client.get_latest_stats() == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
METH_GET,
headers=HEADERS,
params=None,
)
async def test_get_connections(
responses: aioresponses,
client: NYTGamesClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving connections."""
responses.get(
f"{MOCK_URL}/svc/games/state/connections/latests?puzzle_ids=0",
status=200,
body=load_fixture("connections.json"),
)
assert await client.get_connections() == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/svc/games/state/connections/latests",
METH_GET,
headers=HEADERS,
params={"puzzle_ids": "0"},
)
@pytest.mark.parametrize(
"fixture", ["new_account_connections.json", "newer_account_connections.json"]
)
async def test_get_connections_new_player(
responses: aioresponses, client: NYTGamesClient, fixture: str
) -> None:
"""Test retrieving connections."""
responses.get(
f"{MOCK_URL}/svc/games/state/connections/latests?puzzle_ids=0",
status=200,
body=load_fixture(fixture),
)
assert await client.get_connections() is None
responses.assert_called_once_with(
f"{MOCK_URL}/svc/games/state/connections/latests",
METH_GET,
headers=HEADERS,
params={"puzzle_ids": "0"},
)
async def test_get_user_id(
responses: aioresponses,
client: NYTGamesClient,
) -> None:
"""Test retrieving user_id."""
responses.get(
f"{MOCK_URL}/svc/games/state/wordleV2/latests",
status=200,
body=load_fixture("latest.json"),
)
assert await client.get_user_id() == 218886794
@pytest.mark.parametrize(
"fixture",
[
"crossword_stats_existing_player.json",
"crossword_stats_new_player.json",
],
)
async def test_crossword_stats(
responses: aioresponses,
client: NYTGamesClient,
snapshot: SnapshotAssertion,
fixture: str,
) -> None:
"""Test fetching crossword stats."""
fixture_text = load_fixture(fixture)
url = f"{MOCK_URL}/svc/crosswords/v3/10781499/stats-and-streaks.json"
responses.get(
f"{url}?date_start=1988-01-01&start_on_monday=true",
body=fixture_text,
)
stats = await client.get_crossword_stats()
assert stats == snapshot
|