| 1 |
"""Tests for the 'plexupdate' plugin.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from beets.exceptions import UserError |
| 12 |
from beetsplug._utils.requests import SingletonMeta |
| 13 |
from beetsplug.plexupdate import ( |
| 14 |
PLEX_API, |
| 15 |
PlexAPI, |
| 16 |
PlexSession, |
| 17 |
UnauthorizedPlexError, |
| 18 |
) |
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
from requests_mock import Mocker |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(autouse=True) |
| 27 |
def reset_plex_session(): |
| 28 |
"""Drop the PlexSession singleton so each test gets a fresh session.""" |
| 29 |
yield |
| 30 |
SingletonMeta._instances.pop(PlexSession, None) |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def api(tmp_path: Path) -> PlexAPI: |
| 35 |
"""PlexAPI for the local test server with a per-test token file.""" |
| 36 |
return PlexAPI( |
| 37 |
token_path=tmp_path / "plex_token.json", |
| 38 |
host="localhost", |
| 39 |
port=32400, |
| 40 |
library_name="Music", |
| 41 |
secure=False, |
| 42 |
verify=True, |
| 43 |
token_override="", |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
@pytest.fixture |
| 48 |
def session(tmp_path: Path) -> PlexSession: |
| 49 |
"""PlexSession bound to a token file in a per-test directory.""" |
| 50 |
return PlexSession(token_path=tmp_path / "plex_token.json") |
| 51 |
|
| 52 |
|
| 53 |
@pytest.fixture |
| 54 |
def plex_tv(requests_mock: Mocker) -> Mocker: |
| 55 |
"""Mock the plex.tv PIN flow: issuing a pin and still-pending polls.""" |
| 56 |
requests_mock.post( |
| 57 |
f"{PLEX_API}/pins", json={"id": 1, "code": "ABCD", "expiresIn": 1800} |
| 58 |
) |
| 59 |
requests_mock.get(f"{PLEX_API}/pins/1", json={"id": 1, "code": "ABCD"}) |
| 60 |
return requests_mock |
| 61 |
|
| 62 |
|
| 63 |
class TestUpdateLibrary: |
| 64 |
def test_get_named_music_section(self, requests_mock, api): |
| 65 |
requests_mock.get( |
| 66 |
"http://localhost:32400/library/sections", |
| 67 |
json={ |
| 68 |
"MediaContainer": { |
| 69 |
"Directory": [ |
| 70 |
{"key": "3", "title": "Movies"}, |
| 71 |
{"key": "2", "title": "My Music Library"}, |
| 72 |
] |
| 73 |
} |
| 74 |
}, |
| 75 |
) |
| 76 |
api.library_name = "My Music Library" |
| 77 |
|
| 78 |
assert api.get_music_section() == "2" |
| 79 |
|
| 80 |
def test_update_library(self, requests_mock, api): |
| 81 |
requests_mock.get( |
| 82 |
"http://localhost:32400/library/sections", |
| 83 |
json={ |
| 84 |
"MediaContainer": { |
| 85 |
"Directory": [{"key": "2", "title": "Music"}] |
| 86 |
} |
| 87 |
}, |
| 88 |
) |
| 89 |
requests_mock.get("http://localhost:32400/library/sections/2/refresh") |
| 90 |
|
| 91 |
assert api.update_library().status_code == 200 |
| 92 |
|
| 93 |
def test_update_library_unauthorized(self, requests_mock, api): |
| 94 |
"""A rejected token surfaces as an UnauthorizedPlexError.""" |
| 95 |
requests_mock.get( |
| 96 |
"http://localhost:32400/library/sections", |
| 97 |
json={ |
| 98 |
"MediaContainer": { |
| 99 |
"Directory": [{"key": "2", "title": "Music"}] |
| 100 |
} |
| 101 |
}, |
| 102 |
) |
| 103 |
requests_mock.get( |
| 104 |
"http://localhost:32400/library/sections/2/refresh", status_code=401 |
| 105 |
) |
| 106 |
|
| 107 |
with pytest.raises(UnauthorizedPlexError, match="reauthenticate"): |
| 108 |
api.update_library() |
| 109 |
|
| 110 |
def test_update_library_missing_section(self, requests_mock, api): |
| 111 |
# The server has no section with the configured name. |
| 112 |
requests_mock.get( |
| 113 |
"http://localhost:32400/library/sections", |
| 114 |
json={ |
| 115 |
"MediaContainer": { |
| 116 |
"Directory": [{"key": "3", "title": "Other Library"}] |
| 117 |
} |
| 118 |
}, |
| 119 |
) |
| 120 |
|
| 121 |
with pytest.raises(UserError, match="No music library named"): |
| 122 |
api.update_library() |
| 123 |
|
| 124 |
|
| 125 |
class TestPlexSession: |
| 126 |
def test_session_saves_token(self, session): |
| 127 |
session.save_token({"X-Plex-Token": "TOKEN123"}) |
| 128 |
|
| 129 |
assert session.token == "TOKEN123" |
| 130 |
assert session.load_token() == { |
| 131 |
"X-Plex-Token": "TOKEN123", |
| 132 |
"client_identifier": session.client_id, |
| 133 |
} |
| 134 |
|
| 135 |
def test_wait_for_login_timeout(self, plex_tv, session): |
| 136 |
# The pin never gets an auth token. |
| 137 |
with mock.patch("beetsplug.plexupdate.time.sleep"): |
| 138 |
assert session.wait_for_login(1, timeout=1) is None |
| 139 |
|
| 140 |
def test_request_attaches_token_to_local_server( |
| 141 |
self, requests_mock, session |
| 142 |
): |
| 143 |
"""The stored token is sent to the local server.""" |
| 144 |
requests_mock.get("http://localhost:32400/library/sections", json={}) |
| 145 |
session.save_token({"X-Plex-Token": "TOKEN123"}) |
| 146 |
|
| 147 |
session.get("http://localhost:32400/library/sections") |
| 148 |
|
| 149 |
assert ( |
| 150 |
requests_mock.request_history[0].headers["X-Plex-Token"] |
| 151 |
== "TOKEN123" |
| 152 |
) |
| 153 |
|
| 154 |
def test_request_prefers_configured_token(self, requests_mock, tmp_path): |
| 155 |
"""The configured token wins over the stored plex.tv token.""" |
| 156 |
session = PlexSession( |
| 157 |
token_path=tmp_path / "plex_token.json", |
| 158 |
token_override="CONFIG_TOKEN", |
| 159 |
) |
| 160 |
session.save_token({"X-Plex-Token": "TOKEN123"}) |
| 161 |
requests_mock.get("http://localhost:32400/library/sections", json={}) |
| 162 |
|
| 163 |
session.get("http://localhost:32400/library/sections") |
| 164 |
|
| 165 |
assert ( |
| 166 |
requests_mock.request_history[0].headers["X-Plex-Token"] |
| 167 |
== "CONFIG_TOKEN" |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
class TestUIAuthenticateFlow: |
| 172 |
def test_ui_authenticate_flow(self, requests_mock, plex_tv, api): |
| 173 |
# The first poll is still pending, the second grants access. |
| 174 |
requests_mock.get( |
| 175 |
f"{PLEX_API}/pins/1", |
| 176 |
response_list=[ |
| 177 |
{"json": {"id": 1, "code": "ABCD"}}, |
| 178 |
{"json": {"id": 1, "code": "ABCD", "authToken": "TOKEN123"}}, |
| 179 |
], |
| 180 |
) |
| 181 |
|
| 182 |
with ( |
| 183 |
mock.patch("beetsplug.plexupdate.time.sleep"), |
| 184 |
mock.patch("beetsplug.plexupdate.webbrowser.open") as open_mock, |
| 185 |
): |
| 186 |
api.ui_authenticate_flow() |
| 187 |
|
| 188 |
open_mock.assert_called_once() |
| 189 |
|
| 190 |
# The pin request carries the identifying headers. |
| 191 |
request = requests_mock.request_history[0] |
| 192 |
assert ( |
| 193 |
request.headers["X-Plex-Client-Identifier"] == api.session.client_id |
| 194 |
) |
| 195 |
|
| 196 |
# The access token is persisted for later runs. |
| 197 |
assert api.session.token == "TOKEN123" |
| 198 |
assert json.loads( |
| 199 |
api.session.token_path.read_text(encoding="utf-8") |
| 200 |
) == { |
| 201 |
"X-Plex-Token": "TOKEN123", |
| 202 |
"client_identifier": api.session.client_id, |
| 203 |
} |
| 204 |
|
| 205 |
def test_ui_authenticate_flow_timeout(self, plex_tv, api): |
| 206 |
with ( |
| 207 |
mock.patch("beetsplug.plexupdate.time.sleep"), |
| 208 |
mock.patch("beetsplug.plexupdate.webbrowser.open"), |
| 209 |
mock.patch.object(api.session, "wait_for_login", return_value=None), |
| 210 |
): |
| 211 |
with pytest.raises(UserError): |
| 212 |
api.ui_authenticate_flow() |
| 213 |
|
| 214 |
# No token is stored when the login is not completed. |
| 215 |
assert api.session.token is None |
| 216 |
|
| 217 |
def test_ui_authenticate_flow_network_error(self, requests_mock, api): |
| 218 |
# A failing plex.tv request surfaces as a UserError instead of a |
| 219 |
# raw requests exception. |
| 220 |
requests_mock.post(f"{PLEX_API}/pins", status_code=500) |
| 221 |
|
| 222 |
with pytest.raises(UserError, match="Plex login flow failed"): |
| 223 |
api.ui_authenticate_flow() |