|
| 1 | +import datetime |
| 2 | +import filecmp |
| 3 | +from pathlib import Path |
| 4 | +from typing import Type, List, Optional |
| 5 | + |
| 6 | +resources = Path(__file__).parent / 'resources' |
| 7 | + |
| 8 | + |
| 9 | +def test_required_file(client): |
| 10 | + url = "/file/required" |
| 11 | + # Test that we receive a success response if a file is provided |
| 12 | + r = client.post(url, data={"v": (resources / "test.json").open("rb")}) |
| 13 | + assert "success" in r.json |
| 14 | + assert r.json["success"] |
| 15 | + # Test that we receive an error if a file is not provided |
| 16 | + r = client.post(url) |
| 17 | + assert "error" in r.json |
| 18 | + |
| 19 | + |
| 20 | +def test_optional_file(client): |
| 21 | + url = "/file/optional" |
| 22 | + # Test that we receive a success response if a file is provided |
| 23 | + r = client.post(url, data={"v": (resources / "test.json").open("rb")}) |
| 24 | + assert "success" in r.json |
| 25 | + assert r.json["success"] |
| 26 | + assert "file_provided" in r.json |
| 27 | + assert r.json["file_provided"] is True |
| 28 | + # Test that we receive an error if a file is not provided |
| 29 | + r = client.post(url) |
| 30 | + assert "success" in r.json |
| 31 | + assert r.json["success"] |
| 32 | + assert "file_provided" in r.json |
| 33 | + assert r.json["file_provided"] is False |
| 34 | + |
| 35 | + |
| 36 | +def test_file_content_types(client): |
| 37 | + url = "/file/content_types" |
| 38 | + # Test that we receive a success response if a file of correct Content-Type is provided |
| 39 | + r = client.post(url, data={"v": (resources / "test.json").open("rb")}) |
| 40 | + assert "success" in r.json |
| 41 | + assert r.json["success"] |
| 42 | + # Test that we receive an error if a file of incorrect Content-Type is provided |
| 43 | + r = client.post(url, data={"v": (resources / "hubble_mars_10kB.jpg").open("rb")}) |
| 44 | + assert "error" in r.json |
| 45 | + |
| 46 | + |
| 47 | +def test_file_min_length(client): |
| 48 | + url = "/file/min_length" |
| 49 | + # Test that we receive a success response if a file of correct Content-Length is provided |
| 50 | + load_path = resources / "hubble_mars_10kB.jpg" |
| 51 | + r = client.post(url, data={"v": load_path.open("rb")}) |
| 52 | + assert "success" in r.json |
| 53 | + assert r.json["success"] |
| 54 | + assert "save_path" in r.json |
| 55 | + assert filecmp.cmp(load_path, r.json["save_path"]) |
| 56 | + # Test that we receive an error if a file of incorrect Content-Length is provided |
| 57 | + r = client.post(url, data={"v": (resources / "test.json").open("rb")}) |
| 58 | + assert "error" in r.json |
| 59 | + |
| 60 | +def test_file_max_length(client): |
| 61 | + url = "/file/max_length" |
| 62 | + # Test that we receive a success response if a file of correct Content-Length is provided |
| 63 | + load_path = resources / "hubble_mars_10kB.jpg" |
| 64 | + r = client.post(url, data={"v": load_path.open("rb")}) |
| 65 | + assert "success" in r.json |
| 66 | + assert r.json["success"] |
| 67 | + assert "save_path" in r.json |
| 68 | + assert filecmp.cmp(load_path, r.json["save_path"]) |
| 69 | + # Test that we receive an error if a file of incorrect Content-Length is provided |
| 70 | + r = client.post(url, data={"v": (resources / "aldrin_47kB.jpg").open("rb")}) |
| 71 | + assert "error" in r.json |
0 commit comments