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.

90 lines
3.4 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. def test_leaving_works(self):
  11. self.client.login(username="[email protected]", password="password")
  12. form = {"code": str(self.project.code), "leave_project": "1"}
  13. r = self.client.post("/accounts/profile/", form)
  14. self.assertContains(r, "Left project <strong>Alices Project</strong>")
  15. self.assertNotContains(r, "Member")
  16. self.bobs_profile.refresh_from_db()
  17. self.assertFalse(self.bob.memberships.exists())
  18. def test_leaving_checks_membership(self):
  19. self.client.login(username="[email protected]", password="password")
  20. form = {"code": str(self.project.code), "leave_project": "1"}
  21. r = self.client.post("/accounts/profile/", form)
  22. self.assertEqual(r.status_code, 400)
  23. def test_it_shows_project_membership(self):
  24. self.client.login(username="[email protected]", password="password")
  25. r = self.client.get("/accounts/profile/")
  26. self.assertContains(r, "Alices Project")
  27. self.assertContains(r, "Member")
  28. def test_it_shows_readonly_project_membership(self):
  29. self.bobs_membership.rw = False
  30. self.bobs_membership.save()
  31. self.client.login(username="[email protected]", password="password")
  32. r = self.client.get("/accounts/profile/")
  33. self.assertContains(r, "Alices Project")
  34. self.assertContains(r, "Read-only")
  35. def test_it_handles_no_projects(self):
  36. self.project.delete()
  37. self.client.login(username="[email protected]", password="password")
  38. r = self.client.get("/accounts/profile/")
  39. self.assertContains(r, "You do not have any projects. Create one!")
  40. @override_settings(RP_ID=None)
  41. def test_it_hides_2fa_section_if_rp_id_not_set(self):
  42. self.client.login(username="[email protected]", password="password")
  43. r = self.client.get("/accounts/profile/")
  44. self.assertNotContains(r, "Two-factor Authentication")
  45. @override_settings(RP_ID="testserver")
  46. def test_it_handles_no_credentials(self):
  47. self.client.login(username="[email protected]", password="password")
  48. r = self.client.get("/accounts/profile/")
  49. self.assertContains(r, "Two-factor Authentication")
  50. self.assertContains(r, "Your account has no registered security keys")
  51. @override_settings(RP_ID="testserver")
  52. def test_it_shows_security_key(self):
  53. Credential.objects.create(user=self.alice, name="Alices Key")
  54. self.client.login(username="[email protected]", password="password")
  55. r = self.client.get("/accounts/profile/")
  56. self.assertContains(r, "Alices Key")
  57. def test_it_handles_unusable_password(self):
  58. self.alice.set_unusable_password()
  59. self.alice.save()
  60. # Authenticate using the ProfileBackend and a token:
  61. token = self.profile.prepare_token("login")
  62. self.client.login(username="alice", token=token)
  63. r = self.client.get("/accounts/profile/")
  64. self.assertContains(r, "Set Password")
  65. self.assertNotContains(r, "Change Password")