aboutsummaryrefslogtreecommitdiff
path: root/tests.py
blob: 33c9f6dc0fb1c53d9b0aef17c0c63e6139cc779f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python

import json
import sys
import uuid

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_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={"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_post_posts():
    url = f"{BASE_URL}/posts"
    uuid_ = str(uuid.uuid4())
    data = {"id": uuid_, "title": "title", "body": "body"}
    r = requests.post(url, headers={"Accept": "application/json"}, json=data)
    # FIXME need to refactor to fix status code here.
    # assert r.status_code == 201
    assert r.status_code == 200
    j = r.json()

    assert len(j) == 1
    assert "title"  == j[0]["title"]
    assert "body" == j[0]["body"]

    url = f"{BASE_URL}/posts/{uuid_}"
    r = requests.get(url, headers={"Accept": "application/json"})
    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_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()