-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.py
123 lines (98 loc) · 3.7 KB
/
server_test.py
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
import os
import tempfile
import unittest
from server import app
class ServerTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, app.config["DATABASE"] = tempfile.mkstemp()
self.client = app.test_client()
def tearDown(self):
os.close(self.db_fd)
os.unlink(app.config["DATABASE"])
def test_flow(self):
data = {
"one": 1,
"truthy": True,
"a string": "yes, a string 🎉",
}
KEY = "my/test/key1"
PATH = f"/{KEY}"
# it should not previously exist
res = self.client.get(PATH)
self.assertEqual(res.status_code, 404)
res_json = res.get_json()
self.assertEqual(res_json, {})
# deleting should be 404
res = self.client.delete(PATH)
self.assertEqual(res.status_code, 404)
res_json = res.get_json()
self.assertEqual(res_json, {})
# now we create it
res = self.client.post(PATH, json=data)
self.assertEqual(res.status_code, 200)
res_json = res.get_json()
self.assertEqual(res_json["one"], 1)
# now it should exist
res = self.client.get(PATH)
self.assertEqual(res.status_code, 200)
res_json = res.get_json()
self.assertEqual(res_json["a string"], "yes, a string 🎉")
# and delete it
res = self.client.delete(PATH)
self.assertEqual(res.status_code, 200)
res_json = res.get_json()
self.assertEqual(res_json, {})
# now it should no longer exist
res = self.client.get(PATH)
self.assertEqual(res.status_code, 404)
res_json = res.get_json()
self.assertEqual(res_json, {})
def test_updates(self):
data = {"number": 1}
KEY = "my/test/key2"
PATH = f"/{KEY}"
# check that it doesn't exist
res = self.client.get(PATH)
self.assertEqual(res.status_code, 404)
# now create it
res = self.client.post(PATH, json=data)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.get_json(), data)
# check that we can retrieve it correctly
res = self.client.get(PATH)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.get_json(), data)
# now post to it again
data = {"number": 42}
res = self.client.post(PATH, json=data)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.get_json(), data)
# now post to it yet again
data = {"number": 123}
res = self.client.post(PATH, json=data)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.get_json(), data)
# check that it has actually been updated
res = self.client.get(PATH)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.get_json(), data)
def test_invalid_input(self):
# not json
res = self.client.post("/test/key/invalid", data="{XJDE(&*@(CENTHUO")
self.assertEqual(res.status_code, 400)
# the base url is not allowed to be posted to
res = self.client.post("/", json={"hello": True})
self.assertEqual(res.status_code, 405)
def test_api_spec_endpoints(self):
res = self.client.get("/api/spec")
self.assertEqual(res.status_code, 200)
self.assertIn("version", res.get_json()["info"])
res = self.client.get("/docs/")
self.assertEqual(res.status_code, 200)
self.assertIn(b"swagger-ui", res.get_data())
# the base url redirects to the docs
res = self.client.get("/")
self.assertEqual(res.status_code, 302)
self.assertIn("/docs", res.location)
if __name__ == "__main__":
unittest.main()