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.

127 lines
4.1 KiB

  1. from django.conf import settings
  2. from django.core import mail
  3. from django.test.utils import override_settings
  4. from hc.api.models import Check, TokenBucket
  5. from hc.test import BaseTestCase
  6. class LoginTestCase(BaseTestCase):
  7. def setUp(self):
  8. super(LoginTestCase, self).setUp()
  9. self.checks_url = "/projects/%s/checks/" % self.project.code
  10. def test_it_sends_link(self):
  11. form = {"identity": "[email protected]"}
  12. r = self.client.post("/accounts/login/", form)
  13. self.assertRedirects(r, "/accounts/login_link_sent/")
  14. # And email should have been sent
  15. self.assertEqual(len(mail.outbox), 1)
  16. subject = "Log in to %s" % settings.SITE_NAME
  17. self.assertEqual(mail.outbox[0].subject, subject)
  18. def test_it_sends_link_with_next(self):
  19. form = {"identity": "[email protected]"}
  20. r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
  21. self.assertRedirects(r, "/accounts/login_link_sent/")
  22. # The check_token link should have a ?next= query parameter:
  23. self.assertEqual(len(mail.outbox), 1)
  24. body = mail.outbox[0].body
  25. self.assertTrue("/?next=/integrations/add_slack/" in body)
  26. @override_settings(SECRET_KEY="test-secret")
  27. def test_it_rate_limits_emails(self):
  28. # "d60d..." is sha1("[email protected]")
  29. obj = TokenBucket(value="em-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  30. obj.tokens = 0
  31. obj.save()
  32. form = {"identity": "[email protected]"}
  33. r = self.client.post("/accounts/login/", form)
  34. self.assertContains(r, "Too many attempts")
  35. # No email should have been sent
  36. self.assertEqual(len(mail.outbox), 0)
  37. def test_it_pops_bad_link_from_session(self):
  38. self.client.session["bad_link"] = True
  39. self.client.get("/accounts/login/")
  40. assert "bad_link" not in self.client.session
  41. def test_it_ignores_case(self):
  42. form = {"identity": "[email protected]"}
  43. r = self.client.post("/accounts/login/", form)
  44. self.assertRedirects(r, "/accounts/login_link_sent/")
  45. self.profile.refresh_from_db()
  46. self.assertIn("login", self.profile.token)
  47. def test_it_handles_password(self):
  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, self.checks_url)
  55. @override_settings(SECRET_KEY="test-secret")
  56. def test_it_rate_limits_password_attempts(self):
  57. # "d60d..." is sha1("[email protected]")
  58. obj = TokenBucket(value="pw-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  59. obj.tokens = 0
  60. obj.save()
  61. form = {
  62. "action": "login",
  63. "email": "[email protected]",
  64. "password": "password"
  65. }
  66. r = self.client.post("/accounts/login/", form)
  67. self.assertContains(r, "Too many attempts")
  68. def test_it_handles_password_login_with_redirect(self):
  69. check = Check.objects.create(project=self.project)
  70. form = {
  71. "action": "login",
  72. "email": "[email protected]",
  73. "password": "password"
  74. }
  75. samples = [
  76. "/integrations/add_slack/",
  77. "/checks/%s/details/" % check.code
  78. ]
  79. for s in samples:
  80. r = self.client.post("/accounts/login/?next=%s" % s, form)
  81. self.assertRedirects(r, s)
  82. def test_it_handles_bad_next_parameter(self):
  83. form = {
  84. "action": "login",
  85. "email": "[email protected]",
  86. "password": "password"
  87. }
  88. r = self.client.post("/accounts/login/?next=/evil/", form)
  89. self.assertRedirects(r, self.checks_url)
  90. def test_it_handles_wrong_password(self):
  91. form = {
  92. "action": "login",
  93. "email": "[email protected]",
  94. "password": "wrong password"
  95. }
  96. r = self.client.post("/accounts/login/", form)
  97. self.assertContains(r, "Incorrect email or password")