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.

113 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=" + self.channels_url, form)
  21. self.assertRedirects(r, "/accounts/login_link_sent/")
  22. self.assertIn("auto-login", r.cookies)
  23. # The check_token link should have a ?next= query parameter:
  24. self.assertEqual(len(mail.outbox), 1)
  25. body = mail.outbox[0].body
  26. self.assertTrue("/?next=" + self.channels_url in body)
  27. @override_settings(SECRET_KEY="test-secret")
  28. def test_it_rate_limits_emails(self):
  29. # "d60d..." is sha1("[email protected]")
  30. obj = TokenBucket(value="em-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  31. obj.tokens = 0
  32. obj.save()
  33. form = {"identity": "[email protected]"}
  34. r = self.client.post("/accounts/login/", form)
  35. self.assertContains(r, "Too many attempts")
  36. # No email should have been sent
  37. self.assertEqual(len(mail.outbox), 0)
  38. def test_it_pops_bad_link_from_session(self):
  39. self.client.session["bad_link"] = True
  40. self.client.get("/accounts/login/")
  41. assert "bad_link" not in self.client.session
  42. def test_it_ignores_case(self):
  43. form = {"identity": "[email protected]"}
  44. r = self.client.post("/accounts/login/", form)
  45. self.assertRedirects(r, "/accounts/login_link_sent/")
  46. self.profile.refresh_from_db()
  47. self.assertIn("login", self.profile.token)
  48. def test_it_handles_password(self):
  49. form = {"action": "login", "email": "[email protected]", "password": "password"}
  50. r = self.client.post("/accounts/login/", form)
  51. self.assertRedirects(r, self.checks_url)
  52. @override_settings(SECRET_KEY="test-secret")
  53. def test_it_rate_limits_password_attempts(self):
  54. # "d60d..." is sha1("[email protected]")
  55. obj = TokenBucket(value="pw-d60db3b2343e713a4de3e92d4eb417e4f05f06ab")
  56. obj.tokens = 0
  57. obj.save()
  58. form = {"action": "login", "email": "[email protected]", "password": "password"}
  59. r = self.client.post("/accounts/login/", form)
  60. self.assertContains(r, "Too many attempts")
  61. def test_it_handles_password_login_with_redirect(self):
  62. check = Check.objects.create(project=self.project)
  63. form = {"action": "login", "email": "[email protected]", "password": "password"}
  64. samples = [self.channels_url, "/checks/%s/details/" % check.code]
  65. for s in samples:
  66. r = self.client.post("/accounts/login/?next=%s" % s, form)
  67. self.assertRedirects(r, s)
  68. def test_it_handles_bad_next_parameter(self):
  69. form = {"action": "login", "email": "[email protected]", "password": "password"}
  70. r = self.client.post("/accounts/login/?next=/evil/", form)
  71. self.assertRedirects(r, self.checks_url)
  72. def test_it_handles_wrong_password(self):
  73. form = {
  74. "action": "login",
  75. "email": "[email protected]",
  76. "password": "wrong password",
  77. }
  78. r = self.client.post("/accounts/login/", form)
  79. self.assertContains(r, "Incorrect email or password")
  80. @override_settings(REGISTRATION_OPEN=False)
  81. def test_it_obeys_registration_open(self):
  82. r = self.client.get("/accounts/login/")
  83. self.assertNotContains(r, "Create Your Account")