forked from anwen/anwen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
199 lines (153 loc) · 5 KB
/
tests.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# to run tests, make: 'xsrf_cookies': False to be fixed
import uuid
import random
import unittest
import tornado.testing
from json import dumps as jdump
from json import loads as jload
import tornado.web
def random_str():
return uuid.uuid4().hex
def generate_args(*args, **kwargs):
dic = dict([(a, random_str()) for a in args])
dic.update(kwargs)
return dic
def random_args(d):
r = {}
for k, v in d.iteritems():
if isinstance(v, int):
v = v * random.randint(0, 10)
elif isinstance(v, unicode):
v = unicode(random_str())
elif isinstance(v, str):
v = random_str()
r[k] = v
return r
def assert_similar(d1, d2):
assert isinstance(d1, dict)
assert isinstance(d2, dict)
if d1 > d2:
bigger, smaller = d1, d2
else:
bigger, smaller = d2, d1
for k, v in smaller.iteritems():
assert bigger[k] == v
class HttpTest(tornado.testing.AsyncHTTPTestCase):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
create = True
clist = ''
cdict = {}
data = {}
def get_app(self):
from hello import application
return application
def setUp(self):
super(HttpTest, self).setUp()
getattr(self, 'set_up', lambda: None)()
if self.create:
res = self.fetch(
self.res_url, method='POST', body=self.jcreate_args)
assert res.code == 201 or 200
assert res.body
json = jload(res.body)
assert_similar(json, self.create_args)
try:
self.oid = json['_id']
except:
pass
def tearDown(self):
getattr(self, 'tear_down', lambda: None)()
if self.create:
res = self.fetch(self.res_url + self.oid, method='DELETE')
assert res.code == 200
res = self.fetch(self.res_url + self.oid, method='GET')
assert res.code == 404
super(HttpTest, self).tearDown()
def fetch_n(self, *args, **kwargs):
return super(HttpTest, self).fetch(*args, **kwargs)
def fetch(self, *args, **kwargs):
if 'headers' not in kwargs:
kwargs.update({'headers': {'Content-Type': 'application/json'}})
return super(HttpTest, self).fetch(*args, **kwargs)
def common_setup(self):
self.create_args = generate_args(
*self.clist, **self.cdict
)
self.jcreate_args = jdump(self.create_args)
updated = random_args(self.create_args)
try:
updated.update(self.udict)
except:
pass
self.update_args = updated
self.jupdate_args = jdump(self.update_args)
def common_update(self, url, method='PATCH'):
res = self.fetch(
url, method=method, body=jdump(self.update_args))
if method == 'PATCH':
assert res.code == 200
elif method == 'PUT':
assert res.code == 201
assert res.body
def common_read(self):
if hasattr(self, 'oid'):
res = self.fetch(self.res_url + self.oid, method='GET')
assert res.code == 200
assert isinstance(jload(res.body), dict)
if self.create:
assert_similar(jload(res.body), self.create_args)
res = self.fetch(self.res_url, method='GET')
assert res.code == 200
json = jload(res.body)
assert isinstance(json, dict)
return json
class TestIndex(HttpTest):
create = False
def test_read(self):
res = self.fetch('/', method='GET')
assert res.code == 200
assert res.body
class TestUsers(HttpTest):
res_url = '/users/'
clist = 'user_email', 'user_pass'
def set_up(self):
res = self.fetch('/joinus', method='GET')
assert res.code == 200
res = self.fetch('/login', method='GET')
assert res.code == 200
assert 'Set-Cookie' in res.headers
# assert res.headers['Set-Cookie'].startswith('_xsrf=')
# _xsrf = res.headers['Set-Cookie'].split(';')[0].split('=')[1]
assert res.body
common_setup(self)
def tear_down(self):
pass
def test_read(self):
common_read(self)
def test_update(self):
common_update(self, self.res_url + self.oid)
class TestShares(HttpTest):
res_url = '/shares/'
clist = 'markdown', 'title'
def set_up(self):
res = self.fetch('/share', method='GET')
assert res.code == 200
assert res.body
common_setup(self)
def tear_down(self):
pass
def test_read(self):
common_read(self)
def test_update(self):
common_update(self, self.res_url + self.oid)
TEST_MODULES = [
'tests',
]
def all():
return unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULES)
def main():
tornado.testing.main()
if __name__ == '__main__':
main()