@webship-js/immich-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.
- package/LICENSE +21 -0
- package/README.md +249 -2
- package/assets/logo.svg +29 -0
- package/package.json +62 -4
- package/tests/features/albums.feature +26 -0
- package/tests/features/api.feature +30 -0
- package/tests/features/library.feature +46 -0
- package/tests/features/sign-in.feature +21 -0
- package/tests/fixtures/webship-test-blue.jpg +0 -0
- package/tests/fixtures/webship-test-green.jpg +0 -0
- package/tests/fixtures/webship-test-red.jpg +0 -0
- package/tests/step-definitions/immich-albums.steps.js +213 -0
- package/tests/step-definitions/immich-auth.steps.js +286 -0
- package/tests/step-definitions/immich-helpers.js +453 -0
- package/tests/step-definitions/immich-library.steps.js +437 -0
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Shared helpers for the step files of this pack.
|
|
4
|
+
//
|
|
5
|
+
// Not a step file: it registers no step definitions. It holds the plumbing the steps use and that a project can
|
|
6
|
+
// require for its own steps: the library's address and the accounts of worldParameters.users, signing in (a session
|
|
7
|
+
// in the browser, a token or an API key for the API), the photos as the library names them (by the date they were
|
|
8
|
+
// taken), its messages and its confirmations, the files a scenario uploads, and the test content a scenario makes
|
|
9
|
+
// and removes.
|
|
10
|
+
//
|
|
11
|
+
// The library (Immich) draws every page in the browser from its API: a photo is a link named by its image, "Image
|
|
12
|
+
// taken on May 1, 2024"; a change shows a toast, a <div> with no role and no live region, read by its text; a question
|
|
13
|
+
// is a dialog with no name of its own, found by its role and its text. Every page needs JavaScript, so a browser
|
|
14
|
+
// without it has only the API.
|
|
15
|
+
//
|
|
16
|
+
// Everything builds on webship-js's own helpers (`sendRequest`, `friendly`, `failure`) and on the headers of its API
|
|
17
|
+
// steps (`api.setHeader`), so the steps fail the way the core webship-js steps do and the built-in API steps send
|
|
18
|
+
// what this pack signs them with.
|
|
19
|
+
|
|
20
|
+
const fs = require('fs');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
const webship = require('webship-js/tests/step-definitions/webship');
|
|
23
|
+
const api = require('webship-js/tests/step-definitions/api.steps');
|
|
24
|
+
|
|
25
|
+
const { friendly, failure, sendRequest, onNewPage } = webship;
|
|
26
|
+
|
|
27
|
+
// The budgets of the waits (milliseconds): each is a ceiling, not a pause.
|
|
28
|
+
const WAIT = {
|
|
29
|
+
page: 20000, // a page of the library to draw itself
|
|
30
|
+
upload: 30000, // an upload to be answered
|
|
31
|
+
process: 60000, // the library's jobs to finish with an upload (its details read, its thumbnail made)
|
|
32
|
+
photo: 15000, // a photo to show in the timeline, an album, a search, the trash or a shared page
|
|
33
|
+
message: 10000, // a message (a toast, a form's error) to show
|
|
34
|
+
dialog: 10000, // a dialog to ask, and to close once answered
|
|
35
|
+
call: 15000, // a call of the page to its API to be answered
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// The text of a photo's image, as the library writes it: "Image taken on May 1, 2024".
|
|
39
|
+
const TAKEN_ON = 'Image taken on ';
|
|
40
|
+
|
|
41
|
+
// What the pages of a scenario make in the library, by the answers of the library's API: every album and shared link
|
|
42
|
+
// the page asks for, so @removes-test-content removes each of them, named or not (a slow page can leave an album it
|
|
43
|
+
// made untitled).
|
|
44
|
+
if (typeof onNewPage === 'function') {
|
|
45
|
+
onNewPage(async (world, page) => {
|
|
46
|
+
page.on('response', (response) => {
|
|
47
|
+
try {
|
|
48
|
+
const request = response.request();
|
|
49
|
+
if (request.method() !== 'POST' || response.status() !== 201) return;
|
|
50
|
+
const where = new URL(response.url()).pathname;
|
|
51
|
+
if (where !== '/api/albums' && where !== '/api/shared-links') return;
|
|
52
|
+
response.json().then((body) => {
|
|
53
|
+
if (!body || !body.id) return;
|
|
54
|
+
if (!world.immichMade) world.immichMade = [];
|
|
55
|
+
world.immichMade.push({ path: where, id: body.id });
|
|
56
|
+
}).catch(() => {});
|
|
57
|
+
} catch (e) { /* a page that closed meanwhile */ }
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Settings, addresses and accounts
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The settings of the pack, `worldParameters.immich` in cucumber.js, with their defaults:
|
|
68
|
+
* - `fixtures`: the folder, from the project, of the files a scenario uploads, `tests/fixtures` (a file not there is
|
|
69
|
+
* looked for in the pack's own: three test photos, webship-test-red.jpg, -blue.jpg and -green.jpg);
|
|
70
|
+
* - `manager`: the account (of worldParameters.users) that makes and removes test accounts, `admin`.
|
|
71
|
+
* The test content (what @removes-test-content removes) is what the steps name: files webship-test-*, albums, shared
|
|
72
|
+
* links and API keys "Webship test …", accounts webship.test.…@example.com.
|
|
73
|
+
*/
|
|
74
|
+
function settings(world) {
|
|
75
|
+
const own = (world.parameters && world.parameters.immich) || {};
|
|
76
|
+
return {
|
|
77
|
+
fixtures: own.fixtures || 'tests/fixtures',
|
|
78
|
+
manager: own.manager || 'admin',
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The library's address, the launch URL of cucumber.js, without a trailing slash. */
|
|
83
|
+
function base(world) {
|
|
84
|
+
const url = (world.parameters && world.parameters.launchUrl) || world.launchUrl || '';
|
|
85
|
+
if (!url) {
|
|
86
|
+
throw friendly({
|
|
87
|
+
message: 'No launch URL is set, so the library has no address.',
|
|
88
|
+
hint: 'Set worldParameters.launchUrl in cucumber.js, or LAUNCH_URL in the environment, to the library\'s address (http://127.0.0.1:2283).',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return String(url).replace(/\/+$/, '');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A path of the library as a whole address; a whole address as written. */
|
|
95
|
+
function absolute(world, p) {
|
|
96
|
+
const text = String(p || '');
|
|
97
|
+
if (/^https?:\/\//i.test(text)) return text;
|
|
98
|
+
return `${base(world)}/${text.replace(/^\/+/, '')}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* An account of the users registry in cucumber.js (`worldParameters.users`), or one a scenario made, by the handle
|
|
103
|
+
* the scenario names it with. Its address is `email` (or `username`).
|
|
104
|
+
*
|
|
105
|
+
* @returns {{handle: string, email: string, password: string}}
|
|
106
|
+
*/
|
|
107
|
+
function user(world, handle) {
|
|
108
|
+
const made = (world.immichAccounts || {})[handle];
|
|
109
|
+
if (made) return made;
|
|
110
|
+
const users = (world.parameters && world.parameters.users) || {};
|
|
111
|
+
const entry = users[handle];
|
|
112
|
+
if (!entry) {
|
|
113
|
+
throw friendly({
|
|
114
|
+
message: `The user "${handle}" is not in the users registry. Known users: ${Object.keys(users).join(', ') || '(none)'}.`,
|
|
115
|
+
hint: `Add it to worldParameters.users in cucumber.js: users: { ${handle}: { email: process.env.IMMICH_${String(handle).toUpperCase()}_EMAIL, password: process.env.IMMICH_${String(handle).toUpperCase()}_PASSWORD } }.`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const email = entry.email || entry.username;
|
|
119
|
+
if (!email || !entry.password) {
|
|
120
|
+
throw friendly({
|
|
121
|
+
message: `The user "${handle}" has no ${email ? 'password' : 'email address'} in the users registry.`,
|
|
122
|
+
hint: `Set worldParameters.users.${handle}.email and .password in cucumber.js, from environment variables (IMMICH_${String(handle).toUpperCase()}_PASSWORD).`,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return { handle, email: String(email), password: String(entry.password) };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// The API: requests, tokens, keys
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A request of the pack's own (a sign-in, a count, a clean-up), through webship-js's transport. `token` signs it with
|
|
134
|
+
* a bearer token, `key` with an API key; `json` goes out as JSON; `session` sends it with the browser's cookies (and
|
|
135
|
+
* keeps the ones the answer sets).
|
|
136
|
+
*
|
|
137
|
+
* @returns {Promise<{status: number, data: *, headers: Object, config: {method: string, url: string}}>}
|
|
138
|
+
*/
|
|
139
|
+
async function request(world, method, p, options) {
|
|
140
|
+
const { token, key, json, session } = options || {};
|
|
141
|
+
const headers = {};
|
|
142
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
143
|
+
if (key) headers['x-api-key'] = key;
|
|
144
|
+
let data;
|
|
145
|
+
if (json !== undefined) {
|
|
146
|
+
data = JSON.stringify(json);
|
|
147
|
+
headers['Content-Type'] = 'application/json';
|
|
148
|
+
}
|
|
149
|
+
const url = absolute(world, p);
|
|
150
|
+
try {
|
|
151
|
+
return await sendRequest(world, { method, url, headers, data, session: !!session });
|
|
152
|
+
} catch (e) {
|
|
153
|
+
throw friendly({
|
|
154
|
+
message: `The library did not answer ${method} ${url}.`,
|
|
155
|
+
cause: e,
|
|
156
|
+
hint: 'Check that the library is up at the launch URL of cucumber.js (LAUNCH_URL).',
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** What an answer of the API says, as a person reads it: its `message`, else its text. */
|
|
162
|
+
function said(answer) {
|
|
163
|
+
const data = answer && answer.data;
|
|
164
|
+
if (data && typeof data === 'object') {
|
|
165
|
+
if (Array.isArray(data.message)) return data.message.join('; ');
|
|
166
|
+
if (data.message) return String(data.message);
|
|
167
|
+
if (data.error) return String(data.error);
|
|
168
|
+
}
|
|
169
|
+
const text = typeof data === 'string' ? data : JSON.stringify(data);
|
|
170
|
+
return String(text || '').slice(0, 300);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Sign an account in through the API (POST /api/auth/login) and return its access token. With `session`, the answer's
|
|
175
|
+
* cookies stay in the browser, so its pages open signed in.
|
|
176
|
+
*/
|
|
177
|
+
async function signIn(world, handle, options) {
|
|
178
|
+
const account = user(world, handle);
|
|
179
|
+
const answer = await request(world, 'POST', '/api/auth/login', { json: { email: account.email, password: account.password }, session: options && options.session });
|
|
180
|
+
if (answer.status !== 201 || !answer.data || !answer.data.accessToken) {
|
|
181
|
+
throw friendly({
|
|
182
|
+
message: `The library refused the sign-in of "${handle}" (${account.email}): ${answer.status} ${said(answer)}.`,
|
|
183
|
+
hint: answer.status === 401
|
|
184
|
+
? `Check the password of "${handle}" in the users registry (worldParameters.users.${handle}.password).`
|
|
185
|
+
: 'Check that the launch URL of cucumber.js is the library\'s address.',
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (!world.immichSignedIn) world.immichSignedIn = new Set();
|
|
189
|
+
world.immichSignedIn.add(handle);
|
|
190
|
+
return answer.data.accessToken;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The token of an account for the pack's own requests, signed in once a scenario. */
|
|
194
|
+
async function tokenOf(world, handle) {
|
|
195
|
+
if (!world.immichTokens) world.immichTokens = {};
|
|
196
|
+
if (!world.immichTokens[handle]) world.immichTokens[handle] = await signIn(world, handle);
|
|
197
|
+
return world.immichTokens[handle];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Sign webship-js's API steps (and this pack's API steps) with a header: `Authorization: Bearer …` or `x-api-key`.
|
|
202
|
+
* The other one is taken off, so a request is signed one way.
|
|
203
|
+
*/
|
|
204
|
+
function signApi(world, name, value) {
|
|
205
|
+
if (typeof api.setHeader !== 'function') {
|
|
206
|
+
throw friendly({
|
|
207
|
+
message: 'This webship-js cannot sign the requests of its API steps: it has no api.setHeader.',
|
|
208
|
+
hint: 'Update webship-js to 2.0.19 or later (npm install webship-js@^2.0.19).',
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const other = name === 'x-api-key' ? 'Authorization' : 'x-api-key';
|
|
212
|
+
api.setHeader(other, null);
|
|
213
|
+
api.setHeader(name, value);
|
|
214
|
+
world.immichApiAuth = { [name]: value };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The headers the API steps are signed with, for this pack's own API requests; a failure when there are none. */
|
|
218
|
+
function apiAuth(world) {
|
|
219
|
+
if (!world.immichApiAuth) {
|
|
220
|
+
throw friendly({
|
|
221
|
+
message: 'The API is not signed in: this step sends its request as a signed-in account.',
|
|
222
|
+
hint: 'Sign it in first: Given I am signed in to the library\'s API as "user", or Given the library\'s API uses a key of "user".',
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
return world.immichApiAuth;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
// Files to upload
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
/** A file a scenario uploads: from the project's fixtures folder, else from the pack's own. */
|
|
233
|
+
function fixture(world, file) {
|
|
234
|
+
const own = path.resolve(process.cwd(), settings(world).fixtures, file);
|
|
235
|
+
if (fs.existsSync(own)) return own;
|
|
236
|
+
const packs = path.resolve(__dirname, '..', 'fixtures', file);
|
|
237
|
+
if (fs.existsSync(packs)) return packs;
|
|
238
|
+
const known = [path.resolve(process.cwd(), settings(world).fixtures), path.resolve(__dirname, '..', 'fixtures')]
|
|
239
|
+
.filter((d) => fs.existsSync(d)).flatMap((d) => fs.readdirSync(d)).filter((f) => !f.startsWith('.'));
|
|
240
|
+
throw friendly({
|
|
241
|
+
message: `There is no file "${file}" to upload. Known files: ${[...new Set(known)].join(', ') || '(none)'}.`,
|
|
242
|
+
hint: `Put it in ${settings(world).fixtures}/ (worldParameters.immich.fixtures), or upload one of the pack's: webship-test-red.jpg, webship-test-blue.jpg, webship-test-green.jpg.`,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The MIME type of a file to upload, from its extension. */
|
|
247
|
+
function mimeOf(file) {
|
|
248
|
+
const ext = path.extname(file).toLowerCase();
|
|
249
|
+
return { '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif', '.webp': 'image/webp', '.heic': 'image/heic', '.mp4': 'video/mp4', '.mov': 'video/quicktime' }[ext] || 'application/octet-stream';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ---------------------------------------------------------------------------
|
|
253
|
+
// The pages: photos, messages, dialogs
|
|
254
|
+
// ---------------------------------------------------------------------------
|
|
255
|
+
|
|
256
|
+
/** A photo of the page, by the date it was taken, as the library names it ("May 1, 2024"). */
|
|
257
|
+
function photo(scope, date) {
|
|
258
|
+
return scope.getByRole('link', { name: `${TAKEN_ON}${date}`, exact: true });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** The dates of the photos the page shows, for a failure message. */
|
|
262
|
+
async function photosShown(scope) {
|
|
263
|
+
try {
|
|
264
|
+
const names = await scope.getByRole('img').evaluateAll((imgs, prefix) => imgs.map((i) => i.getAttribute('alt') || '').filter((a) => a.startsWith(prefix)).map((a) => a.slice(prefix.length)), TAKEN_ON);
|
|
265
|
+
return [...new Set(names)];
|
|
266
|
+
} catch (e) {
|
|
267
|
+
return [];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Wait until the page shows a photo taken on the date; false after the budget. */
|
|
272
|
+
async function photoShows(scope, date, budget) {
|
|
273
|
+
try {
|
|
274
|
+
await photo(scope, date).first().waitFor({ state: 'visible', timeout: budget || WAIT.photo });
|
|
275
|
+
return true;
|
|
276
|
+
} catch (e) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** The messages the page shows now: its toasts (top-level <div>s of the body) and a form's error, by their text. */
|
|
282
|
+
async function messagesShown(page) {
|
|
283
|
+
try {
|
|
284
|
+
return await page.evaluate(() => {
|
|
285
|
+
const out = [];
|
|
286
|
+
for (const el of document.body.children) {
|
|
287
|
+
if (el.tagName !== 'DIV' || el.querySelector('main, nav, header, form, input, textarea, select')) continue;
|
|
288
|
+
const text = (el.innerText || '').trim();
|
|
289
|
+
if (text && text.length < 400 && el.offsetParent !== null) out.push(text.replace(/\s*\n+\s*/g, ' · '));
|
|
290
|
+
}
|
|
291
|
+
for (const el of document.querySelectorAll('form p, form [role="alert"], [role="alert"]')) {
|
|
292
|
+
const text = (el.innerText || '').trim();
|
|
293
|
+
if (text && el.offsetParent !== null) out.push(text);
|
|
294
|
+
}
|
|
295
|
+
return [...new Set(out)];
|
|
296
|
+
});
|
|
297
|
+
} catch (e) {
|
|
298
|
+
return [];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Wait until the page shows the text (in a toast, a form's error or anywhere it can be seen); false after the budget. */
|
|
303
|
+
async function messageShown(page, text, budget) {
|
|
304
|
+
try {
|
|
305
|
+
await page.getByText(text, { exact: false }).filter({ visible: true }).first().waitFor({ state: 'visible', timeout: budget || WAIT.message });
|
|
306
|
+
return true;
|
|
307
|
+
} catch (e) {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The dialog the page asks with, found by its role and its own text; null when none shows within the budget. */
|
|
313
|
+
async function dialog(page, budget) {
|
|
314
|
+
const d = page.getByRole('dialog').filter({ visible: true }).last();
|
|
315
|
+
try {
|
|
316
|
+
await d.waitFor({ state: 'visible', timeout: budget || WAIT.dialog });
|
|
317
|
+
return d;
|
|
318
|
+
} catch (e) {
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Wait for the page's call to its API that answers `match` (a method and a path pattern), started by `action`.
|
|
325
|
+
*
|
|
326
|
+
* @returns {Promise<import('playwright').Response|null>}
|
|
327
|
+
*/
|
|
328
|
+
async function callOf(page, method, pattern, action, budget) {
|
|
329
|
+
const answered = page.waitForResponse((r) => {
|
|
330
|
+
try {
|
|
331
|
+
return r.request().method() === method && pattern.test(new URL(r.url()).pathname);
|
|
332
|
+
} catch (e) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
}, { timeout: budget || WAIT.call }).catch(() => null);
|
|
336
|
+
await action();
|
|
337
|
+
return answered;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** The library's page of a path, loaded and drawn (its navigation or the photos' area). */
|
|
341
|
+
async function open(world, p) {
|
|
342
|
+
const page = world.page;
|
|
343
|
+
await page.goto(absolute(world, p), { waitUntil: 'domcontentloaded' });
|
|
344
|
+
await page.waitForLoadState('networkidle', { timeout: WAIT.page }).catch(() => {});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Wait until the library has finished with an upload: it reads a file's details (the date it was taken, from its
|
|
349
|
+
* EXIF) and makes its thumbnail in jobs of its own, after it has answered the upload. Until then the photo is named by
|
|
350
|
+
* the day it was uploaded, or not drawn at all. `options` sign the requests as `request` does.
|
|
351
|
+
*/
|
|
352
|
+
async function processed(world, id, options) {
|
|
353
|
+
const deadline = Date.now() + WAIT.process;
|
|
354
|
+
let last;
|
|
355
|
+
while (Date.now() < deadline) {
|
|
356
|
+
last = await request(world, 'GET', `/api/assets/${id}`, options);
|
|
357
|
+
const asset = (last && last.data) || {};
|
|
358
|
+
if (last.status === 200 && asset.exifInfo && asset.thumbhash) return asset;
|
|
359
|
+
await pause(500);
|
|
360
|
+
}
|
|
361
|
+
throw friendly({
|
|
362
|
+
message: `The library took the upload (asset ${id}), but did not finish with it within ${WAIT.process / 1000} s: ${last && last.status === 200 ? 'its details or its thumbnail are not there yet' : `it answered ${last ? last.status : 'nothing'}`}.`,
|
|
363
|
+
hint: 'The library works its uploads in jobs: check its Job Queues (Administration) and its log.',
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Pause; only to let a page settle between two polls, never as a wait for something. */
|
|
368
|
+
function pause(ms) {
|
|
369
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// ---------------------------------------------------------------------------
|
|
373
|
+
// Test content
|
|
374
|
+
// ---------------------------------------------------------------------------
|
|
375
|
+
|
|
376
|
+
const TEST_FILE = /^webship-test-/i;
|
|
377
|
+
const TEST_NAME = /^Webship test/i;
|
|
378
|
+
const TEST_ACCOUNT = /^webship\.test\./i;
|
|
379
|
+
|
|
380
|
+
/** Every asset an account holds (in the timeline, archived or in the trash), through the metadata search. */
|
|
381
|
+
async function assetsOf(world, token) {
|
|
382
|
+
const out = [];
|
|
383
|
+
for (let page = 1; page && page < 50;) {
|
|
384
|
+
const answer = await request(world, 'POST', '/api/search/metadata', { token, json: { page, size: 1000, withDeleted: true, withArchived: true } });
|
|
385
|
+
const block = (answer.data && answer.data.assets) || {};
|
|
386
|
+
out.push(...(block.items || []));
|
|
387
|
+
page = block.nextPage ? Number(block.nextPage) : 0;
|
|
388
|
+
}
|
|
389
|
+
return out;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** What an account holds that a scenario can make: assets, albums, shared links, API keys (and accounts, for the manager). */
|
|
393
|
+
async function counts(world, handle) {
|
|
394
|
+
const token = await tokenOf(world, handle);
|
|
395
|
+
const list = async (p) => {
|
|
396
|
+
const answer = await request(world, 'GET', p, { token });
|
|
397
|
+
return Array.isArray(answer.data) ? answer.data : [];
|
|
398
|
+
};
|
|
399
|
+
const out = {
|
|
400
|
+
assets: (await assetsOf(world, token)).length,
|
|
401
|
+
albums: (await list('/api/albums')).length,
|
|
402
|
+
'shared links': (await list('/api/shared-links')).length,
|
|
403
|
+
'API keys': (await list('/api/api-keys')).length,
|
|
404
|
+
};
|
|
405
|
+
if (handle === settings(world).manager) out.accounts = (await list('/api/admin/users')).length;
|
|
406
|
+
return out;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Remove what the scenario's pages made (albums, shared links), by their ids, through the browser's session. */
|
|
410
|
+
async function removeMade(world) {
|
|
411
|
+
const removed = [];
|
|
412
|
+
for (const made of (world.immichMade || []).reverse()) {
|
|
413
|
+
const answer = await request(world, 'DELETE', `${made.path}/${made.id}`, { session: true }).catch(() => null);
|
|
414
|
+
if (answer && answer.status < 300) removed.push(`${made.path === '/api/albums' ? 'album' : 'shared link'} ${made.id}`);
|
|
415
|
+
}
|
|
416
|
+
world.immichMade = [];
|
|
417
|
+
return removed;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Remove what a scenario made for an account: its webship-test-* files (for good) and its "Webship test …" things. */
|
|
421
|
+
async function removeTestContent(world, handle) {
|
|
422
|
+
const token = await tokenOf(world, handle);
|
|
423
|
+
const removed = [];
|
|
424
|
+
const assets = (await assetsOf(world, token)).filter((a) => TEST_FILE.test(a.originalFileName || ''));
|
|
425
|
+
if (assets.length) {
|
|
426
|
+
await request(world, 'DELETE', '/api/assets', { token, json: { ids: assets.map((a) => a.id), force: true } });
|
|
427
|
+
removed.push(`${assets.length} file(s)`);
|
|
428
|
+
}
|
|
429
|
+
// A shared link before the album it shares, so neither waits on the other.
|
|
430
|
+
for (const [p, field, label] of [['/api/shared-links', 'description', 'shared link'], ['/api/albums', 'albumName', 'album'], ['/api/api-keys', 'name', 'API key']]) {
|
|
431
|
+
const answer = await request(world, 'GET', p, { token });
|
|
432
|
+
for (const item of (Array.isArray(answer.data) ? answer.data : []).filter((x) => TEST_NAME.test(x[field] || ''))) {
|
|
433
|
+
const gone = await request(world, 'DELETE', `${p}/${item.id}`, { token });
|
|
434
|
+
removed.push(gone.status < 300 ? `${label} "${item[field]}"` : `${label} "${item[field]}" (not removed: ${gone.status} ${said(gone)})`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (handle === settings(world).manager) {
|
|
438
|
+
const answer = await request(world, 'GET', '/api/admin/users', { token });
|
|
439
|
+
for (const account of (Array.isArray(answer.data) ? answer.data : []).filter((u) => TEST_ACCOUNT.test(u.email || ''))) {
|
|
440
|
+
await request(world, 'DELETE', `/api/admin/users/${account.id}`, { token, json: { force: true } });
|
|
441
|
+
removed.push(`account ${account.email}`);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return removed;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
module.exports = {
|
|
448
|
+
WAIT, TAKEN_ON, TEST_FILE, TEST_NAME, TEST_ACCOUNT,
|
|
449
|
+
settings, base, absolute, user, request, said, signIn, tokenOf, signApi, apiAuth,
|
|
450
|
+
fixture, mimeOf, photo, photosShown, photoShows, messagesShown, messageShown, dialog, callOf, open, processed, pause,
|
|
451
|
+
assetsOf, counts, removeMade, removeTestContent,
|
|
452
|
+
friendly, failure,
|
|
453
|
+
};
|