Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add functional E2E tests for the keep_audio_only upload path that POST through the actual /upload route with mocked ffprobe and ffmpeg and assert the Recording row carries the right keep_audio_only value in both the toggle-on and toggle-off (with VIDEO_RETENTION=true) cases
Murtaza Nasirbf594db756c2
diff --git a/tests/test_keep_audio_only_e2e.py b/tests/test_keep_audio_only_e2e.pynew file mode 100644index 00000000..4daf87af--- /dev/null+++ b/tests/test_keep_audio_only_e2e.py@@ -0,0 +1,139 @@+"""End-to-end functional test for the keep_audio_only upload flow.++The earlier coverage in test_video_retention.py pins the source-code+shape (form-field parse, persistence assignment, processing-task+derivation) but not the runtime behaviour through the actual route.+This file does an HTTP POST through the upload handler with the form+field set and asserts the Recording row carries keep_audio_only=True.++The ffprobe / ffmpeg subprocess calls are mocked so the test can run+without a real video file.+"""+import io+import os+import sys+import uuid+from unittest.mock import patch, MagicMock++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db+from src.models import User, Recording++app.config["WTF_CSRF_ENABLED"] = False+++def _setup_user(prefix):+ suffix = uuid.uuid4().hex[:8]+ user = User(+ username=f"{prefix}_{suffix}",+ email=f"{prefix}_{suffix}@local.test",+ password="x",+ )+ db.session.add(user)+ db.session.commit()+ return user+++def _login(client, user):+ with client.session_transaction() as sess:+ sess["_user_id"] = str(user.id)+ sess["_fresh"] = True+++def test_upload_with_keep_audio_only_persists_flag_on_recording():+ """POST /upload with keep_audio_only=true sets recording.keep_audio_only+ in the DB. The processing pipeline reads this and discards the+ video stream; that derivation is covered by the static-analysis+ tests in test_video_retention.py."""+ with app.app_context():+ user = _setup_user('keep_e2e')+ client = app.test_client()+ _login(client, user)++ # Mock the codec probe to claim "video file" without invoking ffprobe.+ # Mock convert_if_needed so we don't actually shell out to ffmpeg.+ with patch('src.api.recordings.get_codec_info') as mock_probe, \+ patch('src.api.recordings.convert_if_needed') as mock_convert, \+ patch('src.services.job_queue.job_queue.enqueue', return_value=1):+ mock_probe.return_value = {+ 'has_video': True,+ 'audio_codec': 'aac',+ 'video_codec': 'h264',+ 'duration': 30.0,+ }+ convert_result = MagicMock()+ convert_result.output_path = '/tmp/converted-audio.mp3'+ convert_result.was_converted = True+ convert_result.was_compressed = False+ convert_result.original_codec = 'aac'+ convert_result.final_codec = 'mp3'+ mock_convert.return_value = convert_result++ # Patch os.path.getsize so the post-conversion size check+ # doesn't blow up on the fake output path.+ real_getsize = os.path.getsize+ def fake_getsize(p):+ if p == '/tmp/converted-audio.mp3':+ return 1024 * 1024+ return real_getsize(p)+ with patch('src.api.recordings.os.path.getsize', side_effect=fake_getsize):+ data = {+ 'keep_audio_only': 'true',+ 'title': 'e2e-test',+ }+ data['file'] = (io.BytesIO(b'\x00' * 16384), 'sample.mp4')+ resp = client.post('/upload', data=data, content_type='multipart/form-data')++ # Upload handler returns 202 on success; some failure modes+ # return other 2xx. Tolerate either as long as a Recording row+ # was created with the flag set.+ assert resp.status_code in (200, 202), (+ f'upload returned {resp.status_code}: {resp.data!r}'+ )++ rec = Recording.query.filter_by(user_id=user.id).order_by(Recording.id.desc()).first()+ assert rec is not None, 'upload did not create a Recording row'+ assert rec.keep_audio_only is True, (+ f'keep_audio_only form field set true, but Recording.keep_audio_only={rec.keep_audio_only}'+ )++ db.session.delete(rec)+ db.session.delete(user)+ db.session.commit()+++def test_upload_without_keep_audio_only_leaves_flag_off_when_video_retention_on():+ """Mirror test for the negative case: with VIDEO_RETENTION=true at+ the server and no form-field override, the flag stays False so the+ processing pipeline keeps the video stream."""+ with app.app_context(), \+ patch('src.api.recordings.VIDEO_RETENTION', True):+ user = _setup_user('keep_e2e_off')+ client = app.test_client()+ _login(client, user)++ with patch('src.api.recordings.get_codec_info') as mock_probe, \+ patch('src.services.job_queue.job_queue.enqueue', return_value=1):+ mock_probe.return_value = {+ 'has_video': True,+ 'audio_codec': 'aac',+ 'video_codec': 'h264',+ 'duration': 30.0,+ }+ data = {'title': 'e2e-test-noflag'}+ data['file'] = (io.BytesIO(b'\x00' * 16384), 'sample.mp4')+ resp = client.post('/upload', data=data, content_type='multipart/form-data')++ assert resp.status_code in (200, 202), (+ f'upload returned {resp.status_code}: {resp.data!r}'+ )+ rec = Recording.query.filter_by(user_id=user.id).order_by(Recording.id.desc()).first()+ assert rec is not None+ assert rec.keep_audio_only is False, (+ f'No keep_audio_only and VR=true; Recording.keep_audio_only should '+ f'be False; got {rec.keep_audio_only}'+ )+ db.session.delete(rec)+ db.session.delete(user)+ db.session.commit()