#!/usr/bin/env python3 import logging import sys import uuid import requests BASE_URL = "http://localhost:8888" ENTRIES_URL = f"{BASE_URL}/entries" FONT_RENDERING_ENTRY_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_entry(): r = requests.get(f"{ENTRIES_URL}/{FONT_RENDERING_ENTRY_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_entries(): r = requests.get(ENTRIES_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_entry_not_found(): r = requests.get(f"{ENTRIES_URL}/{BAD_UUID}") assert r.status_code == 404 assert "Resource not found" in r.text r = requests.get(f"{ENTRIES_URL}/{INVALID_UUID}") assert r.status_code == 404 assert "Resource not found" in r.text def test_post_entry(): uuid_ = str(uuid.uuid4()) title = "Moby Dick" body = "Call me Ismael" data = {"id": uuid_, "title": title, "body": body} r = requests.post(ENTRIES_URL, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 201 assert "Resource created successfully." in r.text entry_url = f"{ENTRIES_URL}/{uuid_}" r = requests.get(entry_url) assert r.status_code == 200 assert title in r.text assert body in r.text r = requests.delete(entry_url) assert r.status_code == 200 def test_put_entry(): uuid_ = str(uuid.uuid4()) r = requests.post( ENTRIES_URL, headers=JSON_ACCEPT_HEADER, json={"id": uuid_, "title": "title", "body": "body"}, ) assert r.status_code == 201 assert "Resource created successfully." in r.text entry_url = f"{ENTRIES_URL}/{uuid_}" r = requests.get(entry_url) assert r.status_code == 200 assert "title" in r.text assert "body" in r.text r = requests.put( entry_url, headers=JSON_ACCEPT_HEADER, json={"title": "title2", "body": "body2"}, ) assert r.status_code == 200 assert "OK" in r.text r = requests.get(entry_url) assert r.status_code == 200 # assert len(j) == 1 assert "title2" in r.text assert "body2" in r.text r = requests.delete(entry_url) assert r.status_code == 200 assert "OK" in r.text def test_delete_entry(): uuid_ = str(uuid.uuid4()) entry_url = f"{ENTRIES_URL}/{uuid_}" # Delete entry if exists r = requests.delete(entry_url) assert r.status_code in (200, 404) # FIXME: Still JSON title = "Hackers" body = "Hack the planet!" r = requests.post( ENTRIES_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(entry_url) assert r.status_code == 200 assert "OK" in r.text r = requests.get(entry_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()