aboutsummaryrefslogtreecommitdiff
path: root/tests.py
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-05 13:24:22 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-05 13:24:22 -0600
commita3660adb90fd7c488f9d7baa0350bb4e2301ccb9 (patch)
treef4c9566bf1ec923c31b09b4b65b4bfb1943dc82f /tests.py
parente4ca47cc71bb64d724db3479406d381edc9661bc (diff)
Add tests and test data
Diffstat (limited to 'tests.py')
-rwxr-xr-xtests.py112
1 files changed, 112 insertions, 0 deletions
diff --git a/tests.py b/tests.py
new file mode 100755
index 0000000..384c304
--- /dev/null
+++ b/tests.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+
+import json
+import sys
+
+import requests
+
+BASE_URL = "http://localhost:8888"
+POST_UUID = "2dc42f26-884e-4b10-abfb-4fd394aa9d62"
+BAD_UUID = "00000000-0000-1111-0000-000000000000"
+INVALID_UUID = "0"
+
+
+def test_redirect():
+ url = f"{BASE_URL}/asdf"
+ r = requests.get(url, allow_redirects=False)
+
+ assert r.status_code == 301
+ 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_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={"Accept": "application/json"})
+ 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={"Accept": "application/json"})
+ 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_get_post_not_found():
+ url = f"{BASE_URL}/posts/{BAD_UUID}"
+ r = requests.get(url, headers={"Accept": "application/json"})
+ assert r.status_code == 404
+ j = r.json()
+
+ assert "Resource not found" in j
+
+ url = f"{BASE_URL}/posts/{INVALID_UUID}"
+ r = requests.get(url, headers={"Accept": "application/json"})
+ assert r.status_code == 404
+ j = r.json()
+
+ assert "Resource not found" in j
+
+
+
+def main():
+ for func in dir(sys.modules[__name__]):
+ if func.startswith("test_"):
+ globals()[func]()
+
+ print("🏆 You're winner !")
+
+
+if __name__ == "__main__":
+ main()