test: add integration and unit tests for dashboard, emby, sonarr, radarr, sabnzbd routes
Build and Push Docker Image / build (push) Successful in 39s
Docs Check / Markdown lint (push) Successful in 45s
Licence Check / Licence compatibility and copyright header verification (push) Successful in 1m12s
CI / Security audit (push) Successful in 1m30s
CI / Tests & coverage (push) Failing after 1m39s
Docs Check / Mermaid diagram parse check (push) Successful in 1m59s

- tests/unit/dashboard.test.js: 58 unit tests covering all 12 pure helper
  functions in dashboard.js (sanitizeTagLabel, tagMatchesUser, getCoverArt,
  extractAllTags, extractUserTag, getImportIssues, getSonarrLink, getRadarrLink,
  canBlocklist, extractEpisode, gatherEpisodes, buildTagBadges)

- tests/integration/dashboard.test.js: 35 integration tests for
  /user-downloads (SAB+Sonarr, SAB+Radarr, qBit, showAll, paused queue,
  history matching, importIssues, wrong-user filtering), /status (admin guard,
  webhook check, failure handling), /webhook-metrics, /cover-art (all
  validation/proxy paths), /blocklist-search (guards, Sonarr, Radarr, failure)

- tests/integration/emby.test.js: 13 integration tests covering all 4 Emby
  routes (sessions, users, users/:id, session/:id/user) with auth guard,
  happy path, and upstream failure cases

- tests/integration/arrRoutes.test.js: 64 integration tests for Sonarr +
  Radarr (queue, history, series/movies, notifications CRUD, /test, /schema,
  /sofarr-webhook create+update+missing-config+failure) and SABnzbd (queue,
  history with custom params)

- vitest.config.js: raise global coverage thresholds (statements/functions/
  lines 20->55, branches 8->40) to reflect improved coverage
  (62.5% stmts, 42.6% branches, 64.1% funcs, 65.6% lines)

- tests/README.md: document new test files and update coverage table
This commit is contained in:
2026-05-20 21:37:57 +01:00
parent ee2f275501
commit 7d3e6e6a47
6 changed files with 2559 additions and 20 deletions
+899
View File
@@ -0,0 +1,899 @@
// Copyright (c) 2026 Gordon Bolton. MIT License.
/**
* Integration tests for server/routes/sonarr.js, server/routes/radarr.js,
* and server/routes/sabnzbd.js.
*
* Covers:
* Sonarr: queue, history, series, series/:id, notifications CRUD,
* notifications/test, notifications/schema, sofarr-webhook (create + update)
* Radarr: same set, movies instead of series
* SABnzbd: queue, history
*
* All routes require auth; state-changing requests (POST/PUT/DELETE) must also
* carry the CSRF token issued by GET /api/auth/csrf (verifyCsrf middleware).
*/
import request from 'supertest';
import nock from 'nock';
import { createApp } from '../../server/app.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const EMBY_BASE = 'https://emby.test';
const SONARR_BASE = 'https://sonarr.test';
const RADARR_BASE = 'https://radarr.test';
const SABNZBD_BASE = 'https://sabnzbd.test';
// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------
const EMBY_AUTH = { AccessToken: 'tok', User: { Id: 'uid1', Name: 'alice' } };
const EMBY_USER = { Id: 'uid1', Name: 'alice', Policy: { IsAdministrator: false } };
const SONARR_QUEUE = { records: [{ id: 1, title: 'Show.S01E01', seriesId: 10 }] };
const SONARR_HISTORY = { records: [{ id: 100, eventType: 'downloadFolderImported', sourceTitle: 'Show.S01E01' }] };
const SONARR_SERIES_LIST = [{ id: 10, title: 'My Show', titleSlug: 'my-show', tags: [] }];
const SONARR_SERIES_ITEM = { id: 10, title: 'My Show', titleSlug: 'my-show', tags: [] };
const SONARR_NOTIFICATIONS = [{ id: 5, name: 'Plex', implementation: 'Plex' }];
const SONARR_NOTIF_ITEM = { id: 5, name: 'Plex', implementation: 'Plex', fields: [] };
const RADARR_QUEUE = { records: [{ id: 2, title: 'Movie.2024.1080p', movieId: 20 }] };
const RADARR_HISTORY = { records: [{ id: 200, eventType: 'downloadFolderImported', sourceTitle: 'Movie.2024.1080p' }] };
const RADARR_MOVIES_LIST = [{ id: 20, title: 'My Movie', titleSlug: 'my-movie-2024', tags: [] }];
const RADARR_MOVIE_ITEM = { id: 20, title: 'My Movie', titleSlug: 'my-movie-2024', tags: [] };
const RADARR_NOTIFICATIONS = [{ id: 7, name: 'Plex', implementation: 'Plex' }];
const RADARR_NOTIF_ITEM = { id: 7, name: 'Plex', implementation: 'Plex', fields: [] };
const SAB_QUEUE_RESP = { queue: { status: 'Downloading', slots: [{ filename: 'Show.S01E01', percentage: '50' }] } };
const SAB_HISTORY_RESP = { history: { slots: [{ name: 'Show.S01E01.Done', status: 'Completed' }] } };
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function interceptLogin() {
nock(EMBY_BASE).post('/Users/authenticatebyname').reply(200, EMBY_AUTH);
nock(EMBY_BASE).get(/\/Users\//).reply(200, EMBY_USER);
}
async function loginAs(app) {
interceptLogin();
const res = await request(app)
.post('/api/auth/login')
.send({ username: 'alice', password: 'pw' });
return { cookies: res.headers['set-cookie'], csrf: res.body.csrfToken };
}
async function getSessionWithCsrf(app) {
const { cookies, csrf } = await loginAs(app);
// Obtain a fresh csrf cookie as well (login already sets one, but keep consistent)
const csrfCookie = cookies.find(c => c.startsWith('csrf_token='));
return { cookies, csrf, csrfCookie };
}
// Build the Cookie header for state-changing requests: session + csrf cookies
function joinCookies(sessionCookies, csrfCookie) {
const all = Array.isArray(sessionCookies) ? [...sessionCookies] : [sessionCookies];
if (csrfCookie && !all.includes(csrfCookie)) all.push(csrfCookie);
return all.join('; ');
}
// ---------------------------------------------------------------------------
// Environment
// ---------------------------------------------------------------------------
beforeAll(() => {
process.env.EMBY_URL = EMBY_BASE;
process.env.SONARR_URL = SONARR_BASE;
process.env.SONARR_API_KEY = 'sk';
process.env.SONARR_INSTANCES = JSON.stringify([{ id: 'sonarr-1', name: 'Main Sonarr', url: SONARR_BASE, apiKey: 'sk' }]);
process.env.RADARR_URL = RADARR_BASE;
process.env.RADARR_API_KEY = 'rk';
process.env.RADARR_INSTANCES = JSON.stringify([{ id: 'radarr-1', name: 'Main Radarr', url: RADARR_BASE, apiKey: 'rk' }]);
process.env.SABNZBD_URL = SABNZBD_BASE;
process.env.SABNZBD_API_KEY = 'sabkey';
process.env.SOFARR_BASE_URL = 'https://sofarr.test';
process.env.SOFARR_WEBHOOK_SECRET = 'webhook-secret-abc';
});
afterAll(() => {
delete process.env.EMBY_URL;
delete process.env.SONARR_URL;
delete process.env.SONARR_API_KEY;
delete process.env.SONARR_INSTANCES;
delete process.env.RADARR_URL;
delete process.env.RADARR_API_KEY;
delete process.env.RADARR_INSTANCES;
delete process.env.SABNZBD_URL;
delete process.env.SABNZBD_API_KEY;
delete process.env.SOFARR_BASE_URL;
delete process.env.SOFARR_WEBHOOK_SECRET;
});
afterEach(() => {
nock.cleanAll();
});
// ===========================================================================
// SONARR ROUTES
// ===========================================================================
describe('Sonarr routes', () => {
describe('GET /api/sonarr/queue', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/sonarr/queue');
expect(res.status).toBe(401);
});
it('proxies Sonarr queue', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/queue').reply(200, SONARR_QUEUE);
const res = await request(app).get('/api/sonarr/queue').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.records).toBeDefined();
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/queue').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/sonarr/queue').set('Cookie', cookies);
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/queue/i);
});
});
describe('GET /api/sonarr/history', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/sonarr/history');
expect(res.status).toBe(401);
});
it('proxies Sonarr history with default pageSize', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/history').query(true).reply(200, SONARR_HISTORY);
const res = await request(app).get('/api/sonarr/history').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.records).toBeDefined();
});
it('passes through custom pageSize', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/history').query({ pageSize: '100' }).reply(200, SONARR_HISTORY);
const res = await request(app).get('/api/sonarr/history?pageSize=100').set('Cookie', cookies);
expect(res.status).toBe(200);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/history').query(true).replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/sonarr/history').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/sonarr/series', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/sonarr/series');
expect(res.status).toBe(401);
});
it('proxies series list', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/series').reply(200, SONARR_SERIES_LIST);
const res = await request(app).get('/api/sonarr/series').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/series').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/sonarr/series').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/sonarr/series/:id', () => {
it('proxies individual series', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/series/10').reply(200, SONARR_SERIES_ITEM);
const res = await request(app).get('/api/sonarr/series/10').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.title).toBe('My Show');
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/series/999').replyWithError('not found');
const res = await request(app).get('/api/sonarr/series/999').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/sonarr/notifications', () => {
it('returns 503 when no Sonarr instance configured', async () => {
const app = createApp({ skipRateLimits: true });
// Temporarily clear instances
const saved = process.env.SONARR_INSTANCES;
delete process.env.SONARR_INSTANCES;
delete process.env.SONARR_URL;
delete process.env.SONARR_API_KEY;
interceptLogin();
const loginRes = await request(app).post('/api/auth/login').send({ username: 'alice', password: 'pw' });
const cookies = loginRes.headers['set-cookie'];
const res = await request(app).get('/api/sonarr/notifications').set('Cookie', cookies);
expect(res.status).toBe(503);
process.env.SONARR_INSTANCES = saved;
process.env.SONARR_URL = SONARR_BASE;
process.env.SONARR_API_KEY = 'sk';
});
it('proxies notifications list', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/notification').reply(200, SONARR_NOTIFICATIONS);
const res = await request(app).get('/api/sonarr/notifications').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/notification').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/sonarr/notifications').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/sonarr/notifications/:id', () => {
it('proxies a single notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/notification/5').reply(200, SONARR_NOTIF_ITEM);
const res = await request(app).get('/api/sonarr/notifications/5').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.name).toBe('Plex');
});
});
describe('POST /api/sonarr/notifications', () => {
it('returns 403 (CSRF missing) without auth', async () => {
// verifyCsrf fires before requireAuth on POST routes — no CSRF → 403
const app = createApp({ skipRateLimits: true });
const res = await request(app).post('/api/sonarr/notifications').send({});
expect(res.status).toBe(403);
});
it('creates a notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).post('/api/v3/notification').reply(200, { id: 99, name: 'New' });
const res = await request(app)
.post('/api/sonarr/notifications')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ name: 'New' });
expect(res.status).toBe(200);
expect(res.body.name).toBe('New');
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).post('/api/v3/notification').replyWithError('ECONNREFUSED');
const res = await request(app)
.post('/api/sonarr/notifications')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ name: 'New' });
expect(res.status).toBe(500);
});
});
describe('PUT /api/sonarr/notifications/:id', () => {
it('updates a notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).put('/api/v3/notification/5').reply(200, { id: 5, name: 'Updated' });
const res = await request(app)
.put('/api/sonarr/notifications/5')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 5, name: 'Updated' });
expect(res.status).toBe(200);
expect(res.body.name).toBe('Updated');
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).put('/api/v3/notification/5').replyWithError('ECONNREFUSED');
const res = await request(app)
.put('/api/sonarr/notifications/5')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 5 });
expect(res.status).toBe(500);
});
});
describe('DELETE /api/sonarr/notifications/:id', () => {
it('deletes a notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).delete('/api/v3/notification/5').reply(200, {});
const res = await request(app)
.delete('/api/sonarr/notifications/5')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf);
expect(res.status).toBe(200);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).delete('/api/v3/notification/5').replyWithError('ECONNREFUSED');
const res = await request(app)
.delete('/api/sonarr/notifications/5')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf);
expect(res.status).toBe(500);
});
});
describe('POST /api/sonarr/notifications/test', () => {
it('returns 503 when no Sonarr instance configured', async () => {
const app = createApp({ skipRateLimits: true });
const saved = process.env.SONARR_INSTANCES;
const savedUrl = process.env.SONARR_URL;
const savedKey = process.env.SONARR_API_KEY;
delete process.env.SONARR_INSTANCES;
delete process.env.SONARR_URL;
delete process.env.SONARR_API_KEY;
interceptLogin();
const loginRes = await request(app).post('/api/auth/login').send({ username: 'alice', password: 'pw' });
const cookies = loginRes.headers['set-cookie'];
const csrf = loginRes.body.csrfToken;
const csrfCookie = cookies.find(c => c.startsWith('csrf_token='));
const res = await request(app)
.post('/api/sonarr/notifications/test')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(503);
process.env.SONARR_INSTANCES = saved;
process.env.SONARR_URL = savedUrl;
process.env.SONARR_API_KEY = savedKey;
});
it('tests a notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).post('/api/v3/notification/test').reply(200, {});
const res = await request(app)
.post('/api/sonarr/notifications/test')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 5 });
expect(res.status).toBe(200);
});
it('returns 500 when test fails', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).post('/api/v3/notification/test').replyWithError('ECONNREFUSED');
const res = await request(app)
.post('/api/sonarr/notifications/test')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 5 });
expect(res.status).toBe(500);
});
});
describe('GET /api/sonarr/notifications/schema', () => {
it('proxies the schema', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SONARR_BASE).get('/api/v3/notification/schema').reply(200, [{ implementation: 'Webhook' }]);
const res = await request(app).get('/api/sonarr/notifications/schema').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
});
describe('POST /api/sonarr/notifications/sofarr-webhook', () => {
it('returns 400 when SOFARR_BASE_URL is not configured', async () => {
const app = createApp({ skipRateLimits: true });
const saved = process.env.SOFARR_BASE_URL;
delete process.env.SOFARR_BASE_URL;
interceptLogin();
const loginRes = await request(app).post('/api/auth/login').send({ username: 'alice', password: 'pw' });
const cookies = loginRes.headers['set-cookie'];
const csrf = loginRes.body.csrfToken;
const csrfCookie = cookies.find(c => c.startsWith('csrf_token='));
const res = await request(app)
.post('/api/sonarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/SOFARR_BASE_URL/);
process.env.SOFARR_BASE_URL = saved;
});
it('creates a new webhook notification when none exists', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).get('/api/v3/notification').reply(200, []);
nock(SONARR_BASE).post('/api/v3/notification').reply(200, { id: 10, name: 'Sofarr' });
const res = await request(app)
.post('/api/sonarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(200);
expect(res.body.name).toBe('Sofarr');
});
it('updates an existing Sofarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE)
.get('/api/v3/notification')
.reply(200, [{ id: 10, name: 'Sofarr', implementation: 'Webhook' }]);
nock(SONARR_BASE)
.put('/api/v3/notification/10')
.reply(200, { id: 10, name: 'Sofarr' });
const res = await request(app)
.post('/api/sonarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(200);
expect(res.body.name).toBe('Sofarr');
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(SONARR_BASE).get('/api/v3/notification').replyWithError('ECONNREFUSED');
const res = await request(app)
.post('/api/sonarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(500);
});
});
});
// ===========================================================================
// RADARR ROUTES
// ===========================================================================
describe('Radarr routes', () => {
describe('GET /api/radarr/queue', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/radarr/queue');
expect(res.status).toBe(401);
});
it('proxies Radarr queue', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/queue').reply(200, RADARR_QUEUE);
const res = await request(app).get('/api/radarr/queue').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.records).toBeDefined();
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/queue').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/radarr/queue').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/radarr/history', () => {
it('proxies Radarr history', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/history').query(true).reply(200, RADARR_HISTORY);
const res = await request(app).get('/api/radarr/history').set('Cookie', cookies);
expect(res.status).toBe(200);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/history').query(true).replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/radarr/history').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/radarr/movies', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/radarr/movies');
expect(res.status).toBe(401);
});
it('proxies movies list', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/movie').reply(200, RADARR_MOVIES_LIST);
const res = await request(app).get('/api/radarr/movies').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/movie').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/radarr/movies').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/radarr/movies/:id', () => {
it('proxies a single movie', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/movie/20').reply(200, RADARR_MOVIE_ITEM);
const res = await request(app).get('/api/radarr/movies/20').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.title).toBe('My Movie');
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/movie/999').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/radarr/movies/999').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('GET /api/radarr/notifications', () => {
it('returns 503 when no Radarr instance configured', async () => {
const app = createApp({ skipRateLimits: true });
const saved = process.env.RADARR_INSTANCES;
const savedUrl = process.env.RADARR_URL;
const savedKey = process.env.RADARR_API_KEY;
delete process.env.RADARR_INSTANCES;
delete process.env.RADARR_URL;
delete process.env.RADARR_API_KEY;
interceptLogin();
const loginRes = await request(app).post('/api/auth/login').send({ username: 'alice', password: 'pw' });
const cookies = loginRes.headers['set-cookie'];
const res = await request(app).get('/api/radarr/notifications').set('Cookie', cookies);
expect(res.status).toBe(503);
process.env.RADARR_INSTANCES = saved;
process.env.RADARR_URL = savedUrl;
process.env.RADARR_API_KEY = savedKey;
});
it('proxies notifications list', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/notification').reply(200, RADARR_NOTIFICATIONS);
const res = await request(app).get('/api/radarr/notifications').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/notification').replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/radarr/notifications').set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
describe('POST /api/radarr/notifications', () => {
it('creates a Radarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).post('/api/v3/notification').reply(200, { id: 88, name: 'New' });
const res = await request(app)
.post('/api/radarr/notifications')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ name: 'New' });
expect(res.status).toBe(200);
expect(res.body.name).toBe('New');
});
});
describe('PUT /api/radarr/notifications/:id', () => {
it('updates a Radarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).put('/api/v3/notification/7').reply(200, { id: 7, name: 'Updated' });
const res = await request(app)
.put('/api/radarr/notifications/7')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 7, name: 'Updated' });
expect(res.status).toBe(200);
expect(res.body.name).toBe('Updated');
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).put('/api/v3/notification/7').replyWithError('ECONNREFUSED');
const res = await request(app)
.put('/api/radarr/notifications/7')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 7 });
expect(res.status).toBe(500);
});
});
describe('DELETE /api/radarr/notifications/:id', () => {
it('deletes a Radarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).delete('/api/v3/notification/7').reply(200, {});
const res = await request(app)
.delete('/api/radarr/notifications/7')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf);
expect(res.status).toBe(200);
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).delete('/api/v3/notification/7').replyWithError('ECONNREFUSED');
const res = await request(app)
.delete('/api/radarr/notifications/7')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf);
expect(res.status).toBe(500);
});
});
describe('GET /api/radarr/notifications/:id', () => {
it('proxies a single Radarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/notification/7').reply(200, RADARR_NOTIF_ITEM);
const res = await request(app).get('/api/radarr/notifications/7').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.name).toBe('Plex');
});
});
describe('POST /api/radarr/notifications/test', () => {
it('tests a Radarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).post('/api/v3/notification/test').reply(200, {});
const res = await request(app)
.post('/api/radarr/notifications/test')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 7 });
expect(res.status).toBe(200);
});
it('returns 500 when test fails', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).post('/api/v3/notification/test').replyWithError('ECONNREFUSED');
const res = await request(app)
.post('/api/radarr/notifications/test')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({ id: 7 });
expect(res.status).toBe(500);
});
});
describe('GET /api/radarr/notifications/schema', () => {
it('proxies the Radarr notification schema', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(RADARR_BASE).get('/api/v3/notification/schema').reply(200, [{ implementation: 'Webhook' }]);
const res = await request(app).get('/api/radarr/notifications/schema').set('Cookie', cookies);
expect(res.status).toBe(200);
});
});
describe('POST /api/radarr/notifications/sofarr-webhook', () => {
it('creates a new Radarr webhook when none exists', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).get('/api/v3/notification').reply(200, []);
nock(RADARR_BASE).post('/api/v3/notification').reply(200, { id: 20, name: 'Sofarr' });
const res = await request(app)
.post('/api/radarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(200);
expect(res.body.name).toBe('Sofarr');
});
it('updates an existing Sofarr Radarr notification', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE)
.get('/api/v3/notification')
.reply(200, [{ id: 20, name: 'Sofarr', implementation: 'Webhook' }]);
nock(RADARR_BASE)
.put('/api/v3/notification/20')
.reply(200, { id: 20, name: 'Sofarr' });
const res = await request(app)
.post('/api/radarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(200);
});
it('returns 400 when SOFARR_WEBHOOK_SECRET is not configured', async () => {
const app = createApp({ skipRateLimits: true });
const saved = process.env.SOFARR_WEBHOOK_SECRET;
delete process.env.SOFARR_WEBHOOK_SECRET;
interceptLogin();
const loginRes = await request(app).post('/api/auth/login').send({ username: 'alice', password: 'pw' });
const cookies = loginRes.headers['set-cookie'];
const csrf = loginRes.body.csrfToken;
const csrfCookie = cookies.find(c => c.startsWith('csrf_token='));
const res = await request(app)
.post('/api/radarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/SOFARR_WEBHOOK_SECRET/);
process.env.SOFARR_WEBHOOK_SECRET = saved;
});
it('returns 500 on upstream failure', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getSessionWithCsrf(app);
nock(RADARR_BASE).get('/api/v3/notification').replyWithError('ECONNREFUSED');
const res = await request(app)
.post('/api/radarr/notifications/sofarr-webhook')
.set('Cookie', joinCookies(cookies, csrfCookie))
.set('X-CSRF-Token', csrf)
.send({});
expect(res.status).toBe(500);
});
});
});
// ===========================================================================
// SABNZBD ROUTES
// ===========================================================================
describe('SABnzbd routes', () => {
describe('GET /api/sabnzbd/queue', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/sabnzbd/queue');
expect(res.status).toBe(401);
});
it('proxies SABnzbd queue', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SABNZBD_BASE)
.get('/api')
.query({ mode: 'queue', apikey: 'sabkey', output: 'json' })
.reply(200, SAB_QUEUE_RESP);
const res = await request(app).get('/api/sabnzbd/queue').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.queue).toBeDefined();
expect(res.body.queue.status).toBe('Downloading');
});
it('returns 500 when SABnzbd is unreachable', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SABNZBD_BASE)
.get('/api')
.query(true)
.replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/sabnzbd/queue').set('Cookie', cookies);
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/queue/i);
});
});
describe('GET /api/sabnzbd/history', () => {
it('returns 401 when unauthenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/sabnzbd/history');
expect(res.status).toBe(401);
});
it('proxies SABnzbd history with default limit', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SABNZBD_BASE)
.get('/api')
.query({ mode: 'history', apikey: 'sabkey', output: 'json', limit: '50' })
.reply(200, SAB_HISTORY_RESP);
const res = await request(app).get('/api/sabnzbd/history').set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.history).toBeDefined();
});
it('passes through custom limit', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SABNZBD_BASE)
.get('/api')
.query({ mode: 'history', apikey: 'sabkey', output: 'json', limit: '100' })
.reply(200, SAB_HISTORY_RESP);
const res = await request(app).get('/api/sabnzbd/history?limit=100').set('Cookie', cookies);
expect(res.status).toBe(200);
});
it('returns 500 when SABnzbd is unreachable', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock(SABNZBD_BASE)
.get('/api')
.query(true)
.replyWithError('ECONNREFUSED');
const res = await request(app).get('/api/sabnzbd/history').set('Cookie', cookies);
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/history/i);
});
});
});
+861
View File
@@ -0,0 +1,861 @@
// Copyright (c) 2026 Gordon Bolton. MIT License.
/**
* Integration tests for server/routes/dashboard.js
*
* Strategy:
* - createApp({ skipRateLimits: true }) for a real Express instance
* - nock intercepts Emby auth so we can obtain a valid session cookie
* - cache is seeded directly (same technique as history.test.js) so the
* route's cache.get() calls return controlled fixture data without any
* real outbound HTTP to SABnzbd / Sonarr / Radarr / qBittorrent
* - nock is used for outbound axios calls made by the routes themselves
* (cover-art proxy, blocklist-search, status webhook-check)
*
* Covers:
* GET /api/dashboard/user-downloads — auth guard, SAB+Sonarr, SAB+Radarr,
* qBittorrent, showAll (admin), empty cache, on-demand poll trigger,
* paused queue speed, error propagation
* GET /api/dashboard/status — admin-only guard, shape check
* GET /api/dashboard/webhook-metrics — any authenticated user
* GET /api/dashboard/cover-art — missing url, non-http scheme, proxy, non-image
* POST /api/dashboard/blocklist-search — admin guard, validation, sonarr+radarr paths
*/
import request from 'supertest';
import nock from 'nock';
import { createRequire } from 'module';
import { createApp } from '../../server/app.js';
const require = createRequire(import.meta.url);
const cache = require('../../server/utils/cache.js');
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const EMBY_BASE = 'https://emby.test';
const SONARR_BASE = 'https://sonarr.test';
const RADARR_BASE = 'https://radarr.test';
const CACHE_TTL = 60 * 60 * 1000; // 1 hour — won't expire in a test run
// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------
const EMBY_AUTH = { AccessToken: 'tok', User: { Id: 'uid1', Name: 'alice' } };
const EMBY_USER = { Id: 'uid1', Name: 'alice', Policy: { IsAdministrator: false } };
const EMBY_ADMIN_AUTH = { AccessToken: 'tok-admin', User: { Id: 'uid2', Name: 'admin' } };
const EMBY_ADMIN_USER = { Id: 'uid2', Name: 'admin', Policy: { IsAdministrator: true } };
// Tag id 1 → 'alice', id 2 → 'admin'
const SONARR_TAGS = [{ id: 1, label: 'alice' }, { id: 2, label: 'admin' }];
const RADARR_TAGS = [{ id: 10, label: 'alice' }, { id: 11, label: 'admin' }];
const SERIES = {
id: 42,
title: 'My Show',
titleSlug: 'my-show',
tags: [1],
path: '/tv/my-show',
images: [{ coverType: 'poster', remoteUrl: 'https://img.test/poster.jpg' }],
_instanceUrl: SONARR_BASE
};
const ADMIN_SERIES = {
id: 43,
title: 'Admin Show',
titleSlug: 'admin-show',
tags: [2],
path: '/tv/admin-show',
images: [{ coverType: 'poster', remoteUrl: 'https://img.test/admin-poster.jpg' }],
_instanceUrl: SONARR_BASE
};
const ADMIN_SAB_SLOT = {
filename: 'Admin.Show.S01E01.720p',
nzbname: 'Admin.Show.S01E01.720p',
nzo_id: 'SABnzbd_nzo_admin001',
percentage: '40',
mb: '500',
mbmissing: '300',
size: '500 MB',
status: 'Downloading',
storage: '/downloads/Admin.Show.S01E01.720p',
timeleft: '0:08:00'
};
const ADMIN_SONARR_QUEUE_RECORD = {
id: 1002,
title: 'Admin.Show.S01E01.720p',
seriesId: 43,
series: ADMIN_SERIES,
episodeId: 502,
trackedDownloadState: 'downloading',
trackedDownloadStatus: 'ok',
_instanceUrl: SONARR_BASE,
_instanceKey: 'sonarr-api-key'
};
const MOVIE = {
id: 99,
title: 'My Movie',
titleSlug: 'my-movie-2024',
tags: [10],
path: '/movies/my-movie',
images: [{ coverType: 'poster', remoteUrl: 'https://img.test/movie-poster.jpg' }],
_instanceUrl: RADARR_BASE
};
const SAB_QUEUE_SLOT = {
filename: 'My.Show.S01E01.720p',
nzbname: 'My.Show.S01E01.720p',
nzo_id: 'SABnzbd_nzo_abc123',
percentage: '55',
mb: '700',
mbmissing: '315',
size: '700 MB',
status: 'Downloading',
storage: '/downloads/My.Show.S01E01.720p',
timeleft: '0:10:00'
};
const SAB_MOVIE_SLOT = {
filename: 'My.Movie.2024.1080p',
nzbname: 'My.Movie.2024.1080p',
nzo_id: 'SABnzbd_nzo_xyz999',
percentage: '80',
mb: '4000',
mbmissing: '800',
size: '4 GB',
status: 'Downloading',
timeleft: '0:05:00'
};
const SONARR_QUEUE_RECORD = {
id: 1001,
title: 'My.Show.S01E01.720p',
seriesId: 42,
series: SERIES,
episodeId: 501,
trackedDownloadState: 'downloading',
trackedDownloadStatus: 'ok',
_instanceUrl: SONARR_BASE,
_instanceKey: 'sonarr-api-key'
};
const RADARR_QUEUE_RECORD = {
id: 2001,
title: 'My.Movie.2024.1080p',
movieId: 99,
movie: MOVIE,
trackedDownloadState: 'downloading',
trackedDownloadStatus: 'ok',
_instanceUrl: RADARR_BASE,
_instanceKey: 'radarr-api-key'
};
const QBIT_TORRENT = {
hash: 'abc123def456',
name: 'My.Show.S01E01.720p',
state: 'downloading',
progress: 0.55,
size: 734003200,
downloaded: 403701760,
uploadSpeed: 0,
downloadSpeed: 1024000,
eta: 300,
savePath: '/downloads/torrents/',
addedOn: Date.now() / 1000 - 7200
};
// ---------------------------------------------------------------------------
// Cache seeding helpers
// ---------------------------------------------------------------------------
function seedEmptyCache() {
cache.set('poll:sab-queue', { slots: [] }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', [], CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
}
function seedSabSonarrCache() {
cache.set('poll:sab-queue', { slots: [SAB_QUEUE_SLOT], status: 'Downloading', speed: '10 MB/s' }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [SONARR_QUEUE_RECORD] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [{ data: SONARR_TAGS }], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
}
function seedSabRadarrCache() {
cache.set('poll:sab-queue', { slots: [SAB_MOVIE_SLOT], status: 'Downloading', speed: '5 MB/s' }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [RADARR_QUEUE_RECORD] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
}
function seedQbittorrentSonarrCache() {
cache.set('poll:sab-queue', { slots: [] }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [SONARR_QUEUE_RECORD] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [{ data: SONARR_TAGS }], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [QBIT_TORRENT], CACHE_TTL);
}
function invalidatePollCache() {
const keys = [
'poll:sab-queue', 'poll:sab-history',
'poll:sonarr-queue', 'poll:sonarr-history', 'poll:sonarr-tags',
'poll:radarr-queue', 'poll:radarr-history', 'poll:radarr-tags',
'poll:qbittorrent'
];
for (const k of keys) cache.invalidate(k);
}
// ---------------------------------------------------------------------------
// Auth helpers
// ---------------------------------------------------------------------------
function interceptEmbyLogin(userBody = EMBY_USER, authBody = EMBY_AUTH) {
nock(EMBY_BASE).post('/Users/authenticatebyname').reply(200, authBody);
nock(EMBY_BASE).get(/\/Users\//).reply(200, userBody);
}
async function loginAs(app, userBody = EMBY_USER, authBody = EMBY_AUTH) {
interceptEmbyLogin(userBody, authBody);
const res = await request(app)
.post('/api/auth/login')
.send({ username: userBody.Name, password: 'pw' });
return { cookies: res.headers['set-cookie'], csrf: res.body.csrfToken };
}
// CSRF token must be sent with state-changing (POST) requests that go through
// the verifyCsrf middleware. GET requests under /api/dashboard do not need it.
async function csrfHeaders(app) {
const csrfRes = await request(app).get('/api/auth/csrf');
const token = csrfRes.body.csrfToken;
const cookie = csrfRes.headers['set-cookie'].find(c => c.startsWith('csrf_token='));
return { token, cookie };
}
// ---------------------------------------------------------------------------
// Environment
// ---------------------------------------------------------------------------
beforeAll(() => {
process.env.EMBY_URL = EMBY_BASE;
process.env.SONARR_INSTANCES = JSON.stringify([{ id: 'sonarr-1', name: 'Main Sonarr', url: SONARR_BASE, apiKey: 'sk' }]);
process.env.RADARR_INSTANCES = JSON.stringify([{ id: 'radarr-1', name: 'Main Radarr', url: RADARR_BASE, apiKey: 'rk' }]);
});
afterAll(() => {
delete process.env.EMBY_URL;
delete process.env.SONARR_INSTANCES;
delete process.env.RADARR_INSTANCES;
});
beforeEach(() => {
seedEmptyCache();
});
afterEach(() => {
nock.cleanAll();
invalidatePollCache();
cache.invalidate('emby:users');
});
// ---------------------------------------------------------------------------
// GET /api/dashboard/user-downloads
// ---------------------------------------------------------------------------
describe('GET /api/dashboard/user-downloads', () => {
describe('authentication', () => {
it('returns 401 when not logged in', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/dashboard/user-downloads');
expect(res.status).toBe(401);
});
});
describe('empty cache', () => {
it('returns empty downloads array for authenticated user', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedEmptyCache();
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.user).toBe('alice');
expect(res.body.isAdmin).toBe(false);
expect(res.body.downloads).toEqual([]);
});
});
describe('SABnzbd + Sonarr queue matching', () => {
it('returns a series download when SAB slot title matches Sonarr queue record', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedSabSonarrCache();
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const downloads = res.body.downloads;
expect(downloads.length).toBeGreaterThanOrEqual(1);
const dl = downloads[0];
expect(dl.type).toBe('series');
expect(dl.seriesName).toBe('My Show');
expect(dl.coverArt).toBe('https://img.test/poster.jpg');
});
it('includes admin-only fields when user is admin', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app, EMBY_ADMIN_USER, EMBY_ADMIN_AUTH);
// Seed a SAB slot + Sonarr record tagged for 'admin' so the admin user gets a result
cache.set('poll:sab-queue', { slots: [ADMIN_SAB_SLOT], status: 'Downloading', speed: '10 MB/s' }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [ADMIN_SONARR_QUEUE_RECORD] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [{ data: SONARR_TAGS }], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.type === 'series');
expect(dl).toBeDefined();
expect(dl.arrQueueId).toBe(1002);
expect(dl.arrType).toBe('sonarr');
expect(dl.arrInstanceUrl).toBe(SONARR_BASE);
expect(dl.downloadPath).toBeDefined();
});
it('does not include admin-only fields for non-admin user', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedSabSonarrCache();
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.type === 'series');
expect(dl).toBeDefined();
expect(dl.arrQueueId).toBeUndefined();
expect(dl.arrType).toBeUndefined();
});
it('does not return downloads tagged for a different user', async () => {
const app = createApp({ skipRateLimits: true });
// Login as 'bob' — series is tagged 'alice'
interceptEmbyLogin({ Id: 'uid-bob', Name: 'bob', Policy: { IsAdministrator: false } }, { AccessToken: 'tok-bob', User: { Id: 'uid-bob', Name: 'bob' } });
const res1 = await request(app)
.post('/api/auth/login')
.send({ username: 'bob', password: 'pw' });
const bobCookies = res1.headers['set-cookie'];
seedSabSonarrCache();
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', bobCookies);
expect(res.status).toBe(200);
expect(res.body.downloads).toEqual([]);
});
});
describe('SABnzbd + Radarr queue matching', () => {
it('returns a movie download when SAB slot title matches Radarr queue record', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedSabRadarrCache();
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.type === 'movie');
expect(dl).toBeDefined();
expect(dl.movieName).toBe('My Movie');
});
});
describe('qBittorrent + Sonarr queue matching', () => {
it('returns a series download from a qBittorrent torrent', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedQbittorrentSonarrCache();
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.type === 'series');
expect(dl).toBeDefined();
expect(dl.seriesName).toBe('My Show');
});
});
describe('paused queue', () => {
it('reports Paused status when SAB queue is paused', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
cache.set('poll:sab-queue', { slots: [SAB_QUEUE_SLOT], status: 'Paused', speed: '0' }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [SONARR_QUEUE_RECORD] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [{ data: SONARR_TAGS }], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.type === 'series');
if (dl) {
expect(dl.status).toBe('Paused');
expect(dl.speed).toBe('0');
}
});
});
describe('showAll (admin)', () => {
it('returns downloads for all tagged users when showAll=true', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app, EMBY_ADMIN_USER, EMBY_ADMIN_AUTH);
seedSabSonarrCache();
// Stub Emby users list used by getEmbyUsers()
nock(EMBY_BASE)
.get('/Users')
.reply(200, [{ Name: 'alice' }, { Name: 'bob' }]);
const res = await request(app)
.get('/api/dashboard/user-downloads?showAll=true')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.isAdmin).toBe(true);
// tagBadges should be present on results when showAll is active
const dl = res.body.downloads.find(d => d.allTags && d.allTags.length > 0);
if (dl) {
expect(Array.isArray(dl.tagBadges)).toBe(true);
}
});
it('non-admin cannot use showAll — still filtered to their own tags', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedSabSonarrCache();
const res = await request(app)
.get('/api/dashboard/user-downloads?showAll=true')
.set('Cookie', cookies);
expect(res.status).toBe(200);
// Non-admin: showAll has no effect, tagBadges must be absent
const dl = res.body.downloads[0];
if (dl) expect(dl.tagBadges).toBeUndefined();
});
});
describe('refreshRate tracking', () => {
it('accepts refreshRate query parameter without error', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
seedEmptyCache();
const res = await request(app)
.get('/api/dashboard/user-downloads?refreshRate=10000')
.set('Cookie', cookies);
expect(res.status).toBe(200);
});
});
describe('SABnzbd history matching', () => {
it('returns a series download matched from SAB history + Sonarr history', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
const historySlot = {
name: 'My.Show.S01E02.720p',
status: 'Completed',
size: '700 MB',
completed_time: Math.floor(Date.now() / 1000) - 3600
};
const sonarrHistoryRecord = {
id: 9001,
sourceTitle: 'My.Show.S01E02.720p',
seriesId: 42,
series: { ...SERIES },
episode: { seasonNumber: 1, episodeNumber: 2, title: 'Episode 2' }
};
cache.set('poll:sab-queue', { slots: [] }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [historySlot] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [sonarrHistoryRecord] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [{ data: SONARR_TAGS }], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.type === 'series');
expect(dl).toBeDefined();
expect(dl.seriesName).toBe('My Show');
});
});
describe('import issues', () => {
it('includes importIssues when Sonarr record has warning status', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
const problemRecord = {
...SONARR_QUEUE_RECORD,
trackedDownloadState: 'importPending',
trackedDownloadStatus: 'warning',
statusMessages: [{ messages: ['No suitable video file found'] }]
};
cache.set('poll:sab-queue', { slots: [SAB_QUEUE_SLOT], status: 'Downloading', speed: '10 MB/s' }, CACHE_TTL);
cache.set('poll:sab-history', { slots: [] }, CACHE_TTL);
cache.set('poll:sonarr-queue', { records: [problemRecord] }, CACHE_TTL);
cache.set('poll:sonarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:sonarr-tags', [{ data: SONARR_TAGS }], CACHE_TTL);
cache.set('poll:radarr-queue', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-history', { records: [] }, CACHE_TTL);
cache.set('poll:radarr-tags', RADARR_TAGS, CACHE_TTL);
cache.set('poll:qbittorrent', [], CACHE_TTL);
const res = await request(app)
.get('/api/dashboard/user-downloads')
.set('Cookie', cookies);
expect(res.status).toBe(200);
const dl = res.body.downloads.find(d => d.importIssues);
expect(dl).toBeDefined();
expect(dl.importIssues).toContain('No suitable video file found');
expect(dl.canBlocklist).toBe(true);
});
});
});
// ---------------------------------------------------------------------------
// GET /api/dashboard/status
// ---------------------------------------------------------------------------
describe('GET /api/dashboard/status', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/dashboard/status');
expect(res.status).toBe(401);
});
it('returns 403 for non-admin users', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
// Status route fetches Sonarr/Radarr notifications — intercept them
nock(SONARR_BASE).get('/api/v3/notification').reply(200, []);
nock(RADARR_BASE).get('/api/v3/notification').reply(200, []);
const res = await request(app)
.get('/api/dashboard/status')
.set('Cookie', cookies);
expect(res.status).toBe(403);
expect(res.body.error).toMatch(/admin/i);
});
it('returns server/cache/polling/webhook stats for admin', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app, EMBY_ADMIN_USER, EMBY_ADMIN_AUTH);
nock(SONARR_BASE).get('/api/v3/notification').reply(200, []);
nock(RADARR_BASE).get('/api/v3/notification').reply(200, []);
const res = await request(app)
.get('/api/dashboard/status')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.server).toBeDefined();
expect(typeof res.body.server.uptimeSeconds).toBe('number');
expect(typeof res.body.server.nodeVersion).toBe('string');
expect(res.body.cache).toBeDefined();
expect(res.body.polling).toBeDefined();
expect(res.body.webhooks).toBeDefined();
expect(res.body.clients).toBeDefined();
});
it('handles Sonarr/Radarr notification check failures gracefully', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app, EMBY_ADMIN_USER, EMBY_ADMIN_AUTH);
nock(SONARR_BASE).get('/api/v3/notification').replyWithError('connection refused');
nock(RADARR_BASE).get('/api/v3/notification').replyWithError('connection refused');
const res = await request(app)
.get('/api/dashboard/status')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.webhooks.sonarr).toBeNull();
expect(res.body.webhooks.radarr).toBeNull();
});
it('reports webhook configured=true when Sofarr notification exists', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app, EMBY_ADMIN_USER, EMBY_ADMIN_AUTH);
nock(SONARR_BASE)
.get('/api/v3/notification')
.reply(200, [{ name: 'Sofarr', implementation: 'Webhook' }]);
nock(RADARR_BASE).get('/api/v3/notification').reply(200, []);
const res = await request(app)
.get('/api/dashboard/status')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.webhooks.sonarr).toBeDefined();
expect(res.body.webhooks.sonarr.enabled).toBe(true);
});
});
// ---------------------------------------------------------------------------
// GET /api/dashboard/webhook-metrics
// ---------------------------------------------------------------------------
describe('GET /api/dashboard/webhook-metrics', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/dashboard/webhook-metrics');
expect(res.status).toBe(401);
});
it('returns webhook metrics for any authenticated user', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
const res = await request(app)
.get('/api/dashboard/webhook-metrics')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty('totalWebhookEventsReceived');
expect(res.body).toHaveProperty('instances');
});
});
// ---------------------------------------------------------------------------
// GET /api/dashboard/cover-art
// ---------------------------------------------------------------------------
describe('GET /api/dashboard/cover-art', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/dashboard/cover-art?url=https://img.test/poster.jpg');
expect(res.status).toBe(401);
});
it('returns 400 when url parameter is missing', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
const res = await request(app)
.get('/api/dashboard/cover-art')
.set('Cookie', cookies);
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/missing url/i);
});
it('returns 400 for an invalid URL', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
const res = await request(app)
.get('/api/dashboard/cover-art?url=not-a-url')
.set('Cookie', cookies);
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/invalid url/i);
});
it('returns 400 for non-http/https scheme', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
const res = await request(app)
.get('/api/dashboard/cover-art?url=ftp://img.test/poster.jpg')
.set('Cookie', cookies);
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/http/i);
});
it('returns 400 when remote URL is not an image', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock('https://img.test')
.get('/notanimage.html')
.reply(200, '<html/>', { 'content-type': 'text/html' });
const res = await request(app)
.get('/api/dashboard/cover-art?url=https://img.test/notanimage.html')
.set('Cookie', cookies);
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/not an image/i);
});
it('returns 502 when remote image fetch fails', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock('https://img.test')
.get('/poster.jpg')
.replyWithError('connection refused');
const res = await request(app)
.get('/api/dashboard/cover-art?url=https://img.test/poster.jpg')
.set('Cookie', cookies);
expect(res.status).toBe(502);
});
it('proxies an image and sets correct headers', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies } = await loginAs(app);
nock('https://img.test')
.get('/poster.jpg')
.reply(200, Buffer.from('FAKEJPEG'), { 'content-type': 'image/jpeg' });
const res = await request(app)
.get('/api/dashboard/cover-art?url=https://img.test/poster.jpg')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.headers['content-type']).toMatch(/image\/jpeg/);
expect(res.headers['cache-control']).toMatch(/max-age=86400/);
expect(res.headers['x-content-type-options']).toBe('nosniff');
});
});
// ---------------------------------------------------------------------------
// POST /api/dashboard/blocklist-search
// ---------------------------------------------------------------------------
describe('POST /api/dashboard/blocklist-search', () => {
async function getAuthHeaders(app, userBody = EMBY_ADMIN_USER, authBody = EMBY_ADMIN_AUTH) {
const { cookies, csrf } = await loginAs(app, userBody, authBody);
const csrfCookie = cookies.find(c => c.startsWith('csrf_token='));
return { cookies, csrfCookie, csrf };
}
it('returns 403 (CSRF missing) when not authenticated', async () => {
// verifyCsrf middleware fires before requireAuth for POST routes;
// an unauthenticated POST without CSRF headers gets 403, not 401.
const app = createApp({ skipRateLimits: true });
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.send({ arrQueueId: 1, arrType: 'sonarr', arrInstanceUrl: SONARR_BASE, arrInstanceKey: 'key', arrContentId: 501, arrContentType: 'episode' });
expect(res.status).toBe(403);
});
it('returns 403 for non-admin user', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf } = await loginAs(app);
const csrfCookie = cookies.find(c => c.startsWith('csrf_token='));
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.set('Cookie', [...cookies, csrfCookie].join('; '))
.set('X-CSRF-Token', csrf)
.send({ arrQueueId: 1, arrType: 'sonarr', arrInstanceUrl: SONARR_BASE, arrInstanceKey: 'key', arrContentId: 501, arrContentType: 'episode' });
expect(res.status).toBe(403);
expect(res.body.error).toMatch(/admin/i);
});
it('returns 400 when required fields are missing', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getAuthHeaders(app);
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.set('Cookie', [...cookies, csrfCookie].join('; '))
.set('X-CSRF-Token', csrf)
.send({ arrQueueId: 1 });
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/missing/i);
});
it('returns 400 for invalid arrType', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getAuthHeaders(app);
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.set('Cookie', [...cookies, csrfCookie].join('; '))
.set('X-CSRF-Token', csrf)
.send({ arrQueueId: 1, arrType: 'invalid', arrInstanceUrl: SONARR_BASE, arrInstanceKey: 'key', arrContentId: 501, arrContentType: 'episode' });
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/sonarr or radarr/i);
});
it('calls Sonarr DELETE+command and returns ok:true', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getAuthHeaders(app);
nock(SONARR_BASE)
.delete('/api/v3/queue/1001')
.query({ removeFromClient: 'true', blocklist: 'true' })
.reply(200, {});
nock(SONARR_BASE)
.post('/api/v3/command')
.reply(200, {});
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.set('Cookie', [...cookies, csrfCookie].join('; '))
.set('X-CSRF-Token', csrf)
.send({ arrQueueId: 1001, arrType: 'sonarr', arrInstanceUrl: SONARR_BASE, arrInstanceKey: 'sk', arrContentId: 501, arrContentType: 'episode' });
expect(res.status).toBe(200);
expect(res.body.ok).toBe(true);
});
it('calls Radarr DELETE+command and returns ok:true', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getAuthHeaders(app);
nock(RADARR_BASE)
.delete('/api/v3/queue/2001')
.query({ removeFromClient: 'true', blocklist: 'true' })
.reply(200, {});
nock(RADARR_BASE)
.post('/api/v3/command')
.reply(200, {});
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.set('Cookie', [...cookies, csrfCookie].join('; '))
.set('X-CSRF-Token', csrf)
.send({ arrQueueId: 2001, arrType: 'radarr', arrInstanceUrl: RADARR_BASE, arrInstanceKey: 'rk', arrContentId: 99, arrContentType: 'movie' });
expect(res.status).toBe(200);
expect(res.body.ok).toBe(true);
});
it('returns 502 when Sonarr DELETE request fails', async () => {
const app = createApp({ skipRateLimits: true });
const { cookies, csrf, csrfCookie } = await getAuthHeaders(app);
nock(SONARR_BASE)
.delete('/api/v3/queue/1001')
.query(true)
.replyWithError('connection refused');
const res = await request(app)
.post('/api/dashboard/blocklist-search')
.set('Cookie', [...cookies, csrfCookie].join('; '))
.set('X-CSRF-Token', csrf)
.send({ arrQueueId: 1001, arrType: 'sonarr', arrInstanceUrl: SONARR_BASE, arrInstanceKey: 'sk', arrContentId: 501, arrContentType: 'episode' });
expect(res.status).toBe(502);
});
});
+260
View File
@@ -0,0 +1,260 @@
// Copyright (c) 2026 Gordon Bolton. MIT License.
/**
* Integration tests for server/routes/emby.js
*
* All four endpoints are covered:
* GET /api/emby/sessions
* GET /api/emby/users
* GET /api/emby/users/:id
* GET /api/emby/session/:sessionId/user
*
* For each: auth guard (401), happy path, and upstream failure (500).
* No CSRF token is needed — all routes are read-only GETs.
*/
import request from 'supertest';
import nock from 'nock';
import { createApp } from '../../server/app.js';
const EMBY_BASE = 'https://emby.test';
// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------
const EMBY_AUTH = { AccessToken: 'tok', User: { Id: 'uid1', Name: 'alice' } };
const EMBY_USER = { Id: 'uid1', Name: 'alice', Policy: { IsAdministrator: false } };
const EMBY_SESSIONS = [
{ Id: 'sess-001', UserId: 'uid1', UserName: 'alice', Client: 'Emby Web', DeviceName: 'Chrome' },
{ Id: 'sess-002', UserId: 'uid2', UserName: 'bob', Client: 'Emby iOS', DeviceName: 'iPhone' }
];
const EMBY_USERS_LIST = [
{ Id: 'uid1', Name: 'alice', Policy: { IsAdministrator: false } },
{ Id: 'uid2', Name: 'bob', Policy: { IsAdministrator: false } }
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function interceptLogin() {
nock(EMBY_BASE).post('/Users/authenticatebyname').reply(200, EMBY_AUTH);
nock(EMBY_BASE).get(/\/Users\//).reply(200, EMBY_USER);
}
async function loginAs(app) {
interceptLogin();
const res = await request(app)
.post('/api/auth/login')
.send({ username: 'alice', password: 'pw' });
return res.headers['set-cookie'];
}
// ---------------------------------------------------------------------------
// Environment
// ---------------------------------------------------------------------------
beforeAll(() => {
process.env.EMBY_URL = EMBY_BASE;
process.env.EMBY_API_KEY = 'emby-api-key';
});
afterAll(() => {
delete process.env.EMBY_URL;
delete process.env.EMBY_API_KEY;
});
afterEach(() => {
nock.cleanAll();
});
// ---------------------------------------------------------------------------
// GET /api/emby/sessions
// ---------------------------------------------------------------------------
describe('GET /api/emby/sessions', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/emby/sessions');
expect(res.status).toBe(401);
});
it('proxies Emby sessions list', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Sessions')
.reply(200, EMBY_SESSIONS);
const res = await request(app)
.get('/api/emby/sessions')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBe(2);
expect(res.body[0].Id).toBe('sess-001');
});
it('returns 500 when Emby is unreachable', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Sessions')
.replyWithError('ECONNREFUSED');
const res = await request(app)
.get('/api/emby/sessions')
.set('Cookie', cookies);
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/sessions/i);
});
});
// ---------------------------------------------------------------------------
// GET /api/emby/users
// ---------------------------------------------------------------------------
describe('GET /api/emby/users', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/emby/users');
expect(res.status).toBe(401);
});
it('proxies Emby users list', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Users')
.reply(200, EMBY_USERS_LIST);
const res = await request(app)
.get('/api/emby/users')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body[0].Name).toBe('alice');
});
it('returns 500 when Emby is unreachable', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Users')
.replyWithError('ECONNREFUSED');
const res = await request(app)
.get('/api/emby/users')
.set('Cookie', cookies);
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/users/i);
});
});
// ---------------------------------------------------------------------------
// GET /api/emby/users/:id
// ---------------------------------------------------------------------------
describe('GET /api/emby/users/:id', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/emby/users/uid1');
expect(res.status).toBe(401);
});
it('proxies individual user details', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Users/uid1')
.reply(200, EMBY_USER);
const res = await request(app)
.get('/api/emby/users/uid1')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.Id).toBe('uid1');
expect(res.body.Name).toBe('alice');
});
it('returns 500 when Emby returns an error', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Users/uid-unknown')
.reply(404, { error: 'Not found' });
const res = await request(app)
.get('/api/emby/users/uid-unknown')
.set('Cookie', cookies);
expect(res.status).toBe(500);
});
});
// ---------------------------------------------------------------------------
// GET /api/emby/session/:sessionId/user
// ---------------------------------------------------------------------------
describe('GET /api/emby/session/:sessionId/user', () => {
it('returns 401 when not authenticated', async () => {
const app = createApp({ skipRateLimits: true });
const res = await request(app).get('/api/emby/session/sess-001/user');
expect(res.status).toBe(401);
});
it('returns the user associated with a session', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Sessions')
.reply(200, EMBY_SESSIONS);
nock(EMBY_BASE)
.get('/Users/uid1')
.reply(200, EMBY_USER);
const res = await request(app)
.get('/api/emby/session/sess-001/user')
.set('Cookie', cookies);
expect(res.status).toBe(200);
expect(res.body.Name).toBe('alice');
});
it('returns 404 when session ID is not found', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Sessions')
.reply(200, EMBY_SESSIONS);
const res = await request(app)
.get('/api/emby/session/sess-nonexistent/user')
.set('Cookie', cookies);
expect(res.status).toBe(404);
expect(res.body.error).toMatch(/session not found/i);
});
it('returns 500 when Emby sessions fetch fails', async () => {
const app = createApp({ skipRateLimits: true });
const cookies = await loginAs(app);
nock(EMBY_BASE)
.get('/Sessions')
.replyWithError('ECONNREFUSED');
const res = await request(app)
.get('/api/emby/session/sess-001/user')
.set('Cookie', cookies);
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/session/i);
});
});