@webship-js/codeigniter-steps 0.0.0-stage → 2.0.1
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.
- package/LICENSE +21 -0
- package/README.md +448 -2
- package/package.json +62 -4
- package/tests/features/README.md +24 -0
- package/tests/features/access.feature +52 -0
- package/tests/features/accessibility.feature +32 -0
- package/tests/features/development.feature +59 -0
- package/tests/features/forgery.feature +36 -0
- package/tests/features/framework-pages.feature +42 -0
- package/tests/features/login-link.feature +43 -0
- package/tests/features/registration.feature +65 -0
- package/tests/features/session.feature +104 -0
- package/tests/step-definitions/README.md +37 -0
- package/tests/step-definitions/codeigniter-access.steps.js +122 -0
- package/tests/step-definitions/codeigniter-forms.steps.js +217 -0
- package/tests/step-definitions/codeigniter-helpers.js +702 -0
- package/tests/step-definitions/codeigniter-mail.steps.js +117 -0
- package/tests/step-definitions/codeigniter-pages.steps.js +209 -0
- package/tests/step-definitions/codeigniter-session.steps.js +277 -0
- package/tests/step-definitions/codeigniter-toolbar.steps.js +154 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The mail the app sends, read from the mailbox of the test app
|
|
4
|
+
// (`worldParameters.mailUrl`, a Mailpit): that a mail arrived, that none did,
|
|
5
|
+
// what it says, and a link of it opened as a person does.
|
|
6
|
+
//
|
|
7
|
+
// A mail step reads the mail that arrived since the step that asked for it
|
|
8
|
+
// (a login link, codeigniter-session.steps.js), or since the scenario started:
|
|
9
|
+
// never a mail of an earlier scenario or run.
|
|
10
|
+
|
|
11
|
+
const { When, Then } = require('@cucumber/cucumber');
|
|
12
|
+
const { friendly } = require('webship-js/tests/step-definitions/webship');
|
|
13
|
+
const {
|
|
14
|
+
budget,
|
|
15
|
+
clean,
|
|
16
|
+
listed,
|
|
17
|
+
open,
|
|
18
|
+
addressOf,
|
|
19
|
+
mailUrl,
|
|
20
|
+
mailList,
|
|
21
|
+
mailTo,
|
|
22
|
+
mailLink,
|
|
23
|
+
} = require('./codeigniter-helpers');
|
|
24
|
+
|
|
25
|
+
/** The moment from which a mail step reads the mailbox. */
|
|
26
|
+
function mailSince(world) {
|
|
27
|
+
return world.__mailAskedAt || world.__scenarioStartedAt || 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Assert that the app sent an e-mail with a subject to an address (a handle of
|
|
32
|
+
* the users registry stands for its address) since the step that asked for it.
|
|
33
|
+
*
|
|
34
|
+
* Example #1: Then an email with the subject "Your Login Link" should be sent to "user"
|
|
35
|
+
* Example #2: Then an email with the subject "Your Login Link" should be sent to "admin"
|
|
36
|
+
* Example #3: And an email with the subject "Your Login Link" should be sent to "user@example.com"
|
|
37
|
+
* Example #4: Then an email with the subject "Your Login Link" should be sent to "developer"
|
|
38
|
+
* Example #5: And an email with the subject "Your Login Link" should be sent to "developer@example.com"
|
|
39
|
+
*/
|
|
40
|
+
Then(/^an email with the subject "([^"]*)" should be sent to "([^"]*)"$/, async function (subject, who) {
|
|
41
|
+
await mailTo(this, addressOf(this, who), { since: mailSince(this), subject });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Assert that the app sent no e-mail to an address since the step that asked
|
|
46
|
+
* for one: a request it refused sends nothing.
|
|
47
|
+
*
|
|
48
|
+
* Example #1: Then no email should be sent to "nobody@example.com"
|
|
49
|
+
* Example #2: Then no email should be sent to "user"
|
|
50
|
+
* Example #3: And no email should be sent to "banned"
|
|
51
|
+
* Example #4: Then no email should be sent to "no-such-account@example.org"
|
|
52
|
+
* Example #5: And no email should be sent to "nobody@example.com"
|
|
53
|
+
*/
|
|
54
|
+
Then(/^no email should be sent to "([^"]*)"$/, async function (who) {
|
|
55
|
+
const address = addressOf(this, who);
|
|
56
|
+
// Mail goes out while the app answers the request; a moment more is for a slow mail catcher.
|
|
57
|
+
await this.page.waitForTimeout(1000);
|
|
58
|
+
let messages;
|
|
59
|
+
try {
|
|
60
|
+
messages = (await mailList(this, address)).filter((message) => Date.parse(message.Created) >= mailSince(this) - 1000);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
throw friendly({ message: `The mailbox at ${mailUrl(this)} could not be read.`, cause: err, hint: 'Check worldParameters.mailUrl in cucumber.js.' });
|
|
63
|
+
}
|
|
64
|
+
if (messages.length) {
|
|
65
|
+
throw friendly({ message: `Expected no e-mail to ${address}, but the app sent: ${listed(messages.map((message) => message.Subject))}.`, hint: 'The step counts the mail that arrived since the step that asked for one.' });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Assert that the newest e-mail to an address with a subject holds a text
|
|
71
|
+
* (in its text or its HTML version, white space collapsed).
|
|
72
|
+
*
|
|
73
|
+
* Example #1: Then the email with the subject "Your Login Link" to "user" should contain "Username: user"
|
|
74
|
+
* Example #2: Then the email with the subject "Your Login Link" to "admin" should contain "Some information about the person:"
|
|
75
|
+
* Example #3: And the email with the subject "Your Login Link" to "user" should contain "IP Address:"
|
|
76
|
+
* Example #4: Then the email with the subject "Your Login Link" to "user@example.com" should contain "Login"
|
|
77
|
+
* Example #5: And the email with the subject "Your Login Link" to "developer" should contain "Username: developer"
|
|
78
|
+
*/
|
|
79
|
+
Then(/^the email with the subject "([^"]*)" to "([^"]*)" should contain "([^"]*)"$/, async function (subject, who, text) {
|
|
80
|
+
const mail = await mailTo(this, addressOf(this, who), { since: mailSince(this), subject });
|
|
81
|
+
const entities = (s) => String(s).replace(/ | /g, ' ').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'");
|
|
82
|
+
const plain = clean(entities(mail.text));
|
|
83
|
+
const html = clean(entities(mail.html.replace(/<[^>]+>/g, ' ')));
|
|
84
|
+
if (!plain.includes(clean(text)) && !html.includes(clean(text))) {
|
|
85
|
+
throw friendly({ message: `The e-mail "${mail.subject}" to ${addressOf(this, who)} does not contain "${text}". It reads: ${(plain || html).slice(0, 300)}…`, hint: 'The step compares the text with white space collapsed.' });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Open a link of an e-mail the app sent, by the text a person clicks (the
|
|
91
|
+
* button of the mail), as a person does: the newest e-mail to the address
|
|
92
|
+
* (a handle of the users registry stands for its address) that holds such a
|
|
93
|
+
* link, since the step that asked for it.
|
|
94
|
+
*
|
|
95
|
+
* Example #1: When I open the "Login" link of the email to "user"
|
|
96
|
+
* Example #2: When we open the "Login" link of the email to "admin"
|
|
97
|
+
* Example #3: And I open the "Login" link of the email to "user@example.com"
|
|
98
|
+
* Example #4: When I open the "Login" link of the email to "developer"
|
|
99
|
+
* Example #5: When we open the "Login" link of the email to "developer@example.com"
|
|
100
|
+
*/
|
|
101
|
+
When(/^(I |we )*open the "([^"]*)" link of the email to "([^"]*)"$/, async function (pronounCase, label, who) {
|
|
102
|
+
const address = addressOf(this, who);
|
|
103
|
+
const deadline = Date.now() + budget(this) * 2;
|
|
104
|
+
let link = null;
|
|
105
|
+
let mail = null;
|
|
106
|
+
for (;;) {
|
|
107
|
+
mail = await mailTo(this, address, { since: mailSince(this), timeout: Math.max(deadline - Date.now(), 1000) });
|
|
108
|
+
link = mailLink(mail, label);
|
|
109
|
+
if (link.href || Date.now() > deadline) break;
|
|
110
|
+
await this.page.waitForTimeout(300);
|
|
111
|
+
}
|
|
112
|
+
if (!link.href) {
|
|
113
|
+
throw friendly({ message: `The newest e-mail to ${address} ("${mail.subject}") has no link "${label}". Its links: ${listed(link.anchors.map((a) => a.text))}.`, hint: 'Name the link by the text of the button of the e-mail.' });
|
|
114
|
+
}
|
|
115
|
+
this.__mailLinkOpened = link.href;
|
|
116
|
+
await open(this, link.href);
|
|
117
|
+
});
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The pages the framework itself answers with: the welcome page of a new app
|
|
4
|
+
// and the environment it says it runs in, the error pages by their status,
|
|
5
|
+
// the check that a live app shows no debug information, and a form a page of
|
|
6
|
+
// another site sends to the app.
|
|
7
|
+
//
|
|
8
|
+
// The status a step reads is the one the browser received for the page on
|
|
9
|
+
// screen (webship-js records it for every navigation), so it follows the
|
|
10
|
+
// session and the forms the scenario sent; webship-js's own "the response
|
|
11
|
+
// status code should be ..." reads the same.
|
|
12
|
+
|
|
13
|
+
const { When, Then } = require('@cucumber/cucumber');
|
|
14
|
+
const { friendly } = require('webship-js/tests/step-definitions/webship');
|
|
15
|
+
const {
|
|
16
|
+
budget,
|
|
17
|
+
clean,
|
|
18
|
+
siteUrl,
|
|
19
|
+
currentPath,
|
|
20
|
+
settle,
|
|
21
|
+
resolveUser,
|
|
22
|
+
pageSummary,
|
|
23
|
+
} = require('./codeigniter-helpers');
|
|
24
|
+
|
|
25
|
+
// The words an error page of the framework prints with its code, in its
|
|
26
|
+
// production views (the title, or the text).
|
|
27
|
+
const STATUS_WORDS = {
|
|
28
|
+
400: /bad request/i,
|
|
29
|
+
403: /forbidden|not allowed/i,
|
|
30
|
+
404: /not found|cannot seem to find/i,
|
|
31
|
+
500: /whoops|snag|server error/i,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Assert that the browser shows the error page of a status: the page answered
|
|
36
|
+
* with that status, and it says so: the code in its title or main heading
|
|
37
|
+
* ("404 - Page Not Found"), or the words the framework's own error page uses
|
|
38
|
+
* for it ("Whoops!" for 500). An app with error pages of its own passes as
|
|
39
|
+
* long as they print the code.
|
|
40
|
+
*
|
|
41
|
+
* Example #1: Then I should see the 404 error page
|
|
42
|
+
* Example #2: Then I should see the 500 error page
|
|
43
|
+
* Example #3: And I should see the 403 error page
|
|
44
|
+
* Example #4: Then we should see the 404 error page
|
|
45
|
+
* Example #5: Then I should not see the 404 error page
|
|
46
|
+
*/
|
|
47
|
+
Then(/^(I |we )*should( not)* see the (\d{3}) error page$/, async function (pronounCase, notCase, code) {
|
|
48
|
+
const wanted = Number(code);
|
|
49
|
+
const words = STATUS_WORDS[wanted];
|
|
50
|
+
const shows = (s) => s.status === wanted && (s.title.includes(code) || s.heading.includes(code) || (!!words && (words.test(s.title) || words.test(s.heading))));
|
|
51
|
+
const summary = await pageSummary(this);
|
|
52
|
+
if (notCase ? !shows(summary) : shows(summary)) return;
|
|
53
|
+
const what = `HTTP ${summary.status === null ? '(status unknown)' : summary.status}, "${summary.title}"${summary.heading ? `, heading "${summary.heading}"` : ''}`;
|
|
54
|
+
if (notCase) {
|
|
55
|
+
throw friendly({ message: `Expected not to see the ${code} error page, but the browser shows it (${what}) at ${currentPath(this)}.`, hint: 'The step reads the status the browser received for the page on screen.' });
|
|
56
|
+
}
|
|
57
|
+
throw friendly({
|
|
58
|
+
message: `Expected the ${code} error page, but the browser shows ${what} at ${currentPath(this)}: "${summary.text.slice(0, 160)}".`,
|
|
59
|
+
hint: summary.status === wanted
|
|
60
|
+
? `The status is right, but the page does not say ${code} in its title or main heading: an app with its own error pages should.`
|
|
61
|
+
: 'The step reads the status the browser received for the page on screen (the one the last navigation, link or form brought).',
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/** The signs of a debug page the page shows: what a live app must never print. */
|
|
66
|
+
async function debugSigns(world) {
|
|
67
|
+
return world.page.evaluate(() => {
|
|
68
|
+
const text = (document.body ? document.body.innerText : '').replace(/\s+/g, ' ');
|
|
69
|
+
const headings = [...document.querySelectorAll('h1, h2')].map((h) => h.textContent.replace(/\s+/g, ' ').trim());
|
|
70
|
+
const signs = [];
|
|
71
|
+
const exception = [document.title, ...headings].find((h) => /(?:^|\\)[A-Z][A-Za-z]*(?:Exception|Error)\b/.test(h) && !/^\d{3}\b/.test(h));
|
|
72
|
+
if (exception) signs.push(`the exception class "${exception}"`);
|
|
73
|
+
if (document.querySelector('#backtrace, .trace, ol.trace')) signs.push('a backtrace');
|
|
74
|
+
if (/\b(?:Exception|Stack) trace\b/i.test(text)) signs.push('an exception trace');
|
|
75
|
+
const file = text.match(/[\w./-]+\.php(?::| at line )\d+/);
|
|
76
|
+
if (file) signs.push(`a source file "${file[0]}"`);
|
|
77
|
+
const runtime = text.match(/\bPHP:? \d+\.\d+(?:\.\d+)?\b/);
|
|
78
|
+
if (runtime) signs.push(`the runtime version "${runtime[0]}"`);
|
|
79
|
+
if (/\b[A-Z][A-Za-z]+\\[A-Z][A-Za-z]+\\[A-Z]/.test(text)) signs.push('class names of the app');
|
|
80
|
+
if (/Can't find a route for/i.test(text)) signs.push('the routing detail of a missing page');
|
|
81
|
+
return signs;
|
|
82
|
+
}).catch(() => []);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Assert that the page shows no debug information: no exception class, no
|
|
87
|
+
* backtrace, no source file and line, no runtime version, no class names. A
|
|
88
|
+
* live app answers an error with a plain error page; a debug page shows how
|
|
89
|
+
* the app is built to anybody. "should show" asserts the other side: the
|
|
90
|
+
* debug page of an app that runs in development.
|
|
91
|
+
*
|
|
92
|
+
* Example #1: Then the page should not show debug information
|
|
93
|
+
* Example #2: And the page should not show debug information
|
|
94
|
+
* Example #3: Then the page should show debug information
|
|
95
|
+
* Example #4: But the page should not show debug information
|
|
96
|
+
* Example #5: And the page should show debug information
|
|
97
|
+
*/
|
|
98
|
+
Then(/^the page should( not)* show debug information$/, async function (notCase) {
|
|
99
|
+
const signs = await debugSigns(this);
|
|
100
|
+
if (notCase && signs.length) {
|
|
101
|
+
throw friendly({
|
|
102
|
+
message: `The page shows debug information: ${signs.join(', ')} (${currentPath(this)}).`,
|
|
103
|
+
hint: 'A live app runs in the production environment (CI_ENVIRONMENT = production in its .env): an error then renders the plain error page.',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (!notCase && !signs.length) {
|
|
107
|
+
const summary = await pageSummary(this);
|
|
108
|
+
throw friendly({
|
|
109
|
+
message: `Expected the page to show debug information, but it shows none (HTTP ${summary.status}, "${summary.title}").`,
|
|
110
|
+
hint: 'Only an app that runs in the development environment shows its debug page for an error.',
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Send the login form of an account to an address of the app from a page of
|
|
117
|
+
* another site, as a forged form on somebody else's page does: the browser
|
|
118
|
+
* posts the address and the password of the account (a handle of the users
|
|
119
|
+
* registry), with no valid security token, and says it comes from another
|
|
120
|
+
* site. An app that protects its forms refuses it; assert that with the next
|
|
121
|
+
* step ("I should not be logged in").
|
|
122
|
+
*
|
|
123
|
+
* Example #1: When a page of another site sends the login form of "user" to "/login"
|
|
124
|
+
* Example #2: When a page of another site sends the login form of "admin" to "/login"
|
|
125
|
+
* Example #3: And a page of another site sends the login form of "developer" to "/login"
|
|
126
|
+
* Example #4: When a page of another site sends the login form of "user" to "/register"
|
|
127
|
+
* Example #5: When a page of another site sends the login form of "admin" to "/login/magic-link"
|
|
128
|
+
*/
|
|
129
|
+
When(/^a page of another site sends the login form of "([^"]*)" to "([^"]*)"$/, async function (handle, path) {
|
|
130
|
+
const user = resolveUser(this, handle);
|
|
131
|
+
const action = siteUrl(this, path);
|
|
132
|
+
const page = this.page;
|
|
133
|
+
const esc = (s) => String(s === undefined || s === null ? '' : s).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<');
|
|
134
|
+
await page.goto('about:blank');
|
|
135
|
+
await page.setContent(`<!doctype html><title>Another site</title><form method="post" action="${esc(action)}">`
|
|
136
|
+
+ '<input type="hidden" name="csrf_test_name" value="a-token-of-another-site"><input type="hidden" name="_token" value="a-token-of-another-site">'
|
|
137
|
+
+ `<input type="hidden" name="email" value="${esc(user.email)}"><input type="hidden" name="username" value="${esc(user.username || '')}">`
|
|
138
|
+
+ `<input type="hidden" name="password" value="${esc(user.password)}"><button type="submit">Send</button></form>`);
|
|
139
|
+
try {
|
|
140
|
+
await Promise.all([
|
|
141
|
+
page.waitForNavigation({ timeout: budget(this) }),
|
|
142
|
+
page.locator('button[type="submit"]').click(),
|
|
143
|
+
]);
|
|
144
|
+
} catch (err) {
|
|
145
|
+
throw friendly({ message: `The form of the other site did not reach ${action}.`, cause: err, hint: 'Check launchUrl in cucumber.js.' });
|
|
146
|
+
}
|
|
147
|
+
await page.waitForLoadState('load', { timeout: budget(this) }).catch(() => {});
|
|
148
|
+
await settle(this);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Assert that the browser shows, or does not show, the welcome page of a new
|
|
153
|
+
* app: its main heading starts with "Welcome to", it has the "About this page"
|
|
154
|
+
* section, and its footer says how long the page took and in which
|
|
155
|
+
* environment it runs. A real app replaces it; a new app shows it at its home
|
|
156
|
+
* address.
|
|
157
|
+
*
|
|
158
|
+
* Example #1: Then I should see the welcome page
|
|
159
|
+
* Example #2: Then I should not see the welcome page
|
|
160
|
+
* Example #3: And we should see the welcome page
|
|
161
|
+
* Example #4: Then we should not see the welcome page
|
|
162
|
+
* Example #5: But I should see the welcome page
|
|
163
|
+
*/
|
|
164
|
+
Then(/^(I |we )*should( not)* see the welcome page$/, async function (pronounCase, notCase) {
|
|
165
|
+
const read = () => this.page.evaluate(() => {
|
|
166
|
+
const heading = document.querySelector('header h1, h1');
|
|
167
|
+
const footer = document.querySelector('footer');
|
|
168
|
+
return {
|
|
169
|
+
heading: heading ? heading.textContent.replace(/\s+/g, ' ').trim() : '',
|
|
170
|
+
about: [...document.querySelectorAll('h1, h2')].some((h) => /^\s*About this page\s*$/i.test(h.textContent)),
|
|
171
|
+
footer: footer ? footer.innerText.replace(/\s+/g, ' ').trim() : '',
|
|
172
|
+
};
|
|
173
|
+
}).catch(() => ({ heading: '', about: false, footer: '' }));
|
|
174
|
+
const seen = await read();
|
|
175
|
+
const welcome = /^Welcome to\b/i.test(seen.heading) && seen.about && /Page rendered in .* seconds/i.test(seen.footer) && /Environment:/i.test(seen.footer);
|
|
176
|
+
if (notCase && welcome) {
|
|
177
|
+
throw friendly({ message: `Expected not to see the welcome page, but ${currentPath(this)} shows it ("${seen.heading}").`, hint: 'A new app shows the welcome page at its home address until the app replaces it.' });
|
|
178
|
+
}
|
|
179
|
+
if (!notCase && !welcome) {
|
|
180
|
+
const missing = [];
|
|
181
|
+
if (!/^Welcome to\b/i.test(seen.heading)) missing.push(`a main heading that starts with "Welcome to" (it is "${seen.heading}")`);
|
|
182
|
+
if (!seen.about) missing.push('the "About this page" section');
|
|
183
|
+
if (!/Page rendered in .* seconds/i.test(seen.footer) || !/Environment:/i.test(seen.footer)) missing.push('the footer that says how long the page took and the environment');
|
|
184
|
+
throw friendly({ message: `Expected the welcome page at ${currentPath(this)}, but it has no ${missing.join(', no ')}.`, hint: 'The welcome page is the home page of a new app made from the app starter: open "/".' });
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Assert the environment the page says the app runs in: the "Environment:"
|
|
190
|
+
* line of the welcome page's footer, or of the banner of the debug page.
|
|
191
|
+
* "production" is how a live app runs; "development" shows the debug toolbar
|
|
192
|
+
* and the debug page.
|
|
193
|
+
*
|
|
194
|
+
* Example #1: Then the page should say it runs in the "production" environment
|
|
195
|
+
* Example #2: Then the page should say it runs in the "development" environment
|
|
196
|
+
* Example #3: And the page should say it runs in the "production" environment
|
|
197
|
+
* Example #4: Then the page should say it runs in the "testing" environment
|
|
198
|
+
* Example #5: But the page should say it runs in the "production" environment
|
|
199
|
+
*/
|
|
200
|
+
Then(/^the page should say it runs in the "([^"]*)" environment$/, async function (environment) {
|
|
201
|
+
const text = clean(await this.page.locator('body').innerText({ timeout: budget(this) }).catch(() => ''));
|
|
202
|
+
const said = (text.match(/Environment:\s*([A-Za-z]+)/) || [])[1];
|
|
203
|
+
if (!said) {
|
|
204
|
+
throw friendly({ message: `The page does not say which environment the app runs in (${currentPath(this)}): it has no "Environment:" line.`, hint: 'The welcome page says it in its footer, and the debug page in its banner. Open "/" first.' });
|
|
205
|
+
}
|
|
206
|
+
if (said.toLowerCase() !== environment.toLowerCase()) {
|
|
207
|
+
throw friendly({ message: `Expected the page to say it runs in the "${environment}" environment, but it says "${said}".`, hint: 'The environment is CI_ENVIRONMENT in the .env of the app (php spark env <environment> sets it).' });
|
|
208
|
+
}
|
|
209
|
+
});
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The session: logging in by a handle of the users registry or by an address
|
|
4
|
+
// and a password, logging out, who is signed in, the login form, a session
|
|
5
|
+
// that ends while a remembered login stays, and the login link the app mails.
|
|
6
|
+
//
|
|
7
|
+
// Accounts are configuration, not test data: they are listed in cucumber.js
|
|
8
|
+
// (`worldParameters.users`) and created once by the build of the app. A
|
|
9
|
+
// feature never creates or deletes users.
|
|
10
|
+
//
|
|
11
|
+
// Every assertion waits on the page (the assertion is the wait), and every
|
|
12
|
+
// failure is a friendly error with a hint instead of a raw Playwright timeout.
|
|
13
|
+
|
|
14
|
+
const { Before, Given, When, Then } = require('@cucumber/cucumber');
|
|
15
|
+
const { friendly } = require('webship-js/tests/step-definitions/webship');
|
|
16
|
+
const {
|
|
17
|
+
budget,
|
|
18
|
+
clean,
|
|
19
|
+
listed,
|
|
20
|
+
escapeRegExp,
|
|
21
|
+
siteUrl,
|
|
22
|
+
loginPath,
|
|
23
|
+
logoutPath,
|
|
24
|
+
loginLinkPath,
|
|
25
|
+
accountPath,
|
|
26
|
+
currentPath,
|
|
27
|
+
isOnPath,
|
|
28
|
+
afterNavigation,
|
|
29
|
+
open,
|
|
30
|
+
assertPresence,
|
|
31
|
+
resolveUser,
|
|
32
|
+
addressOf,
|
|
33
|
+
findField,
|
|
34
|
+
documentStatus,
|
|
35
|
+
loginForm,
|
|
36
|
+
LOGIN_HINT,
|
|
37
|
+
loginThroughTheForm,
|
|
38
|
+
submitLogin,
|
|
39
|
+
signedIn,
|
|
40
|
+
logInAs,
|
|
41
|
+
forgetSession,
|
|
42
|
+
} = require('./codeigniter-helpers');
|
|
43
|
+
|
|
44
|
+
// A mail step reads the mail that arrived since the scenario started, or since
|
|
45
|
+
// the step of this pack that asked for mail (a login link): never a mail of an
|
|
46
|
+
// earlier scenario or run.
|
|
47
|
+
Before(function () {
|
|
48
|
+
this.__mailAskedAt = 0;
|
|
49
|
+
this.__scenarioStartedAt = Date.now();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Log in as an account of the users registry (`worldParameters.users` in
|
|
54
|
+
* cucumber.js), by its handle. An account that logged in earlier in the run
|
|
55
|
+
* takes its session over; otherwise the step logs in through the login form.
|
|
56
|
+
* The browser ends on the page the app shows after a login (the home page, or
|
|
57
|
+
* the page a visitor was sent to the login form from).
|
|
58
|
+
*
|
|
59
|
+
* Example #1: Given I am a logged in user with the "admin" user
|
|
60
|
+
* Example #2: Given we are a logged in user with the "developer" user
|
|
61
|
+
* Example #3: Given I am a logged in user with "user"
|
|
62
|
+
* Example #4: And I am a logged in user with the "user" user
|
|
63
|
+
* Example #5: Given we are a logged in user with the "admin" user
|
|
64
|
+
*/
|
|
65
|
+
Given(/^(I am |we are )*a logged in user with( the)* "([^"]*)"( user)*$/, { timeout: 120000 }, async function (pronounCase, theCase, handle, userCase) {
|
|
66
|
+
await logInAs(this, handle);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Log in through the login form as an account of the users registry, as a
|
|
71
|
+
* person does: a new session, never one taken over. For the scenarios that
|
|
72
|
+
* test the form itself; the step fails when the app refuses the login. "and
|
|
73
|
+
* ask to be remembered" ticks the "Remember me?" box first.
|
|
74
|
+
*
|
|
75
|
+
* Example #1: When I log in as "user" with the login form
|
|
76
|
+
* Example #2: When we log in as "admin" with the login form
|
|
77
|
+
* Example #3: When I log in as "user" with the login form and ask to be remembered
|
|
78
|
+
* Example #4: And I log in as "developer" with the login form
|
|
79
|
+
* Example #5: When we log in as "user" with the login form and ask to be remembered
|
|
80
|
+
*/
|
|
81
|
+
When(/^(I |we )*log in as "([^"]*)" with the login form( and ask to be remembered)?$/, { timeout: 120000 }, async function (pronounCase, handle, rememberCase) {
|
|
82
|
+
const user = resolveUser(this, handle);
|
|
83
|
+
const outcome = await loginThroughTheForm(this, user, { remember: !!rememberCase });
|
|
84
|
+
if (outcome.error) {
|
|
85
|
+
throw friendly({
|
|
86
|
+
message: `The login form refused "${handle}" (${user.email}): ${outcome.error}`,
|
|
87
|
+
hint: 'Check the address and the password of the account in worldParameters.users, and that the build of the app created the account.',
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Send the login form with an address and a password. The step does not
|
|
94
|
+
* expect the login to succeed: assert the outcome with the next step (the
|
|
95
|
+
* error message of the login page, the page the app sends a signed-in
|
|
96
|
+
* account to).
|
|
97
|
+
*
|
|
98
|
+
* Example #1: When I log in with the email "user@example.com" and the password "wrong-password"
|
|
99
|
+
* Example #2: When we log in with the email "nobody@example.com" and the password "Whatever-123!"
|
|
100
|
+
* Example #3: When I log in with the email "banned@example.com" and the password "wrong-password"
|
|
101
|
+
* Example #4: And I log in with the email "a@b" and the password "wrong-password"
|
|
102
|
+
* Example #5: When we log in with the email "no-such-account@example.org" and the password "Nothing-at-all-1!"
|
|
103
|
+
*/
|
|
104
|
+
When(/^(I |we )*log in with the email "([^"]*)" and the password "([^"]*)"$/, { timeout: 120000 }, async function (pronounCase, email, password) {
|
|
105
|
+
await submitLogin(this, { email, password });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Log out as a person does: the "Log out" (or "Logout", "Sign out") link or
|
|
110
|
+
* button of the page; on a page that offers none, the logout address of the
|
|
111
|
+
* app (`worldParameters.logoutPath`, /logout). The session ends on the
|
|
112
|
+
* server, so no later scenario takes it over.
|
|
113
|
+
*
|
|
114
|
+
* Example #1: When I log out
|
|
115
|
+
* Example #2: When we log out
|
|
116
|
+
* Example #3: And I log out
|
|
117
|
+
* Example #4: Then I log out
|
|
118
|
+
* Example #5: Given I log out
|
|
119
|
+
*/
|
|
120
|
+
When(/^(I |we )*log out$/, async function (pronounCase) {
|
|
121
|
+
const page = this.page;
|
|
122
|
+
forgetSession(this);
|
|
123
|
+
const names = /^\s*(log ?out|sign ?out)\s*$/i;
|
|
124
|
+
const control = page.getByRole('link', { name: names }).or(page.getByRole('button', { name: names })).filter({ visible: true });
|
|
125
|
+
if (await control.count()) {
|
|
126
|
+
await afterNavigation(this, () => control.first().click());
|
|
127
|
+
} else {
|
|
128
|
+
await open(this, siteUrl(this, logoutPath(this)));
|
|
129
|
+
}
|
|
130
|
+
if (await loginForm(this).isVisible().catch(() => false)) return;
|
|
131
|
+
if (isOnPath(this, logoutPath(this))) {
|
|
132
|
+
throw friendly({ message: `Logging out stopped at ${this.page.url()} (HTTP ${await documentStatus(this)}).`, hint: 'The app ends a session at its logout address (worldParameters.logoutPath, /logout) and sends the browser on to the login page.' });
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Assert that somebody is, or nobody is, logged in. The step opens the login
|
|
138
|
+
* page (`loginPath`, /login): the app sends a signed-in account away from it,
|
|
139
|
+
* and shows its form to a visitor.
|
|
140
|
+
*
|
|
141
|
+
* Example #1: Then I should be logged in
|
|
142
|
+
* Example #2: Then I should not be logged in
|
|
143
|
+
* Example #3: Then we should be logged in
|
|
144
|
+
* Example #4: And we should not be logged in
|
|
145
|
+
* Example #5: But I should not be logged in
|
|
146
|
+
*/
|
|
147
|
+
Then(/^(I |we )*should( not)* be logged in$/, async function (pronounCase, notCase) {
|
|
148
|
+
const yes = await signedIn(this);
|
|
149
|
+
if (notCase && yes) {
|
|
150
|
+
throw friendly({ message: `Expected nobody to be logged in, but the login page sent the browser on to ${currentPath(this)}: somebody is signed in.`, hint: 'Log out first (I log out), or start from a new visitor (I am an anonymous user).' });
|
|
151
|
+
}
|
|
152
|
+
if (!notCase && !yes) {
|
|
153
|
+
throw friendly({ message: `Expected to be logged in, but ${loginPath(this)} shows the login form: nobody is signed in.`, hint: 'Log in first (I am a logged in user with the "..." user).' });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Assert who is logged in, by a handle of the users registry: the page that
|
|
159
|
+
* names the signed-in account (`worldParameters.accountPath`, /account on the
|
|
160
|
+
* test app) shows its user name or its address.
|
|
161
|
+
*
|
|
162
|
+
* Example #1: Then I should be logged in as "user"
|
|
163
|
+
* Example #2: Then we should be logged in as "admin"
|
|
164
|
+
* Example #3: And I should be logged in as "developer"
|
|
165
|
+
* Example #4: Then I should be logged in as "admin"
|
|
166
|
+
* Example #5: And we should be logged in as "user"
|
|
167
|
+
*/
|
|
168
|
+
Then(/^(I |we )*should be logged in as "([^"]*)"$/, async function (pronounCase, handle) {
|
|
169
|
+
const user = resolveUser(this, handle);
|
|
170
|
+
await open(this, siteUrl(this, accountPath(this)));
|
|
171
|
+
if (isOnPath(this, loginPath(this)) || (await loginForm(this).isVisible().catch(() => false))) {
|
|
172
|
+
throw friendly({ message: `Expected "${handle}" (${user.email}) to be logged in, but ${accountPath(this)} sent the browser to the login form: nobody is signed in.`, hint: 'Log in first (I am a logged in user with the "..." user).' });
|
|
173
|
+
}
|
|
174
|
+
const status = await documentStatus(this);
|
|
175
|
+
if (status && status >= 400) {
|
|
176
|
+
throw friendly({ message: `The account page ${accountPath(this)} answered HTTP ${status}.`, hint: 'Set worldParameters.accountPath in cucumber.js to a page of the app that names the signed-in account (its user name or its address).' });
|
|
177
|
+
}
|
|
178
|
+
const text = clean(await this.page.locator('body').innerText().catch(() => ''));
|
|
179
|
+
const names = [user.email, user.username].filter(Boolean);
|
|
180
|
+
// The page names the account by its address, else by its user name as a word of its own (a user
|
|
181
|
+
// name such as "admin" is also a word of "Admin area").
|
|
182
|
+
const shown = (text.match(/[^\s@()<>,;:"']+@[^\s@()<>,;:"']+\.[A-Za-z]{2,}/g) || []).map((m) => m.replace(/[.]+$/, ''));
|
|
183
|
+
const named = shown.length
|
|
184
|
+
? shown.some((address) => address.toLowerCase() === user.email.toLowerCase())
|
|
185
|
+
: !!user.username && new RegExp(`(?<![\\w.@-])${escapeRegExp(user.username)}(?![\\w@-])`).test(text);
|
|
186
|
+
if (!named) {
|
|
187
|
+
throw friendly({
|
|
188
|
+
message: `Expected "${handle}" (${names.join(', ')}) to be logged in, but ${accountPath(this)} names ${shown.length ? listed(shown) : 'neither'}.`,
|
|
189
|
+
hint: 'worldParameters.accountPath is the page that names the signed-in account, by its address or its user name.',
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Assert that the page shows, or does not show, the login form (an address or
|
|
196
|
+
* user name field, a password field and a button), for instance after a page
|
|
197
|
+
* that needs a session sent a visitor to it.
|
|
198
|
+
*
|
|
199
|
+
* Example #1: Then I should see the login form
|
|
200
|
+
* Example #2: Then I should not see the login form
|
|
201
|
+
* Example #3: And we should see the login form
|
|
202
|
+
* Example #4: Then we should not see the login form
|
|
203
|
+
* Example #5: But I should see the login form
|
|
204
|
+
*/
|
|
205
|
+
Then(/^(I |we )*should( not)* see the login form$/, async function (pronounCase, notCase) {
|
|
206
|
+
await assertPresence(loginForm(this), notCase, {
|
|
207
|
+
label: 'the login form',
|
|
208
|
+
hint: LOGIN_HINT,
|
|
209
|
+
timeout: budget(this),
|
|
210
|
+
found: async () => `The page is ${this.page.url()} ("${clean(await this.page.title())}").`,
|
|
211
|
+
});
|
|
212
|
+
if (!notCase && !isOnPath(this, loginPath(this))) {
|
|
213
|
+
throw friendly({ message: `A login form is on the page, but the page is ${currentPath(this)}, not ${loginPath(this)}.`, hint: 'Check worldParameters.loginPath in cucumber.js.' });
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Let the session of the browser end, as an expired session does: the
|
|
219
|
+
* browser drops the session cookie of the app and keeps its other cookies,
|
|
220
|
+
* such as the one of a remembered login. Reload or open a page next, and
|
|
221
|
+
* assert who is still signed in. The session cookie is the one named by
|
|
222
|
+
* `worldParameters.sessionCookie`, else the cookie of the site whose name
|
|
223
|
+
* says "session".
|
|
224
|
+
*
|
|
225
|
+
* Example #1: When my session expires
|
|
226
|
+
* Example #2: When our session expires
|
|
227
|
+
* Example #3: And my session expires
|
|
228
|
+
* Example #4: Given my session expires
|
|
229
|
+
* Example #5: When the session expires
|
|
230
|
+
*/
|
|
231
|
+
When(/^(my|our|the) session expires$/, async function (whoseCase) {
|
|
232
|
+
const context = this.page.context();
|
|
233
|
+
const host = new URL(siteUrl(this, '/')).hostname;
|
|
234
|
+
const all = await context.cookies();
|
|
235
|
+
const named = this.parameters && this.parameters.sessionCookie;
|
|
236
|
+
const ofSite = all.filter((c) => host === c.domain.replace(/^\./, '') || host.endsWith(c.domain.replace(/^\./, '')));
|
|
237
|
+
const session = ofSite.filter((c) => (named ? c.name === named : /sess/i.test(c.name)));
|
|
238
|
+
if (!session.length) {
|
|
239
|
+
throw friendly({
|
|
240
|
+
message: `The browser has no session cookie of ${host} to drop. Its cookies there: ${listed(ofSite.map((c) => c.name))}.`,
|
|
241
|
+
hint: 'Log in first. If the app names its session cookie another way, set worldParameters.sessionCookie in cucumber.js.',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const kept = all.filter((c) => !session.includes(c));
|
|
245
|
+
await context.clearCookies();
|
|
246
|
+
if (kept.length) await context.addCookies(kept);
|
|
247
|
+
forgetSession(this);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Ask for a login link by mail, as a person does: the page of the login form
|
|
252
|
+
* that sends one (`worldParameters.loginLinkPath`, /login/magic-link), the
|
|
253
|
+
* address of an account (a handle of the users registry stands for its
|
|
254
|
+
* address, anything else is used as it is), the "Send" button. Assert the
|
|
255
|
+
* outcome with the next step: the page's message, the mail.
|
|
256
|
+
*
|
|
257
|
+
* Example #1: When I ask for a login link for "user"
|
|
258
|
+
* Example #2: When we ask for a login link for "admin"
|
|
259
|
+
* Example #3: When I ask for a login link for "nobody@example.com"
|
|
260
|
+
* Example #4: And I ask for a login link for "developer"
|
|
261
|
+
* Example #5: When we ask for a login link for "user@example.com"
|
|
262
|
+
*/
|
|
263
|
+
When(/^(I |we )*ask for a login link for "([^"]*)"$/, async function (pronounCase, who) {
|
|
264
|
+
const address = addressOf(this, who);
|
|
265
|
+
await open(this, siteUrl(this, loginLinkPath(this)));
|
|
266
|
+
let field;
|
|
267
|
+
try {
|
|
268
|
+
field = await findField(this, 'email', { timeout: budget(this) });
|
|
269
|
+
} catch (err) {
|
|
270
|
+
throw friendly({ message: `The page that sends a login link did not show its form (${this.page.url()}).`, hint: 'It is at /login/magic-link (worldParameters.loginLinkPath), linked from the login form as "Use a Login Link": an "Email Address" field and a "Send" button.' });
|
|
271
|
+
}
|
|
272
|
+
await field.fill(address);
|
|
273
|
+
const form = this.page.locator('form').filter({ has: field });
|
|
274
|
+
const submit = form.locator('button[type="submit"], input[type="submit"]').first();
|
|
275
|
+
this.__mailAskedAt = Date.now();
|
|
276
|
+
await afterNavigation(this, () => submit.click());
|
|
277
|
+
});
|