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.

95 lines
3.0 KiB

  1. from django.conf import settings
  2. from django.core import mail
  3. from hc.api.models import Check
  4. from hc.test import BaseTestCase
  5. class LoginTestCase(BaseTestCase):
  6. def setUp(self):
  7. super(LoginTestCase, self).setUp()
  8. self.checks_url = "/projects/%s/checks/" % self.project.code
  9. def test_it_sends_link(self):
  10. form = {"identity": "[email protected]"}
  11. r = self.client.post("/accounts/login/", form)
  12. self.assertRedirects(r, "/accounts/login_link_sent/")
  13. # And email should have been sent
  14. self.assertEqual(len(mail.outbox), 1)
  15. subject = "Log in to %s" % settings.SITE_NAME
  16. self.assertEqual(mail.outbox[0].subject, subject)
  17. def test_it_sends_link_with_next(self):
  18. form = {"identity": "[email protected]"}
  19. r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
  20. self.assertRedirects(r, "/accounts/login_link_sent/")
  21. # The check_token link should have a ?next= query parameter:
  22. self.assertEqual(len(mail.outbox), 1)
  23. body = mail.outbox[0].body
  24. self.assertTrue("/?next=/integrations/add_slack/" in body)
  25. def test_it_pops_bad_link_from_session(self):
  26. self.client.session["bad_link"] = True
  27. self.client.get("/accounts/login/")
  28. assert "bad_link" not in self.client.session
  29. def test_it_ignores_case(self):
  30. form = {"identity": "[email protected]"}
  31. r = self.client.post("/accounts/login/", form)
  32. self.assertRedirects(r, "/accounts/login_link_sent/")
  33. self.profile.refresh_from_db()
  34. self.assertIn("login", self.profile.token)
  35. def test_it_handles_password(self):
  36. form = {
  37. "action": "login",
  38. "email": "[email protected]",
  39. "password": "password"
  40. }
  41. r = self.client.post("/accounts/login/", form)
  42. self.assertRedirects(r, self.checks_url)
  43. def test_it_handles_password_login_with_redirect(self):
  44. check = Check.objects.create(project=self.project)
  45. form = {
  46. "action": "login",
  47. "email": "[email protected]",
  48. "password": "password"
  49. }
  50. samples = [
  51. "/integrations/add_slack/",
  52. "/checks/%s/details/" % check.code
  53. ]
  54. for s in samples:
  55. r = self.client.post("/accounts/login/?next=%s" % s, form)
  56. self.assertRedirects(r, s)
  57. def test_it_handles_bad_next_parameter(self):
  58. form = {
  59. "action": "login",
  60. "email": "[email protected]",
  61. "password": "password"
  62. }
  63. r = self.client.post("/accounts/login/?next=/evil/", form)
  64. self.assertRedirects(r, self.checks_url)
  65. def test_it_handles_wrong_password(self):
  66. form = {
  67. "action": "login",
  68. "email": "[email protected]",
  69. "password": "wrong password"
  70. }
  71. r = self.client.post("/accounts/login/", form)
  72. self.assertContains(r, "Incorrect email or password")