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.

163 lines
5.9 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_shows_form(self):
  12. r = self.client.get("/accounts/login/")
  13. self.assertContains(r, "Email Me a Link")
  14. def test_it_redirects_authenticated_get(self):
  15. self.client.login(username="[email protected]", password="password")
  16. r = self.client.get("/accounts/login/")
  17. self.assertRedirects(r, self.checks_url)
  18. def test_it_sends_link(self):
  19. form = {"identity": "[email protected]"}
  20. r = self.client.post("/accounts/login/", form)
  21. self.assertRedirects(r, "/accounts/login_link_sent/")
  22. # And email should have been sent
  23. self.assertEqual(len(mail.outbox), 1)
  24. subject = "Log in to %s" % settings.SITE_NAME
  25. self.assertEqual(mail.outbox[0].subject, subject)
  26. def test_it_sends_link_with_next(self):
  27. form = {"identity": "[email protected]"}
  28. r = self.client.post("/accounts/login/?next=" + self.channels_url, form)
  29. self.assertRedirects(r, "/accounts/login_link_sent/")
  30. self.assertIn("auto-login", r.cookies)
  31. # The check_token link should have a ?next= query parameter:
  32. self.assertEqual(len(mail.outbox), 1)
  33. body = mail.outbox[0].body
  34. self.assertTrue("/?next=" + self.channels_url in body)
  35. @override_settings(SECRET_KEY="test-secret")
  36. def test_it_rate_limits_emails(self):
  37. # "d60d..." is sha1("[email protected]")
  38. obj = TokenBucket(value="em-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  39. obj.tokens = 0
  40. obj.save()
  41. form = {"identity": "[email protected]"}
  42. r = self.client.post("/accounts/login/", form)
  43. self.assertContains(r, "Too many attempts")
  44. # No email should have been sent
  45. self.assertEqual(len(mail.outbox), 0)
  46. def test_it_pops_bad_link_from_session(self):
  47. self.client.session["bad_link"] = True
  48. self.client.get("/accounts/login/")
  49. assert "bad_link" not in self.client.session
  50. def test_it_ignores_case(self):
  51. form = {"identity": "[email protected]"}
  52. r = self.client.post("/accounts/login/", form)
  53. self.assertRedirects(r, "/accounts/login_link_sent/")
  54. self.profile.refresh_from_db()
  55. self.assertIn("login", self.profile.token)
  56. def test_it_handles_password(self):
  57. form = {"action": "login", "email": "[email protected]", "password": "password"}
  58. r = self.client.post("/accounts/login/", form)
  59. self.assertRedirects(r, self.checks_url)
  60. @override_settings(SECRET_KEY="test-secret")
  61. def test_it_rate_limits_password_attempts(self):
  62. # "d60d..." is sha1("[email protected]")
  63. obj = TokenBucket(value="pw-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  64. obj.tokens = 0
  65. obj.save()
  66. form = {"action": "login", "email": "[email protected]", "password": "password"}
  67. r = self.client.post("/accounts/login/", form)
  68. self.assertContains(r, "Too many attempts")
  69. def test_it_handles_password_login_with_redirect(self):
  70. check = Check.objects.create(project=self.project)
  71. form = {"action": "login", "email": "[email protected]", "password": "password"}
  72. samples = [self.channels_url, "/checks/%s/details/" % check.code]
  73. for s in samples:
  74. r = self.client.post("/accounts/login/?next=%s" % s, form)
  75. self.assertRedirects(r, s)
  76. def test_it_handles_bad_next_parameter(self):
  77. form = {"action": "login", "email": "[email protected]", "password": "password"}
  78. samples = [
  79. "/evil/",
  80. f"https://example.org/projects/{self.project.code}/checks/",
  81. ]
  82. for sample in samples:
  83. r = self.client.post("/accounts/login/?next=" + sample, form)
  84. self.assertRedirects(r, self.checks_url)
  85. def test_it_handles_wrong_password(self):
  86. form = {
  87. "action": "login",
  88. "email": "[email protected]",
  89. "password": "wrong password",
  90. }
  91. r = self.client.post("/accounts/login/", form)
  92. self.assertContains(r, "Incorrect email or password")
  93. @override_settings(REGISTRATION_OPEN=False)
  94. def test_it_obeys_registration_open(self):
  95. r = self.client.get("/accounts/login/")
  96. self.assertNotContains(r, "Create Your Account")
  97. def test_it_redirects_to_webauthn_form(self):
  98. Credential.objects.create(user=self.alice, name="Alices Key")
  99. form = {"action": "login", "email": "[email protected]", "password": "password"}
  100. r = self.client.post("/accounts/login/", form)
  101. self.assertRedirects(
  102. r, "/accounts/login/two_factor/", fetch_redirect_response=False
  103. )
  104. # It should not log the user in yet
  105. self.assertNotIn("_auth_user_id", self.client.session)
  106. # Instead, it should set 2fa_user_id in the session
  107. user_id, email, valid_until = self.client.session["2fa_user"]
  108. self.assertEqual(user_id, self.alice.id)
  109. def test_it_redirects_to_totp_form(self):
  110. self.profile.totp = "0" * 32
  111. self.profile.save()
  112. form = {"action": "login", "email": "[email protected]", "password": "password"}
  113. r = self.client.post("/accounts/login/", form)
  114. self.assertRedirects(
  115. r, "/accounts/login/two_factor/totp/", fetch_redirect_response=False
  116. )
  117. # It should not log the user in yet
  118. self.assertNotIn("_auth_user_id", self.client.session)
  119. # Instead, it should set 2fa_user_id in the session
  120. user_id, email, valid_until = self.client.session["2fa_user"]
  121. self.assertEqual(user_id, self.alice.id)