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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#!/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()
|