forked from vovantics/flask-bluebone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
322 lines (279 loc) · 11.7 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
import json
import datetime
from flask import current_app
from flask.ext.testing import (TestCase as Base, Twill)
from app import create_app
from app.user import User, UserDetail, ADMIN, USER, ACTIVE
from config import TestConfig
from app.extensions import db, mail
class TestCase(Base):
"""Base TestClass for your application."""
ENVIRON_BASE = {
'HTTP_USER_AGENT': 'Chrome',
'REMOTE_ADDR': '127.0.0.1',
'HTTP_ORIGIN': 'http://localhost:9000'
}
def create_app(self):
"""Create and return a testing flask app."""
app = create_app(TestConfig)
self.twill = Twill(app, port=3000)
return app
def init_data(self):
demo = User(
username=u'demo',
email='demo@example.com',
password='default',
role_id=USER,
status_id=ACTIVE,
user_detail=UserDetail(
first_name=u'Demo',
last_name=u'Dude',
gender=u'female',
dob=datetime.date(1985, 01, 17),
phone=1234567890,
bio=u'Demo dude is pretty sweet.',
url=u'http://www.example.com/demo',
),
)
admin = User(
username=u'admin',
email='admin@example.com',
password='default',
role_id=ADMIN,
status_id=ACTIVE,
user_detail=UserDetail(
first_name=u'Admin',
last_name=u'Dude',
gender=u'male',
dob=datetime.date(1985, 01, 17),
phone=1234567890,
bio=u'Admin dude is the administrator.',
url=u'http://www.example.com/admin',
),
)
db.session.add(demo)
db.session.add(admin)
db.session.commit()
assert demo.id is not None
assert admin.id is not None
#self.user = user TODO
def setUp(self):
"""Reset all tables before testing."""
db.create_all()
self.init_data()
def tearDown(self):
"""Clean db session and drop all tables."""
db.session.remove()
db.drop_all()
def login(self, email, password, remember=False):
"""Helper function to login"""
rv = self.client.post('/session/',
data={
'email': email,
'password': password,
'remember': remember
},
follow_redirects=True,
environ_base=self.ENVIRON_BASE
)
self.assert_200(rv)
assert 'success' in rv.data
return rv
def logout(self):
"""Helper function to logout"""
rv = self.client.delete('/session/', follow_redirects=True,
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
return rv
def _test_get_request(self, endpoint, template=None):
rv = self.client.get(endpoint)
self.assert_200(rv)
return rv
class TestUser(TestCase):
def test_delete_activate(self):
# User logs in and deactivates account
self.login(email='demo@example.com', password='default')
active_user = User.query.filter_by(email='demo@example.com').first()
assert active_user.is_active() is True
rv = self.client.delete('/users/%d/' % 1,
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
deactivated_user = User.query.filter_by(email='demo@example.com').first()
assert deactivated_user.is_active() is False
self.logout()
# User logs in, initiating an activation
self.login(email='demo@example.com', password='default')
deactivated_user = User.query.filter_by(email='demo@example.com').first()
# User enters new password
data = {'status': 'active'}
rv = self.client.put(
'/users/activate/%s/%s/' % (deactivated_user.email, deactivated_user.activation_key),
data=json.dumps(data), content_type='application/json',
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
active_user = User.query.filter_by(email='demo@example.com').first()
assert active_user.is_active() is True
#self.assert_405(rv)
def test_get(self):
self.login(email='demo@example.com', password='default')
# Get a user.
rv = self.client.get('/users/%d/' % 1, environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
# Get list of users.
rv = self.client.get('/users/', environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
self.logout()
def test_register(self):
data = {
'username': 'member',
'email': 'member@example.com',
'password': 'default',
'password_again': 'default'
}
rv = self.client.post('/users/', data=json.dumps(data),
content_type='application/json',
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
new_user = User.query.filter_by(email=data['email']).first()
assert new_user is not None
def test_update_profile(self):
self.login(email='demo@example.com', password='default')
data = {
'email': 'demo@example.com',
}
user = User.query.filter_by(email=data.get('email')).first()
assert user is not None
data = {
'first_name': 'Bilbo',
'last_name': 'Baggins',
'username': user.username,
'email': user.email,
'gender': user.user_detail.gender,
'dob': user.get_dob(),
'phone': user.user_detail.phone,
'bio': user.user_detail.bio,
'url': user.user_detail.url
}
rv = self.client.put('/users/%d/' % user.id,
data=json.dumps(data),
content_type='application/json',
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
user = User.query.filter_by(email=data.get('email')).first()
assert user.user_detail.first_name == 'Bilbo'
assert user.user_detail.last_name == 'Baggins'
self.logout()
def test_password_reset(self):
# User enters email for account that DNE
data = {'email': 'missing@example.com'}
response = self.client.post('/users/password/reset/',
data=json.dumps(data),
content_type='application/json',
environ_base=self.ENVIRON_BASE)
assert 'Sorry, no user found for that email address.' in response.data
self.assert_200(response)
# User enters email and clicks 'Send instructions'
data = {'email': 'demo@example.com'}
user = User.query.filter_by(email=data.get('email')).first()
assert user is not None
assert user.activation_key is None
with mail.record_messages() as outbox:
# Application sends password reset email
response = self.client.post('/users/password/reset/',
data=json.dumps(data),
content_type='application/json',
environ_base=self.ENVIRON_BASE)
assert len(outbox) == 1
assert outbox[0].subject == "Recover your password"
user = User.query.filter_by(email=data.get('email')).first()
assert user.activation_key is not None
# User enters new password
data = {
'password': 'new password',
'password_again': 'new password',
}
user = User.query.filter_by(activation_key=user.activation_key) \
.filter_by(email=user.email).first()
assert user is not None
rv = self.client.put(
'/users/password/%s/%s/' % (user.email, user.activation_key),
data=json.dumps(data), content_type='application/json',
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
user = User.query.filter_by(email='demo@example.com').first()
assert user is not None
assert user.activation_key is None
assert user.check_password(data.get('password')) is True
def test_password_change(self):
# User logs in
self.login(email='demo@example.com', password='default')
# User enters new password
data = {
'password': 'new password',
'password_again': 'new password',
}
user = User.query.filter_by(email='demo@example.com').first()
assert user is not None
rv = self.client.put('/users/%d/' % user.id, data=json.dumps(data),
content_type='application/json',
environ_base=self.ENVIRON_BASE)
self.assert_200(rv)
assert 'success' in rv.data
user = User.query.filter_by(email='demo@example.com').first()
assert user is not None
assert user.check_password(data.get('password')) is True
class TestMeta(TestCase):
def test_contact(self):
# User fills in form and clicks 'Send Message'
data = {
'full_name': 'Troubled User',
'email': 'troubled.user@example.com',
'subject': 'Help me!',
'message': 'I have the blue screen of death! Call the doctor!',
}
with mail.record_messages() as outbox:
# Application sends email to admin
response = self.client.post('/mail/', data=json.dumps(data),
content_type='application/json',
environ_base=self.ENVIRON_BASE)
assert len(outbox) == 1
subject = '[%s] Message from %s: %s' % (current_app.config['APP_NAME'], data.get('full_name'), data.get('subject'))
assert outbox[0].subject == subject
self.assert_200(response)
# TODO assert "Thanks for your message. We'll get back to you shortly." in response.data
class TestErrors(TestCase):
def test_401(self):
response = self.client.get('/users/1/',
environ_base=self.ENVIRON_BASE)
self.assert_401(response)
def test_403(self):
response = self.client.get('/session/',
environ_base={
'HTTP_ORIGIN': 'http://malicious.com'
})
self.assert_403(response)
def test_404(self):
response = self.client.get('/i-am-not-found/',
environ_base=self.ENVIRON_BASE)
self.assert_404(response)
def test_405(self):
response = self.client.post('/', data={},
content_type='application/json',
environ_base=self.ENVIRON_BASE)
self.assert_405(response)
def test_400(self):
response = self.client.get('/session/',
environ_base={
'HTTP_ORIGINS': 'http://localhost:6666'
})
self.assert_400(response)
def test_static_text_file_request(self):
response = self.client.get('/robots.txt')
self.assert_200(response)