forked from mtnbarreto/flask-base-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_users.py
More file actions
241 lines (215 loc) · 10.8 KB
/
test_users.py
File metadata and controls
241 lines (215 loc) · 10.8 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
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
# project/tests/test_users.py
import json
import datetime
from project.api.common.utils.constants import Constants
from tests.base import BaseTestCase
from project.models.user import UserRole
from tests.utils import add_user
class TestUsersBlueprint(BaseTestCase):
"""Tests for the Users Service."""
def test_users(self):
"""Ensure the /ping route behaves correctly."""
response = self.client.get('/v1/ping', headers=[('Accept', 'application/json')])
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertIn('pong!', data['message'])
self.assertIn('success', data['status'])
def test_add_user(self):
"""Ensure a new user can be added to the database."""
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.post(
'/v1/users',
data=json.dumps(dict(
username='michael',
email='michael@realpython.com',
password='password'
)),
content_type='application/json',
headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])]
)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode())
self.assertIn('michael@realpython.com was added!', data['message'])
self.assertIn('success', data['status'])
def test_add_user_invalid_json(self):
"""Ensure error is thrown if the JSON object is empty."""
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.post(
'/v1/users',
data=json.dumps(dict()),
content_type='application/json',
headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])]
)
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 400)
self.assertIn('Invalid payload.', data['message'])
self.assertIn('error', data['status'])
def test_add_user_invalid_json_keys(self):
"""Ensure error is thrown if the JSON object does not have a username key."""
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.post(
'/v1/users',
data=json.dumps(dict(email='michael@realpython.com', password='password')),
content_type='application/json',
headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])]
)
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 400)
self.assertIn('Invalid payload.', data['message'])
self.assertIn('error', data['status'])
def test_add_user_duplicate_user(self):
"""Ensure error is thrown if the email already exists."""
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
self.client.post(
'/v1/users',
data=json.dumps(dict(
username='michael',
email='michael@realpython.com',
password='password'
)),
content_type='application/json',
headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])]
)
response = self.client.post(
'/v1/users',
data=json.dumps(dict(
username='michael',
email='michael@realpython.com',
password='password'
)),
content_type='application/json',
headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])]
)
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 400)
self.assertIn(
'Sorry. That email or username already exists.', data['message'])
self.assertIn('error', data['status'])
def test_single_user(self):
"""Ensure get single user behaves correctly."""
user = add_user('michael', 'michael@realpython.com', 'password', roles=UserRole.BACKEND_ADMIN)
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.get(f'/v1/users/{user.id}', headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])])
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertTrue('created_at' in data['data'])
self.assertIn('michael', data['data']['username'])
self.assertIn('michael@realpython.com', data['data']['email'])
self.assertIn('success', data['status'])
def test_single_user_no_id(self):
"""Ensure error is thrown if an id is not provided."""
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.get('/v1/users/blah', headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])])
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 404)
self.assertIn('User does not exist.', data['message'])
self.assertIn('error', data['status'])
def test_single_user_incorrect_id(self):
"""Ensure error is thrown if the id does not exist."""
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.get('/v1/users/999', headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])])
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 404)
self.assertIn('User does not exist.', data['message'])
self.assertIn('error', data['status'])
def test_all_users(self):
"""Ensure get all users behaves correctly."""
created = datetime.datetime.utcnow() + datetime.timedelta(-30)
add_user('michael', 'michael@realpython.com', 'password', created_at=created)
add_user('fletcher', 'fletcher@realpython.com', 'password')
created = created + datetime.timedelta(-30)
add_user('test', 'test@test.com', 'test', roles=UserRole.BACKEND_ADMIN, created_at=created)
with self.client:
resp_login = self.client.post(
'/v1/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='test'
)),
content_type='application/json',
headers=[('Accept', 'application/json')]
)
response = self.client.get('/v1/users', headers=[('Accept', 'application/json'), (Constants.HttpHeaders.AUTHORIZATION, 'Bearer ' + json.loads(resp_login.data.decode())['auth_token'])])
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertEqual(len(data['data']['users']), 3)
self.assertTrue('created_at' in data['data']['users'][0])
self.assertTrue('created_at' in data['data']['users'][1])
self.assertTrue('created_at' in data['data']['users'][2])
self.assertIn('test', data['data']['users'][2]['username'])
self.assertIn('test@test.com', data['data']['users'][2]['email'])
self.assertIn('michael', data['data']['users'][1]['username'])
self.assertIn('michael@realpython.com', data['data']['users'][1]['email'])
self.assertIn('fletcher', data['data']['users'][0]['username'])
self.assertIn('fletcher@realpython.com', data['data']['users'][0]['email'])
self.assertIn('success', data['status'])