File: test_password_form.py

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (144 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from django.contrib.admin.tests import AdminSeleniumTestCase
from django.contrib.auth.models import User
from django.test import override_settings
from django.urls import reverse


@override_settings(ROOT_URLCONF="auth_tests.urls_admin")
class SeleniumAuthTests(AdminSeleniumTestCase):
    available_apps = AdminSeleniumTestCase.available_apps

    def setUp(self):
        self.superuser = User.objects.create_superuser(
            username="super",
            password="secret",
            email="super@example.com",
        )

    def test_add_new_user(self):
        """A user with no password can be added.

        Enabling/disabling the usable password field shows/hides the password
        fields when adding a user.
        """
        from selenium.common import NoSuchElementException
        from selenium.webdriver.common.by import By

        user_add_url = reverse("auth_test_admin:auth_user_add")
        self.admin_login(username="super", password="secret")
        self.selenium.get(self.live_server_url + user_add_url)

        pw_switch_on = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="usable_password"][value="true"]'
        )
        pw_switch_off = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="usable_password"][value="false"]'
        )
        password1 = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="password1"]'
        )
        password2 = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="password2"]'
        )

        # Default is to set a password on user creation.
        self.assertIs(pw_switch_on.is_selected(), True)
        self.assertIs(pw_switch_off.is_selected(), False)

        # The password fields are visible.
        self.assertIs(password1.is_displayed(), True)
        self.assertIs(password2.is_displayed(), True)

        # Click to disable password-based authentication.
        pw_switch_off.click()

        # Radio buttons are updated accordingly.
        self.assertIs(pw_switch_on.is_selected(), False)
        self.assertIs(pw_switch_off.is_selected(), True)

        # The password fields are hidden.
        self.assertIs(password1.is_displayed(), False)
        self.assertIs(password2.is_displayed(), False)

        # The warning message should not be shown.
        with self.assertRaises(NoSuchElementException):
            self.selenium.find_element(By.ID, "id_unusable_warning")

    def test_change_password_for_existing_user(self):
        """A user can have their password changed or unset.

        Enabling/disabling the usable password field shows/hides the password
        fields and the warning about password lost.
        """
        from selenium.webdriver.common.by import By

        user = User.objects.create_user(
            username="ada", password="charles", email="ada@example.com"
        )
        user_url = reverse("auth_test_admin:auth_user_password_change", args=(user.pk,))
        self.admin_login(username="super", password="secret")
        self.selenium.get(self.live_server_url + user_url)

        pw_switch_on = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="usable_password"][value="true"]'
        )
        pw_switch_off = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="usable_password"][value="false"]'
        )
        password1 = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="password1"]'
        )
        password2 = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[name="password2"]'
        )
        submit_set = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[type="submit"].set-password'
        )
        submit_unset = self.selenium.find_element(
            By.CSS_SELECTOR, 'input[type="submit"].unset-password'
        )

        # By default password-based authentication is enabled.
        self.assertIs(pw_switch_on.is_selected(), True)
        self.assertIs(pw_switch_off.is_selected(), False)

        # The password fields are visible.
        self.assertIs(password1.is_displayed(), True)
        self.assertIs(password2.is_displayed(), True)

        # Only the set password submit button is visible.
        self.assertIs(submit_set.is_displayed(), True)
        self.assertIs(submit_unset.is_displayed(), False)

        # Click to disable password-based authentication.
        pw_switch_off.click()

        # Radio buttons are updated accordingly.
        self.assertIs(pw_switch_on.is_selected(), False)
        self.assertIs(pw_switch_off.is_selected(), True)

        # The password fields are hidden.
        self.assertIs(password1.is_displayed(), False)
        self.assertIs(password2.is_displayed(), False)

        # Only the unset password submit button is visible.
        self.assertIs(submit_unset.is_displayed(), True)
        self.assertIs(submit_set.is_displayed(), False)

        # The warning about password being lost is shown.
        warning = self.selenium.find_element(By.ID, "id_unusable_warning")
        self.assertIs(warning.is_displayed(), True)

        # Click to enable password-based authentication.
        pw_switch_on.click()

        # The warning disappears.
        self.assertIs(warning.is_displayed(), False)

        # The password fields are shown.
        self.assertIs(password1.is_displayed(), True)
        self.assertIs(password2.is_displayed(), True)

        # Only the set password submit button is visible.
        self.assertIs(submit_set.is_displayed(), True)
        self.assertIs(submit_unset.is_displayed(), False)