test_password_form.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from django.contrib.admin.tests import AdminSeleniumTestCase
  2. from django.contrib.auth.models import User
  3. from django.test import override_settings
  4. from django.urls import reverse
  5. @override_settings(ROOT_URLCONF="auth_tests.urls_admin")
  6. class SeleniumAuthTests(AdminSeleniumTestCase):
  7. available_apps = AdminSeleniumTestCase.available_apps
  8. def setUp(self):
  9. self.superuser = User.objects.create_superuser(
  10. username="super",
  11. password="secret",
  12. email="super@example.com",
  13. )
  14. def test_add_new_user(self):
  15. """A user with no password can be added.
  16. Enabling/disabling the usable password field shows/hides the password
  17. fields when adding a user.
  18. """
  19. from selenium.common import NoSuchElementException
  20. from selenium.webdriver.common.by import By
  21. user_add_url = reverse("auth_test_admin:auth_user_add")
  22. self.admin_login(username="super", password="secret")
  23. self.selenium.get(self.live_server_url + user_add_url)
  24. pw_switch_on = self.selenium.find_element(
  25. By.CSS_SELECTOR, 'input[name="usable_password"][value="true"]'
  26. )
  27. pw_switch_off = self.selenium.find_element(
  28. By.CSS_SELECTOR, 'input[name="usable_password"][value="false"]'
  29. )
  30. password1 = self.selenium.find_element(
  31. By.CSS_SELECTOR, 'input[name="password1"]'
  32. )
  33. password2 = self.selenium.find_element(
  34. By.CSS_SELECTOR, 'input[name="password2"]'
  35. )
  36. # Default is to set a password on user creation.
  37. self.assertIs(pw_switch_on.is_selected(), True)
  38. self.assertIs(pw_switch_off.is_selected(), False)
  39. # The password fields are visible.
  40. self.assertIs(password1.is_displayed(), True)
  41. self.assertIs(password2.is_displayed(), True)
  42. # Click to disable password-based authentication.
  43. pw_switch_off.click()
  44. # Radio buttons are updated accordingly.
  45. self.assertIs(pw_switch_on.is_selected(), False)
  46. self.assertIs(pw_switch_off.is_selected(), True)
  47. # The password fields are hidden.
  48. self.assertIs(password1.is_displayed(), False)
  49. self.assertIs(password2.is_displayed(), False)
  50. # The warning message should not be shown.
  51. with self.assertRaises(NoSuchElementException):
  52. self.selenium.find_element(By.ID, "id_unusable_warning")
  53. def test_change_password_for_existing_user(self):
  54. """A user can have their password changed or unset.
  55. Enabling/disabling the usable password field shows/hides the password
  56. fields and the warning about password lost.
  57. """
  58. from selenium.webdriver.common.by import By
  59. user = User.objects.create_user(
  60. username="ada", password="charles", email="ada@example.com"
  61. )
  62. user_url = reverse("auth_test_admin:auth_user_password_change", args=(user.pk,))
  63. self.admin_login(username="super", password="secret")
  64. self.selenium.get(self.live_server_url + user_url)
  65. pw_switch_on = self.selenium.find_element(
  66. By.CSS_SELECTOR, 'input[name="usable_password"][value="true"]'
  67. )
  68. pw_switch_off = self.selenium.find_element(
  69. By.CSS_SELECTOR, 'input[name="usable_password"][value="false"]'
  70. )
  71. password1 = self.selenium.find_element(
  72. By.CSS_SELECTOR, 'input[name="password1"]'
  73. )
  74. password2 = self.selenium.find_element(
  75. By.CSS_SELECTOR, 'input[name="password2"]'
  76. )
  77. submit_set = self.selenium.find_element(
  78. By.CSS_SELECTOR, 'input[type="submit"].set-password'
  79. )
  80. submit_unset = self.selenium.find_element(
  81. By.CSS_SELECTOR, 'input[type="submit"].unset-password'
  82. )
  83. # By default password-based authentication is enabled.
  84. self.assertIs(pw_switch_on.is_selected(), True)
  85. self.assertIs(pw_switch_off.is_selected(), False)
  86. # The password fields are visible.
  87. self.assertIs(password1.is_displayed(), True)
  88. self.assertIs(password2.is_displayed(), True)
  89. # Only the set password submit button is visible.
  90. self.assertIs(submit_set.is_displayed(), True)
  91. self.assertIs(submit_unset.is_displayed(), False)
  92. # Click to disable password-based authentication.
  93. pw_switch_off.click()
  94. # Radio buttons are updated accordingly.
  95. self.assertIs(pw_switch_on.is_selected(), False)
  96. self.assertIs(pw_switch_off.is_selected(), True)
  97. # The password fields are hidden.
  98. self.assertIs(password1.is_displayed(), False)
  99. self.assertIs(password2.is_displayed(), False)
  100. # Only the unset password submit button is visible.
  101. self.assertIs(submit_unset.is_displayed(), True)
  102. self.assertIs(submit_set.is_displayed(), False)
  103. # The warning about password being lost is shown.
  104. warning = self.selenium.find_element(By.ID, "id_unusable_warning")
  105. self.assertIs(warning.is_displayed(), True)
  106. # Click to enable password-based authentication.
  107. pw_switch_on.click()
  108. # The warning disappears.
  109. self.assertIs(warning.is_displayed(), False)
  110. # The password fields are shown.
  111. self.assertIs(password1.is_displayed(), True)
  112. self.assertIs(password2.is_displayed(), True)
  113. # Only the set password submit button is visible.
  114. self.assertIs(submit_set.is_displayed(), True)
  115. self.assertIs(submit_unset.is_displayed(), False)