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.

83 lines
2.9 KiB

  1. from unittest.mock import patch
  2. from hc.test import BaseTestCase
  3. class AddTotpTestCase(BaseTestCase):
  4. def setUp(self):
  5. super().setUp()
  6. self.url = "/accounts/two_factor/totp/"
  7. def test_it_requires_sudo_mode(self):
  8. self.client.login(username="[email protected]", password="password")
  9. r = self.client.get(self.url)
  10. self.assertContains(r, "We have sent a confirmation code")
  11. def test_it_shows_form(self):
  12. self.client.login(username="[email protected]", password="password")
  13. self.set_sudo_flag()
  14. r = self.client.get(self.url)
  15. self.assertContains(r, "Enter the six-digit code")
  16. # It should put a "totp_secret" key in the session:
  17. self.assertIn("totp_secret", self.client.session)
  18. @patch("hc.accounts.views.pyotp.totp.TOTP")
  19. def test_it_adds_totp(self, mock_TOTP):
  20. mock_TOTP.return_value.verify.return_value = True
  21. self.client.login(username="[email protected]", password="password")
  22. self.set_sudo_flag()
  23. payload = {"code": "000000"}
  24. r = self.client.post(self.url, payload, follow=True)
  25. self.assertRedirects(r, "/accounts/profile/")
  26. self.assertContains(r, "Successfully set up the Authenticator app")
  27. # totp_secret should be gone from the session:
  28. self.assertNotIn("totp_secret", self.client.session)
  29. self.profile.refresh_from_db()
  30. self.assertTrue(self.profile.totp)
  31. self.assertTrue(self.profile.totp_created)
  32. @patch("hc.accounts.views.pyotp.totp.TOTP")
  33. def test_it_handles_wrong_code(self, mock_TOTP):
  34. mock_TOTP.return_value.verify.return_value = False
  35. mock_TOTP.return_value.provisioning_uri.return_value = "test-uri"
  36. self.client.login(username="[email protected]", password="password")
  37. self.set_sudo_flag()
  38. payload = {"code": "000000"}
  39. r = self.client.post(self.url, payload, follow=True)
  40. self.assertContains(r, "The code you entered was incorrect.")
  41. self.profile.refresh_from_db()
  42. self.assertIsNone(self.profile.totp)
  43. self.assertIsNone(self.profile.totp_created)
  44. def test_it_checks_if_totp_already_configured(self):
  45. self.profile.totp = "0" * 32
  46. self.profile.save()
  47. self.client.login(username="[email protected]", password="password")
  48. self.set_sudo_flag()
  49. r = self.client.get(self.url)
  50. self.assertEqual(r.status_code, 400)
  51. @patch("hc.accounts.views.pyotp.totp.TOTP")
  52. def test_it_handles_non_numeric_code(self, mock_TOTP):
  53. mock_TOTP.return_value.verify.return_value = False
  54. mock_TOTP.return_value.provisioning_uri.return_value = "test-uri"
  55. self.client.login(username="[email protected]", password="password")
  56. self.set_sudo_flag()
  57. payload = {"code": "AAAAAA"}
  58. r = self.client.post(self.url, payload, follow=True)
  59. self.assertContains(r, "Enter a valid value")