tests/test_browser_copy.py (3288 bytes)
1 """During the beta, a job in the browser also sends a copy of its audio and its 2 book to the server, as the privacy and user data policy says (terms.html, 3 "Input data kept for debugging"). The server keeps the copy with the 4 subtitles, in the folder of the job. A job in the browser never spends a 5 credit, with or without the copy. 6 """ 7 8 from __future__ import annotations 9 10 from backend.runner import Paths 11 from backend.settings import settings 12 13 from .conftest import make_job 14 15 BOOK = { 16 "audio_filename": "copy.m4b", "audio_bytes": 11, 17 "text_filename": "copy.epub", "language": "en", 18 } 19 SRT = "1\n00:00:00,000 --> 00:00:01,000\nhello\n\n" 20 21 22 def begin(client) -> dict: 23 r = client.post("/api/local/jobs", json=BOOK) 24 assert r.status_code == 200 25 return r.json() 26 27 28 def send_copy(client, job_id: str, files: list[tuple[str, bytes]]): 29 return client.post( 30 f"/api/local/jobs/{job_id}/files", 31 files=[("files", (name, data, "application/octet-stream")) for name, data in files], 32 ) 33 34 35 def test_the_page_is_told_to_send_a_copy(client): 36 assert client.get("/api/account").json()["browser_copy"] is True 37 38 39 def test_a_browser_job_keeps_audio_book_and_subtitles_together(client): 40 job = begin(client) 41 r = send_copy(client, job["id"], [("copy.m4b", b"audio"), ("copy.epub", b"book")]) 42 assert r.status_code == 200 and r.json() == {"saved": 2} 43 44 done = client.post(f"/api/local/jobs/{job['id']}/finish", 45 json={"srt": SRT, "filename": "copy.en.srt"}) 46 assert done.status_code == 200 47 48 root = Paths.for_job(job["id"]).root 49 assert (root / "input" / "copy.m4b").read_bytes() == b"audio" 50 assert (root / "input" / "copy.epub").read_bytes() == b"book" 51 assert (root / "copy.en.srt").read_bytes() == SRT.encode("utf-8") 52 53 54 def test_a_browser_job_and_its_copy_spend_no_credit(client, monkeypatch): 55 monkeypatch.setattr(settings, "cloud_enabled", True) # the free credit exists 56 before = client.get("/api/account").json()["credits"] 57 job = begin(client) 58 assert send_copy(client, job["id"], [("copy.m4b", b"audio")]).status_code == 200 59 assert client.get("/api/account").json()["credits"] == before == 1 60 61 62 def test_only_the_owner_can_send_a_copy(client, second_client): 63 job = begin(client) 64 assert send_copy(second_client, job["id"], [("copy.m4b", b"x")]).status_code == 404 65 66 67 def test_a_file_name_cannot_leave_the_folder_of_the_job(client): 68 job = begin(client) 69 r = send_copy(client, job["id"], [("../../escape.m4b", b"x"), ("..\\..\\win.epub", b"y")]) 70 assert r.status_code == 200 71 inp = Paths.for_job(job["id"]).inp 72 assert (inp / "escape.m4b").read_bytes() == b"x" 73 assert (inp / "win.epub").read_bytes() == b"y" 74 assert not (inp.parent.parent / "escape.m4b").exists() 75 76 77 def test_no_copy_when_the_beta_setting_is_off(client, monkeypatch): 78 monkeypatch.setattr(settings, "browser_copy", False) 79 assert client.get("/api/account").json()["browser_copy"] is False 80 job = begin(client) 81 assert send_copy(client, job["id"], [("copy.m4b", b"x")]).status_code == 404 82 83 84 def test_a_server_job_takes_no_copy(client): 85 job_id = make_job(client) # a server job: its files came with the upload 86 assert send_copy(client, job_id, [("copy.m4b", b"x")]).status_code == 409