Files
web-framework-resources/tests/test_media_plugins.py
T

319 lines
16 KiB
Python

"""End-to-end contracts use real persisted jobs and mock only external services."""
from pathlib import Path
import hashlib
import importlib
import json
import re
import pytest
from web_framework import create_app
from web_framework.plugin_runtime import run_one, managed_path
PLUGINS = Path(__file__).resolve().parents[1] / 'plugins'
ALL = ['queues', 'anime-providers', 'anime-search', 'thumbnails', 'discord-notifications', 'ai-subtitles', 'media-handoff', 'watchlist']
@pytest.fixture
def app(tmp_path, monkeypatch):
monkeypatch.setenv('WEB_FRAMEWORK_DATA_DIR', str(tmp_path / 'data'))
source, target = tmp_path / 'source', tmp_path / 'library'
source.mkdir(); target.mkdir()
app = create_app(overrides={'runtime': {'plugin_dir': str(PLUGINS), 'theme_dir': str(tmp_path / 'themes'), 'background_workers': False},
'plugins': {'enabled': ALL}, 'resources': {'check_updates_on_startup': False}, 'logging': {'console': False}})
state = app.extensions['state']
state.set('plugin.watchlist.settings', {'queue': 'queues', 'search': 'anime-search', 'thumbnails': 'thumbnails', 'handoff': 'media-handoff', 'notifications': 'discord-notifications', 'scheduler': True, 'interval': 3600, 'auto-handoff': False})
state.set('plugin.media-handoff.settings', {'source-root': str(source), 'target-root': str(target)})
state.set('plugin.ai-subtitles.settings', {'root': str(target), 'model': 'local-test-model'})
state.set('plugin.discord-notifications.settings', {'webhook': 'https://discord.com/api/webhooks/123/testing-secret-token'})
app.testing = True
return app
def service(app, key, capability=None):
return app.extensions['plugins'].service(key, capability or key)
def module(app, key):
return importlib.import_module(app.extensions['plugins'].registry[key].blueprint.import_name)
def client(app):
result = app.test_client()
result.get('/login')
with result.session_transaction() as session:
csrf = session['csrf']
password = (app.extensions['state'].directory / 'initial-admin-password').read_text().strip()
result.post('/login', data={'username': 'admin', 'password': password, 'csrf_token': csrf})
with result.session_transaction() as session:
csrf = session['csrf']
result.post('/account', data={'current_password': password, 'new_password': 'Media-test-password!42', 'confirm_password': 'Media-test-password!42', 'csrf_token': csrf})
return result
def post(result, path, data):
with result.session_transaction() as session:
csrf = session['csrf']
return result.post(path, data={**data, 'csrf_token': csrf}, follow_redirects=True)
class Response:
def __init__(self, data=b'', status=200):
self.data, self.status_code = data, status
def __enter__(self):
return self
def __exit__(self, *args):
return False
def iter_content(self, size):
yield self.data
def test_pages_settings_capabilities_csrf_and_viewer_permissions(app):
browser = client(app)
html = browser.get('/config/plugins').data
assert b'Search plugin' in html and b'Thumbnail plugin' in html
assert b'value="anime-search"' in html and b'value="thumbnails"' in html
for key in ALL:
assert browser.get(f'/plugins/{key}/').status_code == 200
assert browser.post('/plugins/watchlist/').status_code == 400
with app.extensions['state'].db() as db:
db.execute("UPDATE users SET role='viewer'")
assert post(browser, '/plugins/watchlist/', {'title': 'Forbidden'}).status_code == 403
assert post(browser, '/plugins/discord-notifications/', {}).status_code == 403
assert post(browser, '/plugins/ai-subtitles/', {'path': 'x.srt'}).status_code == 403
assert post(browser, '/plugins/media-handoff/', {}).status_code == 403
def test_search_watchlist_scheduler_and_queue_integration(app, monkeypatch):
provider = service(app, 'anime-providers', 'anime-provider')
monkeypatch.setattr(provider, 'search', lambda *args, **kwargs: {'results': [{'id': 'anikoto:show', 'title': 'Test Show', 'provider': 'anikoto'}]})
monkeypatch.setattr(provider, 'info', lambda *args, **kwargs: {'title': 'Test Show', 'episodes': 2, 'image': ''})
monkeypatch.setattr(provider, 'episodes', lambda *args, **kwargs: [{'id': 'e1'}, {'id': 'e2'}])
browser = client(app)
response = browser.get('/plugins/anime-search/?q=Test')
assert b'Test Show' in response.data
response = post(browser, '/plugins/anime-search/watchlist', {'watchlist': 'watchlist', 'title': 'Test Show', 'external_id': 'anikoto:show', 'q': 'Test'})
assert b'Added to watchlist' in response.data
watchlist = service(app, 'watchlist')
item = watchlist.items()[0]
watchlist.update(item['id'], category='watching')
watchlist.run_due(); watchlist.run_due()
queue = service(app, 'queues', 'queue')
assert len(queue.jobs(source='watchlist', status='queued')) == 1
run_one(watchlist)
refreshed = watchlist.get(item['id'])
assert refreshed['available'] == 2 and refreshed['expected'] == 2 and refreshed['checked']
assert len(queue.jobs(source='thumbnails')) == 1
assert len(queue.jobs(source='discord-notifications')) == 1
assert queue.jobs(source='watchlist')[0]['status'] == 'completed'
watchlist.edit(item['id'], title='Edited', external_id='anikoto:show', category='finished', media_path='Test Show/Season 01', expected=2, season=1)
assert watchlist.get(item['id'])['media_path'] == 'Test Show/Season 01'
assert watchlist.handoff(item['id'])
def test_optional_integrations_allow_plain_general_watchlists(app):
app.extensions['state'].set('plugin.watchlist.settings', {'queue': 'queues'})
watchlist = service(app, 'watchlist')
identifier = watchlist.add('<script>General</script>', category='watching')
browser = client(app)
assert b'&lt;script&gt;' in browser.get('/plugins/watchlist/').data
with pytest.raises(ValueError, match='Select a search plugin'):
watchlist.refresh(identifier, lambda *args: None)
with pytest.raises(ValueError):
watchlist.add('Invalid', media_path='../outside')
assert create_app(overrides={'runtime': {'plugin_dir': str(PLUGINS), 'theme_dir': str(app.extensions['resources'].theme_dir), 'background_workers': False}, 'plugins': {'enabled': ALL}, 'resources': {'check_updates_on_startup': False}}).extensions['plugins'].service('watchlist', 'watchlist').get(identifier)
def setup_media(app, count=2):
handoff = service(app, 'media-handoff')
source, target = handoff.roots()
season = source / 'Show/Season 01'
season.mkdir(parents=True)
for episode in range(1, count + 1):
(season / f'Show S01E{episode:02d}.mkv').write_bytes(b'video content ' + str(episode).encode())
(season / 'Show S01E01.en.srt').write_bytes(b'1\n00:00:01,000 --> 00:00:02,000\nHello.\n')
return handoff, source, target, {'title': 'Show', 'category': 'finished', 'media_path': 'Show/Season 01', 'season': 1, 'expected': 2, 'media_type': 'tv'}
def test_handoff_checks_completion_preserves_other_seasons_and_is_idempotent(app):
handoff, source, target, item = setup_media(app)
other = source / 'Show/Season 02'
other.mkdir(); (other / 'Show S02E01.mkv').write_bytes(b'other season')
job = handoff.submit(item)
run_one(handoff)
queue = service(app, 'queues', 'queue')
assert queue.get(job)['status'] == 'completed'
assert (source / 'Show/Season 01/Show S01E01.mkv').exists() # copy is the default
assert (target / 'Show/Season 01/Show S01E01.en.srt').exists()
assert other.exists()
app.extensions['state'].set('plugin.media-handoff.settings', {**handoff.settings(), 'mode': 'move'})
second = handoff.submit(item)
run_one(handoff)
assert queue.get(second)['status'] == 'completed'
assert not (source / 'Show/Season 01').exists() and other.exists()
third = handoff.submit(item)
run_one(handoff)
assert queue.get(third)['status'] == 'completed'
@pytest.mark.parametrize('condition', ['missing', 'collision', 'symlink', 'wrong-season', 'traversal'])
def test_handoff_refuses_unsafe_or_incomplete_transfers_without_deleting_sources(app, condition):
handoff, source, target, item = setup_media(app, count=1 if condition == 'missing' else 2)
original = source / 'Show/Season 01/Show S01E01.mkv'
if condition == 'collision':
destination = target / 'Show/Season 01/Show S01E01.mkv'
destination.parent.mkdir(parents=True)
destination.write_bytes(b'different destination')
elif condition == 'symlink':
(source / 'Show/Season 01/escape.mkv').symlink_to(original)
elif condition == 'wrong-season':
item['season'] = 2
elif condition == 'traversal':
item['media_path'] = '../outside'
job = handoff.submit(item)
run_one(handoff)
assert service(app, 'queues', 'queue').get(job)['status'] == 'failed'
assert original.read_bytes() == b'video content 1'
if condition == 'collision':
assert destination.read_bytes() == b'different destination'
def test_subtitle_translation_preserves_timestamps_markup_and_originals(app, monkeypatch):
subtitles = service(app, 'ai-subtitles', 'subtitles')
root = Path(subtitles.settings()['root'])
path = root / 'episode.en.srt'
original = b'1\r\n00:00:01,000 --> 00:00:02,000\r\n<i>Hello</i> there.\r\n\r\n2\r\n00:00:03,000 --> 00:00:04,000\r\nGoodbye!\r\n'
path.write_bytes(original)
calls = []
def ai(url, **kwargs):
calls.append((url, kwargs))
answer = json.dumps({'translations': ['Ahoj__TAG0__ tam.', 'Na shledanou!']})
return Response(json.dumps({'response': answer}).encode())
monkeypatch.setattr(module(app, 'ai-subtitles').requests, 'post', ai)
job = subtitles.submit('episode.en.srt')
run_one(subtitles)
assert service(app, 'queues', 'queue').get(job)['status'] == 'completed'
assert path.read_bytes() == original
output = subtitles.state.items('ai-subtitles')[0]
data = (root / output['output']).read_bytes()
assert b'00:00:01,000 --> 00:00:02,000\r\n' in data
assert '<i>Ahoj</i> tam.'.encode() in data and b'\r\n' in data
assert subtitles.submit('episode.en.srt') is None
assert len(calls) == 1
browser = client(app)
assert browser.get('/plugins/ai-subtitles/outputs/' + output['id']).data == data
def test_openai_responses_request_and_vtt_metadata(app, monkeypatch):
subtitles = service(app, 'ai-subtitles', 'subtitles')
subtitles.state.set('plugin.ai-subtitles.settings', {**subtitles.settings(), 'backend': 'openai', 'endpoint': 'https://api.openai.com/v1', 'api-key': 'unit-test-secret'})
root = Path(subtitles.settings()['root'])
original = b'WEBVTT\n\nNOTE keep me\n\ncue-id\n00:00:01.000 --> 00:00:02.000 align:start\nHello\n'
(root / 'episode.vtt').write_bytes(original)
def ai(url, **kwargs):
assert url.endswith('/v1/responses')
assert kwargs['json']['store'] is False
assert kwargs['headers']['Authorization'] == 'Bearer unit-test-secret'
return Response(json.dumps({'output': [{'content': [{'type': 'output_text', 'text': json.dumps({'translations': ['Ahoj']})}]}]}).encode())
monkeypatch.setattr(module(app, 'ai-subtitles').requests, 'post', ai)
job = subtitles.submit('episode.vtt')
run_one(subtitles)
assert service(app, 'queues', 'queue').get(job)['status'] == 'completed'
output = (root / subtitles.state.items('ai-subtitles')[0]['output']).read_bytes()
assert b'NOTE keep me' in output and b'cue-id' in output and b'align:start' in output
assert b'unit-test-secret' not in subtitles.state.path.read_bytes()
@pytest.mark.parametrize('translations', [[], [''], ['<b>bad</b>'], ['00:00 --> changed'], ['one\n\ntwo']])
def test_invalid_ai_outputs_never_publish_or_change_originals(app, monkeypatch, translations):
subtitles = service(app, 'ai-subtitles', 'subtitles')
root = Path(subtitles.settings()['root'])
source = root / 'episode.srt'
source.write_text('1\n00:00:01,000 --> 00:00:02,000\nHello\n')
monkeypatch.setattr(module(app, 'ai-subtitles').requests, 'post', lambda *args, **kwargs: Response(json.dumps({'response': json.dumps({'translations': translations})}).encode()))
job = subtitles.submit('episode.srt')
run_one(subtitles)
assert service(app, 'queues', 'queue').get(job)['status'] == 'failed'
assert not list(root.glob('*.ai.*'))
assert source.read_text().endswith('Hello\n')
def test_discord_secrets_filtering_mentions_and_delivery(app, monkeypatch):
notifications = service(app, 'discord-notifications', 'notify')
sent = []
def deliver(url, **kwargs):
sent.append(kwargs['json'])
assert kwargs['allow_redirects'] is False
return Response(status=204)
monkeypatch.setattr(module(app, 'discord-notifications').requests, 'post', deliver)
job = notifications.send('watchlist.episodes', '@everyone New episode')
queue = service(app, 'queues', 'queue')
assert 'testing-secret-token' not in json.dumps(queue.jobs())
run_one(notifications)
assert queue.get(job)['status'] == 'completed'
assert sent[0]['allowed_mentions'] == {'parse': []}
notifications.state.set('plugin.discord-notifications.settings', {**notifications.settings(), 'events': 'media.handoff'})
assert notifications.send('other', 'Ignored') is None
notifications.state.set('plugin.discord-notifications.settings', {**notifications.settings(), 'webhook': 'https://evil.example/api/webhooks/123/token'})
with pytest.raises(ValueError):
notifications.send('media.handoff', 'Do not send')
def test_thumbnail_cache_validates_image_and_serves_authenticated_bytes(app, monkeypatch):
thumbnails = service(app, 'thumbnails')
thumbnails.state.set('plugin.thumbnails.settings', {**thumbnails.settings(), 'source': 'provider-url', 'hosts': 'images.example.test'})
image = b'\x89PNG\r\n\x1a\n' + b'fixture image payload'
monkeypatch.setattr(module(app, 'thumbnails').requests, 'get', lambda *args, **kwargs: Response(image))
job = thumbnails.request('Show', 'https://images.example.test/cover.png')
run_one(thumbnails)
assert service(app, 'queues', 'queue').get(job)['status'] == 'completed'
key = thumbnails.key('Show')
assert app.test_client().get('/plugins/thumbnails/images/' + key).status_code == 302
browser = client(app)
result = browser.get('/plugins/thumbnails/images/' + key)
assert result.data == image and result.mimetype == 'image/png'
bad = thumbnails.request('Other', 'https://not-allowed.example/picture')
run_one(thumbnails)
assert service(app, 'queues', 'queue').get(bad)['status'] == 'failed'
def test_disabled_handles_and_path_escape_are_rejected(app, tmp_path):
thumbnails = service(app, 'thumbnails')
app.extensions['state'].set('plugins.enabled', ['queues'])
with pytest.raises(ValueError):
thumbnails.request('Disabled')
with pytest.raises(ValueError):
managed_path(tmp_path, '../escape')
(tmp_path / 'link').symlink_to('/tmp')
with pytest.raises(ValueError):
managed_path(tmp_path, 'link/escape', exists=False)
def test_background_workers_execute_scheduled_watchlist_jobs_without_browser_requests(app, monkeypatch):
from threading import Event
done = Event()
provider = service(app, 'anime-providers', 'anime-provider')
monkeypatch.setattr(provider, 'info', lambda *args, **kwargs: {'episodes': 1, 'image': ''})
def episodes(*args, **kwargs):
done.set()
return [{'id': 'e1'}]
monkeypatch.setattr(provider, 'episodes', episodes)
monkeypatch.setattr(module(app, 'discord-notifications').requests, 'post', lambda *args, **kwargs: Response(status=204))
thumbnails = service(app, 'thumbnails')
thumbnails.state.set('plugin.thumbnails.settings', {**thumbnails.settings(), 'source': 'provider-url'})
watchlist = service(app, 'watchlist')
identifier = watchlist.add('Scheduled', external_id='anikoto:one', category='watching')
manager = app.extensions['plugins']
try:
manager.start_workers()
assert done.wait(8)
# The event fires during the provider call; wait until the durable update completes.
import time
deadline = time.monotonic() + 5
while time.monotonic() < deadline and watchlist.get(identifier)['available'] != 1:
manager.worker_stop.wait(0.05)
assert watchlist.get(identifier)['available'] == 1
finally:
manager.shutdown()
for thread in manager.worker_threads:
thread.join(timeout=2)
assert all(not thread.is_alive() for thread in manager.worker_threads)