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.

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