Commit 7aa4b42d3e1ec9fa212d394a48015e797bc3c613
1 parent
4af65c21da
Exists in
master
Fixed registration again?
Showing 2 changed files with 35 additions and 9 deletions Side-by-side Diff
flashcards/tests/test_api.py
View file @
7aa4b42
... | ... | @@ -35,25 +35,46 @@ |
35 | 35 | class RegistrationTest(APITestCase): |
36 | 36 | def test_create_account(self): |
37 | 37 | url = '/api/users/me' |
38 | + | |
39 | + # missing password | |
40 | + data = {'email': 'none@none.com'} | |
41 | + response = self.client.post(url, data, format='json') | |
42 | + self.assertContains(response, 'password', status_code=400) | |
43 | + | |
44 | + # missing email | |
45 | + data = {'password': '1234'} | |
46 | + response = self.client.post(url, data, format='json') | |
47 | + self.assertContains(response, 'email', status_code=400) | |
48 | + | |
49 | + # create a user | |
38 | 50 | data = {'email': 'none@none.com', 'password': '1234'} |
39 | 51 | response = self.client.post(url, data, format='json') |
40 | 52 | self.assertEqual(response.status_code, HTTP_201_CREATED) |
41 | 53 | |
54 | + # user should not be confirmed | |
42 | 55 | user = User.objects.get(email="none@none.com") |
56 | + self.assertFalse(user.is_confirmed) | |
57 | + | |
58 | + # check that the confirmation key was sent | |
43 | 59 | self.assertEqual(len(mail.outbox), 1) |
44 | 60 | self.assertIn(user.confirmation_key, mail.outbox[0].body) |
45 | 61 | |
62 | + # log the user out | |
63 | + response = self.client.post('/api/logout', format='json') | |
64 | + self.assertEqual(response.status_code, 204) | |
65 | + | |
66 | + # log the user in with their registered credentials | |
46 | 67 | data = {'email': 'none@none.com', 'password': '1234'} |
47 | 68 | response = self.client.post('/api/login', data, format='json') |
48 | 69 | self.assertEqual(response.status_code, HTTP_200_OK) |
49 | 70 | |
50 | - data = {'email': 'none@none.com'} | |
51 | - response = self.client.post(url, data, format='json') | |
52 | - self.assertContains(response, 'password', status_code=400) | |
71 | + # try activating with an invalid key | |
72 | + response = self.client.patch(url, {'confirmation_key': 'NOT A KEY'}) | |
73 | + self.assertContains(response, 'confirmation_key is invalid', status_code=400) | |
53 | 74 | |
54 | - data = {'password': '1234'} | |
55 | - response = self.client.post(url, data, format='json') | |
56 | - self.assertContains(response, 'email', status_code=400) | |
75 | + # try activating with the valid key | |
76 | + response = self.client.patch(url, {'confirmation_key': user.confirmation_key}) | |
77 | + self.assertTrue(response.data['is_confirmed']) | |
57 | 78 | |
58 | 79 | |
59 | 80 | class ProfileViewTest(APITestCase): |
flashcards/views.py
View file @
7aa4b42
... | ... | @@ -10,6 +10,7 @@ |
10 | 10 | from rest_framework.views import APIView |
11 | 11 | from rest_framework.response import Response |
12 | 12 | from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated, ValidationError |
13 | +from simple_email_confirmation import EmailAddress | |
13 | 14 | |
14 | 15 | |
15 | 16 | class SectionViewSet(ReadOnlyModelViewSet): |
16 | 17 | |
17 | 18 | |
... | ... | @@ -28,15 +29,19 @@ |
28 | 29 | """ |
29 | 30 | data = UserUpdateSerializer(data=request.data, context={'user': request.user}) |
30 | 31 | data.is_valid(raise_exception=True) |
32 | + data = data.validated_data | |
31 | 33 | |
32 | 34 | if 'new_password' in data: |
33 | 35 | if not request.user.check_password(data['old_password']): |
34 | 36 | raise ValidationError('old_password is incorrect') |
35 | - request.user.set_password(request.data['new_password']) | |
37 | + request.user.set_password(data['new_password']) | |
36 | 38 | request.user.save() |
37 | 39 | |
38 | - if 'confirmation_key' in data and not request.user.confirm_email(data['confirmation_key']): | |
39 | - raise ValidationError('confirmation_key is invalid') | |
40 | + if 'confirmation_key' in data: | |
41 | + try: | |
42 | + request.user.confirm_email(data['confirmation_key']) | |
43 | + except EmailAddress.DoesNotExist: | |
44 | + raise ValidationError('confirmation_key is invalid') | |
40 | 45 | |
41 | 46 | return Response(UserSerializer(request.user).data) |
42 | 47 |