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.

63 lines
2.2 KiB

  1. from django.test.utils import override_settings
  2. from hc.test import BaseTestCase
  3. from hc.accounts.models import Credential
  4. @override_settings(RP_ID="testserver")
  5. class RemoveCredentialTestCase(BaseTestCase):
  6. def setUp(self):
  7. super().setUp()
  8. self.c = Credential.objects.create(user=self.alice, name="Alices Key")
  9. self.url = f"/accounts/two_factor/{self.c.code}/remove/"
  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, "Remove Security Key")
  25. self.assertContains(r, "Alices Key")
  26. self.assertContains(r, "two-factor authentication will no longer be active")
  27. def test_it_skips_warning_when_other_2fa_methods_exist(self):
  28. self.profile.totp = "0" * 32
  29. self.profile.save()
  30. self.client.login(username="[email protected]", password="password")
  31. self.set_sudo_flag()
  32. r = self.client.get(self.url)
  33. self.assertNotContains(r, "two-factor authentication will no longer be active")
  34. def test_it_removes_credential(self):
  35. self.client.login(username="[email protected]", password="password")
  36. self.set_sudo_flag()
  37. r = self.client.post(self.url, {"remove_credential": ""}, follow=True)
  38. self.assertRedirects(r, "/accounts/profile/")
  39. self.assertContains(r, "Removed security key <strong>Alices Key</strong>")
  40. self.assertFalse(self.alice.credentials.exists())
  41. def test_it_checks_owner(self):
  42. self.client.login(username="[email protected]", password="password")
  43. self.set_sudo_flag()
  44. r = self.client.post(self.url, {"remove_credential": ""})
  45. self.assertEqual(r.status_code, 400)