test_usercp_views.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from path import Path
  2. from django.contrib.auth import get_user_model
  3. from django.core import mail
  4. from django.core.urlresolvers import reverse
  5. from misago.acl.testutils import override_acl
  6. from misago.conf import settings
  7. from misago.core import threadstore
  8. from misago.users.avatars import store
  9. from misago.users.testutils import AuthenticatedUserTestCase
  10. class ChangeEmailPasswordTests(AuthenticatedUserTestCase):
  11. def setUp(self):
  12. super(ChangeEmailPasswordTests, self).setUp()
  13. self.view_link = reverse('misago:usercp_change_email_password')
  14. threadstore.clear()
  15. def _link_from_mail(self, mail_body):
  16. for line in mail.outbox[0].body.splitlines():
  17. if line.strip().startswith('http://testserver/'):
  18. return line.strip()[len('http://testserver'):]
  19. raise ValueError("mail body didn't contain link with token")
  20. def test_change_email_password_get(self):
  21. """GET to usercp change email/pass view returns 200"""
  22. response = self.client.get(self.view_link)
  23. self.assertEqual(response.status_code, 200)
  24. self.assertIn('Change email or password', response.content)
  25. def test_change_email(self):
  26. """POST to usercp change email view returns 302"""
  27. response = self.client.post(self.view_link,
  28. data={'new_email': 'newmail@test.com',
  29. 'current_password': 'Pass.123'})
  30. self.assertEqual(response.status_code, 302)
  31. self.assertIn('Confirm changes to', mail.outbox[0].subject)
  32. confirmation_link = self._link_from_mail(mail.outbox[0].body)
  33. response = self.client.get(confirmation_link)
  34. self.assertEqual(response.status_code, 302)
  35. User = get_user_model()
  36. User.objects.get(email='newmail@test.com')
  37. def test_change_password(self):
  38. """POST to usercp change password view returns 302"""
  39. response = self.client.post(self.view_link,
  40. data={'new_password': 'newpass123',
  41. 'current_password': 'Pass.123'})
  42. self.assertEqual(response.status_code, 302)
  43. self.assertIn('Confirm changes to', mail.outbox[0].subject)
  44. confirmation_link = self._link_from_mail(mail.outbox[0].body)
  45. response = self.client.get(confirmation_link)
  46. self.assertEqual(response.status_code, 302)
  47. User = get_user_model()
  48. test_user = User.objects.get(pk=self.user.pk)
  49. self.assertFalse(test_user.check_password('Pass.123'))
  50. self.assertTrue(test_user.check_password('newpass123'))