-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_server.py
More file actions
executable file
·124 lines (105 loc) · 4.24 KB
/
test_server.py
File metadata and controls
executable file
·124 lines (105 loc) · 4.24 KB
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
#!/usr/bin/env python2
import server
import unittest
import uuid
import json
import validators
class ServerTestCase(unittest.TestCase):
'''test the actual server interaction'''
def setUp(self):
server.app.config['TESTING'] = True
self.app = server.app.test_client()
def _json_get(self, url):
result = self.app.get(url)
# let the next line blow up if JSON error
return json.loads(result.data)
def _json_post(self, url, data):
json_d = json.dumps(data)
result = self.app.post(url, data=json_d,
content_type="application/json")
# let the next line blow up if JSON error
return json.loads(result.data)
def test_get_root_json_len(self):
rootj = self._json_get('/')
self.assertEqual(len(rootj), 2)
def test_get_root_json_has_token(self):
rootj = self._json_get('/')
self.assertIn('token', rootj)
def test_get_root_json_valid_token(self):
rootj = self._json_get('/')
try:
uid = uuid.UUID(rootj['token']) # noqa
except ValueError:
self.fail("Invalid id for token")
def test_first_url(self):
rootj = self._json_get('/')
rootj.pop('token')
key, url = rootj.popitem()
self.assertTrue(validators.url(url, require_tld=False))
def test_post_2nd_step(self):
rootj = self._json_get('/')
token = rootj.pop('token')
key, url = rootj.popitem()
secondj = self._json_post(url, data={'token': token})
self.assertIn('token', secondj)
secondj.pop('token')
key, url = secondj.popitem()
self.assertTrue(validators.url(url, require_tld=False))
def test_all_steps(self):
rootj = self._json_get('/')
token = rootj.pop('token')
key, url = rootj.popitem()
while url:
nextj = self._json_post(url, data={'token': token})
if 'answer' in nextj: # we found the last item
break
self.assertIn('token', nextj)
nextj.pop('token')
key, url = nextj.popitem()
self.assertTrue(validators.url(url, require_tld=False))
def test_http_methods_root(self):
response = self.app.post('/')
self.assertEquals(response.status_code, 405)
response = self.app.put('/')
self.assertEquals(response.status_code, 405)
response = self.app.delete('/')
self.assertEquals(response.status_code, 405)
response = self.app.options('/')
self.assertEquals(response.status_code, 200)
response = self.app.head('/')
self.assertEquals(response.status_code, 200)
response = self.app.get('/')
self.assertEquals(response.status_code, 200)
def test_http_methods_notoken_step(self):
response = self.app.get('/steps/abc')
self.assertEquals(response.status_code, 405)
response = self.app.put('/steps/abc')
self.assertEquals(response.status_code, 405)
response = self.app.delete('/steps/abc')
self.assertEquals(response.status_code, 405)
response = self.app.options('/steps/abc')
self.assertEquals(response.status_code, 200)
response = self.app.head('/steps/abc')
self.assertEquals(response.status_code, 405)
def test_post_no_data(self):
response = self.app.post('/steps/abc')
self.assertEquals(response.status_code, 400)
def test_post_invalid_content_type(self):
response = self.app.post('/steps/abc', data={'token': '123'})
self.assertEquals(response.status_code, 400)
def test_post_valid_token_invalid_ct(self):
rootj = self._json_get('/')
token = rootj.pop('token')
key, url = rootj.popitem()
result = self.app.post(url, data={'token': token})
self.assertEquals(result.status_code, 400)
def test_invalid_json_req(self):
rootj = self._json_get('/')
token = rootj.pop('token')
key, url = rootj.popitem()
json_d = json.dumps({'nonsense': token})
result = self.app.post(url, data=json_d,
content_type="application/json")
self.assertEquals(result.status_code, 400)
if __name__ == '__main__':
unittest.main()