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.

115 lines
3.7 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. self.assertRedirects(r, "/accounts/login_link_sent/")
  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_sends_link_with_next(self):
  20. alice = User(username="alice", email="[email protected]")
  21. alice.save()
  22. form = {"identity": "[email protected]"}
  23. r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
  24. self.assertRedirects(r, "/accounts/login_link_sent/")
  25. # The check_token link should have a ?next= query parameter:
  26. self.assertEqual(len(mail.outbox), 1)
  27. body = mail.outbox[0].body
  28. self.assertTrue("/?next=/integrations/add_slack/" in body)
  29. def test_it_pops_bad_link_from_session(self):
  30. self.client.session["bad_link"] = True
  31. self.client.get("/accounts/login/")
  32. assert "bad_link" not in self.client.session
  33. def test_it_ignores_case(self):
  34. alice = User(username="alice", email="[email protected]")
  35. alice.save()
  36. form = {"identity": "[email protected]"}
  37. r = self.client.post("/accounts/login/", form)
  38. self.assertRedirects(r, "/accounts/login_link_sent/")
  39. # There should be exactly one user:
  40. self.assertEqual(User.objects.count(), 1)
  41. profile = Profile.objects.for_user(alice)
  42. self.assertIn("login", profile.token)
  43. def test_it_handles_password(self):
  44. alice = User(username="alice", email="[email protected]")
  45. alice.set_password("password")
  46. alice.save()
  47. form = {
  48. "action": "login",
  49. "email": "[email protected]",
  50. "password": "password"
  51. }
  52. r = self.client.post("/accounts/login/", form)
  53. self.assertRedirects(r, "/checks/")
  54. def test_it_handles_password_login_with_redirect(self):
  55. alice = User(username="alice", email="[email protected]")
  56. alice.set_password("password")
  57. alice.save()
  58. form = {
  59. "action": "login",
  60. "email": "[email protected]",
  61. "password": "password"
  62. }
  63. r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
  64. self.assertRedirects(r, "/integrations/add_slack/")
  65. def test_it_handles_bad_next_parameter(self):
  66. alice = User(username="alice", email="[email protected]")
  67. alice.set_password("password")
  68. alice.save()
  69. form = {
  70. "action": "login",
  71. "email": "[email protected]",
  72. "password": "password"
  73. }
  74. r = self.client.post("/accounts/login/?next=/evil/", form)
  75. self.assertRedirects(r, "/checks/")
  76. def test_it_handles_wrong_password(self):
  77. alice = User(username="alice", email="[email protected]")
  78. alice.set_password("password")
  79. alice.save()
  80. form = {
  81. "action": "login",
  82. "email": "[email protected]",
  83. "password": "wrong password"
  84. }
  85. r = self.client.post("/accounts/login/", form)
  86. self.assertContains(r, "Incorrect email or password")