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.

59 lines
1.8 KiB

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