#!/usr/bin/env python3 import json import logging import sys import uuid import requests BASE_URL = "http://localhost:8888" DELETE_UUID = "00000000-0000-dddd-0000-000000000000" POST_UUID = "6d011855-6b0d-4202-a243-4d9db9807f14" BAD_UUID = "00000000-0000-1111-0000-000000000000" INVALID_UUID = "0" JSON_ACCEPT_HEADER = {"Accept": "application/json"} log = logging.getLogger(__name__) log.setLevel(logging.INFO) # def test_health(): # url = f"{BASE_URL}/health" # r = requests.get(url) # assert r.status_code == 200 # text = r.text # assert "OK" in text def test_redirect(): url = f"{BASE_URL}/asdf" r = requests.get(url, allow_redirects=False) assert r.status_code == 307 assert r.headers["Location"] == "/" def test_html_get_post(): url = f"{BASE_URL}/posts/{POST_UUID}" r = requests.get(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 "Debugging CGI / CGit" not in text def test_html_get_base_url(): url = BASE_URL r = requests.get(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 "Debugging CGI / CGit" in text def test_html_get_posts(): url = f"{BASE_URL}/posts" r = requests.get(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 "Debugging CGI / CGit" in text def test_html_get_post_not_found(): url = f"{BASE_URL}/posts/{BAD_UUID}" r = requests.get(url) assert r.status_code == 404 text = r.text assert "Resource not found" in text url = f"{BASE_URL}/posts/{INVALID_UUID}" r = requests.get(url) assert r.status_code == 404 text = r.text assert "Resource not found" in text def test_json_get_post(): url = f"{BASE_URL}/posts/{POST_UUID}" r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() post = j[0] assert len(j) == 1 assert "A Solid Breakdown of the Linux Font Rendering Stack" == post["title"] assert "hinting and anti-aliasing" in post["body"] def test_json_get_posts(): url = f"{BASE_URL}/posts" r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() assert len(j) >= 1 text = json.dumps(j) assert "A Solid Breakdown of the Linux Font Rendering Stack" in text assert "hinting and anti-aliasing" in text assert "Debugging CGI / CGit" in text def test_json_post_posts(): url = f"{BASE_URL}/posts" uuid_ = str(uuid.uuid4()) data = {"id": uuid_, "title": "title", "body": "body"} r = requests.post(url, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 201 j = r.json() assert "Resource created successfully." == j url = f"{BASE_URL}/posts/{uuid_}" r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() assert len(j) == 1 assert "title" == j[0]["title"] assert "body" == j[0]["body"] def test_json_put_posts(): url = f"{BASE_URL}/posts" uuid_ = str(uuid.uuid4()) data = {"id": uuid_, "title": "title", "body": "body"} r = requests.post(url, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 201 j = r.json() assert "Resource created successfully." == j url = f"{BASE_URL}/posts/{uuid_}" r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() assert len(j) == 1 assert "title" == j[0]["title"] assert "body" == j[0]["body"] data = {"title": "title2", "body": "body2"} r = requests.put(url, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 200 j = r.json() assert "OK" == j url = f"{BASE_URL}/posts/{uuid_}" r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() assert len(j) == 1 assert "title2" == j[0]["title"] assert "body2" == j[0]["body"] def test_json_delete_posts(): url = f"{BASE_URL}/posts" uuid_ = DELETE_UUID data = {"id": uuid_, "title": "title", "body": "body"} # Create post copy-pasta. r = requests.post(url, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 201 j = r.json() url = f"{BASE_URL}/posts/{uuid_}" r = requests.delete(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 404 j = r.json() def test_json_get_post_not_found(): url = f"{BASE_URL}/posts/{BAD_UUID}" r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 404 j = r.json() assert "Resource not found" in j url = f"{BASE_URL}/posts/{INVALID_UUID}" r = requests.delete(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 404 j = r.json() assert "Resource not found" in j def main(): winner = True for func in dir(sys.modules[__name__]): if func.startswith("test_"): try: globals()[func]() except Exception: winner = False log.exception(f"🚫 {func} failed 🚫🏆") print(f"🚫 {func} failed 🚫🏆") if winner: log.info("🏆 You're winner !") print("🏆 You're winner !") if __name__ == "__main__": main()