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.

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