Files
directory/apps/web/e2e/configured-images.e2e.ts
eleith 5e9c949801
ci/woodpecker/pr/cli Pipeline was successful
ci/woodpecker/pr/config Pipeline was successful
ci/woodpecker/pr/web Pipeline was successful
Adopt SvelteKit 3 APIs
2026-11-03 06:54:26 -08:00

442 lines
16 KiB
TypeScript

import { mkdir, mkdtemp, rename, rm, symlink, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { expect, test } from '@playwright/test';
import { credentialsAuth, password, username } from './support/configuration.js';
import { startServer, type TestServer } from './support/server.js';
import { responseCookieHeader } from './support/session.js';
const siteDescription = 'Useful services with familiar images.';
const privateLogo = `<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Illustrator export -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_ai "http://ns.adobe.com/AdobeIllustrator/10.0/">
]>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:i="&ns_ai;" viewBox="0 0 128 128">
<g i:extraneous="self"><rect width="128" height="128" fill="#91bdce"/><text>PRIVATE_IMAGE_BYTES</text></g>
<i:aipgf><![CDATA[opaque Illustrator editing metadata]]></i:aipgf>
</svg>`;
const privateBrand =
'<svg xmlns="http://www.w3.org/2000/svg"><text>PRIVATE_BRAND_BYTES</text></svg>';
const replacementImage =
'<svg xmlns="http://www.w3.org/2000/svg"><text>Replacement image</text></svg>';
const privateValues = [
'PRIVATE_BOOKMARK_DESCRIPTION',
'PRIVATE_IMAGE_BYTES',
'PRIVATE_BRAND_BYTES',
username,
password
];
const png = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aX1sAAAAASUVORK5CYII=',
'base64'
);
test.setTimeout(60_000);
function configuration(auth = credentialsAuth) {
return `site:
url: '\${TEST_ORIGIN}'
title: Configured images fixture
description: ${siteDescription}
logo_path: /public/brand logo.svg
links:
photos:
title: Photos
url: https://photos.example.test
description: PRIVATE_BOOKMARK_DESCRIPTION
group: Family
logo_path: /public/photo.svg
router:
title: Router
url: http://router.example.test
thumbnail:
title: Photo thumbnail
url: https://thumbnail.example.test
logo_path: /public/photo.png
${auth}`;
}
async function fixtures() {
const directory = await mkdtemp(join(tmpdir(), 'directory-configured-images-'));
const publicDir = join(directory, 'public');
try {
await mkdir(publicDir);
await writeFile(join(publicDir, 'brand logo.svg'), privateBrand);
await writeFile(join(publicDir, 'photo.svg'), privateLogo);
await writeFile(join(publicDir, 'photo.png'), png);
await writeFile(join(publicDir, 'unreferenced.svg'), privateLogo);
await mkdir(join(publicDir, 'images', 'bookmarks'), { recursive: true });
await writeFile(join(publicDir, 'images', 'bookmarks', 'test-ling.svg'), privateLogo);
return { directory, publicDir };
} catch (error) {
await rm(directory, { recursive: true, force: true });
throw error;
}
}
async function signIn(server: TestServer) {
const response = await server.fetch('/signin', {
method: 'POST',
headers: { accept: 'text/html', origin: server.origin },
body: new URLSearchParams({ username, password, redirectTo: '/' })
});
expect([302, 303]).toContain(response.status);
const cookie = responseCookieHeader(response);
expect(cookie).not.toBe('');
return cookie;
}
function expectPrivateHeaders(response: Response) {
expect(response.headers.get('cache-control')).toBe('private, no-store');
expect(response.headers.get('x-content-type-options')).toBe('nosniff');
}
async function expectNoPrivateData(response: Response) {
const text = await response.text();
for (const value of privateValues) expect(text).not.toContain(value);
}
async function stop(server: TestServer, directory: string) {
try {
expect(await server.stop()).toEqual({ code: 0, signal: null });
const output = server.output();
expect(server.outputTruncated()).toBe(false);
for (const value of [...privateValues, directory, server.path]) {
if (value) expect(output).not.toContain(value);
}
} finally {
await rm(directory, { recursive: true, force: true });
}
}
test('mounted files are public while generated bookmark logos require a session, including encoded HTML requests', async () => {
const fixture = await fixtures();
const server = await startServer({
configuration: configuration(),
publicDir: fixture.publicDir
});
try {
await server.ready();
for (const method of ['GET', 'HEAD']) {
for (const [path, contentType, bytes] of [
['/public/photo.svg', 'image/svg+xml', Buffer.from(privateLogo)],
['/public/photo.png', 'image/png', png],
['/public/brand%20logo.svg', 'image/svg+xml', Buffer.from(privateBrand)],
['/%70ublic/photo.svg', 'image/svg+xml', Buffer.from(privateLogo)],
['/public/photo%2Esvg', 'image/svg+xml', Buffer.from(privateLogo)],
['/public/unreferenced.svg', 'image/svg+xml', Buffer.from(privateLogo)],
['/public/images/bookmarks/test-ling.svg', 'image/svg+xml', Buffer.from(privateLogo)]
] as const) {
const image = await server.fetch(path, {
method,
headers: { accept: 'text/html' }
});
expect(image.status).toBe(200);
expectPrivateHeaders(image);
expect(image.headers.get('content-type')).toBe(contentType);
expect(image.headers.get('content-length')).toBe(String(bytes.length));
expect(Buffer.from(await image.arrayBuffer())).toEqual(
method === 'HEAD' ? Buffer.alloc(0) : bytes
);
}
for (const path of [
'/bookmarks/photos/logo',
'/bookmarks/missing/logo',
'/%62ookmarks/photos/logo',
'/bookmarks/%70hotos/logo',
'/bookmarks/photos/%6cogo'
]) {
const denied = await server.fetch(path, {
method,
headers: { accept: 'text/html' }
});
expect(denied.status).toBe(401);
expect(denied.headers.has('location')).toBe(false);
expectPrivateHeaders(denied);
if (method === 'HEAD') expect(await denied.text()).toBe('');
else await expectNoPrivateData(denied);
}
}
for (const path of ['/healthz', '/favicon.png', '/signin']) {
const response = await server.fetch(path);
expect(response.status).toBe(200);
await expectNoPrivateData(response);
}
const denied = await server.fetch('/__data.json');
expect(denied.status).toBe(401);
await expectNoPrivateData(denied);
const imageData = await server.fetch('/public/photo.svg/__data.json');
expect(imageData.status).toBe(404);
await expectNoPrivateData(imageData);
const cookie = await signIn(server);
for (const path of [
'/public/photo.svg',
'/public/photo.png',
'/public/brand%20logo.svg',
'/%70ublic/photo.svg',
'/bookmarks/photos/logo'
]) {
const image = await server.fetch(path, { headers: { cookie } });
expect(image.status).toBe(200);
expectPrivateHeaders(image);
const head = await server.fetch(path, { method: 'HEAD', headers: { cookie } });
expect(head.status).toBe(200);
expectPrivateHeaders(head);
for (const header of ['content-length', 'content-type']) {
expect(head.headers.get(header)).toBe(image.headers.get(header));
}
expect(await head.text()).toBe('');
await image.arrayBuffer();
}
expect(await (await server.fetch('/public/photo.svg', { headers: { cookie } })).text()).toBe(
privateLogo
);
const data = await server.fetch('/__data.json', { headers: { cookie } });
const payload = await data.text();
expect(payload).toContain('/public/photo.svg');
for (const value of [username, password, server.path, fixture.publicDir, '<svg']) {
expect(payload).not.toContain(value);
}
} finally {
await stop(server, fixture.directory);
}
});
test('open mode serves contained images regardless of references, with MIME and bodyless HEAD responses', async () => {
const fixture = await fixtures();
await writeFile(join(fixture.directory, 'outside.svg'), privateLogo);
await symlink(join(fixture.directory, 'outside.svg'), join(fixture.publicDir, 'escape.svg'));
await symlink(join(fixture.publicDir, 'photo.svg'), join(fixture.publicDir, 'inside.svg'));
await writeFile(join(fixture.publicDir, 'unsupported.txt'), 'PRIVATE_IMAGE_BYTES');
await writeFile(join(fixture.publicDir, '家 icon.svg'), replacementImage);
const extra = `
extra:
title: Extra
url: https://extra.example.test
logo_path: /public/escape.svg
inside:
title: Inside
url: https://inside.example.test
logo_path: /public/inside.svg
unicode:
title: Unicode image
url: https://unicode.example.test
logo_path: /public/家 icon.svg
`;
const server = await startServer({
configuration: configuration('') + extra,
publicDir: fixture.publicDir
});
try {
await server.ready();
for (const [path, bytes, contentType] of [
['/public/photo.svg', Buffer.from(privateLogo), 'image/svg+xml'],
['/public/photo.png', png, 'image/png'],
['/public/unreferenced.svg', Buffer.from(privateLogo), 'image/svg+xml'],
['/public/inside.svg', Buffer.from(privateLogo), 'image/svg+xml'],
['/public/%E5%AE%B6%20icon.svg', Buffer.from(replacementImage), 'image/svg+xml']
] as const) {
const image = await server.fetch(path, {
headers: {
'if-none-match': '*',
'if-modified-since': new Date().toUTCString(),
range: 'bytes=0-2'
}
});
expect(image.status).toBe(200);
expect(image.headers.get('content-type')).toBe(contentType);
expect(image.headers.get('content-length')).toBe(String(bytes.length));
expect(Buffer.from(await image.arrayBuffer())).toEqual(bytes);
expectPrivateHeaders(image);
for (const header of ['etag', 'last-modified', 'accept-ranges', 'content-range']) {
expect(image.headers.has(header)).toBe(false);
}
const head = await server.fetch(path, { method: 'HEAD' });
expect(head.status).toBe(200);
expectPrivateHeaders(head);
expect(head.headers.get('content-type')).toBe(contentType);
expect(head.headers.get('content-length')).toBe(String(bytes.length));
expect(await head.text()).toBe('');
}
for (const path of [
'/public/escape.svg',
'/public/unsupported.txt',
'/public/missing.svg',
'/public',
'/public/PHOTO.svg',
'/bookmarks/missing/logo',
'/bookmarks/constructor/logo',
'/public/%2e%2e%2foutside.svg',
'/public/%252e%252e%252foutside.svg',
'/public/%5coutside.svg'
]) {
for (const method of ['GET', 'HEAD']) {
const response = await server.fetch(path, { method });
expect(response.status).toBe(404);
expect(await response.text()).toBe(method === 'HEAD' ? '' : 'Not found');
expect(response.headers.get('content-length')).toBe('9');
expect(response.headers.get('content-type')).toBe('text/plain; charset=utf-8');
expectPrivateHeaders(response);
}
}
const unsupported = await server.fetch('/public/photo.svg', {
method: 'POST',
headers: { origin: server.origin }
});
expect(unsupported.status).toBe(405);
const generated = await server.fetch('/bookmarks/photos/logo');
expect(generated.status).toBe(200);
expectPrivateHeaders(generated);
const svg = await generated.text();
expect(svg).toContain('width="64"');
expect(svg).toContain('height="64"');
expect(svg).toContain('>PH</text>');
} finally {
await stop(server, fixture.directory);
}
});
test('config reloads update generated logos but never gate mounted files on references', async () => {
const fixture = await fixtures();
await writeFile(join(fixture.publicDir, 'replacement.svg'), replacementImage);
const server = await startServer({
configuration: configuration(),
publicDir: fixture.publicDir
});
async function expectPublicFiles() {
for (const [path, bytes] of [
['/public/photo.svg', privateLogo],
['/public/replacement.svg', replacementImage]
] as const) {
const response = await server.fetch(path);
expect(response.status).toBe(200);
expect(await response.text()).toBe(bytes);
}
}
try {
await server.ready();
const cookie = await signIn(server);
const bookmarkLogo = async () =>
(await server.fetch('/bookmarks/photos/logo', { headers: { cookie } })).text();
const original = await bookmarkLogo();
await expectPublicFiles();
const replacement = configuration()
.replace('/public/photo.svg', '/public/replacement.svg')
.replace('title: Photos', 'title: Updated Photos');
await server.replace(replacement);
await expect.poll(bookmarkLogo).toContain('>UP</text>');
const changed = await bookmarkLogo();
expect(changed).not.toBe(original);
await expectPublicFiles();
await server.replace('version: [invalid');
await expect.poll(() => server.output()).toContain('load_failed');
await expectPublicFiles();
expect(await bookmarkLogo()).toBe(changed);
await server.replace(configuration().replace(password, 'CHANGED_PASSWORD'));
await expect.poll(() => server.output()).toContain('restart_required');
await expectPublicFiles();
expect(await bookmarkLogo()).toBe(changed);
await server.replace(configuration());
await expect.poll(bookmarkLogo).toBe(original);
await expectPublicFiles();
} finally {
await stop(server, fixture.directory);
}
});
test('missing and atomically replaced bytes recover without changing the prepared bookmark API', async () => {
const fixture = await fixtures();
await rm(join(fixture.publicDir, 'photo.svg'));
const server = await startServer({
configuration: configuration(''),
publicDir: fixture.publicDir
});
try {
await server.ready();
const before = await (await server.fetch('/bookmarks/photos/logo')).text();
expect((await server.fetch('/public/photo.svg')).status).toBe(404);
await writeFile(join(fixture.publicDir, 'new.svg'), privateLogo);
await rename(join(fixture.publicDir, 'new.svg'), join(fixture.publicDir, 'photo.svg'));
expect(await (await server.fetch('/public/photo.svg')).text()).toBe(privateLogo);
await writeFile(join(fixture.publicDir, 'new.svg'), replacementImage);
await rename(join(fixture.publicDir, 'new.svg'), join(fixture.publicDir, 'photo.svg'));
expect(await (await server.fetch('/public/photo.svg')).text()).toBe(replacementImage);
await rm(join(fixture.publicDir, 'photo.svg'));
expect((await server.fetch('/public/photo.svg')).status).toBe(404);
expect(await (await server.fetch('/bookmarks/photos/logo')).text()).toBe(before);
} finally {
await stop(server, fixture.directory);
}
});
test('a missing public directory is valid at startup and recovers when an image appears', async () => {
const fixture = await fixtures();
await rm(fixture.publicDir, { recursive: true });
const server = await startServer({
configuration: configuration(''),
publicDir: fixture.publicDir
});
try {
await server.ready();
expect((await server.fetch('/healthz')).status).toBe(200);
expect((await server.fetch('/')).status).toBe(200);
expect((await server.fetch('/public/photo.svg')).status).toBe(404);
await mkdir(fixture.publicDir);
await writeFile(join(fixture.publicDir, 'photo.svg'), privateLogo);
expect(await (await server.fetch('/public/photo.svg')).text()).toBe(privateLogo);
} finally {
await stop(server, fixture.directory);
}
});
test('existing SSR cards and details render mounted logos and omitted-path bookmark logos without JavaScript', async ({
browser
}) => {
const fixture = await fixtures();
const server = await startServer({
configuration: configuration(),
publicDir: fixture.publicDir
});
const context = await browser.newContext({ javaScriptEnabled: false });
try {
await server.ready();
const page = await context.newPage();
await page.goto(server.url);
await expect(page).toHaveURL(`${server.url}/signin?callbackUrl=%2F`);
await page.getByLabel('Username').fill(username);
await page.getByLabel('Password').fill(password);
await page.getByRole('button', { name: 'Sign in', exact: true }).click();
await expect(page.getByRole('button', { name: 'Sign out' })).toBeVisible();
for (const [id, path] of [
['photos', '/public/photo.svg'],
['router', '/bookmarks/router/logo'],
['thumbnail', '/public/photo.png']
] as const) {
const image = page.locator(`a[href$="?link=${id}"] img`);
await expect(image).toHaveAttribute('src', path);
await expect
.poll(() => image.evaluate((node) => (node as HTMLImageElement).naturalWidth))
.toBeGreaterThan(0);
}
await page.getByRole('link', { name: 'Photos', exact: true }).click();
const details = page
.getByRole('region', { name: 'Photos', exact: true })
.locator('img[src="/public/photo.svg"]');
await expect(details).toBeVisible();
await expect
.poll(() => details.evaluate((node) => (node as HTMLImageElement).naturalWidth))
.toBeGreaterThan(0);
const image = await context.request.get(`${server.url}/public/photo.svg`);
expect(image.status()).toBe(200);
expect(await image.text()).toBe(privateLogo);
} finally {
await context.close();
await stop(server, fixture.directory);
}
});