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.

105 lines
4.0 KiB

  1. from django.test.utils import override_settings
  2. from hc.test import BaseTestCase
  3. from hc.accounts.models import Credential
  4. class ProfileTestCase(BaseTestCase):
  5. def test_it_shows_profile_page(self):
  6. self.client.login(username="[email protected]", password="password")
  7. r = self.client.get("/accounts/profile/")
  8. self.assertContains(r, "Email and Password")
  9. self.assertContains(r, "Change Password")
  10. self.assertContains(r, "Set Up Authenticator App")
  11. def test_leaving_works(self):
  12. self.client.login(username="[email protected]", password="password")
  13. form = {"code": str(self.project.code), "leave_project": "1"}
  14. r = self.client.post("/accounts/profile/", form)
  15. self.assertContains(r, "Left project <strong>Alices Project</strong>")
  16. self.assertNotContains(r, "Member")
  17. self.bobs_profile.refresh_from_db()
  18. self.assertFalse(self.bob.memberships.exists())
  19. def test_leaving_checks_membership(self):
  20. self.client.login(username="[email protected]", password="password")
  21. form = {"code": str(self.project.code), "leave_project": "1"}
  22. r = self.client.post("/accounts/profile/", form)
  23. self.assertEqual(r.status_code, 400)
  24. def test_it_shows_project_membership(self):
  25. self.client.login(username="[email protected]", password="password")
  26. r = self.client.get("/accounts/profile/")
  27. self.assertContains(r, "Alices Project")
  28. self.assertContains(r, "Member")
  29. def test_it_shows_readonly_project_membership(self):
  30. self.bobs_membership.role = "r"
  31. self.bobs_membership.save()
  32. self.client.login(username="[email protected]", password="password")
  33. r = self.client.get("/accounts/profile/")
  34. self.assertContains(r, "Alices Project")
  35. self.assertContains(r, "Read-only")
  36. def test_it_handles_no_projects(self):
  37. self.project.delete()
  38. self.client.login(username="[email protected]", password="password")
  39. r = self.client.get("/accounts/profile/")
  40. self.assertContains(r, "You do not have any projects. Create one!")
  41. @override_settings(RP_ID=None)
  42. def test_it_hides_security_keys_bits_if_rp_id_not_set(self):
  43. self.client.login(username="[email protected]", password="password")
  44. r = self.client.get("/accounts/profile/")
  45. self.assertContains(r, "Two-factor Authentication")
  46. self.assertNotContains(r, "Security keys")
  47. self.assertNotContains(r, "Add Security Key")
  48. @override_settings(RP_ID="testserver")
  49. def test_it_handles_no_credentials(self):
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.get("/accounts/profile/")
  52. self.assertContains(r, "Two-factor Authentication")
  53. self.assertContains(r, "Your account does not have any configured two-factor")
  54. @override_settings(RP_ID="testserver")
  55. def test_it_shows_security_key(self):
  56. Credential.objects.create(user=self.alice, name="Alices Key")
  57. self.client.login(username="[email protected]", password="password")
  58. r = self.client.get("/accounts/profile/")
  59. self.assertContains(r, "Alices Key")
  60. def test_it_handles_unusable_password(self):
  61. self.alice.set_unusable_password()
  62. self.alice.save()
  63. # Authenticate using the ProfileBackend and a token:
  64. token = self.profile.prepare_token("login")
  65. self.client.login(username="alice", token=token)
  66. r = self.client.get("/accounts/profile/")
  67. self.assertContains(r, "Set Password")
  68. self.assertNotContains(r, "Change Password")
  69. def test_it_shows_totp(self):
  70. self.profile.totp = "0" * 32
  71. self.profile.totp_created = "2020-01-01T00:00:00+00:00"
  72. self.profile.save()
  73. self.client.login(username="[email protected]", password="password")
  74. r = self.client.get("/accounts/profile/")
  75. self.assertContains(r, "Enabled")
  76. self.assertContains(r, "configured on Jan 1, 2020")
  77. self.assertNotContains(r, "Set Up Authenticator App")