@webship-js/django-steps 0.0.0-stage → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ @users
2
+ Feature: The accounts and the groups of the admin
3
+ As the administrator
4
+ I want to see each account's status and groups, and to give a group its permissions
5
+ So that every member of the staff can do their work, and no more
6
+
7
+ No scenario creates or deletes an account: the accounts are configuration.
8
+
9
+ Background:
10
+ Given I am a logged in user with the "administrator" user
11
+
12
+ @critical
13
+ Scenario: The accounts of the site and their groups
14
+ Then the user "administrator" should be a superuser
15
+ And the user "editor" should be staff
16
+ And the user "editor" should not be a superuser
17
+ And the user "editor" should be in the "Editors" group
18
+ And the user "viewer" should be in the "Viewers" group
19
+ And the user "viewer" should be active
20
+
21
+ Scenario: The groups carry the permissions of their members
22
+ Then the "Editors" group should have the "Polls | question | Can change question" permission
23
+ And the "Editors" group should have the "Can delete question" permission
24
+ And the "Viewers" group should have the "Can view question" permission
25
+ And the "Viewers" group should not have the "Can change question" permission
26
+
27
+ Scenario: The users list is searched and filtered
28
+ Given I am on the "Users" list
29
+ When I search the list for "edit"
30
+ Then the list should show "editor"
31
+ And the list should not show "viewer"
32
+ When I search the list for ""
33
+ And I choose "Viewers" in the "By groups" filter
34
+ Then the list should show "viewer"
35
+ And the list should not show "editor"
36
+ And the "viewer" row should show "yes" in the "Staff status" column
37
+
38
+ Scenario: A group gets its permissions with the two-list selector
39
+ Given there is no "Reviewers" in the "Groups" list
40
+ When I open the add form of "Groups"
41
+ And I set "Name" to "Reviewers"
42
+ And I filter the available "Permissions" with "can view question"
43
+ And I choose "Polls | question | Can view question" in the "Permissions" selector
44
+ Then the "Permissions" selector should have "Polls | question | Can view question" chosen
45
+ When I save the form
46
+ Then I should see the success message "The group “Reviewers” was added successfully."
47
+ And the "Reviewers" group should have the "Can view question" permission
48
+ And the "Reviewers" group should not have the "Can change question" permission
49
+
50
+ Scenario: A group name in use is refused
51
+ When I open the add form of "Groups"
52
+ And I set "Name" to "Editors"
53
+ And I save the form
54
+ Then the field "Name" should have the error "Group with this Name already exists."
55
+
56
+ @js-off
57
+ Scenario: The editor may not open the accounts
58
+ Given I am a logged in user with the "editor" user
59
+ Then I should not be allowed to open "/admin/auth/user/"
60
+ And I should not be allowed to open "/admin/auth/group/add/"
61
+ And I should be allowed to open "/admin/polls/question/"
@@ -0,0 +1,33 @@
1
+ @view-only
2
+ Feature: An account that may only view
3
+ As the administrator
4
+ I want an account with only the view permission to read the polls without changing them
5
+ So that a reviewer sees everything and breaks nothing
6
+
7
+ Background:
8
+ Given I am a logged in user with the "viewer" user
9
+
10
+ @critical
11
+ Scenario: The list has no add button and no actions
12
+ Given I am on the "Questions" list
13
+ Then I should see "Select question to view"
14
+ And the list should not offer the action "Delete selected questions"
15
+ And I should not see "Add question"
16
+ And the list should count 24 questions
17
+
18
+ Scenario: An object opens in a read-only form
19
+ Given I am on the "Questions" list
20
+ When I search the list for "What's up"
21
+ And I open "What's up?" from the list
22
+ Then I should see the "View question" form for "What's up?"
23
+ And the form should be read-only
24
+ And "Question text" should be read-only and show "What's up?"
25
+ And "Tags" should be read-only and show "Fun, Quick"
26
+ And the "Choices" inline should list "The sky"
27
+ And I should see "Close"
28
+
29
+ @js-off
30
+ Scenario: The forms that change are refused
31
+ Then I should not be allowed to open "/admin/polls/question/add/"
32
+ And I should not be allowed to open "/admin/auth/user/"
33
+ And I should be allowed to open "/admin/polls/question/"
@@ -0,0 +1,6 @@
1
+ # Step definitions
2
+
3
+ The pack's steps, one file per area of the admin; `django-helpers.js` holds the
4
+ shared plumbing and registers no step. Every step carries a JSDoc block with five
5
+ `Example #n:` lines, which `npm run test:examples` checks against the step and
6
+ every other definition loaded with it.
@@ -0,0 +1,229 @@
1
+ 'use strict';
2
+
3
+ // The accounts: changing one's own password (and its refusals), and the users
4
+ // and groups of the admin: an account's status, its groups, a group's
5
+ // permissions. A changed password is set back after the scenario, as the data
6
+ // administrator; no step creates or deletes an account.
7
+
8
+ const { When, Then } = require('@cucumber/cucumber');
9
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
10
+ const {
11
+ clean,
12
+ normal,
13
+ listed,
14
+ adminUrl,
15
+ hintFor,
16
+ open,
17
+ afterNavigation,
18
+ pageSummary,
19
+ formErrors,
20
+ currentUser,
21
+ userName,
22
+ forgetSession,
23
+ findObjects,
24
+ setPasswordOf,
25
+ afterScenario,
26
+ cleansUp,
27
+ } = require('./django-helpers');
28
+
29
+ /** Open the password change form of the account that is logged in (the user tools' "Change password"). */
30
+ async function openPasswordChange(world) {
31
+ const page = world.page;
32
+ await open(world, adminUrl(world, 'password_change/'));
33
+ if (!(await page.locator('input[name="old_password"]').count())) {
34
+ throw friendly({ message: `The password change form did not open: ${await pageSummary(world)}.`, hint: hintFor(world, 'Log in first: Given I am a logged in user with the "editor" user') });
35
+ }
36
+ }
37
+
38
+ /** Send the password change form with an old password and two new ones. */
39
+ async function sendPasswordChange(world, oldPassword, first, second) {
40
+ const page = world.page;
41
+ await openPasswordChange(world);
42
+ await page.locator('input[name="old_password"]').fill(oldPassword);
43
+ await page.locator('input[name="new_password1"]').fill(first);
44
+ await page.locator('input[name="new_password2"]').fill(second);
45
+ await afterNavigation(world, () => page.locator('#content-main form [type="submit"]').first().click());
46
+ }
47
+
48
+ /** Whether the page says the password was changed: the admin's "Password change successful" page. */
49
+ async function passwordChanged(page) {
50
+ try {
51
+ return /\/password_change\/done\/?$/.test(new URL(page.url()).pathname);
52
+ } catch {
53
+ return false;
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Change the password of the account that is logged in, through the admin's
59
+ * own form ("Change password" in the user tools): its password from the users
60
+ * registry, then the new one twice. The step sets the password of the
61
+ * registry back after the scenario, as the data administrator.
62
+ *
63
+ * Example #1: When I change my password to "Changed-pass-2026!"
64
+ * Example #2: When I change my password to "Another-pass-2026?"
65
+ * Example #3: And I change my password to "Changed-pass-2026!"
66
+ * Example #4: When I change my password to "Third-pass-2026#"
67
+ * Example #5: Given I change my password to "Changed-pass-2026!"
68
+ */
69
+ When(/^(I |we )*change my password to "([^"]*)"$/, async function (pronounCase, password) {
70
+ const user = currentUser(this);
71
+ if (cleansUp(this)) {
72
+ afterScenario(this, `password:${user.username}`, async () => {
73
+ await setPasswordOf(this, user.username, user.password);
74
+ forgetSession(this, user.username);
75
+ });
76
+ }
77
+ await sendPasswordChange(this, user.password, password, password);
78
+ if (!(await passwordChanged(this.page))) {
79
+ const errors = [...await formErrors(this.page), ...(await this.page.locator('#content-main ul.errorlist li').allTextContents().catch(() => [])).map(clean)];
80
+ throw friendly({ message: `The admin refused the new password: ${listed(errors)}.`, hint: 'The admin\'s password rules (AUTH_PASSWORD_VALIDATORS) refuse short, common, numeric passwords and ones close to the account\'s name.' });
81
+ }
82
+ forgetSession(this, user.username);
83
+ });
84
+
85
+ /**
86
+ * Send the password change form with values that the admin should refuse:
87
+ * an old password, and the new password and its confirmation. "with my old
88
+ * password" types the password of the users registry. Assert the refusal with
89
+ * the next step; a password the admin accepts is set back after the scenario.
90
+ *
91
+ * Example #1: When I try to change my password with the old password "Not-the-password-1" and the new passwords "Changed-pass-2026!" and "Changed-pass-2026!"
92
+ * Example #2: When I try to change my password with my old password and the new passwords "Changed-pass-2026!" and "Changed-pass-2026?"
93
+ * Example #3: And we try to change my password with my old password and the new passwords "password" and "password"
94
+ * Example #4: When I try to change my password with my old password and the new passwords "12345678901" and "12345678901"
95
+ * Example #5: And I try to change my password with the old password "" and the new passwords "" and ""
96
+ */
97
+ When(/^(I |we )*try to change my password with (?:the old password "([^"]*)"|my old password) and the new passwords "([^"]*)" and "([^"]*)"$/, async function (pronounCase, oldPassword, first, second) {
98
+ const user = currentUser(this);
99
+ if (cleansUp(this)) {
100
+ afterScenario(this, `password:${user.username}`, async () => {
101
+ await setPasswordOf(this, user.username, user.password);
102
+ forgetSession(this, user.username);
103
+ });
104
+ }
105
+ await sendPasswordChange(this, oldPassword === null || oldPassword === undefined ? user.password : oldPassword, first || '', second || '');
106
+ });
107
+
108
+ /**
109
+ * Assert that the password change went through: the admin's "Password change
110
+ * successful" page.
111
+ *
112
+ * Example #1: Then my password should be changed
113
+ * Example #2: And my password should be changed
114
+ * Example #3: Then my password should be changed
115
+ * Example #4: And my password should be changed
116
+ * Example #5: Then my password should be changed
117
+ */
118
+ Then(/^my password should be changed$/, async function () {
119
+ if (!(await passwordChanged(this.page))) {
120
+ const errors = await formErrors(this.page);
121
+ throw friendly({ message: `The password was not changed: ${errors.length ? listed(errors) : await pageSummary(this)}.`, hint: 'Change it with: When I change my password to "…"' });
122
+ }
123
+ });
124
+
125
+ /**
126
+ * Assert that the admin refused the password change with an error (or a part
127
+ * of it), on the form or on one of its fields.
128
+ *
129
+ * Example #1: Then the password change should be refused with "Your old password was entered incorrectly. Please enter it again."
130
+ * Example #2: Then the password change should be refused with "The two password fields didn’t match."
131
+ * Example #3: And the password change should be refused with "This password is too common."
132
+ * Example #4: Then the password change should be refused with "This password is entirely numeric."
133
+ * Example #5: And the password change should be refused with "This field is required."
134
+ */
135
+ Then(/^the password change should be refused with "([^"]*)"$/, async function (expected) {
136
+ const page = this.page;
137
+ const errors = [...await formErrors(page), ...(await page.locator('#content-main ul.errorlist li').allTextContents().catch(() => [])).map(clean)];
138
+ if ((await passwordChanged(page)) || !errors.some((e) => normal(e).includes(normal(expected)))) {
139
+ throw friendly({
140
+ message: (await passwordChanged(page)) ? 'The password change was not refused: the password was changed.' : `The password change was not refused with "${expected}". Its errors: ${listed(errors)}.`,
141
+ hint: hintFor(this, 'Send the form first: When I try to change my password with …'),
142
+ });
143
+ }
144
+ });
145
+
146
+ /** The page of a user, by a handle of the users registry or a user name. */
147
+ async function openUser(world, who) {
148
+ const name = userName(world, who);
149
+ const urls = await findObjects(world, 'Users', name);
150
+ if (!urls.length) {
151
+ throw friendly({ message: `The "Users" list has no "${name}".`, hint: 'The build of the site makes the accounts of the users registry.' });
152
+ }
153
+ await open(world, urls[0]);
154
+ return name;
155
+ }
156
+
157
+ /**
158
+ * Assert an account's status on its page in the admin: staff (it may log in
159
+ * to the admin), a superuser (every permission), active. The account that is
160
+ * logged in must be allowed to see users.
161
+ *
162
+ * Example #1: Then the user "editor" should be staff
163
+ * Example #2: Then the user "editor" should not be a superuser
164
+ * Example #3: And the user "administrator" should be a superuser
165
+ * Example #4: Then the user "viewer" should be active
166
+ * Example #5: And the user "viewer" should not be a superuser
167
+ */
168
+ Then(/^the user "([^"]*)" should( not)* be (staff|a superuser|active)$/, async function (who, notCase, status) {
169
+ const name = await openUser(this, who);
170
+ const field = { staff: 'is_staff', 'a superuser': 'is_superuser', active: 'is_active' }[status];
171
+ const box = this.page.locator(`input[name="${field}"]`);
172
+ const readonly = this.page.locator(`.field-${field} .readonly img, .field-${field} .readonly`);
173
+ let on;
174
+ if (await box.count()) on = await box.first().isChecked();
175
+ else if (await readonly.count()) on = /true|yes/i.test((await readonly.first().getAttribute('alt').catch(() => '')) || (await readonly.first().innerText()));
176
+ else throw friendly({ message: `The page of "${name}" has no "${status}" field: ${await pageSummary(this)}.`, hint: 'The account that is logged in must be allowed to see users.' });
177
+ if (on === !!notCase) {
178
+ throw friendly({ message: `The user "${name}" is ${on ? '' : 'not '}${status}.`, hint: 'The build of the site sets the accounts\' status.' });
179
+ }
180
+ });
181
+
182
+ /**
183
+ * Assert that an account is (or is not) in a group, as its page in the admin
184
+ * shows it.
185
+ *
186
+ * Example #1: Then the user "editor" should be in the "Editors" group
187
+ * Example #2: Then the user "viewer" should be in the "Viewers" group
188
+ * Example #3: And the user "editor" should not be in the "Viewers" group
189
+ * Example #4: Then the user "administrator" should not be in the "Editors" group
190
+ * Example #5: And the user "viewer" should not be in the "Editors" group
191
+ */
192
+ Then(/^the user "([^"]*)" should( not)* be in the "([^"]*)" group$/, async function (who, notCase, group) {
193
+ const name = await openUser(this, who);
194
+ const page = this.page;
195
+ let groups = (await page.locator('select[name="groups"] option').evaluateAll((os) => os.filter((o) => o.selected || o.closest('select').id.endsWith('_to')).map((o) => o.textContent.trim()))).map(clean);
196
+ if (!groups.length && !(await page.locator('select[name="groups"]').count())) {
197
+ groups = (await page.locator('.field-groups .readonly').allTextContents().catch(() => [])).flatMap((t) => t.split(',')).map(clean).filter(Boolean);
198
+ }
199
+ const has = groups.some((g) => normal(g) === normal(group));
200
+ if (has === !!notCase) {
201
+ throw friendly({ message: notCase ? `The user "${name}" is in the "${group}" group.` : `The user "${name}" is not in the "${group}" group. Its groups: ${listed(groups)}.`, hint: 'The groups of an account are the chosen list of its "Groups" selector.' });
202
+ }
203
+ });
204
+
205
+ /**
206
+ * Assert that a group has (or does not have) a permission, as the group's
207
+ * page in the admin shows it: the permission as the selector lists it
208
+ * ("Polls | question | Can change question"), or its last part ("Can change
209
+ * question").
210
+ *
211
+ * Example #1: Then the "Editors" group should have the "Polls | question | Can change question" permission
212
+ * Example #2: Then the "Viewers" group should have the "Can view question" permission
213
+ * Example #3: And the "Viewers" group should not have the "Can change question" permission
214
+ * Example #4: Then the "Editors" group should not have the "Can delete user" permission
215
+ * Example #5: And the "Editors" group should have the "Can add choice" permission
216
+ */
217
+ Then(/^the "([^"]*)" group should( not)* have the "([^"]*)" permission$/, async function (group, notCase, permission) {
218
+ const urls = await findObjects(this, 'Groups', group);
219
+ if (!urls.length) {
220
+ throw friendly({ message: `The "Groups" list has no "${group}".`, hint: 'Write the group as the "Groups" list shows it.' });
221
+ }
222
+ await open(this, urls[0]);
223
+ const page = this.page;
224
+ const chosen = (await page.locator('select[name="permissions"] option').evaluateAll((os) => os.filter((o) => o.selected || o.closest('select').id.endsWith('_to')).map((o) => o.textContent.trim()))).map(clean);
225
+ const has = chosen.some((p) => normal(p) === normal(permission) || normal(p).endsWith('| ' + normal(permission)));
226
+ if (has === !!notCase) {
227
+ throw friendly({ message: notCase ? `The "${group}" group has the "${permission}" permission.` : `The "${group}" group does not have the "${permission}" permission. It has: ${listed(chosen)}.`, hint: 'A group\'s permissions are the chosen list of its "Permissions" selector.' });
228
+ }
229
+ });