73 行
2.1 KiB
Python
73 行
2.1 KiB
Python
|
|
import pytest
|
||
|
|
|
||
|
|
from subscription_service import (
|
||
|
|
SubscriptionService,
|
||
|
|
build_subscription_payload,
|
||
|
|
build_torrent_payload,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class FakeResponse:
|
||
|
|
def __init__(self, data):
|
||
|
|
self.data = data
|
||
|
|
|
||
|
|
|
||
|
|
class FakeClient:
|
||
|
|
def __init__(self):
|
||
|
|
self.calls = []
|
||
|
|
|
||
|
|
async def post(self, path, *, params=None, json=None):
|
||
|
|
self.calls.append(("POST", path, json))
|
||
|
|
if path == "/api/v1/subscribe/":
|
||
|
|
return FakeResponse({"id": 8})
|
||
|
|
return FakeResponse({"download_id": "download-1"})
|
||
|
|
|
||
|
|
async def get(self, path, *, params=None):
|
||
|
|
self.calls.append(("GET", path, params))
|
||
|
|
return FakeResponse({"ok": True})
|
||
|
|
|
||
|
|
async def delete(self, path, *, params=None):
|
||
|
|
self.calls.append(("DELETE", path, params))
|
||
|
|
return FakeResponse({"ok": True})
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def service():
|
||
|
|
service = SubscriptionService({})
|
||
|
|
service._client = FakeClient()
|
||
|
|
return service
|
||
|
|
|
||
|
|
|
||
|
|
def test_subscription_payload_uses_public_media_fields() -> None:
|
||
|
|
payload = build_subscription_payload(
|
||
|
|
{"id": "tmdb:123", "title": "沙丘", "year": "2021", "type": "电影"}
|
||
|
|
)
|
||
|
|
|
||
|
|
assert payload == {
|
||
|
|
"name": "沙丘",
|
||
|
|
"year": "2021",
|
||
|
|
"type": "movie",
|
||
|
|
"tmdbid": 123,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def test_torrent_payload_drops_sensitive_or_unknown_fields() -> None:
|
||
|
|
payload = build_torrent_payload(
|
||
|
|
{"title": "Dune", "enclosure": "signed", "site_cookie": "secret", "extra": 1}
|
||
|
|
)
|
||
|
|
|
||
|
|
assert payload == {"title": "Dune", "enclosure": "signed"}
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_subscription_and_download_requests(service) -> None:
|
||
|
|
media = {"id": "tmdb:123", "title": "沙丘", "type": "movie"}
|
||
|
|
resource = {"title": "Dune", "enclosure": "signed"}
|
||
|
|
|
||
|
|
assert (await service.create_subscription(media))["id"] == 8
|
||
|
|
assert (await service.trigger_subscription_search(8))["ok"]
|
||
|
|
assert (await service.download_resource(resource, media))["download_id"] == "download-1"
|
||
|
|
assert service._client.calls[0][1] == "/api/v1/subscribe/"
|
||
|
|
assert service._client.calls[1][1] == "/api/v1/subscribe/search/8"
|
||
|
|
assert service._client.calls[2][1] == "/api/v1/download/add"
|