-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
executable file
·259 lines (204 loc) · 6.81 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
#!/usr/bin/env python
import unittest
import simplejson as json
from jsonschema import validate
import notify
from notify.models import Notification, User
TEST_NOTIFICATION = dict(
message='Checkout this cool new feature on the Balanced dashboard',
)
CREATE_SCHEMA = {
"type": "object",
"properties": {
"data": {"type": "string"}
},
"required": ["data"]
}
CREATE_MULTI_SCHEMA = {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": True
}
},
"required": ["data"]
}
GET_NO_NOTIFICATIONS_SCHEMA = {
"type": "object",
"properties": {
"data": {
"type": "array",
"maxItems": 0
}
},
"required": ["data"]
}
GET_NOTIFICATIONS_SCHEMA = {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"message": {"type": "string"},
"id": {"type": "string"}
},
"required": ["id", "message"]
},
"minItems": 1,
"uniqueItems": True
}
},
"required": ["data"]
}
GET_USERS_SCHEMA = {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"email": {"type": "string"},
"id": {"type": "string"}
},
"required": ["id", "email"]
},
"minItems": 1,
"uniqueItems": True
}
},
"required": ["data"]
}
class TestCase(unittest.TestCase):
def setUp(self):
app = notify.make_app()
self.app = app.test_client()
for fixture in [
{'email': '[email protected]'},
{'email': '[email protected]'}
]:
User(**fixture).save()
def tearDown(self):
Notification.objects.delete()
User.objects.delete()
def assertStatus(self, response, status_code):
"""
Helper method to check matching response status.
:param response: Flask response
:param status_code: response status code (e.g. 200)
"""
self.assertEqual(response.status_code, status_code)
def validateResponse(self, response, json_schema):
"""
Helper method to parse a response as json and validate it given a
json_schema
:param response: Flask response
:param json_schema: a json schema
"""
data = json.loads(response.data)
validate(data, json_schema)
return data
def test_create_notification(self):
res = self.app.post(
'/notifications',
data=TEST_NOTIFICATION,
headers={'x-balanced-admin': '1'})
data = self.validateResponse(res, CREATE_SCHEMA)
self.assertStatus(res, 201)
return data['data']
def test_create_notification_unauthorized(self):
res = self.app.post(
'/notifications',
data=TEST_NOTIFICATION)
self.assertStatus(res, 401)
def test_create_notification_not_admin_authorized(self):
res = self.app.post(
'/notifications',
data=TEST_NOTIFICATION,
headers={'x-balanced-user': USER_ID})
self.assertStatus(res, 401)
def test_create_multi_notification(self):
TEST_NOTIFICATION.pop('uid', None)
res = self.app.post(
'/notifications',
data=TEST_NOTIFICATION,
headers={'x-balanced-admin': '1'})
data = self.validateResponse(res, GET_NOTIFICATIONS_SCHEMA)
self.assertStatus(res, 201)
return data['data']
def test_get_notifications(self):
notification_id = self.test_create_notification()
res = self.app.get(
'/notifications',
headers={'x-balanced-user': USER_ID})
data = self.validateResponse(res, GET_NOTIFICATIONS_SCHEMA)
self.assertEqual(notification_id, data['data'][0]['id'])
self.assertEqual(
TEST_NOTIFICATION.get('message'),
data['data'][0]['message'])
self.assertStatus(res, 200)
def test_get_notifications_unauthorized(self):
notification_id = self.test_create_notification()
res = self.app.get(
'/notifications')
self.assertStatus(res, 401)
def test_delete_notifications(self):
notification_id = self.test_create_notification()
res = self.app.delete(
'/notifications/' + notification_id,
headers={'x-balanced-user': USER_ID})
self.assertStatus(res, 204)
self.test_get_no_notifications()
def test_delete_notifications_twice(self):
notification_id = self.test_create_notification()
for expected_status_code in (204, 403):
resp = self.app.delete(
'/notifications/' + notification_id,
headers={'x-balanced-user': USER_ID})
self.assertStatus(resp, expected_status_code)
def test_delete_notifications_unauthorized(self):
notification_id = self.test_create_notification()
res = self.app.delete(
'/notifications/' + notification_id)
self.assertStatus(res, 401)
def test_delete_notifications_another_user(self):
notification_id = self.test_create_notification()
res = self.app.delete(
'/notifications/' + notification_id,
headers={'x-balanced-user': '5'})
self.assertStatus(res, 403)
def test_delete_notifications_random_id(self):
res = self.app.delete(
'/notifications/1dnnn',
headers={'x-balanced-user': '5'})
self.assertStatus(res, 403)
def test_get_no_notifications(self):
res = self.app.get(
'/notifications', headers={'x-balanced-user': USER_ID})
self.assertIn('[]', res.data)
self.validateResponse(res, GET_NO_NOTIFICATIONS_SCHEMA)
self.assertStatus(res, 200)
def test_get_users(self):
res = self.app.get(
'/users', headers={'x-balanced-admin': '1'})
data = json.loads(res.data)
self.validateResponse(res, GET_USERS_SCHEMA)
self.assertGreaterEqual(len(data['data']), 1)
self.assertStatus(res, 200)
def test_get_users_unauthorized(self):
res = self.app.get(
'/users')
self.assertStatus(res, 401)
def test_get_users_not_admin_authorized(self):
res = self.app.get(
'/users', headers={'x-balanced-user': '1'})
self.assertStatus(res, 401)
if __name__ == '__main__':
unittest.main()