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.

73 lines
2.2 KiB

  1. from django.contrib.auth.models import User
  2. from django.core import mail
  3. from django.test import TestCase
  4. from hc.accounts.models import Profile
  5. from django.conf import settings
  6. class LoginTestCase(TestCase):
  7. def test_it_sends_link(self):
  8. alice = User(username="alice", email="[email protected]")
  9. alice.save()
  10. form = {"identity": "[email protected]"}
  11. r = self.client.post("/accounts/login/", form)
  12. assert r.status_code == 302
  13. # Alice should be the only existing user
  14. self.assertEqual(User.objects.count(), 1)
  15. # And email should have been sent
  16. self.assertEqual(len(mail.outbox), 1)
  17. subject = "Log in to %s" % settings.SITE_NAME
  18. self.assertEqual(mail.outbox[0].subject, subject)
  19. def test_it_pops_bad_link_from_session(self):
  20. self.client.session["bad_link"] = True
  21. self.client.get("/accounts/login/")
  22. assert "bad_link" not in self.client.session
  23. def test_it_ignores_case(self):
  24. alice = User(username="alice", email="[email protected]")
  25. alice.save()
  26. form = {"identity": "[email protected]"}
  27. r = self.client.post("/accounts/login/", form)
  28. assert r.status_code == 302
  29. # There should be exactly one user:
  30. self.assertEqual(User.objects.count(), 1)
  31. profile = Profile.objects.for_user(alice)
  32. self.assertIn("login", profile.token)
  33. def test_it_handles_password(self):
  34. alice = User(username="alice", email="[email protected]")
  35. alice.set_password("password")
  36. alice.save()
  37. form = {
  38. "action": "login",
  39. "email": "[email protected]",
  40. "password": "password"
  41. }
  42. r = self.client.post("/accounts/login/", form)
  43. self.assertEqual(r.status_code, 302)
  44. def test_it_handles_wrong_password(self):
  45. alice = User(username="alice", email="[email protected]")
  46. alice.set_password("password")
  47. alice.save()
  48. form = {
  49. "action": "login",
  50. "email": "[email protected]",
  51. "password": "wrong password"
  52. }
  53. r = self.client.post("/accounts/login/", form)
  54. self.assertContains(r, "Incorrect email or password")