Commit b705da3be9aa4ef42dd8407c27d5f44d633ea0bc

Authored by Rohan Rangray
1 parent efebf24610
Exists in master

Added a password reset test.

Showing 1 changed file with 24 additions and 1 deletions Side-by-side Diff

flashcards/tests/test_api.py View file @ b705da3
... ... @@ -2,6 +2,7 @@
2 2 from flashcards.models import User
3 3 from rest_framework.status import HTTP_201_CREATED, HTTP_200_OK, HTTP_401_UNAUTHORIZED
4 4 from rest_framework.test import APITestCase
  5 +from re import search
5 6  
6 7  
7 8 class LoginTests(APITestCase):
... ... @@ -46,6 +47,28 @@
46 47 response = self.client.get('/api/users/me', format='json')
47 48 # since we're not logged in, we shouldn't be able to see this
48 49 self.assertEqual(response.status_code, HTTP_401_UNAUTHORIZED)
  50 +
  51 +class PasswordResetTest(APITestCase):
  52 + def setUp(self):
  53 + email = "test@flashy.cards"
  54 + User.objects.create_user(email=email, password="12345")
  55 +
  56 + def reset_password(self):
  57 + url = '/api/reset_password'
  58 + post_data = {'email': 'test@flashy.cards'}
  59 + patch_data = {'new_password': '54321',
  60 + 'uid': '', 'token': ''}
  61 + self.client.post(url, post_data, format='json')
  62 + self.assertEqual(len(mail.outbox), 1)
  63 + self.assertIn('reset your password', mail.outbox[0].body)
  64 +
  65 + capture = search('https://flashy.cards/app/reset_password/(\d+)/(.*)',
  66 + mail.outbox[0].body)
  67 + data['uid'] = capture.group(0)
  68 + data['token'] = capture.group(1)
  69 + self.client.patch(url, patch_data, format='json')
  70 + user = User.objects.get(id=data['uid'])
  71 + assert user.check_password(data['new_password'])
49 72  
50 73  
51 74 class RegistrationTest(APITestCase):