-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rpc.py
More file actions
253 lines (166 loc) · 8.24 KB
/
Copy pathtest_rpc.py
File metadata and controls
253 lines (166 loc) · 8.24 KB
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
from __future__ import annotations
import httpx
import pytest
from tests.conftest import FakeResponse, FakeSession
from vesper.errors import CiderRpcError
from vesper.rpc import CiderRpcClient
def _flaky_session(fail: int, *, status: int = 503, exc: Exception | None = None) -> FakeSession:
"""Return a FakeSession whose first ``fail`` GET attempts fail transiently.
``exc`` (a connection error) takes precedence over a 5xx ``status``. The
final attempt always returns a 200 with ``{"ok": True}``.
"""
calls = {"count": 0}
def responder(method: str, path: str, headers, body) -> FakeResponse:
calls["count"] += 1
if calls["count"] <= fail:
if exc is not None:
raise exc
return FakeResponse(status, {"status": "error"})
return FakeResponse(200, {"ok": True})
return FakeSession(responder)
def test_get_retries_connection_error_then_succeeds(settings) -> None:
# The first attempt raises a connection error; the retry succeeds.
session = _flaky_session(1, exc=httpx.ConnectError("offline"))
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
result = client.playback_get("/is-playing")
assert result == {"ok": True}
assert len(session.requests) == 2 # one failure + one success
def test_get_retries_5xx_then_succeeds(settings) -> None:
session = _flaky_session(2, status=503)
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
result = client.playback_get("/now-playing")
assert result == {"ok": True}
assert len(session.requests) == 3 # two failures + one success
def test_get_exhausts_retries_and_raises(settings) -> None:
session = _flaky_session(3, status=503) # more failures than retry_count=2
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
with pytest.raises(CiderRpcError) as exc_info:
client.playback_get("/is-playing")
# default cider_retry_count is 2 -> 3 total attempts
assert len(session.requests) == 3
assert exc_info.value.status_code == 503
def test_get_uses_exponential_backoff(settings) -> None:
delays: list[float] = []
session = _flaky_session(2, status=503)
client = CiderRpcClient(settings, session=session, sleep=delays.append)
client.playback_get("/is-playing")
# 2 retries -> delays for attempts 0 and 1: 0.1 and 0.2
assert delays == [0.1, 0.2]
def test_post_does_not_retry_connection_error(settings) -> None:
calls = {"count": 0}
def responder(method, path, headers, body) -> FakeResponse:
calls["count"] += 1
raise httpx.ConnectError("offline")
session = FakeSession(responder)
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
with pytest.raises(CiderRpcError):
client.playback_post("/play", {"track": "x"})
assert calls["count"] == 1 # no retries for POST
def test_post_does_not_retry_5xx(settings) -> None:
calls = {"count": 0}
def responder(method, path, headers, body) -> FakeResponse:
calls["count"] += 1
return FakeResponse(503, {"status": "error"})
session = FakeSession(responder)
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
with pytest.raises(CiderRpcError) as exc_info:
client.playback_post("/play", {"track": "x"})
assert calls["count"] == 1 # POST is never retried, even on 5xx
assert exc_info.value.status_code == 503
@pytest.mark.parametrize("status_code", [400, 401, 403, 409, 422])
def test_get_does_not_retry_client_errors(settings, status_code: int) -> None:
calls = {"count": 0}
def responder(method, path, headers, body) -> FakeResponse:
calls["count"] += 1
return FakeResponse(status_code, {"detail": "bad request"})
session = FakeSession(responder)
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
with pytest.raises(CiderRpcError) as exc_info:
client.playback_get("/is-playing")
assert calls["count"] == 1 # 4xx is definitive, never retried
assert exc_info.value.status_code == status_code
def test_get_204_returns_none_without_retry(settings) -> None:
calls = {"count": 0}
def responder(method, path, headers, body) -> FakeResponse:
calls["count"] += 1
return FakeResponse(204)
session = FakeSession(responder)
client = CiderRpcClient(settings, session=session, sleep=lambda _: None)
assert client.playback_get("/is-playing") is None
assert calls["count"] == 1
def test_failure_callback_reports_once_after_retries_exhausted(settings) -> None:
reports: list[dict] = []
session = _flaky_session(3, status=503) # exceeds retry_count=2
client = CiderRpcClient(
settings,
session=session,
failure_callback=reports.append,
sleep=lambda _: None,
)
with pytest.raises(CiderRpcError):
client.playback_get("/is-playing")
# The callback fires exactly once (on the final failure), not per attempt.
assert len(reports) == 1
assert reports[0]["status_code"] == 503
def test_cider_retry_count_zero_disables_retries(settings) -> None:
from vesper.config import Settings as _Settings
values = {k: getattr(settings, k) for k in type(settings).model_fields if k != "config_path"}
values["cider_retry_count"] = 0
no_retry_settings = _Settings(**values)
session = _flaky_session(1, status=503)
client = CiderRpcClient(no_retry_settings, session=session, sleep=lambda _: None)
with pytest.raises(CiderRpcError):
client.playback_get("/is-playing")
assert len(session.requests) == 1 # no retry when count is 0
def test_playback_get_uses_dual_token_headers(rpc_client) -> None:
client, session = rpc_client
client.playback_get("/is-playing")
assert session.requests[0]["path"] == "/api/v1/playback/is-playing"
assert session.requests[0]["headers"]["apptoken"] == "secret-token"
assert session.requests[0]["headers"]["apitoken"] == "secret-token"
def test_run_amapi_v3_supports_method_and_body(rpc_client) -> None:
client, session = rpc_client
client.run_amapi_v3("/v1/me/library/playlists", method="POST", body={"attributes": {"name": "Mix"}})
assert session.requests[0]["path"] == "/api/v1/amapi/run-v3"
assert session.requests[0]["json"] == {
"path": "/v1/me/library/playlists",
"method": "POST",
"body": {"attributes": {"name": "Mix"}},
}
def test_search_catalog_passes_through_valid_storefront(rpc_client) -> None:
client, session = rpc_client
client.search_catalog("some query", storefront="jp")
assert session.requests[0]["json"]["path"].startswith("/v1/catalog/jp/search")
def test_search_catalog_rejects_unsafe_storefront(rpc_client) -> None:
client, session = rpc_client
client.search_catalog("some query", storefront="../../admin")
path = session.requests[0]["json"]["path"]
assert path.startswith("/v1/catalog/us/search")
assert "../" not in path
assert "admin" not in path
def test_catalog_resource_search_sanitizes_storefront(rpc_client) -> None:
from vesper.catalog import catalog_resource_search
client, session = rpc_client
catalog_resource_search(client, "query", resource_type="songs", storefront="us/../../v1/me/library")
path = session.requests[0]["json"]["path"]
assert path.startswith("/v1/catalog/us/search")
assert "../" not in path
assert "library" not in path
def test_catalog_relationship_tracks_sanitizes_storefront(rpc_client) -> None:
from vesper.catalog import catalog_relationship_tracks
client, session = rpc_client
catalog_relationship_tracks(
client, "/albums/album-1/tracks", result_limit=1, page_limit=1, storefront="us/../../v1/me/library"
)
path = session.requests[0]["json"]["path"]
assert path.startswith("/v1/catalog/us/albums/album-1/tracks")
assert "../" not in path
assert "library" not in path
def test_load_genre_map_sanitizes_storefront(rpc_client) -> None:
from vesper.catalog import load_genre_map
client, session = rpc_client
load_genre_map(client, {}, storefront="us/../../v1/me/library")
path = session.requests[0]["json"]["path"]
assert path == "/v1/catalog/us/genres"
assert "../" not in path
assert "library" not in path