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.

66 lines
2.4 KiB

9 years ago
9 years ago
9 years ago
  1. from django.contrib.auth.hashers import make_password
  2. from hc.accounts.models import Credential
  3. from hc.test import BaseTestCase
  4. class CheckTokenTestCase(BaseTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.profile.token = make_password("secret-token", "login")
  8. self.profile.save()
  9. self.checks_url = "/projects/%s/checks/" % self.project.code
  10. def test_it_shows_form(self):
  11. r = self.client.get("/accounts/check_token/alice/secret-token/")
  12. self.assertContains(r, "You are about to log in")
  13. def test_it_redirects(self):
  14. r = self.client.post("/accounts/check_token/alice/secret-token/")
  15. self.assertRedirects(r, self.checks_url)
  16. # After login, token should be blank
  17. self.profile.refresh_from_db()
  18. self.assertEqual(self.profile.token, "")
  19. def test_it_redirects_already_logged_in(self):
  20. # Login
  21. self.client.login(username="[email protected]", password="password")
  22. # Login again, when already authenticated
  23. r = self.client.post("/accounts/check_token/alice/secret-token/")
  24. self.assertRedirects(r, self.checks_url)
  25. def test_it_redirects_bad_login(self):
  26. # Login with a bad token
  27. url = "/accounts/check_token/alice/invalid-token/"
  28. r = self.client.post(url, follow=True)
  29. self.assertRedirects(r, "/accounts/login/")
  30. self.assertContains(r, "incorrect or expired")
  31. def test_it_handles_next_parameter(self):
  32. url = "/accounts/check_token/alice/secret-token/?next=" + self.channels_url
  33. r = self.client.post(url)
  34. self.assertRedirects(r, self.channels_url)
  35. def test_it_ignores_bad_next_parameter(self):
  36. url = "/accounts/check_token/alice/secret-token/?next=/evil/"
  37. r = self.client.post(url)
  38. self.assertRedirects(r, self.checks_url)
  39. def test_it_redirects_to_webauthn_form(self):
  40. Credential.objects.create(user=self.alice, name="Alices Key")
  41. r = self.client.post("/accounts/check_token/alice/secret-token/")
  42. self.assertRedirects(
  43. r, "/accounts/login/two_factor/", fetch_redirect_response=False
  44. )
  45. # It should not log the user in yet
  46. self.assertNotIn("_auth_user_id", self.client.session)
  47. # Instead, it should set 2fa_user_id in the session
  48. user_id, email, valid_until = self.client.session["2fa_user"]
  49. self.assertEqual(user_id, self.alice.id)