-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,64 @@ | ||
import json | ||
from fastapi.testclient import TestClient | ||
|
||
from muckraker.main import app | ||
|
||
# TODO: get rid off explicit header setting. I currently have to do that | ||
# as POST /issue/ recieves a complicated multipart data. Maybe it would | ||
# be better to move image uploading to another endpoint | ||
FORM_HEADERS = {'Content-Type': 'application/x-www-form-urlencoded'} | ||
# TODO: add image processing tests. For now TestClient doesn't send them, | ||
# might be due to a mixed multipart data. | ||
|
||
client = TestClient(app) | ||
|
||
|
||
def test_issue_correct(issue_dict): | ||
content = "issue=" + json.dumps(issue_dict) | ||
resp = client.post( | ||
"/issue/", | ||
content=content, | ||
headers=FORM_HEADERS | ||
) | ||
def test_correct(issue_dict, good_image): | ||
resp = client.post("/issue/", json=issue_dict) | ||
assert resp.status_code == 200 | ||
issue_id = resp.json()["issue_id"] | ||
|
||
files = {"image": good_image} | ||
resp = client.patch(f"/issue/{issue_id}", files=files) | ||
assert resp.status_code == 200 | ||
|
||
resp = client.get(f"/issue/{issue_id}") # Deletes tempdir | ||
assert resp.status_code == 200 | ||
with open("file.pdf", "wb") as fd: | ||
fd.write(resp.content) | ||
|
||
def test_issue_thick_body(issue_dict): | ||
issue_dict["body"] = issue_dict["body"] * 100 | ||
content = "issue=" + json.dumps(issue_dict) | ||
resp = client.post("/issue/", content=content, headers=FORM_HEADERS) | ||
|
||
def test_thick_body(issue_dict): | ||
# Won't create tempdir | ||
issue_dict["body"] = "a" * 6001 | ||
resp = client.post("/issue/", json=issue_dict) | ||
assert resp.status_code == 422 | ||
detail = resp.json()["detail"][0] | ||
assert detail["type"] == "string_too_long" | ||
assert detail["loc"] == ["body", "body"] | ||
|
||
|
||
def test_issue_thick_heading(issue_dict): | ||
def test_thick_heading_field(issue_dict): | ||
# Won't create tempdir | ||
any_field = list(issue_dict["heading"].keys())[0] | ||
issue_dict["heading"][any_field] = "a" * 100 | ||
content = "issue=" + json.dumps(issue_dict) | ||
resp = client.post("/issue/", content=content, headers=FORM_HEADERS) | ||
issue_dict["heading"][any_field] = "a" * 51 | ||
resp = client.post("/issue/", json=issue_dict) | ||
assert resp.status_code == 422 | ||
detail = resp.json()["detail"][0] | ||
assert detail["type"] == "string_too_long" | ||
assert detail["loc"] == ["body", "issue", "heading", any_field] | ||
assert detail["loc"] == ["body", "heading", any_field] | ||
|
||
|
||
def test_thick_image(issue_dict, thick_image): | ||
resp = client.post("/issue/", json=issue_dict) | ||
issue_id = resp.json()["issue_id"] | ||
|
||
files = {"image": thick_image} | ||
# Deletes tempdir on 413 | ||
resp = client.patch(f"/issue/{issue_id}", files=files) | ||
assert resp.status_code == 413 | ||
|
||
|
||
def test_issue_not_found(issue_dict, good_image): | ||
resp = client.post("/issue/", json=issue_dict) | ||
issue_id = resp.json()["issue_id"] # Deletes tempdir | ||
client.get(f"/issue/{issue_id}") | ||
|
||
resp = client.get(f"/issue/{issue_id}") | ||
assert resp.status_code == 404 | ||
|
||
files = {"image": good_image} | ||
resp = client.patch(f"/issue/{issue_id}", files=files) | ||
assert resp.status_code == 404 |