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.

124 lines
3.9 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 hc.api.models import Check
  6. from django.conf import settings
  7. class LoginTestCase(TestCase):
  8. def test_it_sends_link(self):
  9. alice = User(username="alice", email="[email protected]")
  10. alice.save()
  11. form = {"identity": "[email protected]"}
  12. r = self.client.post("/accounts/login/", form)
  13. self.assertRedirects(r, "/accounts/login_link_sent/")
  14. # Alice should be the only existing user
  15. self.assertEqual(User.objects.count(), 1)
  16. # And email should have been sent
  17. self.assertEqual(len(mail.outbox), 1)
  18. subject = "Log in to %s" % settings.SITE_NAME
  19. self.assertEqual(mail.outbox[0].subject, subject)
  20. def test_it_sends_link_with_next(self):
  21. alice = User(username="alice", email="[email protected]")
  22. alice.save()
  23. form = {"identity": "[email protected]"}
  24. r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
  25. self.assertRedirects(r, "/accounts/login_link_sent/")
  26. # The check_token link should have a ?next= query parameter:
  27. self.assertEqual(len(mail.outbox), 1)
  28. body = mail.outbox[0].body
  29. self.assertTrue("/?next=/integrations/add_slack/" in body)
  30. def test_it_pops_bad_link_from_session(self):
  31. self.client.session["bad_link"] = True
  32. self.client.get("/accounts/login/")
  33. assert "bad_link" not in self.client.session
  34. def test_it_ignores_case(self):
  35. alice = User(username="alice", email="[email protected]")
  36. alice.save()
  37. form = {"identity": "[email protected]"}
  38. r = self.client.post("/accounts/login/", form)
  39. self.assertRedirects(r, "/accounts/login_link_sent/")
  40. # There should be exactly one user:
  41. self.assertEqual(User.objects.count(), 1)
  42. profile = Profile.objects.for_user(alice)
  43. self.assertIn("login", profile.token)
  44. def test_it_handles_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": "password"
  52. }
  53. r = self.client.post("/accounts/login/", form)
  54. self.assertRedirects(r, "/checks/")
  55. def test_it_handles_password_login_with_redirect(self):
  56. alice = User(username="alice", email="[email protected]")
  57. alice.set_password("password")
  58. alice.save()
  59. check = Check.objects.create(user=alice)
  60. form = {
  61. "action": "login",
  62. "email": "[email protected]",
  63. "password": "password"
  64. }
  65. samples = [
  66. "/integrations/add_slack/",
  67. "/checks/%s/details/" % check.code
  68. ]
  69. for s in samples:
  70. r = self.client.post("/accounts/login/?next=%s" % s, form)
  71. self.assertRedirects(r, s)
  72. def test_it_handles_bad_next_parameter(self):
  73. alice = User(username="alice", email="[email protected]")
  74. alice.set_password("password")
  75. alice.save()
  76. form = {
  77. "action": "login",
  78. "email": "[email protected]",
  79. "password": "password"
  80. }
  81. r = self.client.post("/accounts/login/?next=/evil/", form)
  82. self.assertRedirects(r, "/checks/")
  83. def test_it_handles_wrong_password(self):
  84. alice = User(username="alice", email="[email protected]")
  85. alice.set_password("password")
  86. alice.save()
  87. form = {
  88. "action": "login",
  89. "email": "[email protected]",
  90. "password": "wrong password"
  91. }
  92. r = self.client.post("/accounts/login/", form)
  93. self.assertContains(r, "Incorrect email or password")