mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-12 19:57:37 +00:00
83b3d932e5
* feat: add Apprise as a fourth notification channel Support keyed and stateless Apprise endpoints with secret-safe public DTOs, fail-closed malformed config, and mode-specific Settings UI. Docs and screenshots updated for four-channel Channels and routing. * fix: harden Apprise secrets at rest and preserve-on-write saves Encrypt Apprise endpoint and config with CryptoService so a downgrade cannot leak via SELECT *. Align channel and routing saves so blank destination fields omit config on same-mode URL edits, enforce keyed notify IDs, and keep secrets_redacted truthful. * fix: harden Apprise route type changes and mixed-version config UI Require a raw channel_url when switching notification-route types so ciphertext cannot strand under Discord/Slack/webhook. Default missing remote apprise status, replace Channels state on node switch, and exercise the production config-column migrator. * fix: tolerate stub fleet configuration payloads without agents Normalize remote Apprise agent status only when notifications.agents is present so successful Pilot/stub fetches stay online instead of throwing into the offline catch path. * fix: correct TypeScript in configuration normalize tests * fix: ignore stale Channels agent bodies after node switch Compare the active node after response JSON parsing so a slow body cannot overwrite the newly selected node's channel state. * fix: isolate corrupt Apprise crypto and keep keyed Tags visible Decrypt failures on one Apprise row no longer 500 agent/route lists or suppress sibling channel dispatch. Treat public /notify/<redacted> as keyed so Tags remain editable after reload.
246 lines
8.8 KiB
TypeScript
246 lines
8.8 KiB
TypeScript
/**
|
|
* Integration tests for /api/agents (notification-channel configuration).
|
|
* Locks down auth, admin gating, and validation before extraction.
|
|
*/
|
|
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
|
|
import request from 'supertest';
|
|
import bcrypt from 'bcrypt';
|
|
import { setupTestDb, cleanupTestDb, loginAsTestAdmin } from './helpers/setupTestDb';
|
|
|
|
let tmpDir: string;
|
|
let app: import('express').Express;
|
|
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
|
|
let adminCookie: string;
|
|
let viewerCookie: string;
|
|
|
|
beforeAll(async () => {
|
|
tmpDir = await setupTestDb();
|
|
({ DatabaseService } = await import('../services/DatabaseService'));
|
|
|
|
const { LicenseService } = await import('../services/LicenseService');
|
|
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('paid');
|
|
|
|
({ app } = await import('../index'));
|
|
adminCookie = await loginAsTestAdmin(app);
|
|
|
|
const viewerHash = await bcrypt.hash('viewerpass', 1);
|
|
DatabaseService.getInstance().addUser({ username: 'agents-viewer', password_hash: viewerHash, role: 'viewer' });
|
|
const viewerRes = await request(app).post('/api/auth/login').send({ username: 'agents-viewer', password: 'viewerpass' });
|
|
const cookies = viewerRes.headers['set-cookie'] as string | string[];
|
|
viewerCookie = Array.isArray(cookies) ? cookies[0] : cookies;
|
|
});
|
|
|
|
afterAll(() => cleanupTestDb(tmpDir));
|
|
|
|
beforeEach(() => {
|
|
const db = DatabaseService.getInstance().getDb();
|
|
db.prepare('DELETE FROM agents').run();
|
|
});
|
|
|
|
describe('GET /api/agents', () => {
|
|
it('rejects unauthenticated requests with 401', async () => {
|
|
const res = await request(app).get('/api/agents');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('returns empty array when no agents configured', async () => {
|
|
const res = await request(app).get('/api/agents').set('Cookie', adminCookie);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual([]);
|
|
});
|
|
|
|
it('lists configured agents for authenticated users', async () => {
|
|
const db = DatabaseService.getInstance();
|
|
db.upsertAgent(1, { type: 'discord', url: 'https://discord.com/api/webhooks/abc/def', enabled: true });
|
|
const res = await request(app).get('/api/agents').set('Cookie', adminCookie);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.length).toBe(1);
|
|
expect(res.body[0].type).toBe('discord');
|
|
});
|
|
});
|
|
|
|
describe('POST /api/agents', () => {
|
|
const validPayload = {
|
|
type: 'discord',
|
|
url: 'https://discord.com/api/webhooks/1/token',
|
|
enabled: true,
|
|
};
|
|
|
|
it('rejects unauthenticated requests with 401', async () => {
|
|
const res = await request(app).post('/api/agents').send(validPayload);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('rejects non-admin users with 403', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', viewerCookie).send(validPayload);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('rejects unsupported channel types with 400', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
...validPayload, type: 'carrier-pigeon',
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects non-HTTPS urls with 400', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
...validPayload, url: 'http://example.com/hook',
|
|
});
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toMatch(/url/);
|
|
});
|
|
|
|
it('rejects non-boolean enabled with 400', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
...validPayload, enabled: 'yes',
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('upserts a valid agent', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send(validPayload);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.success).toBe(true);
|
|
|
|
const agents = DatabaseService.getInstance().getAgents(1);
|
|
expect(agents.length).toBe(1);
|
|
expect(agents[0].type).toBe('discord');
|
|
expect(Boolean(agents[0].enabled)).toBe(true);
|
|
});
|
|
|
|
it('replaces an existing agent of the same type (upsert)', async () => {
|
|
const db = DatabaseService.getInstance();
|
|
db.upsertAgent(1, { type: 'slack', url: 'https://hooks.slack.com/old', enabled: false });
|
|
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
type: 'slack', url: 'https://hooks.slack.com/new', enabled: true,
|
|
});
|
|
expect(res.status).toBe(200);
|
|
|
|
const agents = db.getAgents(1);
|
|
expect(agents.length).toBe(1);
|
|
expect(agents[0].url).toBe('https://hooks.slack.com/new');
|
|
expect(Boolean(agents[0].enabled)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Apprise agents - redaction and preserve-on-write', () => {
|
|
const keyedUrl = 'http://apprise.local/notify/key-secret-value';
|
|
const serviceUrl = 'discord://webhook-id/webhook-token?token=query-secret';
|
|
|
|
it('GET redacts keyed endpoint and never returns destination secrets', async () => {
|
|
DatabaseService.getInstance().upsertAgent(1, {
|
|
type: 'apprise',
|
|
url: keyedUrl,
|
|
enabled: true,
|
|
config: JSON.stringify({ tags: 'ops' }),
|
|
});
|
|
|
|
const res = await request(app).get('/api/agents').set('Cookie', adminCookie);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body[0].url).toBe('http://apprise.local/notify/<redacted>');
|
|
expect(res.body[0].config).toMatchObject({ mode: 'keyed', tags: 'ops', has_urls: false });
|
|
expect(res.body[0].secrets_redacted).toBe(true);
|
|
expect(JSON.stringify(res.body)).not.toContain('key-secret-value');
|
|
});
|
|
|
|
it('GET for stateless mode exposes providers and url_count only', async () => {
|
|
DatabaseService.getInstance().upsertAgent(1, {
|
|
type: 'apprise',
|
|
url: 'http://apprise.local/notify',
|
|
enabled: true,
|
|
config: JSON.stringify({ urls: serviceUrl }),
|
|
});
|
|
|
|
const res = await request(app).get('/api/agents').set('Cookie', adminCookie);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body[0].url).toBe('http://apprise.local/notify');
|
|
expect(res.body[0].config).toMatchObject({
|
|
mode: 'stateless',
|
|
has_urls: true,
|
|
providers: ['discord'],
|
|
url_count: 1,
|
|
});
|
|
expect(JSON.stringify(res.body)).not.toContain('webhook-token');
|
|
expect(JSON.stringify(res.body)).not.toContain('query-secret');
|
|
});
|
|
|
|
it('rejects posting a redacted endpoint URL', async () => {
|
|
DatabaseService.getInstance().upsertAgent(1, {
|
|
type: 'apprise',
|
|
url: keyedUrl,
|
|
enabled: true,
|
|
config: JSON.stringify({ tags: 'ops' }),
|
|
});
|
|
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
type: 'apprise',
|
|
url: 'http://apprise.local/notify/<redacted>',
|
|
enabled: true,
|
|
config: { tags: 'ops' },
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects public DTO config shapes on write', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
type: 'apprise',
|
|
url: 'http://apprise.local/notify',
|
|
enabled: true,
|
|
config: { mode: 'stateless', has_urls: true, providers: ['discord'], url_count: 1 },
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('preserves stored secrets when url and config are omitted', async () => {
|
|
const db = DatabaseService.getInstance();
|
|
db.upsertAgent(1, {
|
|
type: 'apprise',
|
|
url: keyedUrl,
|
|
enabled: true,
|
|
config: JSON.stringify({ tags: 'ops' }),
|
|
});
|
|
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
type: 'apprise',
|
|
enabled: false,
|
|
});
|
|
expect(res.status).toBe(200);
|
|
|
|
const agents = db.getAgents(1);
|
|
expect(agents[0].url).toBe(keyedUrl);
|
|
expect(agents[0].config).toBe(JSON.stringify({ tags: 'ops' }));
|
|
expect(Boolean(agents[0].enabled)).toBe(false);
|
|
});
|
|
|
|
it('creates a keyed agent with no config and persists {}', async () => {
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
type: 'apprise',
|
|
url: 'http://apprise.local/notify/new-key',
|
|
enabled: true,
|
|
});
|
|
expect(res.status).toBe(200);
|
|
const agent = DatabaseService.getInstance().getAgents(1).find(a => a.type === 'apprise');
|
|
expect(agent?.url).toBe('http://apprise.local/notify/new-key');
|
|
expect(agent?.config).toBe('{}');
|
|
});
|
|
|
|
it('rejects preserve-on-write when stored Apprise config is malformed', async () => {
|
|
DatabaseService.getInstance().upsertAgent(1, {
|
|
type: 'apprise',
|
|
url: keyedUrl,
|
|
enabled: true,
|
|
config: '{not-json',
|
|
});
|
|
|
|
const res = await request(app).post('/api/agents').set('Cookie', adminCookie).send({
|
|
type: 'apprise',
|
|
enabled: false,
|
|
});
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toMatch(/invalid/i);
|
|
expect(JSON.stringify(res.body)).not.toContain('key-secret-value');
|
|
});
|
|
});
|