|
| 1 | +import time |
| 2 | +from io import BytesIO |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from tiklocal.app import create_app |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def client(tmp_path, monkeypatch): |
| 11 | + media_root = tmp_path / "media" |
| 12 | + media_root.mkdir(parents=True, exist_ok=True) |
| 13 | + cookie_root = tmp_path / "cookies" |
| 14 | + cookie_root.mkdir(parents=True, exist_ok=True) |
| 15 | + (cookie_root / "x.com.txt").write_text("# Netscape HTTP Cookie File\n", encoding="utf-8") |
| 16 | + (cookie_root / "youtube.com.cookies").write_text("# Netscape HTTP Cookie File\n", encoding="utf-8") |
| 17 | + |
| 18 | + data_root = tmp_path / "tiklocal-data" |
| 19 | + monkeypatch.setenv("MEDIA_ROOT", str(media_root)) |
| 20 | + monkeypatch.setenv("TIKLOCAL_INSTANCE", str(data_root)) |
| 21 | + monkeypatch.setenv("TIKLOCAL_COOKIE_DIR", str(cookie_root)) |
| 22 | + |
| 23 | + def fake_execute_download(self, job_id): # noqa: ARG001 |
| 24 | + return 0, "", "mock-output.mp4" |
| 25 | + |
| 26 | + monkeypatch.setattr("tiklocal.services.downloader.DownloadManager._execute_download", fake_execute_download) |
| 27 | + |
| 28 | + app = create_app({"TESTING": True, "MEDIA_ROOT": media_root}) |
| 29 | + return app.test_client() |
| 30 | + |
| 31 | + |
| 32 | +def _wait_for_job(client, job_id, timeout=2.0): |
| 33 | + end = time.time() + timeout |
| 34 | + while time.time() < end: |
| 35 | + res = client.get(f"/api/download/jobs/{job_id}") |
| 36 | + assert res.status_code == 200 |
| 37 | + data = res.get_json() |
| 38 | + job = data["data"]["job"] |
| 39 | + if job["status"] in {"success", "failed", "canceled"}: |
| 40 | + return job |
| 41 | + time.sleep(0.05) |
| 42 | + return job |
| 43 | + |
| 44 | + |
| 45 | +def test_download_config_api(client): |
| 46 | + res = client.get("/api/download/config") |
| 47 | + data = res.get_json() |
| 48 | + assert res.status_code == 200 |
| 49 | + assert data["success"] is True |
| 50 | + assert data["data"]["effective"]["max_concurrent"] == 2 |
| 51 | + |
| 52 | + res = client.post( |
| 53 | + "/api/download/config", |
| 54 | + json={ |
| 55 | + "enabled": True, |
| 56 | + "default_to_root": True, |
| 57 | + "allow_playlist": False, |
| 58 | + "max_concurrent": 0, |
| 59 | + }, |
| 60 | + ) |
| 61 | + data = res.get_json() |
| 62 | + assert res.status_code == 200 |
| 63 | + assert data["success"] is True |
| 64 | + assert data["data"]["effective"]["max_concurrent"] == 0 |
| 65 | + |
| 66 | + |
| 67 | +def test_download_config_validation(client): |
| 68 | + res = client.post( |
| 69 | + "/api/download/config", |
| 70 | + json={ |
| 71 | + "enabled": True, |
| 72 | + "default_to_root": True, |
| 73 | + "allow_playlist": False, |
| 74 | + "max_concurrent": -1, |
| 75 | + }, |
| 76 | + ) |
| 77 | + data = res.get_json() |
| 78 | + assert res.status_code == 400 |
| 79 | + assert data["success"] is False |
| 80 | + assert "max_concurrent" in data["error"] |
| 81 | + |
| 82 | + |
| 83 | +def test_create_download_job_success(client): |
| 84 | + res = client.post("/api/download/jobs", json={"url": "https://example.com/video"}) |
| 85 | + data = res.get_json() |
| 86 | + assert res.status_code == 200 |
| 87 | + assert data["success"] is True |
| 88 | + job_id = data["data"]["job"]["id"] |
| 89 | + |
| 90 | + final_job = _wait_for_job(client, job_id) |
| 91 | + assert final_job["status"] == "success" |
| 92 | + assert final_job["output_path_rel"] == "mock-output.mp4" |
| 93 | + assert final_job["cookie_match_mode"] == "none" |
| 94 | + |
| 95 | + |
| 96 | +def test_create_download_job_validation(client): |
| 97 | + res = client.post("/api/download/jobs", json={"url": "file:///tmp/a.mp4"}) |
| 98 | + data = res.get_json() |
| 99 | + assert res.status_code == 400 |
| 100 | + assert data["success"] is False |
| 101 | + assert "http/https" in data["error"] |
| 102 | + |
| 103 | + |
| 104 | +def test_cancel_download_job(client, monkeypatch): |
| 105 | + def slow_execute_download(self, job_id): # noqa: ARG001 |
| 106 | + time.sleep(0.25) |
| 107 | + return 0, "", "mock-output.mp4" |
| 108 | + |
| 109 | + monkeypatch.setattr("tiklocal.services.downloader.DownloadManager._execute_download", slow_execute_download) |
| 110 | + |
| 111 | + res = client.post("/api/download/jobs", json={"url": "https://example.com/video2"}) |
| 112 | + data = res.get_json() |
| 113 | + assert res.status_code == 200 |
| 114 | + job_id = data["data"]["job"]["id"] |
| 115 | + |
| 116 | + cancel_res = client.post(f"/api/download/jobs/{job_id}/cancel") |
| 117 | + cancel_data = cancel_res.get_json() |
| 118 | + assert cancel_res.status_code == 200 |
| 119 | + assert cancel_data["success"] is True |
| 120 | + |
| 121 | + final_job = _wait_for_job(client, job_id) |
| 122 | + assert final_job["status"] in {"canceled", "success"} |
| 123 | + |
| 124 | + |
| 125 | +def test_download_probe_api(client): |
| 126 | + res = client.post("/api/download/probe") |
| 127 | + data = res.get_json() |
| 128 | + assert res.status_code == 200 |
| 129 | + assert data["success"] is True |
| 130 | + assert "yt_dlp_available" in data["data"] |
| 131 | + assert "ffmpeg_available" in data["data"] |
| 132 | + |
| 133 | + |
| 134 | +def test_download_cookie_files_api(client): |
| 135 | + res = client.get("/api/download/cookies") |
| 136 | + data = res.get_json() |
| 137 | + assert res.status_code == 200 |
| 138 | + assert data["success"] is True |
| 139 | + assert "x.com.txt" in data["data"]["files"] |
| 140 | + assert "youtube.com.cookies" in data["data"]["files"] |
| 141 | + |
| 142 | + |
| 143 | +def test_download_job_auto_cookie_match(client): |
| 144 | + res = client.post("/api/download/jobs", json={"url": "https://m.x.com/video/123"}) |
| 145 | + data = res.get_json() |
| 146 | + assert res.status_code == 200 |
| 147 | + assert data["success"] is True |
| 148 | + job = data["data"]["job"] |
| 149 | + assert job["cookie_match_mode"] == "auto" |
| 150 | + assert job["cookie_file"] == "x.com.txt" |
| 151 | + |
| 152 | + |
| 153 | +def test_download_job_manual_cookie_file(client): |
| 154 | + res = client.post( |
| 155 | + "/api/download/jobs", |
| 156 | + json={"url": "https://example.com/private", "cookie_mode": "manual", "cookie_file": "youtube.com.cookies"}, |
| 157 | + ) |
| 158 | + data = res.get_json() |
| 159 | + assert res.status_code == 200 |
| 160 | + assert data["success"] is True |
| 161 | + job = data["data"]["job"] |
| 162 | + assert job["cookie_match_mode"] == "manual" |
| 163 | + assert job["cookie_file"] == "youtube.com.cookies" |
| 164 | + |
| 165 | + |
| 166 | +def test_download_job_rejects_invalid_cookie_file(client): |
| 167 | + res = client.post( |
| 168 | + "/api/download/jobs", |
| 169 | + json={"url": "https://example.com/private", "cookie_mode": "manual", "cookie_file": "../secrets.txt"}, |
| 170 | + ) |
| 171 | + data = res.get_json() |
| 172 | + assert res.status_code == 400 |
| 173 | + assert data["success"] is False |
| 174 | + assert "cookie_file" in data["error"] |
| 175 | + |
| 176 | + |
| 177 | +def test_upload_cookie_file_and_replace(client): |
| 178 | + res = client.post( |
| 179 | + "/api/download/cookies/upload", |
| 180 | + data={"file": (BytesIO(b"# Netscape HTTP Cookie File\n"), "instagram.com.txt")}, |
| 181 | + content_type="multipart/form-data", |
| 182 | + ) |
| 183 | + data = res.get_json() |
| 184 | + assert res.status_code == 200 |
| 185 | + assert data["success"] is True |
| 186 | + assert data["data"]["filename"] == "instagram.com.txt" |
| 187 | + |
| 188 | + res = client.post( |
| 189 | + "/api/download/cookies/upload", |
| 190 | + data={"file": (BytesIO(b"# Netscape HTTP Cookie File\n"), "instagram.com.txt")}, |
| 191 | + content_type="multipart/form-data", |
| 192 | + ) |
| 193 | + data = res.get_json() |
| 194 | + assert res.status_code == 200 |
| 195 | + assert data["success"] is True |
| 196 | + |
| 197 | + |
| 198 | +def test_retry_failed_job(client, monkeypatch): |
| 199 | + def fail_execute(self, job_id): # noqa: ARG001 |
| 200 | + return 1, "network", "" |
| 201 | + |
| 202 | + monkeypatch.setattr("tiklocal.services.downloader.DownloadManager._execute_download", fail_execute) |
| 203 | + res = client.post("/api/download/jobs", json={"url": "https://example.com/fail"}) |
| 204 | + data = res.get_json() |
| 205 | + assert res.status_code == 200 |
| 206 | + failed_job = _wait_for_job(client, data["data"]["job"]["id"]) |
| 207 | + assert failed_job["status"] == "failed" |
| 208 | + |
| 209 | + def ok_execute(self, job_id): # noqa: ARG001 |
| 210 | + return 0, "", "retry-ok.mp4" |
| 211 | + |
| 212 | + monkeypatch.setattr("tiklocal.services.downloader.DownloadManager._execute_download", ok_execute) |
| 213 | + retry_res = client.post(f"/api/download/jobs/{failed_job['id']}/retry") |
| 214 | + retry_data = retry_res.get_json() |
| 215 | + assert retry_res.status_code == 200 |
| 216 | + new_job_id = retry_data["data"]["job"]["id"] |
| 217 | + assert new_job_id != failed_job["id"] |
| 218 | + assert retry_data["data"]["job"]["retry_of"] == failed_job["id"] |
| 219 | + final = _wait_for_job(client, new_job_id) |
| 220 | + assert final["status"] == "success" |
| 221 | + |
| 222 | + |
| 223 | +def test_delete_and_clear_history(client): |
| 224 | + res1 = client.post("/api/download/jobs", json={"url": "https://example.com/a"}) |
| 225 | + res2 = client.post("/api/download/jobs", json={"url": "https://example.com/b"}) |
| 226 | + j1 = _wait_for_job(client, res1.get_json()["data"]["job"]["id"]) |
| 227 | + _wait_for_job(client, res2.get_json()["data"]["job"]["id"]) |
| 228 | + |
| 229 | + del_res = client.delete(f"/api/download/jobs/{j1['id']}") |
| 230 | + del_data = del_res.get_json() |
| 231 | + assert del_res.status_code == 200 |
| 232 | + assert del_data["success"] is True |
| 233 | + |
| 234 | + check_res = client.get(f"/api/download/jobs/{j1['id']}") |
| 235 | + assert check_res.status_code == 404 |
| 236 | + |
| 237 | + clear_res = client.post("/api/download/jobs/clear") |
| 238 | + clear_data = clear_res.get_json() |
| 239 | + assert clear_res.status_code == 200 |
| 240 | + assert clear_data["success"] is True |
| 241 | + assert clear_data["data"]["deleted"] >= 0 |
0 commit comments