You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

  1. from django.contrib.auth.models import User
  2. from django.core import mail
  3. from django.test import TestCase
  4. from django.test.utils import override_settings
  5. from hc.api.models import Check
  6. from django.conf import settings
  7. class LoginTestCase(TestCase):
  8. def test_it_sends_link(self):
  9. check = Check()
  10. check.save()
  11. session = self.client.session
  12. session["welcome_code"] = str(check.code)
  13. session.save()
  14. form = {"email": "[email protected]"}
  15. r = self.client.post("/accounts/login/", form)
  16. assert r.status_code == 302
  17. # An user should have been created
  18. self.assertEqual(User.objects.count(), 1)
  19. # And email sent
  20. self.assertEqual(len(mail.outbox), 1)
  21. subject = "Log in to %s" % settings.SITE_NAME
  22. self.assertEqual(mail.outbox[0].subject, subject)
  23. # And check should be associated with the new user
  24. check_again = Check.objects.get(code=check.code)
  25. assert check_again.user
  26. def test_it_pops_bad_link_from_session(self):
  27. self.client.session["bad_link"] = True
  28. self.client.get("/accounts/login/")
  29. assert "bad_link" not in self.client.session
  30. def test_it_handles_missing_welcome_check(self):
  31. # This check does not exist in database,
  32. # but login should still work.
  33. session = self.client.session
  34. session["welcome_code"] = "00000000-0000-0000-0000-000000000000"
  35. session.save()
  36. form = {"email": "[email protected]"}
  37. r = self.client.post("/accounts/login/", form)
  38. assert r.status_code == 302
  39. # An user should have been created
  40. self.assertEqual(User.objects.count(), 1)
  41. # And email sent
  42. self.assertEqual(len(mail.outbox), 1)
  43. subject = "Log in to %s" % settings.SITE_NAME
  44. self.assertEqual(mail.outbox[0].subject, subject)
  45. @override_settings(REGISTRATION_OPEN=False)
  46. def test_it_obeys_registration_open(self):
  47. form = {"email": "[email protected]"}
  48. r = self.client.post("/accounts/login/", form)
  49. assert r.status_code == 200
  50. self.assertContains(r, "Incorrect email")