@webship-js/gitea-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.
@@ -0,0 +1,235 @@
1
+ 'use strict';
2
+
3
+ // Session and repository step definitions: logging in with the users registry, the login form and its refusal,
4
+ // logging out, who the session is, and a repository an account may open or is refused.
5
+ //
6
+ // The other step files: gitea-issues.steps.js (the issue list, one issue, its comments, closing and reopening it, the
7
+ // time spent on it, its assignee and milestone, a new issue) and gitea-milestones.steps.js (the milestones and the
8
+ // issues of one). The steps serve Gitea and Forgejo alike.
9
+ //
10
+ // Steps that webship-js already covers with its generic click, fill and see vocabulary are not duplicated here.
11
+ // Every assertion waits on its locator (the assertion is the wait), and every failure is a friendly error with a hint
12
+ // instead of a raw Playwright timeout stack.
13
+
14
+ const { Given, When, Then } = require('@cucumber/cucumber');
15
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
16
+ const {
17
+ budget,
18
+ exactly,
19
+ tidy,
20
+ open,
21
+ waitForValue,
22
+ resolveUser,
23
+ currentUser,
24
+ currentName,
25
+ dropSession,
26
+ submitLogin,
27
+ signInAs,
28
+ siteUrl,
29
+ findRepository,
30
+ } = require('./gitea-helpers');
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // Logging in (the first steps of the pack), then the rest of the session
34
+ // ---------------------------------------------------------------------------
35
+
36
+ /**
37
+ * Log in with an account of the users registry in cucumber.js (`worldParameters.users`), by its handle, and make sure
38
+ * the session is that account's. An account that is already logged in is kept. The first time in a run the account goes
39
+ * through the login form; after that its session is put back, so switching accounts inside a scenario is quick.
40
+ *
41
+ * Example #1: Given I am a logged in user with the "member" user
42
+ * Example #2: Given I am a logged in user with the "owner" user
43
+ * Example #3: Given we are a logged in user with the "reader" user
44
+ * Example #4: And I am a logged in user with the "stranger" user
45
+ * Example #5: When I am a logged in user with the "member" user
46
+ *
47
+ */
48
+ Given(/^(I am|we are) a logged in user with the "([^"]*)" user$/, async function (pronounCase, handle) {
49
+ await signInAs(this, handle);
50
+ });
51
+
52
+ /**
53
+ * Log in through the form with the login and the password of an account of the users registry, without the checks of
54
+ * "I am a logged in user with the … user": the step to use when the scenario is about the login itself. Follow it with
55
+ * a step that says what should happen.
56
+ *
57
+ * Example #1: When I log in with the "member" user
58
+ * Example #2: When I log in with the "owner" user
59
+ * Example #3: When we log in with the "reader" user
60
+ * Example #4: And I log in with the "stranger" user
61
+ * Example #5: When log in with the "member" user
62
+ *
63
+ */
64
+ When(/^(I |we )*log in with the "([^"]*)" user$/, async function (pronounCase, handle) {
65
+ const user = resolveUser(this, handle);
66
+ await dropSession(this);
67
+ await submitLogin(this, user.username, user.password);
68
+ });
69
+
70
+ /**
71
+ * Fill in the login form with a login and a password as typed in the step, and submit it. It asserts nothing: follow
72
+ * it with a step that says what should happen (logged in, or the login refused). For the accounts of the registry use
73
+ * "I am a logged in user with the … user".
74
+ *
75
+ * Example #1: When I log in as "member" with the password "not-the-right-password"
76
+ * Example #2: When I log in as "nobody" with the password "whatever"
77
+ * Example #3: When we log in as "reader" with the password "wrong"
78
+ * Example #4: And I log in as "owner" with the password "wrong-password"
79
+ * Example #5: When I log in as "member" with the password ""
80
+ *
81
+ */
82
+ When(/^(I |we )*log in as "([^"]*)" with the password "([^"]*)"$/, async function (pronounCase, login, password) {
83
+ await dropSession(this);
84
+ await submitLogin(this, login, password);
85
+ });
86
+
87
+ /**
88
+ * Make sure nobody is logged in: the browser forgets its session. (To end a session through the screens, use
89
+ * "I log out".)
90
+ *
91
+ * Example #1: Given I am not logged in
92
+ * Example #2: Given we are not logged in
93
+ * Example #3: And I am not logged in
94
+ * Example #4: When I am not logged in
95
+ * Example #5: Given I am not logged in
96
+ *
97
+ */
98
+ Given(/^(I am|we are) not logged in$/, async function (pronounCase) {
99
+ await dropSession(this);
100
+ });
101
+
102
+ /**
103
+ * Log out through the site, the way its account menu does (Gitea's "Sign Out" link, Forgejo's "Sign out" button of
104
+ * the menu's form), and wait until the session is a visitor's.
105
+ *
106
+ * Example #1: When I log out
107
+ * Example #2: When we log out
108
+ * Example #3: And I log out
109
+ * Example #4: Then I log out
110
+ * Example #5: When log out
111
+ *
112
+ */
113
+ When(/^(I |we )*log out$/, async function (pronounCase) {
114
+ const page = this.page;
115
+ if (!page.url().startsWith(siteUrl(this, '/'))) await open(this, '/');
116
+ const opened = await page.evaluate(() => {
117
+ const form = document.getElementById('logout-user-action');
118
+ if (form) { form.requestSubmit(); return 'form'; }
119
+ const link = document.querySelector('a[href$="/user/logout"]');
120
+ if (link) { link.click(); return 'link'; }
121
+ return '';
122
+ });
123
+ if (!opened) {
124
+ throw friendly({ message: `There is no way to log out on ${page.url()}: no "Sign out" in the account menu.`, hint: 'Log in first; a visitor has no account menu.' });
125
+ }
126
+ const result = await waitForValue(() => currentUser(this), (login) => login === '', budget(this));
127
+ if (!result.ok) {
128
+ throw friendly({ message: `Logging out did not end the session: it is still "${result.value}"'s.`, hint: 'The forge logs out with the Sign Out entry of its account menu; a single sign-on provider may keep the session elsewhere.' });
129
+ }
130
+ });
131
+
132
+ /**
133
+ * Assert who the session is: the account's login ("member"), or the full name the forge shows for it ("Member Test").
134
+ *
135
+ * Example #1: Then I should be logged in as "Member Test"
136
+ * Example #2: Then I should be logged in as "member"
137
+ * Example #3: Then we should be logged in as "Owner Test"
138
+ * Example #4: And I should be logged in as "reader"
139
+ * Example #5: Then should be logged in as "Stranger Test"
140
+ *
141
+ */
142
+ Then(/^(I |we )*should be logged in as "([^"]*)"$/, async function (pronounCase, who) {
143
+ const result = await waitForValue(async () => [await currentUser(this), await currentName(this)], ([login, name]) => login === who || exactly(who).test(name), budget(this));
144
+ if (!result.ok) {
145
+ const [login, name] = result.value || ['', ''];
146
+ throw friendly({
147
+ message: login ? `Expected the session to be "${who}", but it is ${name} (login "${login}").` : `Expected the session to be "${who}", but nobody is logged in.`,
148
+ hint: 'Log in first with Given I am a logged in user with the "…" user, or with the login form.',
149
+ });
150
+ }
151
+ });
152
+
153
+ /**
154
+ * Assert that nobody is logged in: the session is a visitor's.
155
+ *
156
+ * Example #1: Then I should not be logged in
157
+ * Example #2: Then we should not be logged in
158
+ * Example #3: And I should not be logged in
159
+ * Example #4: But I should not be logged in
160
+ * Example #5: Then should not be logged in
161
+ *
162
+ */
163
+ Then(/^(I |we )*should not be logged in$/, async function (pronounCase) {
164
+ const result = await waitForValue(() => currentUser(this), (login) => login === '', budget(this));
165
+ if (!result.ok) {
166
+ throw friendly({ message: `Expected nobody to be logged in, but the session is "${result.value}"'s.`, hint: 'A refused login keeps the visitor\'s session; a login that worked opens the account\'s.' });
167
+ }
168
+ });
169
+
170
+ /**
171
+ * Assert that the login form refused the last login with a message that holds the text (the forge's error banner,
172
+ * "Username or password is incorrect.").
173
+ *
174
+ * Example #1: Then the login should be refused with "Username or password is incorrect."
175
+ * Example #2: Then the login should be refused with "incorrect"
176
+ * Example #3: And the login should be refused with "Username or password"
177
+ * Example #4: But the login should be refused with "Username or password is incorrect."
178
+ * Example #5: Then the login should be refused with "password is incorrect"
179
+ *
180
+ */
181
+ Then(/^the login should be refused with "([^"]*)"$/, async function (text) {
182
+ const banner = this.page.locator('.flash-error').filter({ hasText: text }).first();
183
+ try {
184
+ await banner.waitFor({ state: 'visible', timeout: budget(this) });
185
+ } catch {
186
+ const shown = tidy(await this.page.locator('.flash-message').first().innerText({ timeout: 1000 }).catch(() => ''));
187
+ throw friendly({
188
+ message: `Expected the login to be refused with "${text}", but ${shown ? `the page says "${shown}"` : 'the page shows no error'} (${this.page.url()}).`,
189
+ hint: 'Use it right after "I log in as … with the password …". A login that worked shows no error.',
190
+ });
191
+ }
192
+ });
193
+
194
+ // ---------------------------------------------------------------------------
195
+ // Repositories
196
+ // ---------------------------------------------------------------------------
197
+
198
+ /**
199
+ * Open a repository's front page, by owner/name.
200
+ *
201
+ * Example #1: When I open the repository "owner/webship-site"
202
+ * Example #2: When we open the repository "owner/webship-site"
203
+ * Example #3: And I open the repository "owner/webship-site"
204
+ * Example #4: Given I open the repository "owner/webship-site"
205
+ * Example #5: When open the repository "owner/webship-site"
206
+ *
207
+ */
208
+ When(/^(I |we )*open the repository "([^"]*)"$/, async function (pronounCase, which) {
209
+ const repo = await findRepository(this, which);
210
+ this.gitea = Object.assign(this.gitea || {}, { repository: repo.path });
211
+ await open(this, `/${repo.path}`);
212
+ });
213
+
214
+ /**
215
+ * Assert that the session may not open a repository: the forge answers 404 ("…not authorized to view it"), as it does
216
+ * for a private repository an account has no access to. Give such a scenario @js-off: the browser logs the 404.
217
+ *
218
+ * Example #1: Then I should be refused the repository "owner/webship-site"
219
+ * Example #2: Then we should be refused the repository "owner/webship-site"
220
+ * Example #3: And I should be refused the repository "owner/webship-site"
221
+ * Example #4: But I should be refused the repository "owner/webship-site"
222
+ * Example #5: Then should be refused the repository "owner/webship-site"
223
+ *
224
+ */
225
+ Then(/^(I |we )*should be refused the repository "([^"]*)"$/, async function (pronounCase, which) {
226
+ const repo = await findRepository(this, which);
227
+ const response = await this.page.goto(siteUrl(this, `/${repo.path}`)).catch(() => null);
228
+ const status = response ? response.status() : 0;
229
+ if (status !== 404 && status !== 403) {
230
+ throw friendly({
231
+ message: `Expected the repository ${repo.path} to be refused, but the site answered ${status || 'nothing'} (${this.page.url()}).`,
232
+ hint: 'Its owner, its collaborators and an administrator may open a private repository, and anyone a public one; log in as an account with no access.',
233
+ });
234
+ }
235
+ });