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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
"""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_DELETE, METH_GET, METH_POST, METH_PUT
from aioresponses import CallbackResult, aioresponses
import pytest
from python_overseerr import MediaType, OverseerrClient
from python_overseerr.exceptions import (
OverseerrAuthenticationError,
OverseerrConnectionError,
OverseerrError,
)
from python_overseerr.models import (
IssueStatus,
IssueType,
NotificationType,
RequestFilterStatus,
RequestSortStatus,
)
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}/request/count",
status=200,
body=load_fixture("request_count.json"),
)
async with aiohttp.ClientSession() as session:
overseerr = OverseerrClient("192.168.0.30", 443, "abc", session=session)
await overseerr.get_request_count()
assert overseerr.session is not None
assert not overseerr.session.closed
await overseerr.close()
assert not overseerr.session.closed
async def test_creating_own_session(
responses: aioresponses,
) -> None:
"""Test creating own session."""
responses.get(
f"{MOCK_URL}/request/count",
status=200,
body=load_fixture("request_count.json"),
)
overseerr = OverseerrClient("192.168.0.30", 443, "abc")
await overseerr.get_request_count()
assert overseerr.session is not None
assert not overseerr.session.closed
await overseerr.close()
assert overseerr.session.closed
async def test_unexpected_server_response(
responses: aioresponses,
client: OverseerrClient,
) -> None:
"""Test handling unexpected response."""
responses.get(
f"{MOCK_URL}/request/count",
status=404,
headers={"Content-Type": "plain/text"},
body="Yes",
)
with pytest.raises(OverseerrError):
await client.get_request_count()
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}/request/count",
callback=response_handler,
)
async with OverseerrClient(
"192.168.0.30",
443,
"abc",
request_timeout=1,
) as overseerr:
with pytest.raises(OverseerrConnectionError):
await overseerr.get_request_count()
async def test_client_error(
client: OverseerrClient,
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}/request/count",
callback=response_handler,
)
with pytest.raises(OverseerrConnectionError):
await client.get_request_count()
async def test_authentication_error(
client: OverseerrClient,
responses: aioresponses,
) -> None:
"""Test authentication error."""
responses.get(
f"{MOCK_URL}/request/count",
status=403,
body=load_fixture("no_access.json"),
)
with pytest.raises(OverseerrAuthenticationError):
await client.get_request_count()
@pytest.mark.parametrize(
("endpoint", "fixture", "method"),
[
("request/count", "request_count.json", "get_request_count"),
("status", "status.json", "get_status"),
(
"settings/notifications/webhook",
"webhook_config.json",
"get_webhook_notification_config",
),
(
"discover/watchlist",
"watchlist.json",
"get_watchlist",
),
("issue/count", "issue_count.json", "get_issue_count"),
],
ids=[
"request_count",
"status",
"webhook_config",
"watchlist",
"issue_count",
],
)
async def test_data_retrieval(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
endpoint: str,
fixture: str,
method: str,
) -> None:
"""Test data retrieval."""
responses.get(
f"{MOCK_URL}/{endpoint}",
status=200,
body=load_fixture(fixture),
)
assert await getattr(client, method)() == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/{endpoint}",
METH_GET,
headers=HEADERS,
params=None,
json=None,
)
@pytest.mark.parametrize(
"fixtures",
[
"search_1.json",
"search_2.json",
],
)
async def test_search(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
fixtures: str,
) -> None:
"""Test searching for media."""
responses.get(
f"{MOCK_URL}/search?query=frosty",
status=200,
body=load_fixture(fixtures),
)
assert await client.search("frosty") == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/search",
METH_GET,
headers=HEADERS,
params={"query": "frosty"},
json=None,
)
async def test_setting_webhook_configuration(
responses: aioresponses,
client: OverseerrClient,
) -> None:
"""Test setting webhook configuration."""
responses.post(
f"{MOCK_URL}/settings/notifications/webhook",
status=200,
)
await client.set_webhook_notification_config(
enabled=True,
types=NotificationType.REQUEST_APPROVED,
webhook_url="http://localhost",
json_payload="{}",
)
responses.assert_called_once_with(
f"{MOCK_URL}/settings/notifications/webhook",
METH_POST,
headers=HEADERS,
params=None,
json={
"enabled": True,
"types": 4,
"options": {
"webhookUrl": "http://localhost",
"jsonPayload": "{}",
},
},
)
async def test_webhook_config_test(
responses: aioresponses,
client: OverseerrClient,
) -> None:
"""Test setting webhook configuration."""
responses.post(
f"{MOCK_URL}/settings/notifications/webhook/test",
status=204,
)
assert (
await client.test_webhook_notification_config(
webhook_url="http://localhost",
json_payload="{}",
)
is True
)
responses.assert_called_once_with(
f"{MOCK_URL}/settings/notifications/webhook/test",
METH_POST,
headers=HEADERS,
params=None,
json={
"enabled": True,
"types": 2,
"options": {
"webhookUrl": "http://localhost",
"jsonPayload": "{}",
},
},
)
async def test_failing_webhook_config_test(
responses: aioresponses,
client: OverseerrClient,
) -> None:
"""Test setting webhook configuration."""
responses.post(
f"{MOCK_URL}/settings/notifications/webhook/test",
status=500,
body='{"message": "Failed to send webhook notification."}',
)
assert (
await client.test_webhook_notification_config(
webhook_url="http://localhost",
json_payload="{}",
)
is False
)
responses.assert_called_once_with(
f"{MOCK_URL}/settings/notifications/webhook/test",
METH_POST,
headers=HEADERS,
params=None,
json={
"enabled": True,
"types": 2,
"options": {
"webhookUrl": "http://localhost",
"jsonPayload": "{}",
},
},
)
async def test_fetching_requests(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test fetching requests."""
responses.get(
f"{MOCK_URL}/request",
status=200,
body=load_fixture("request.json"),
)
assert await client.get_requests() == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/request", METH_GET, headers=HEADERS, params={}, json=None
)
@pytest.mark.parametrize(
("kwargs", "params", "query_string"),
[
({"status": RequestFilterStatus.ALL}, {"filter": "all"}, "filter=all"),
({"sort": RequestSortStatus.ADDED}, {"sort": "added"}, "sort=added"),
({"requested_by": 1}, {"requestedBy": 1}, "requestedBy=1"),
],
)
async def test_fetching_request_parameters(
responses: aioresponses,
client: OverseerrClient,
kwargs: dict[str, Any],
params: dict[str, Any],
query_string: str,
) -> None:
"""Test fetching requests with parameters."""
responses.get(
f"{MOCK_URL}/request?{query_string}",
status=200,
body=load_fixture("request.json"),
)
await client.get_requests(**kwargs)
responses.assert_called_once_with(
f"{MOCK_URL}/request", METH_GET, headers=HEADERS, params=params, json=None
)
async def test_fetching_issues(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test fetching issues."""
responses.get(
f"{MOCK_URL}/issue",
status=200,
body=load_fixture("issue.json"),
)
assert await client.get_issues() == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/issue", METH_GET, headers=HEADERS, params={}, json=None
)
@pytest.mark.parametrize(
("kwargs", "params", "query_string"),
[
({"status": RequestFilterStatus.ALL}, {"filter": "all"}, "filter=all"),
({"sort": RequestSortStatus.ADDED}, {"sort": "added"}, "sort=added"),
({"requested_by": 1}, {"requestedBy": 1}, "requestedBy=1"),
],
)
async def test_fetching_issue_parameters(
responses: aioresponses,
client: OverseerrClient,
kwargs: dict[str, Any],
params: dict[str, Any],
query_string: str,
) -> None:
"""Test fetching issues with parameters."""
responses.get(
f"{MOCK_URL}/issue?{query_string}",
status=200,
body=load_fixture("issue.json"),
)
await client.get_issues(**kwargs)
responses.assert_called_once_with(
f"{MOCK_URL}/issue", METH_GET, headers=HEADERS, params=params, json=None
)
async def test_fetching_movie_details(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test fetching movie details."""
responses.get(
f"{MOCK_URL}/movie/1156593",
status=200,
body=load_fixture("movie.json"),
)
assert await client.get_movie_details(1156593) == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/movie/1156593", METH_GET, headers=HEADERS, params=None, json=None
)
async def test_fetching_tv_details(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test fetching tv details."""
responses.get(
f"{MOCK_URL}/tv/249522",
status=200,
body=load_fixture("tv.json"),
)
assert await client.get_tv_details(249522) == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/tv/249522", METH_GET, headers=HEADERS, params=None, json=None
)
@pytest.mark.parametrize(
("args", "fixture", "json"),
[
(
(MediaType.MOVIE, 1156593),
"create_movie_request.json",
{"mediaType": "movie", "mediaId": 1156593},
),
(
(MediaType.TV, 249522, "all"),
"create_tv_request.json",
{"mediaType": "tv", "mediaId": 249522, "seasons": "all"},
),
(
(MediaType.TV, 249522, [1]),
"create_tv_request.json",
{"mediaType": "tv", "mediaId": 249522, "seasons": [1]},
),
],
)
async def test_creating_request(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
args: tuple[Any, ...],
fixture: str,
json: dict[str, Any],
) -> None:
"""Test creating a request."""
responses.post(
f"{MOCK_URL}/request",
status=201,
body=load_fixture(fixture),
)
assert await client.create_request(*args) == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/request", METH_POST, headers=HEADERS, params=None, json=json
)
async def test_fetching_single_issue(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test fetching a single issue."""
responses.get(
f"{MOCK_URL}/issue/11",
status=200,
body=load_fixture("issue_single.json"),
)
assert await client.get_issue(11) == snapshot
responses.assert_called_once_with(
f"{MOCK_URL}/issue/11", METH_GET, headers=HEADERS, params=None, json=None
)
async def test_creating_issue(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test creating an issue."""
responses.post(
f"{MOCK_URL}/issue",
status=201,
body=load_fixture("issue_created.json"),
)
assert (
await client.create_issue(
issue_type=IssueType.VIDEO,
message="Video playback not working",
media_id=1156593,
problem_season=0,
problem_episode=0,
)
== snapshot
)
responses.assert_called_once_with(
f"{MOCK_URL}/issue",
METH_POST,
headers=HEADERS,
params=None,
json={
"issueType": 1,
"message": "Video playback not working",
"mediaId": 1156593,
"problemSeason": 0,
"problemEpisode": 0,
},
)
async def test_updating_issue_status(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test updating issue status."""
responses.put(
f"{MOCK_URL}/issue/11",
status=200,
body=load_fixture("issue_updated.json"),
)
assert (
await client.update_issue(
issue_id=11,
status=IssueStatus.RESOLVED,
)
== snapshot
)
responses.assert_called_once_with(
f"{MOCK_URL}/issue/11",
METH_PUT,
headers=HEADERS,
params=None,
json={"status": 2},
)
async def test_updating_issue_with_comment(
responses: aioresponses,
client: OverseerrClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test updating issue with comment."""
responses.put(
f"{MOCK_URL}/issue/11",
status=200,
body=load_fixture("issue_updated.json"),
)
assert (
await client.update_issue(
issue_id=11,
status=IssueStatus.RESOLVED,
message="Issue has been resolved",
)
== snapshot
)
responses.assert_called_once_with(
f"{MOCK_URL}/issue/11",
METH_PUT,
headers=HEADERS,
params=None,
json={"status": 2, "message": "Issue has been resolved"},
)
async def test_deleting_issue(
responses: aioresponses,
client: OverseerrClient,
) -> None:
"""Test deleting an issue."""
responses.delete(
f"{MOCK_URL}/issue/11",
status=204,
)
await client.delete_issue(11)
responses.assert_called_once_with(
f"{MOCK_URL}/issue/11", METH_DELETE, headers=HEADERS, params=None, json=None
)
|