From adf9993b10789cb985aaec81d6f1250a7963ae84 Mon Sep 17 00:00:00 2001 From: WonderAliceUwU Date: Tue, 17 Nov 2026 16:33:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20cover=20the=20calendar=20an?= =?UTF-8?q?d=20hidden=20sections=20end=20to=20end=20through=20the=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- server/test/api.e2e.test.ts | 138 +++++++++++++++++++++++++++++++++++ server/test/mock-services.ts | 17 ++++- 3 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 server/test/api.e2e.test.ts diff --git a/README.md b/README.md index 6913c3f..4a9cb1c 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,9 @@ npm test ``` The tests run against small stand-ins for Jellyfin, Sonarr and Radarr -(`server/test/mock-services.ts`). To point a running OmniArr at them while +(`server/test/mock-services.ts`). Some call the code directly and `api.e2e.test.ts` +starts the real server and signs in as the administrator and as profiles, so it +checks what each of them receives from the API. To point a running OmniArr at them while developing, start them with `node dist-server/server/test/mock-services.js` after a build; it prints the three URLs, accepts any API key and offers a `full` profile and a `limited` one that cannot open Anime or Anime Movies. diff --git a/server/test/api.e2e.test.ts b/server/test/api.e2e.test.ts new file mode 100644 index 0000000..691c40b --- /dev/null +++ b/server/test/api.e2e.test.ts @@ -0,0 +1,138 @@ +import assert from 'node:assert/strict'; +import { spawn, type ChildProcess } from 'node:child_process'; +import { randomUUID } from 'node:crypto'; +import { mkdtemp, rm } from 'node:fs/promises'; +import { createServer } from 'node:net'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { after, before, describe, it } from 'node:test'; +import { defaultConfig, type CalendarResponse } from '../../shared/contracts.js'; +import { startMockServices } from './mock-services.js'; + +// The real server, started on its own port and data folder, talking to the +// mock Jellyfin, Sonarr and Radarr. It signs in through the app's own login +// endpoints, so what a profile receives is what the browser would receive. +let mocks: Awaited>; +let server: ChildProcess; +let dataDir = ''; +let baseUrl = ''; +let adminCookie = ''; + +async function freePort(): Promise { + return new Promise((resolve) => { + const probe = createServer().listen(0, '127.0.0.1', () => { + const { port } = probe.address() as { port: number }; + probe.close(() => resolve(port)); + }); + }); +} + +async function call(path: string, options: { method?: string; cookie?: string; body?: unknown } = {}) { + const response = await fetch(`${baseUrl}${path}`, { + method: options.method ?? 'GET', + headers: { + 'Content-Type': 'application/json', + ...(options.cookie ? { Cookie: options.cookie } : {}) + }, + body: options.body === undefined ? undefined : JSON.stringify(options.body) + }); + const cookie = response.headers.get('set-cookie')?.split(';')[0] ?? ''; + const text = await response.text(); + return { status: response.status, cookie, json: text ? JSON.parse(text) : null }; +} + +async function signInAs(userId: string): Promise { + const result = await call('/api/auth/profile-login', { method: 'POST', body: { userId, password: 'any-password' } }); + assert.equal(result.status, 200); + return result.cookie; +} + +async function calendarTitles(cookie: string): Promise { + const result = await call('/api/calendar?start=2026-11-01&end=2026-11-30', { cookie }); + assert.equal(result.status, 200); + return (result.json as CalendarResponse).items.map((item) => item.title).sort(); +} + +before(async () => { + mocks = await startMockServices(); + dataDir = await mkdtemp(join(tmpdir(), 'omniarr-e2e-')); + const port = await freePort(); + baseUrl = `http://127.0.0.1:${port}`; + server = spawn(process.execPath, ['dist-server/server/index.js'], { + env: { ...process.env, PORT: String(port), OMNIARR_DATA_DIR: dataDir }, + stdio: 'ignore' + }); + + for (let attempt = 0; attempt < 50; attempt += 1) { + const ready = await fetch(`${baseUrl}/api/health`).then((response) => response.ok).catch(() => false); + if (ready) { + break; + } + await new Promise((resolve) => setTimeout(resolve, 100)); + } + + // A throwaway master password for this throwaway data folder. + const registered = await call('/api/auth/register', { method: 'POST', body: { password: randomUUID() } }); + assert.equal(registered.status, 200); + adminCookie = registered.cookie; + + const saved = await call('/api/settings', { + method: 'PUT', + cookie: adminCookie, + body: { + ...defaultConfig, + preferences: { ...defaultConfig.preferences, hiddenSections: { limited: ['downloads', 'anime'] } }, + jellyfin: { baseUrl: mocks.jellyfinUrl, apiKey: 'key', userId: '' }, + sonarr: { ...defaultConfig.sonarr, baseUrl: mocks.sonarrUrl, apiKey: 'key' }, + radarr: { ...defaultConfig.radarr, baseUrl: mocks.radarrUrl, apiKey: 'key' } + } + }); + assert.equal(saved.status, 200); +}); + +after(async () => { + server?.kill(); + await mocks?.close(); + if (dataDir) { + await rm(dataDir, { recursive: true, force: true }); + } +}); + +describe('upcoming calendar through the API', () => { + it('gives the administrator every release', async () => { + assert.deepEqual(await calendarTitles(adminCookie), ['Anime', 'Better Call Saul', 'Dune', 'Frieren', 'Your Name']); + }); + + it('gives a profile with every library every release', async () => { + assert.deepEqual(await calendarTitles(await signInAs('full')), ['Anime', 'Better Call Saul', 'Dune', 'Frieren', 'Your Name']); + }); + + it('keeps releases of libraries a profile cannot open out of its calendar', async () => { + assert.deepEqual(await calendarTitles(await signInAs('limited')), ['Anime', 'Better Call Saul', 'Dune']); + }); +}); + +describe('sections hidden for a profile', () => { + it('closes the hidden sections for that profile only', async () => { + const limited = await signInAs('limited'); + const full = await signInAs('full'); + assert.equal((await call('/api/downloads', { cookie: limited })).status, 403); + assert.equal((await call('/api/hubs/anime', { cookie: limited })).status, 403); + assert.notEqual((await call('/api/downloads', { cookie: full })).status, 403); + assert.notEqual((await call('/api/downloads', { cookie: adminCookie })).status, 403); + }); + + it('lets only the administrator read the library access and change settings', async () => { + const limited = await signInAs('limited'); + assert.equal((await call('/api/admin/profile-access', { cookie: limited })).status, 403); + assert.equal((await call('/api/settings', { method: 'PUT', cookie: limited, body: defaultConfig })).status, 403); + + const access = await call('/api/admin/profile-access', { cookie: adminCookie }); + assert.equal(access.status, 200); + assert.deepEqual( + (access.json as { profiles: Array<{ userId: string; libraries: Array<{ name: string; access: boolean }> }> }) + .profiles.find((profile) => profile.userId === 'limited')?.libraries.filter((library) => library.access).map((library) => library.name), + ['Shows', 'Movies'] + ); + }); +}); diff --git a/server/test/mock-services.ts b/server/test/mock-services.ts index 18069f9..ff0cc66 100644 --- a/server/test/mock-services.ts +++ b/server/test/mock-services.ts @@ -41,16 +41,25 @@ function listen(server: Server): Promise { }); } -function jsonServer(handler: (path: string, query: URLSearchParams) => unknown): Server { +function jsonServer(handler: (path: string, query: URLSearchParams, body: string) => unknown): Server { return createServer((request, response) => { const url = new URL(request.url || '/', 'http://localhost'); - response.writeHead(200, { 'Content-Type': 'application/json' }); - response.end(JSON.stringify(handler(url.pathname, url.searchParams))); + const chunks: Buffer[] = []; + request.on('data', (chunk: Buffer) => chunks.push(chunk)); + request.on('end', () => { + response.writeHead(200, { 'Content-Type': 'application/json' }); + response.end(JSON.stringify(handler(url.pathname, url.searchParams, Buffer.concat(chunks).toString('utf8')))); + }); }); } export async function startMockServices() { - const jellyfin = jsonServer((path, query) => { + const jellyfin = jsonServer((path, query, body) => { + // Any password signs a profile in, so the app's own login can be exercised. + if (path === '/Users/AuthenticateByName') { + const username = (JSON.parse(body || '{}') as { Username?: string }).Username; + return { User: { Id: MOCK_USERS.find((user) => user.Name === username)?.Id } }; + } if (path === '/Library/VirtualFolders') { return MOCK_LIBRARIES; }