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.

107 lines
3.8 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 = {"action": "login", "email": "[email protected]", "password": "password"}
  49. r = self.client.post("/accounts/login/", form)
  50. self.assertRedirects(r, self.checks_url)
  51. @override_settings(SECRET_KEY="test-secret")
  52. def test_it_rate_limits_password_attempts(self):
  53. # "d60d..." is sha1("[email protected]")
  54. obj = TokenBucket(value="pw-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  55. obj.tokens = 0
  56. obj.save()
  57. form = {"action": "login", "email": "[email protected]", "password": "password"}
  58. r = self.client.post("/accounts/login/", form)
  59. self.assertContains(r, "Too many attempts")
  60. def test_it_handles_password_login_with_redirect(self):
  61. check = Check.objects.create(project=self.project)
  62. form = {"action": "login", "email": "[email protected]", "password": "password"}
  63. samples = ["/integrations/add_slack/", "/checks/%s/details/" % check.code]
  64. for s in samples:
  65. r = self.client.post("/accounts/login/?next=%s" % s, form)
  66. self.assertRedirects(r, s)
  67. def test_it_handles_bad_next_parameter(self):
  68. form = {"action": "login", "email": "[email protected]", "password": "password"}
  69. r = self.client.post("/accounts/login/?next=/evil/", form)
  70. self.assertRedirects(r, self.checks_url)
  71. def test_it_handles_wrong_password(self):
  72. form = {
  73. "action": "login",
  74. "email": "[email protected]",
  75. "password": "wrong password",
  76. }
  77. r = self.client.post("/accounts/login/", form)
  78. self.assertContains(r, "Incorrect email or password")