Debian Codegraph @

tests/admin_views/test_password_form.py

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