test_api.py
14.2 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from django.core import mail
from flashcards.models import *
from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_403_FORBIDDEN
from rest_framework.test import APITestCase
from re import search
from django.utils.timezone import now
class LoginTests(APITestCase):
fixtures = ['testusers']
def test_login(self):
url = '/api/login'
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, HTTP_200_OK)
data = {'email': 'none@none.com', 'password': '4321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Invalid email or password', status_code=403)
data = {'email': 'bad@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Invalid email or password', status_code=403)
data = {'password': '4321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'email', status_code=400)
data = {'email': 'none@none.com'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'password', status_code=400)
user = User.objects.get(email="none@none.com")
user.is_active = False
user.save()
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Account is disabled', status_code=403)
def test_logout(self):
self.client.login(email='none@none.com', password='1234')
response = self.client.post('/api/logout')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
# since we're not logged in, we should get a 403 response
response = self.client.get('/api/me', format='json')
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
class PasswordResetTest(APITestCase):
fixtures = ['testusers']
def test_reset_password(self):
# submit the request to reset the password
url = '/api/request_password_reset'
post_data = {'email': 'none@none.com'}
self.client.post(url, post_data, format='json')
self.assertEqual(len(mail.outbox), 1)
self.assertIn('reset your password', mail.outbox[0].body)
# capture the reset token from the email
capture = search('https://flashy.cards/app/resetpassword/(\d+)/(.*)',
mail.outbox[0].body)
patch_data = {'new_password': '4321'}
patch_data['uid'] = capture.group(1)
reset_token = capture.group(2)
# try to reset the password with the wrong reset token
patch_data['token'] = 'wrong_token'
url = '/api/reset_password'
response = self.client.post(url, patch_data, format='json')
self.assertContains(response, 'Could not verify reset token', status_code=400)
# try to reset the password with the correct token
patch_data['token'] = reset_token
response = self.client.post(url, patch_data, format='json')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
user = User.objects.get(id=patch_data['uid'])
assert user.check_password(patch_data['new_password'])
class RegistrationTest(APITestCase):
def test_create_account(self):
url = '/api/register'
# missing password
data = {'email': 'none@none.com'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'password', status_code=400)
# missing email
data = {'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'email', status_code=400)
# create a user
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, HTTP_201_CREATED)
# user should not be confirmed
user = User.objects.get(email="none@none.com")
self.assertFalse(user.is_confirmed)
# check that the confirmation key was sent
self.assertEqual(len(mail.outbox), 1)
self.assertIn(user.confirmation_key, mail.outbox[0].body)
# log the user out
self.client.logout()
# log the user in with their registered credentials
self.client.login(email='none@none.com', password='1234')
# try activating with an invalid key
url = '/api/me'
response = self.client.patch(url, {'confirmation_key': 'NOT A KEY'})
self.assertContains(response, 'confirmation_key is invalid', status_code=400)
# try activating with the valid key
response = self.client.patch(url, {'confirmation_key': user.confirmation_key})
self.assertTrue(response.data['is_confirmed'])
class ProfileViewTest(APITestCase):
fixtures = ['testusers']
def test_get_me(self):
url = '/api/me'
response = self.client.get(url, format='json')
# since we're not logged in, we shouldn't be able to see this
self.assertEqual(response.status_code, 403)
self.client.login(email='none@none.com', password='1234')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, HTTP_200_OK)
class PasswordChangeTest(APITestCase):
fixtures = ['testusers']
def test_change_password(self):
url = '/api/me'
user = User.objects.get(email='none@none.com')
self.assertTrue(user.check_password('1234'))
response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json')
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
self.client.login(email='none@none.com', password='1234')
response = self.client.patch(url, {'new_password': '4321'}, format='json')
self.assertContains(response, 'old_password is required', status_code=400)
response = self.client.patch(url, {'new_password': '4321', 'old_password': '4321'}, format='json')
self.assertContains(response, 'old_password is incorrect', status_code=400)
response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json')
self.assertEqual(response.status_code, 200)
user = User.objects.get(email='none@none.com')
self.assertFalse(user.check_password('1234'))
self.assertTrue(user.check_password('4321'))
class DeleteUserTest(APITestCase):
fixtures = ['testusers']
def test_delete_user(self):
url = '/api/me'
user = User.objects.get(email='none@none.com')
self.client.login(email='none@none.com', password='1234')
self.client.delete(url)
self.assertFalse(User.objects.filter(email='none@none.com').exists())
class FlashcardDetailTest(APITestCase):
fixtures = ['testusers', 'testsections']
def setUp(self):
section = Section.objects.get(pk=1)
user = User.objects.get(email='none@none.com')
section.enroll(user)
self.inaccessible_flashcard = Flashcard(text="you can't see me!", section=Section.objects.get(pk=2),
material_date=now(), author=user)
self.inaccessible_flashcard.save()
self.flashcard = Flashcard(text="jason", section=section, material_date=now(), author=user)
self.flashcard.save()
def test_edit_flashcard(self):
self.client.login(email='none@none.com', password='1234')
user = User.objects.get(email='none@none.com')
user.sections.add(Section.objects.get(pk=1))
user.save()
def test_create_flashcard(self):
self.client.login(email='none@none.com', password='1234')
user = User.objects.get(email='none@none.com')
user.sections.add(Section.objects.get(pk=1))
user.save()
data = {'text': 'this is a flashcard',
'material_date': now(),
'mask': '[]',
'section': '1',
'previous': None}
response = self.client.post("/api/flashcards/", data, format="json")
self.assertEqual(response.status_code, HTTP_201_CREATED)
self.assertEqual(response.data['text'], data['text'])
self.assertTrue(Flashcard.objects.filter(section__pk=1, text=data['text']).exists())
def test_get_flashcard(self):
self.client.login(email='none@none.com', password='1234')
response = self.client.get("/api/flashcards/%d/" % self.flashcard.id, format="json")
self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(response.data["text"], "jason")
class SectionViewSetTest(APITestCase):
fixtures = ['testusers', 'testsections']
def setUp(self):
self.client.login(email='none@none.com', password='1234')
self.user = User.objects.get(email='none@none.com')
self.flashcard = Flashcard(text="jason", section=Section.objects.get(pk=1), material_date=now(),
author=self.user)
self.flashcard.save()
self.section = Section.objects.get(pk=1)
def test_list_sections(self):
response = self.client.get("/api/sections/", format="json")
self.assertEqual(response.status_code, HTTP_200_OK)
def test_section_enroll(self):
section = self.section
self.assertFalse(self.user.sections.filter(pk=section.pk))
# test enrolling in a section without a whitelist
response = self.client.post('/api/sections/%d/enroll/' % section.pk)
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
section = Section.objects.get(pk=2)
WhitelistedAddress.objects.create(email='bad@none.com', section=section)
# test enrolling in a section when not on the whitelist
response = self.client.post('/api/sections/%d/enroll/' % section.pk)
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
self.assertFalse(self.user.sections.filter(pk=section.pk).exists())
WhitelistedAddress.objects.create(email=self.user.email, section=section)
# test enrolling in a section when on the whitelist
response = self.client.post('/api/sections/%d/enroll/' % section.pk)
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
def test_section_drop(self):
section = self.section
# test dropping a section that the user isn't in
response = self.client.post('/api/sections/%d/drop/' % section.pk)
self.assertEqual(response.status_code, 400)
self.user.sections.add(section)
self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
# test dropping a section that the user is in
response = self.client.post('/api/sections/%d/drop/' % section.pk)
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
self.assertFalse(self.user.sections.filter(pk=section.pk).exists())
def test_section_flashcards(self):
# test to get flashcards for section 1
response = self.client.get('/api/sections/1/flashcards/')
self.assertEqual(response.status_code, HTTP_200_OK)
# test: Making FlashcardHide object, so no card should be seen.
flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard)
flashcard_hide.save()
response = self.client.get('/api/sections/1/flashcards/')
self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(response.content, '[]')
def test_section_search(self):
response = self.client.get('/api/sections/search/?q=Kramer')
self.assertEqual(response.status_code, HTTP_200_OK)
def test_section_deck(self):
self.user.sections.add(self.section)
self.user.save()
response = self.client.get('/api/sections/1/deck/')
self.assertEqual(response.status_code, HTTP_200_OK)
def test_section_feed(self):
response = self.client.get('/api/sections/1/feed/')
self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(response.data[0]['id'], 1)
def test_section_ordered_deck(self):
self.user.sections.add(self.section)
self.user.save()
response = self.client.get('/api/sections/1/ordered_deck/')
self.assertEqual(response.status_code, HTTP_200_OK)
class FlashcardViewSetTest(APITestCase):
fixtures = ['testusers', 'testsections']
def setUp(self):
self.client.login(email='none@none.com', password='1234')
self.user = User.objects.get(email='none@none.com')
self.section = Section.objects.get(pk=1)
self.section.enroll(self.user)
self.flashcard = Flashcard(text="jason", section=self.section, material_date=now(),
author=self.user)
self.flashcard.save()
self.inaccessible_flashcard = Flashcard(text="can't touch this", section=Section.objects.get(pk=2),
material_date=now(), author=self.user)
self.inaccessible_flashcard.save()
def test_hide_flashcard(self):
url = '/api/flashcards/1/hide/'
response = self.client.post(url, format='json')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
response = self.client.post('/api/flashcards/%d/hide/' % self.inaccessible_flashcard.pk, format='json')
# This should fail because the user is not enrolled in section id 2
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
def test_unhide_flashcard(self):
url = '/api/flashcards/1/unhide/'
flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard)
flashcard_hide.save()
response = self.client.post(url, format='json')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
response = self.client.post('/api/flashcards/%d/unhide/' % self.inaccessible_flashcard.pk, format='json')
# This should fail because the user is not enrolled in section id 2
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)