"""POST /v1/check.""" from __future__ import annotations import pytest from conftest import error_of def check(client, media, name: str, target: str): return client.post("/v1/check", files={"file": (name, media[name].read_bytes())}, data={"target": target}) def test_photo_of_the_wrong_size_fails_with_the_checks_that_failed(client, media): response = check(client, media, "team.jpg", "instagram_feed") assert response.status_code == 200, response.text body = response.json() assert (body["file"], body["kind"], body["target"], body["pass"]) == ("team.jpg", "image", "instagram_feed", False) by_id = {item["id"]: item for item in body["checks"]} assert (by_id["image.width"]["measured"], by_id["image.width"]["expected"]) == (1200, 1080) assert all(item["status"] in ("fail", "advisory") and item["message"] for item in body["checks"]) assert set(body["checks"][0]) == {"id", "status", "hard", "measured", "expected", "message", "source_url", "checked_on"} assert str(client.app.state.store._scratch_dir) not in response.text def test_photo_that_fits_passes(client, tmp_path): from PIL import Image path = tmp_path / "fits.jpg" Image.new("RGB", (1080, 1350), (200, 180, 150)).save(path, quality=90) body = client.post("/v1/check", files={"file": ("fits.jpg", path.read_bytes())}, data={"target": "instagram_feed"}).json() assert body["pass"] is True and not any(item["hard"] and item["status"] == "fail" for item in body["checks"]) def test_video_is_checked_against_a_video_target(client, media): body = check(client, media, "clip.mp4", "tiktok").json() assert (body["file"], body["kind"], body["target"]) == ("clip.mp4", "video", "tiktok") assert isinstance(body["pass"], bool) and isinstance(body["checks"], list) def test_nothing_is_kept_after_a_check(client, media): check(client, media, "team.jpg", "x_image") assert list(client.app.state.store._scratch_dir.iterdir()) == [] def test_unknown_target_and_wrong_kind_are_refused(client, media): error_of(check(client, media, "team.jpg", "myspace_wall"), 422, "unknown_target") error_of(check(client, media, "team.jpg", "instagram"), 422, "unknown_target") error = error_of(check(client, media, "team.jpg", "tiktok"), 422, "wrong_media_kind") assert "tiktok takes videos" in error["message"] error_of(check(client, media, "clip.mp4", "x_image"), 422, "wrong_media_kind") assert list(client.app.state.store._scratch_dir.iterdir()) == [] @pytest.mark.parametrize(("content", "status", "code"), [ (b"#EXTM3U\n#EXTINF:1,\nfile:///etc/passwd\n", 415, "unsupported_type"), (b'', 415, "unsupported_type"), (b"\x89PNG\r\n\x1a\n" + b"\x00" * 64, 422, "unreadable_file"), ]) def test_files_that_are_not_media_are_refused(client, content, status, code): error_of(client.post("/v1/check", files={"file": ("a.png", content)}, data={"target": "x_image"}), status, code) def test_missing_file_or_target_is_refused(client, media): error_of(client.post("/v1/check", data={"target": "x_image"}), 422, "invalid_request") error_of(client.post("/v1/check", files={"file": ("team.jpg", media["team.jpg"].read_bytes())}), 422, "invalid_request") def test_oversize_file_is_refused(make_client, media): error_of(check(make_client(photo_bytes=1000), media, "team.jpg", "x_image"), 413, "file_too_large") def test_check_is_turned_away_when_too_many_are_running(client, media, monkeypatch): store = client.app.state.store monkeypatch.setattr(store._checks, "acquire", lambda timeout: False) response = check(client, media, "team.jpg", "x_image") error_of(response, 503, "busy") assert response.headers["retry-after"] == "5" and list(store._scratch_dir.iterdir()) == [] def test_check_that_runs_too_long_is_stopped(make_client, media, monkeypatch): import subprocess import sys from app import jobs started = [] def slow(*args, **kwargs): started.append(subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"], start_new_session=True, **kwargs)) return started[-1] monkeypatch.setattr(jobs, "_spawn", slow) error_of(check(make_client(check_timeout_seconds=1), media, "team.jpg", "x_image"), 504, "check_timeout") assert started[0].poll() is not None