@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,228 @@
1
+ 'use strict';
2
+
3
+ // The session: logging in to the admin by a handle of the users registry or
4
+ // with a user name and a password, the refusal of the login form, logging out,
5
+ // and who is signed in.
6
+ //
7
+ // Accounts are configuration, not test data: they are listed in cucumber.js
8
+ // (`worldParameters.users`) and made once by the build of the site. A feature
9
+ // never creates or deletes an account.
10
+
11
+ const { Given, When, Then } = require('@cucumber/cucumber');
12
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
13
+ const {
14
+ budget,
15
+ normal,
16
+ listed,
17
+ adminUrl,
18
+ cutRequest,
19
+ CUT_HINT,
20
+ hintFor,
21
+ open,
22
+ afterNavigation,
23
+ pageSummary,
24
+ eventually,
25
+ resolveUser,
26
+ userName,
27
+ signedInName,
28
+ submitLogin,
29
+ logInAs,
30
+ forgetSession,
31
+ formErrors,
32
+ } = require('./django-helpers');
33
+
34
+ /**
35
+ * Log in to the admin as an account of the users registry
36
+ * (`worldParameters.users` in cucumber.js), by its handle. An account that
37
+ * logged in earlier in the run takes its session over; otherwise the step logs
38
+ * in through the login form. The step ends on the admin index.
39
+ *
40
+ * Example #1: Given I am a logged in user with the "editor" user
41
+ * Example #2: Given we are a logged in user with the "administrator" user
42
+ * Example #3: Given I am a logged in user with "viewer"
43
+ * Example #4: And I am a logged in user with the "viewer" user
44
+ * Example #5: Given we are a logged in user with the "editor" user
45
+ */
46
+ Given(/^(I am |we are )*a logged in user with( the)* "([^"]*)"( user)*$/, { timeout: 120000 }, async function (pronounCase, theCase, handle, userCase) {
47
+ await logInAs(this, handle);
48
+ });
49
+
50
+ /**
51
+ * Log in through the admin login form as an account of the users registry, as
52
+ * a person does: a new session, never one taken over. For the scenarios that
53
+ * test the form itself; the step fails when the admin refuses the login.
54
+ *
55
+ * Example #1: When I log in as "editor" with the login form
56
+ * Example #2: When we log in as "viewer" with the login form
57
+ * Example #3: And I log in as "administrator" with the login form
58
+ * Example #4: When I log in as "viewer" with the login form
59
+ * Example #5: Then we log in as "editor" with the login form
60
+ */
61
+ When(/^(I |we )*log in as "([^"]*)" with the login form$/, { timeout: 120000 }, async function (pronounCase, handle) {
62
+ const user = resolveUser(this, handle);
63
+ const outcome = await submitLogin(this, user.username, user.password);
64
+ if (!outcome.name) {
65
+ throw friendly({
66
+ message: `The login form refused "${handle}" (${user.username}): ${listed(outcome.errors)}.`,
67
+ hint: outcome.cut ? CUT_HINT : 'Check the user name and the password of the account in worldParameters.users, and that the build of the site made it a staff account.',
68
+ });
69
+ }
70
+ this.__djangoUser = user;
71
+ });
72
+
73
+ /**
74
+ * Send the admin login form with a user name and a password. The step does
75
+ * not expect the login to succeed: assert the outcome with the next step. A
76
+ * new password (after a password change) logs in this way too.
77
+ *
78
+ * Example #1: When I log in with the user name "nobody" and the password "Wrong-password-1"
79
+ * Example #2: When we log in with the user name "viewer" and the password "Not-the-password-2"
80
+ * Example #3: And I log in with the user name "editor" and the password "Changed-pass-2026!"
81
+ * Example #4: When I log in with the user name "" and the password "Wrong-password-1"
82
+ * Example #5: When we log in with the user name "Someone unknown" and the password "x"
83
+ */
84
+ When(/^(I |we )*log in with the user name "([^"]*)" and the password "([^"]*)"$/, { timeout: 120000 }, async function (pronounCase, username, password) {
85
+ const outcome = await submitLogin(this, username || '', password || '');
86
+ this.__loginOutcome = outcome;
87
+ if (outcome.name) {
88
+ const users = (this.parameters && this.parameters.users) || {};
89
+ const handle = Object.keys(users).find((h) => String(users[h].username).toLowerCase() === String(username).toLowerCase());
90
+ this.__djangoUser = { handle: handle || username, username, password };
91
+ }
92
+ });
93
+
94
+ /**
95
+ * Assert that the login page refused the login with an error (or a part of
96
+ * it), and that nobody is signed in. The admin writes one error for a wrong
97
+ * password, an unknown user name and an account that is not staff alike.
98
+ *
99
+ * Example #1: Then the login should be refused with "Please enter the correct username and password for a staff account."
100
+ * Example #2: Then the login should be refused with "Note that both fields may be case-sensitive."
101
+ * Example #3: And the login should be refused with "Please enter the correct username and password"
102
+ * Example #4: Then the login should be refused with "for a staff account"
103
+ * Example #5: Then the login should be refused with "Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive."
104
+ */
105
+ Then(/^the login should be refused with "([^"]*)"$/, async function (expected) {
106
+ const page = this.page;
107
+ const name = await signedInName(page);
108
+ const errors = await formErrors(page);
109
+ const onLogin = await page.locator('#login-form').count();
110
+ if (name || !onLogin || !errors.some((e) => normal(e).includes(normal(expected)))) {
111
+ throw friendly({
112
+ message: name
113
+ ? `The login was not refused: the admin greets "${name}".`
114
+ : `The login page did not refuse the login with "${expected}". Its errors: ${listed(errors)}; ${await pageSummary(this)}.`,
115
+ hint: hintFor(this, 'Send the form first (When I log in with the user name "..." and the password "..."); the admin writes its error above the form.'),
116
+ });
117
+ }
118
+ });
119
+
120
+ /**
121
+ * Start as a visitor of the admin: no session (the cookies of the browser
122
+ * cleared), on the admin login page. webship-js's "I am an anonymous user"
123
+ * opens the site's root instead, which a plain project does not have (the
124
+ * browser then logs its 404).
125
+ *
126
+ * Example #1: Given I am not logged in
127
+ * Example #2: Given we are not logged in
128
+ * Example #3: And I am not logged in
129
+ * Example #4: Given I am not logged in
130
+ * Example #5: When we are not logged in
131
+ */
132
+ Given(/^(I am |we are )*not logged in$/, async function (pronounCase) {
133
+ await this.page.context().clearCookies();
134
+ forgetSession(this);
135
+ this.__djangoUser = null;
136
+ await open(this, adminUrl(this, 'login/'));
137
+ });
138
+
139
+ /**
140
+ * Log out as a person does: the "Log out" button of the user tools (a form
141
+ * the admin sends with POST), then the "Logged out" page. On a page that is
142
+ * not the admin's, the step opens the admin index first.
143
+ *
144
+ * Example #1: When I log out
145
+ * Example #2: When we log out
146
+ * Example #3: And I log out
147
+ * Example #4: Then I log out
148
+ * Example #5: Given I log out
149
+ */
150
+ When(/^(I |we )*log out$/, { timeout: 90000 }, async function (pronounCase) {
151
+ const page = this.page;
152
+ forgetSession(this, this.__djangoUser && this.__djangoUser.username);
153
+ if (!(await page.locator('#logout-form').count())) await open(this, adminUrl(this));
154
+ if (!(await page.locator('#logout-form').count())) {
155
+ this.__djangoUser = null;
156
+ return; // nobody is signed in
157
+ }
158
+ await afterNavigation(this, () => page.locator('#logout-form [type="submit"]').first().click());
159
+ this.__djangoUser = null;
160
+ await eventually(this, () => signedInName(page), (name) => !name,
161
+ async (name) => ({ message: `After "Log out" the admin still greets "${name}": ${await pageSummary(this)}.`, hint: hintFor(this, 'The "Log out" button sends the logout form of the user tools.') }));
162
+ });
163
+
164
+ /**
165
+ * Assert who the admin greets in its user tools ("Welcome, editor."): a
166
+ * handle of the users registry or a user name. The admin greets an account
167
+ * by its first name when it has one, else by its user name.
168
+ *
169
+ * Example #1: Then I should be logged in as "editor"
170
+ * Example #2: Then we should be logged in as "administrator"
171
+ * Example #3: And I should be logged in as "viewer"
172
+ * Example #4: Then I should be logged in as "editor"
173
+ * Example #5: And we should be logged in as "viewer"
174
+ */
175
+ Then(/^(I |we )*should be logged in as "([^"]*)"$/, async function (pronounCase, who) {
176
+ const expected = userName(this, who);
177
+ const page = this.page;
178
+ if (!(await page.locator('#user-tools').count())) await open(this, adminUrl(this));
179
+ const name = await signedInName(page);
180
+ if (!name || normal(name) !== normal(expected)) {
181
+ throw friendly({
182
+ message: name ? `The admin greets "${name}", not "${expected}".` : `Nobody is logged in to the admin: ${await pageSummary(this)}.`,
183
+ hint: hintFor(this, 'Log in first: Given I am a logged in user with the "editor" user'),
184
+ });
185
+ }
186
+ });
187
+
188
+ /**
189
+ * Assert that someone is, or nobody is, logged in to the admin. On a page
190
+ * that is not the admin's, the step opens the admin index to see.
191
+ *
192
+ * Example #1: Then I should be logged in
193
+ * Example #2: Then I should not be logged in
194
+ * Example #3: And we should be logged in
195
+ * Example #4: And we should not be logged in
196
+ * Example #5: Then I should not be logged in
197
+ */
198
+ Then(/^(I |we )*should( not)* be logged in$/, async function (pronounCase, notCase) {
199
+ const page = this.page;
200
+ if (!(await page.locator('#user-tools, #login-form').count()) || cutRequest(this)) await open(this, adminUrl(this));
201
+ const name = await signedInName(page);
202
+ if (notCase && name) {
203
+ throw friendly({ message: `The admin greets "${name}": someone is logged in.`, hint: 'Log out first: When I log out' });
204
+ }
205
+ if (!notCase && !name) {
206
+ throw friendly({ message: `Nobody is logged in to the admin: ${await pageSummary(this)}.`, hint: hintFor(this, 'Log in first: Given I am a logged in user with the "editor" user') });
207
+ }
208
+ });
209
+
210
+ /**
211
+ * Assert that the page is (or is not) the admin login page: the form with
212
+ * "Username" and "Password" that a visitor is sent to.
213
+ *
214
+ * Example #1: Then I should be on the admin login page
215
+ * Example #2: Then I should not be on the admin login page
216
+ * Example #3: And we should be on the admin login page
217
+ * Example #4: Then we should not be on the admin login page
218
+ * Example #5: And I should be on the admin login page
219
+ */
220
+ Then(/^(I |we )*should( not)* be on the admin login page$/, async function (pronounCase, notCase) {
221
+ const page = this.page;
222
+ const read = async () => (await page.locator('#login-form input[name="username"]').count()) > 0;
223
+ const on = await eventually(this, read, (v) => v === !notCase, async () => ({
224
+ message: notCase ? `The page is the admin login page: ${await pageSummary(this)}.` : `The page is not the admin login page: ${await pageSummary(this)}.`,
225
+ hint: hintFor(this, 'A visitor who opens a page of the admin is sent to its login page.'),
226
+ }), notCase ? 2000 : budget(this)).catch((err) => { throw err; });
227
+ return on;
228
+ });