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.

130 lines
4.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.accounts.models import Credential
  5. from hc.api.models import Check, TokenBucket
  6. from hc.test import BaseTestCase
  7. class LoginTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.checks_url = f"/projects/{self.project.code}/checks/"
  11. def test_it_sends_link(self):
  12. form = {"identity": "[email protected]"}
  13. r = self.client.post("/accounts/login/", form)
  14. self.assertRedirects(r, "/accounts/login_link_sent/")
  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. form = {"identity": "[email protected]"}
  21. r = self.client.post("/accounts/login/?next=" + self.channels_url, form)
  22. self.assertRedirects(r, "/accounts/login_link_sent/")
  23. self.assertIn("auto-login", r.cookies)
  24. # The check_token link should have a ?next= query parameter:
  25. self.assertEqual(len(mail.outbox), 1)
  26. body = mail.outbox[0].body
  27. self.assertTrue("/?next=" + self.channels_url in body)
  28. @override_settings(SECRET_KEY="test-secret")
  29. def test_it_rate_limits_emails(self):
  30. # "d60d..." is sha1("[email protected]")
  31. obj = TokenBucket(value="em-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  32. obj.tokens = 0
  33. obj.save()
  34. form = {"identity": "[email protected]"}
  35. r = self.client.post("/accounts/login/", form)
  36. self.assertContains(r, "Too many attempts")
  37. # No email should have been sent
  38. self.assertEqual(len(mail.outbox), 0)
  39. def test_it_pops_bad_link_from_session(self):
  40. self.client.session["bad_link"] = True
  41. self.client.get("/accounts/login/")
  42. assert "bad_link" not in self.client.session
  43. def test_it_ignores_case(self):
  44. form = {"identity": "[email protected]"}
  45. r = self.client.post("/accounts/login/", form)
  46. self.assertRedirects(r, "/accounts/login_link_sent/")
  47. self.profile.refresh_from_db()
  48. self.assertIn("login", self.profile.token)
  49. def test_it_handles_password(self):
  50. form = {"action": "login", "email": "[email protected]", "password": "password"}
  51. r = self.client.post("/accounts/login/", form)
  52. self.assertRedirects(r, self.checks_url)
  53. @override_settings(SECRET_KEY="test-secret")
  54. def test_it_rate_limits_password_attempts(self):
  55. # "d60d..." is sha1("[email protected]")
  56. obj = TokenBucket(value="pw-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  57. obj.tokens = 0
  58. obj.save()
  59. form = {"action": "login", "email": "[email protected]", "password": "password"}
  60. r = self.client.post("/accounts/login/", form)
  61. self.assertContains(r, "Too many attempts")
  62. def test_it_handles_password_login_with_redirect(self):
  63. check = Check.objects.create(project=self.project)
  64. form = {"action": "login", "email": "[email protected]", "password": "password"}
  65. samples = [self.channels_url, "/checks/%s/details/" % check.code]
  66. for s in samples:
  67. r = self.client.post("/accounts/login/?next=%s" % s, form)
  68. self.assertRedirects(r, s)
  69. def test_it_handles_bad_next_parameter(self):
  70. form = {"action": "login", "email": "[email protected]", "password": "password"}
  71. r = self.client.post("/accounts/login/?next=/evil/", form)
  72. self.assertRedirects(r, self.checks_url)
  73. def test_it_handles_wrong_password(self):
  74. form = {
  75. "action": "login",
  76. "email": "[email protected]",
  77. "password": "wrong password",
  78. }
  79. r = self.client.post("/accounts/login/", form)
  80. self.assertContains(r, "Incorrect email or password")
  81. @override_settings(REGISTRATION_OPEN=False)
  82. def test_it_obeys_registration_open(self):
  83. r = self.client.get("/accounts/login/")
  84. self.assertNotContains(r, "Create Your Account")
  85. def test_it_redirects_to_webauthn_form(self):
  86. Credential.objects.create(user=self.alice, name="Alices Key")
  87. form = {"action": "login", "email": "[email protected]", "password": "password"}
  88. r = self.client.post("/accounts/login/", form)
  89. self.assertRedirects(
  90. r, "/accounts/login/two_factor/", fetch_redirect_response=False
  91. )
  92. # It should not log the user in yet
  93. self.assertNotIn("_auth_user_id", self.client.session)
  94. # Instead, it should set 2fa_user_id in the session
  95. user_id, email, valid_until = self.client.session["2fa_user"]
  96. self.assertEqual(user_id, self.alice.id)