#!/usr/bin/env python3 import logging import sys import uuid import requests BASE_URL = "http://localhost:8888" POSTS_URL = f"{BASE_URL}/posts" 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__) log.setLevel(logging.INFO) def test_redirect(): r = requests.get(f"{BASE_URL}/asdf", allow_redirects=False) assert r.status_code == 307 assert r.headers["Location"] == "/" def test_get_post(): r = requests.get(f"{POSTS_URL}/{FONT_RENDERING_POST_UUID}") assert r.status_code == 200 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 r.text def test_get_base_url(): r = requests.get(BASE_URL) assert r.status_code == 200 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 r.text def test_get_posts(): r = requests.get(POSTS_URL) assert r.status_code == 200 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 r.text def test_get_post_not_found(): r = requests.get(f"{POSTS_URL}/{BAD_UUID}") assert r.status_code == 404 assert "Resource not found" in r.text r = requests.get(f"{POSTS_URL}/{INVALID_UUID}") assert r.status_code == 404 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 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(): winner = True for func in dir(sys.modules[__name__]): if func.startswith("test_"): try: globals()[func]() except Exception as e: winner = False log.exception(f"🚫 {func} failed {e} 🚫") print(f"🚫 {func} failed {e} 🚫") if winner: log.info("🏆 You're winner !") print("🏆 You're winner !") if __name__ == "__main__": main()