diff options
Diffstat (limited to 'tests.py')
-rwxr-xr-x | tests.py | 142 |
1 files changed, 104 insertions, 38 deletions
@@ -2,19 +2,17 @@ import logging import sys import uuid -from typing import Dict, List, Optional import requests BASE_URL = "http://localhost:8888" POSTS_URL = f"{BASE_URL}/posts" - -DELETE_UUID = "00000000-0000-dddd-0000-000000000000" -POST_UUID = "6d011855-6b0d-4202-a243-4d9db9807f14" +FONT_RENDERING_POST_UUID = "6d011855-6b0d-4202-a243-4d9db9807f14" BAD_UUID = "00000000-0000-1111-0000-000000000000" INVALID_UUID = "0" +# FIXME: Should only support x-www-form-urlencoded going forward JSON_ACCEPT_HEADER = {"Accept": "application/json"} log = logging.getLogger(__name__) @@ -22,60 +20,128 @@ log.setLevel(logging.INFO) def test_redirect(): - url = f"{BASE_URL}/asdf" - r = requests.get(url, allow_redirects=False) - + r = requests.get(f"{BASE_URL}/asdf", allow_redirects=False) assert r.status_code == 307 assert r.headers["Location"] == "/" -def test_html_get_post(): - url = f"{POSTS_URL}/{POST_UUID}" - r = requests.get(url) +def test_get_post(): + r = requests.get(f"{POSTS_URL}/{FONT_RENDERING_POST_UUID}") assert r.status_code == 200 - text = r.text - assert "A Solid Breakdown of the Linux Font Rendering Stack" in text - assert "hinting and anti-aliasing" in text + assert "A Solid Breakdown of the Linux Font Rendering Stack" in r.text + assert "hinting and anti-aliasing" in r.text - assert "Debugging CGI / CGit" not in text + assert "Debugging CGI / CGit" not in r.text -def test_html_get_base_url(): - url = BASE_URL - r = requests.get(url) +def test_get_base_url(): + r = requests.get(BASE_URL) assert r.status_code == 200 - text = r.text - assert "A Solid Breakdown of the Linux Font Rendering Stack" in text - assert "hinting and anti-aliasing" in text + assert "A Solid Breakdown of the Linux Font Rendering Stack" in r.text + assert "hinting and anti-aliasing" in r.text - assert "Debugging CGI / CGit" in text + assert "Debugging CGI / CGit" in r.text -def test_html_get_posts(): - url = POSTS_URL - r = requests.get(url) +def test_get_posts(): + r = requests.get(POSTS_URL) assert r.status_code == 200 - text = r.text - assert "A Solid Breakdown of the Linux Font Rendering Stack" in text - assert "hinting and anti-aliasing" in text + assert "A Solid Breakdown of the Linux Font Rendering Stack" in r.text + assert "hinting and anti-aliasing" in r.text - assert "Debugging CGI / CGit" in text + assert "Debugging CGI / CGit" in r.text -def test_html_get_post_not_found(): - url = f"{POSTS_URL}/{BAD_UUID}" - r = requests.get(url) +def test_get_post_not_found(): + r = requests.get(f"{POSTS_URL}/{BAD_UUID}") assert r.status_code == 404 - text = r.text + assert "Resource not found" in r.text - assert "Resource not found" in text - - url = f"{POSTS_URL}/{INVALID_UUID}" - r = requests.get(url) + r = requests.get(f"{POSTS_URL}/{INVALID_UUID}") assert r.status_code == 404 - text = r.text + assert "Resource not found" in r.text + + +def test_post_posts(): + uuid_ = str(uuid.uuid4()) + title = "Moby Dick" + body = "Call me Ismael" + data = {"id": uuid_, "title": title, "body": body} + r = requests.post(POSTS_URL, headers=JSON_ACCEPT_HEADER, json=data) + assert r.status_code == 201 + assert "Resource created successfully." in r.text + + posts_uuid_url = f"{POSTS_URL}/{uuid_}" + r = requests.get(posts_uuid_url) + assert r.status_code == 200 + assert title in r.text + assert body in r.text - assert "Resource not found" in text + r = requests.delete(posts_uuid_url) + assert r.status_code == 200 + + +def test_put_posts(): + uuid_ = str(uuid.uuid4()) + r = requests.post( + POSTS_URL, + headers=JSON_ACCEPT_HEADER, + json={"id": uuid_, "title": "title", "body": "body"}, + ) + assert r.status_code == 201 + assert "Resource created successfully." in r.text + + post_uuid_url = f"{POSTS_URL}/{uuid_}" + r = requests.get(post_uuid_url) + assert r.status_code == 200 + assert "title" in r.text + assert "body" in r.text + + r = requests.put( + post_uuid_url, + headers=JSON_ACCEPT_HEADER, + json={"title": "title2", "body": "body2"}, + ) + assert r.status_code == 200 + assert "OK" in r.text + + r = requests.get(post_uuid_url) + assert r.status_code == 200 + # assert len(j) == 1 + assert "title2" in r.text + assert "body2" in r.text + + r = requests.delete(post_uuid_url) + assert r.status_code == 200 + assert "OK" in r.text + + +def test_delete_posts(): + uuid_ = str(uuid.uuid4()) + post_uuid_url = f"{POSTS_URL}/{uuid_}" + + # Delete POST if exists + r = requests.delete(post_uuid_url) + assert r.status_code in (200, 404) + + # FIXME: Still JSON + title = "Hackers" + body = "Hack the planet!" + r = requests.post( + POSTS_URL, + headers=JSON_ACCEPT_HEADER, + json={"id": uuid_, "title": title, "body": body}, + ) + assert r.status_code == 201, f"Unexpected status code: {r.status_code} == 201" + assert "Resource created successfully" in r.text + + r = requests.delete(post_uuid_url) + assert r.status_code == 200 + assert "OK" in r.text + + r = requests.get(post_uuid_url) + assert r.status_code == 404 + assert "Resource not found" in r.text def main(): |