Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python
12.3 KB
Raw Blame
"""Regression tests for the PWA Web Share Target endpoint (issue #285).

The endpoint is what Android Chrome / iOS Safari 16.4+ POST to when the
user picks Speakr from the native share sheet. We verify:

- the manifest declares /share-target as the action
- a missing file redirects with share_target_error=missing_file
- a valid POST creates a Recording row owned by the current user
- the endpoint is CSRF-exempt (the share sheet has no CSRF token)
- the endpoint is login-required (unauthenticated visits bounce to login)
"""

import io
import json
import os
import sys
import uuid
from pathlib import Path
from unittest.mock import patch

import pytest

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


def _setup_user(prefix):
    suffix = uuid.uuid4().hex[:8]
    user = User(
        # The username column holds 20 characters (PostgreSQL enforces it)
        username=f"{prefix[:11]}_{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



from contextlib import contextmanager as _contextmanager


@_contextmanager
def _no_media_tools():
    """The share target runs the standard upload ingestion (#412), which probes
    and converts audio with ffmpeg. These tests send placeholder bytes, so the
    media steps are stubbed as in tests/test_cov_recordings_write.py."""
    from unittest.mock import MagicMock, patch as _patch

    def _convert(filepath, **kwargs):
        r = MagicMock()
        r.output_path = filepath
        r.was_converted = r.was_compressed = False
        r.original_codec = r.final_codec = "opus"
        r.size_reduction_percent = 0.0
        return r
    with _patch("src.api.recordings.convert_if_needed", side_effect=_convert), \
         _patch("src.api.recordings.get_codec_info",
                return_value={"has_video": False, "audio_codec": "opus", "video_codec": None, "duration": 5.0}), \
         _patch("src.api.recordings.get_duration", return_value=5.0), \
         _patch("src.api.recordings.get_creation_date", return_value=None):
        yield


def test_manifest_share_target_action():
    """The PWA manifest must point share_target.action at /share-target."""
    manifest_path = Path(__file__).parent.parent / "static" / "manifest.json"
    manifest = json.loads(manifest_path.read_text())
    assert manifest["share_target"]["action"] == "/share-target"
    assert manifest["share_target"]["method"] == "POST"
    assert manifest["share_target"]["enctype"] == "multipart/form-data"
    files = manifest["share_target"]["params"]["files"]
    assert any(f["name"] == "shared_audio" for f in files)


def test_share_target_missing_file_redirects_with_error_flag():
    with app.app_context():
        user = _setup_user("share_missing")
        client = app.test_client()
        _login(client, user)
        resp = client.post("/share-target", data={}, follow_redirects=False)
        assert resp.status_code == 302
        assert "share_target_error=missing_file" in resp.headers.get("Location", "")
        db.session.delete(user)
        db.session.commit()


def test_share_target_empty_file_redirects_with_unreadable_flag():
    with app.app_context():
        user = _setup_user("share_empty")
        client = app.test_client()
        _login(client, user)
        with _no_media_tools():
            resp = client.post("/share-target",
                               data={"shared_audio": (io.BytesIO(b""), "memo.m4a")},
                               content_type="multipart/form-data", follow_redirects=False)
        assert resp.status_code == 302
        assert "share_target_error=unreadable" in resp.headers.get("Location", "")
        assert Recording.query.filter_by(user_id=user.id).count() == 0
        db.session.delete(user)
        db.session.commit()


def test_share_target_creates_recording_and_enqueues_job():
    with app.app_context():
        user = _setup_user("share_ok")
        client = app.test_client()
        _login(client, user)

        captured = {}

        def fake_enqueue(*args, **kwargs):
            captured.update(kwargs)
            return 999

        with _no_media_tools(), patch("src.services.job_queue.job_queue.enqueue", side_effect=fake_enqueue), \
             patch("src.api.recordings.os.path.getsize", return_value=12345):
            resp = client.post(
                "/share-target",
                data={
                    "title": "Shared from Recorder app",
                    "text": "Meeting notes",
                    "shared_audio": (io.BytesIO(b"fake-audio-bytes"), "shared.webm"),
                },
                content_type="multipart/form-data",
                follow_redirects=False,
            )
        assert resp.status_code == 302, resp.data
        location = resp.headers.get("Location", "")
        assert "share_target=ok" in location, location
        assert "recording_id=" in location, location

        recordings = Recording.query.filter_by(user_id=user.id).all()
        assert len(recordings) == 1
        rec = recordings[0]
        assert rec.title == "Shared from Recorder app"
        assert rec.notes and "Meeting notes" in rec.notes
        assert rec.status == "PENDING"
        assert captured.get("user_id") == user.id
        assert captured.get("recording_id") == rec.id
        assert captured.get("job_type") == "transcribe", (
            f"share-target must use the same job_type as upload_file (transcribe), "
            f"got {captured.get('job_type')!r}"
        )
        assert captured.get("is_new_upload") is True, "share-target uploads should be flagged as new uploads"
        # Clean up the saved file artefact if present
        if rec.audio_path and os.path.exists(rec.audio_path):
            try:
                os.remove(rec.audio_path)
            except OSError:
                pass
        db.session.delete(rec)
        db.session.delete(user)
        db.session.commit()


def test_share_target_without_title_gets_ai_titleable_placeholder():
    """Regression: a file shared WITHOUT an explicit title must get the same
    placeholder ('Recording - <filename>') a normal upload gets, so the AI
    title task recognises it and generates a title. Previously the share
    target used the filename STEM, which the title task treated as a
    user-chosen title and skipped, leaving shared files untitled."""
    from src.utils.titles import is_placeholder_title

    with app.app_context():
        user = _setup_user("share_notitle")
        client = app.test_client()
        _login(client, user)

        with _no_media_tools(), patch("src.services.job_queue.job_queue.enqueue", return_value=1), \
             patch("src.api.recordings.os.path.getsize", return_value=12345):
            resp = client.post(
                "/share-target",
                data={
                    # No "title" field — the common case from a share sheet.
                    "shared_audio": (io.BytesIO(b"fake-audio-bytes"), "voice_memo_001.webm"),
                },
                content_type="multipart/form-data",
                follow_redirects=False,
            )
        assert resp.status_code == 302, resp.data

        rec = Recording.query.filter_by(user_id=user.id).one()
        # The title must be the recognised placeholder, NOT the filename stem.
        assert rec.title == "Recording - voice_memo_001.webm", rec.title
        assert is_placeholder_title(rec.title, rec.original_filename) is True

        if rec.audio_path and os.path.exists(rec.audio_path):
            try:
                os.remove(rec.audio_path)
            except OSError:
                pass
        db.session.delete(rec)
        db.session.delete(user)
        db.session.commit()


def test_share_target_requires_login():
    """Unauthenticated POSTs hit @login_required and redirect to /login."""
    with app.app_context():
        client = app.test_client()
        resp = client.post(
            "/share-target",
            data={"shared_audio": (io.BytesIO(b"x"), "x.webm")},
            content_type="multipart/form-data",
            follow_redirects=False,
        )
        assert resp.status_code == 302
        loc = resp.headers.get("Location", "")
        assert "/login" in loc or "next=" in loc, loc


def test_share_target_is_csrf_exempt():
    """The share sheet has no CSRF token; the route must be exempted."""
    from src.app import csrf
    # Flask-WTF stores exempt names on the protect instance
    exempt_set = getattr(csrf, "_exempt_views", set())
    # Match by qualified function name
    assert any("share_target" in name for name in exempt_set), \
        f"share_target view should be CSRF-exempt; current exempts: {exempt_set}"


@pytest.mark.parametrize("site", ["cross-site", "same-site"])
def test_share_target_refuses_a_post_from_another_site(site):
    """A forged share from another site carries the user's cookies (#435)."""
    with app.app_context():
        user = _setup_user(f"share_xsite_{site[:5]}")
        client = app.test_client()
        _login(client, user)
        data = {"shared_audio": (io.BytesIO(b"\x00" * 512), "a.mp3"), "text": "<b>x</b>"}
        resp = client.post("/share-target", data=data, headers={"Sec-Fetch-Site": site},
                           content_type="multipart/form-data", follow_redirects=False)
        assert resp.status_code == 302
        assert "share_target_error=cross_site" in resp.headers.get("Location", "")
        assert Recording.query.filter_by(user_id=user.id).count() == 0
        db.session.delete(user)
        db.session.commit()


@pytest.mark.parametrize("site", ["none", "same-origin"])
def test_share_target_accepts_the_share_sheet(site):
    """The operating system's share sheet is marked "none"."""
    with app.app_context():
        user = _setup_user(f"share_ok_{site[:4]}")
        client = app.test_client()
        _login(client, user)
        resp = client.post("/share-target", data={}, headers={"Sec-Fetch-Site": site},
                           follow_redirects=False)
        assert "share_target_error=missing_file" in resp.headers.get("Location", "")
        db.session.delete(user)
        db.session.commit()


def teardown_module(module):
    with app.app_context():
        for u in User.query.filter(User.username.like("share_%")).all():
            db.session.delete(u)
        db.session.commit()


if __name__ == "__main__":
    test_manifest_share_target_action()
    test_share_target_missing_file_redirects_with_error_flag()
    test_share_target_creates_recording_and_enqueues_job()
    test_share_target_requires_login()
    test_share_target_is_csrf_exempt()
    print("All share-target tests passed.")


def test_share_target_is_limited_per_user():
    """#435: shares are counted per signed-in user; another user is not affected."""
    from types import SimpleNamespace
    from limits import parse
    from limits.storage import MemoryStorage
    from limits.strategies import FixedWindowRateLimiter
    from src.api import recordings as rec_module
    with app.app_context():
        ids = [_setup_user(p).id for p in ("rl_first", "rl_second")]
    clients = []
    for uid in ids:
        client = app.test_client()
        with client.session_transaction() as sess:
            sess["_user_id"] = str(uid)
            sess["_fresh"] = True
        clients.append(client)
    # The suite runs with rate limiting off, so Flask-Limiter has no storage;
    # an enabled limiter with its own memory storage stands in for it.
    old_limiter, old_limit = rec_module.limiter, rec_module.SHARE_TARGET_LIMIT
    rec_module.limiter = SimpleNamespace(enabled=True, limiter=FixedWindowRateLimiter(MemoryStorage()))
    rec_module.SHARE_TARGET_LIMIT = parse("2 per hour")
    try:
        places = [clients[0].post("/share-target", data={}).headers["Location"] for _ in range(3)]
        assert "missing_file" in places[0] and "missing_file" in places[1]
        assert "share_target_error=rate_limited" in places[2]
        assert "missing_file" in clients[1].post("/share-target", data={}).headers["Location"]
    finally:
        rec_module.limiter, rec_module.SHARE_TARGET_LIMIT = old_limiter, old_limit
        with app.app_context():
            for uid in ids:
                db.session.delete(db.session.get(User, uid))
            db.session.commit()