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.

98 lines
3.1 KiB

  1. from unittest.mock import patch
  2. from django.test.utils import override_settings
  3. from hc.test import BaseTestCase
  4. from hc.accounts.models import Credential
  5. @override_settings(RP_ID="testserver")
  6. class AddCredentialTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.url = "/accounts/two_factor/add/"
  10. def test_it_requires_sudo_mode(self):
  11. self.client.login(username="[email protected]", password="password")
  12. r = self.client.get(self.url)
  13. self.assertContains(r, "We have sent a confirmation code")
  14. @override_settings(RP_ID=None)
  15. def test_it_requires_rp_id(self):
  16. self.client.login(username="[email protected]", password="password")
  17. self.set_sudo_flag()
  18. r = self.client.get(self.url)
  19. self.assertEqual(r.status_code, 404)
  20. def test_it_shows_form(self):
  21. self.client.login(username="[email protected]", password="password")
  22. self.set_sudo_flag()
  23. r = self.client.get(self.url)
  24. self.assertContains(r, "Add Security Key")
  25. # It should put a "state" key in the session:
  26. self.assertIn("state", self.client.session)
  27. @patch("hc.accounts.views._get_credential_data")
  28. def test_it_adds_credential(self, mock_get_credential_data):
  29. mock_get_credential_data.return_value = b"dummy-credential-data"
  30. self.client.login(username="[email protected]", password="password")
  31. self.set_sudo_flag()
  32. payload = {
  33. "name": "My New Key",
  34. "client_data_json": "e30=",
  35. "attestation_object": "e30=",
  36. }
  37. r = self.client.post(self.url, payload, follow=True)
  38. self.assertRedirects(r, "/accounts/profile/")
  39. self.assertContains(r, "Added security key <strong>My New Key</strong>")
  40. c = Credential.objects.get()
  41. self.assertEqual(c.name, "My New Key")
  42. def test_it_rejects_bad_base64(self):
  43. self.client.login(username="[email protected]", password="password")
  44. self.set_sudo_flag()
  45. payload = {
  46. "name": "My New Key",
  47. "client_data_json": "not valid base64",
  48. "attestation_object": "not valid base64",
  49. }
  50. r = self.client.post(self.url, payload)
  51. self.assertEqual(r.status_code, 400)
  52. def test_it_requires_client_data_json(self):
  53. self.client.login(username="[email protected]", password="password")
  54. self.set_sudo_flag()
  55. payload = {
  56. "name": "My New Key",
  57. "attestation_object": "e30=",
  58. }
  59. r = self.client.post(self.url, payload)
  60. self.assertEqual(r.status_code, 400)
  61. @patch("hc.accounts.views._get_credential_data")
  62. def test_it_handles_authentication_failure(self, mock_get_credential_data):
  63. mock_get_credential_data.return_value = None
  64. self.client.login(username="[email protected]", password="password")
  65. self.set_sudo_flag()
  66. payload = {
  67. "name": "My New Key",
  68. "client_data_json": "e30=",
  69. "attestation_object": "e30=",
  70. }
  71. r = self.client.post(self.url, payload, follow=True)
  72. self.assertEqual(r.status_code, 400)