mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +00:00
ac96644508
Four files taken apart rather than moved, because they only worked as a knot. The mailer becomes its own leaf module. AuthService sends the password-reset mail while NotificationsModule imports AuthModule for the demo gate in NotificationsMcp - a mailer inside the notifications domain would have closed a hard cycle that only a forwardRef could open again. Webhook and ntfy stay with notifications; auth has no business importing them. The channel registry stays a module singleton and does NOT become a provider. PluginRuntimeService pushes its channel getter in at onModuleInit, and notifications.bridge.ts builds its own NotificationsService for the scheduler crons and the memories senders. A provider would hand the bridge a second, empty registry - every plugin channel would then go quiet with no error anywhere. CHOVR-015 drives that seam end to end: a channel published through the runtime, delivered through the bridge. Nothing pinned it before; the existing cases all set the source themselves. The built-ins no longer self-register by side-effect import. They used to exist only because notificationPreferencesService happened to `import './notifications/builtins'`; removing that line would have silenced email, webhook and ntfy without a failure. They are built from the injected transports and registered from NotificationsService's constructor, so both instances have them. No cached nodemailer transport: MailerService.createTransport re-reads the SMTP config and skipTlsVerify per send, exactly as the three inline call sites did, so an admin changing them does not have to restart. Preferences become a provider over DatabaseService. Eight places built NotificationsService by hand, so a tests/helpers/notifications.ts factory keeps the next constructor parameter a one-file diff instead of an eight-file one.
165 lines
7.6 KiB
TypeScript
165 lines
7.6 KiB
TypeScript
/**
|
|
* Notifications module e2e — exercises the migrated /api/notifications endpoints
|
|
* through the real JwtAuthGuard against a temp SQLite db. NotificationsService
|
|
* runs its real (DI-native) in-app SQL — notifications DDL below; the channel
|
|
* transports and the preference matrix (still plain services/* modules) are
|
|
* mocked. Focuses on auth, the inline admin gate on /test-smtp, routing (the
|
|
* /in-app/all ordering trap) and status/body shapes.
|
|
*/
|
|
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
|
|
import request from 'supertest';
|
|
import cookieParser from 'cookie-parser';
|
|
import type { Server } from 'http';
|
|
import { Test } from '@nestjs/testing';
|
|
import { seedUser, sessionCookie } from './harness';
|
|
|
|
const { db } = vi.hoisted(() => {
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const Database = require('better-sqlite3');
|
|
const tmp = new Database(':memory:');
|
|
tmp.exec('PRAGMA journal_mode = WAL');
|
|
tmp.exec(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL,
|
|
email TEXT NOT NULL UNIQUE, role TEXT NOT NULL DEFAULT 'user', password_version INTEGER NOT NULL DEFAULT 0,
|
|
avatar TEXT);`);
|
|
// NotificationsService runs its real SQL (DI-native since the notifications
|
|
// fold) — full notifications column set for listInApp/markRead/deleteAll.
|
|
tmp.exec(`CREATE TABLE notifications (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
type TEXT NOT NULL CHECK(type IN ('simple', 'boolean', 'navigate')),
|
|
scope TEXT NOT NULL CHECK(scope IN ('trip', 'user', 'admin')),
|
|
target INTEGER NOT NULL,
|
|
sender_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
|
|
recipient_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
title_key TEXT NOT NULL,
|
|
title_params TEXT DEFAULT '{}',
|
|
text_key TEXT NOT NULL,
|
|
text_params TEXT DEFAULT '{}',
|
|
positive_text_key TEXT,
|
|
negative_text_key TEXT,
|
|
positive_callback TEXT,
|
|
negative_callback TEXT,
|
|
response TEXT CHECK(response IN ('positive', 'negative')),
|
|
navigate_text_key TEXT,
|
|
navigate_target TEXT,
|
|
is_read INTEGER DEFAULT 0,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
);`);
|
|
return { db: tmp };
|
|
});
|
|
|
|
vi.mock('../../src/db/database', () => ({ db, closeDb: () => {}, reinitialize: () => {} }));
|
|
|
|
// The preference matrix and the channel transports are providers since the
|
|
// fold — overridden at the container instead of mocked by module path; the
|
|
// in-app store is real SQL.
|
|
const { prefs, mailer, webhook, ntfy } = vi.hoisted(() => ({
|
|
prefs: { getPreferencesMatrix: vi.fn(), setPreferences: vi.fn() },
|
|
mailer: { testSmtp: vi.fn(), isSmtpConfigured: vi.fn(() => true), getUserEmail: vi.fn(), getUserLanguage: vi.fn(() => 'en') },
|
|
webhook: { testWebhook: vi.fn(), getUserWebhookUrl: vi.fn(), getAdminWebhookUrl: vi.fn() },
|
|
ntfy: { testNtfy: vi.fn(), getUserNtfyConfig: vi.fn(), getAdminNtfyConfig: vi.fn() },
|
|
}));
|
|
|
|
import { DatabaseModule } from '../../src/nest/database/database.module';
|
|
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
|
import { NotificationsModule } from '../../src/nest/notifications/notifications.module';
|
|
import { MailerService } from '../../src/nest/notifications/mailer/mailer.service';
|
|
import { NotificationPreferencesService } from '../../src/nest/notifications/notification-preferences.service';
|
|
import { NtfyService } from '../../src/nest/notifications/transports/ntfy.service';
|
|
import { WebhookService } from '../../src/nest/notifications/transports/webhook.service';
|
|
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
|
|
|
function seedNotification(recipientId: number, overrides: { is_read?: number } = {}): number {
|
|
const r = db.prepare(
|
|
`INSERT INTO notifications (type, scope, target, recipient_id, title_key, text_key, is_read)
|
|
VALUES ('simple', 'user', ?, ?, 'notif.test.title', 'notif.test.text', ?)`
|
|
).run(recipientId, recipientId, overrides.is_read ?? 0);
|
|
return r.lastInsertRowid as number;
|
|
}
|
|
|
|
describe('Notifications e2e (real auth guard + temp SQLite)', () => {
|
|
let server: Server;
|
|
let app: Awaited<ReturnType<typeof build>>;
|
|
|
|
async function build() {
|
|
const moduleRef = await Test.createTestingModule({
|
|
imports: [DatabaseModule, RealtimeModule, NotificationsModule],
|
|
})
|
|
.overrideProvider(NotificationPreferencesService).useValue(prefs)
|
|
.overrideProvider(MailerService).useValue(mailer)
|
|
.overrideProvider(WebhookService).useValue(webhook)
|
|
.overrideProvider(NtfyService).useValue(ntfy)
|
|
.compile();
|
|
const nest = moduleRef.createNestApplication();
|
|
nest.use(cookieParser());
|
|
nest.useGlobalFilters(new TrekExceptionFilter());
|
|
await nest.init();
|
|
return nest;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
seedUser(db as never, { id: 1, role: 'admin', email: 'admin@example.test' });
|
|
seedUser(db as never, { id: 2, role: 'user', email: 'user@example.test' });
|
|
app = await build();
|
|
server = app.getHttpServer();
|
|
prefs.getPreferencesMatrix.mockReturnValue({ preferences: {}, available_channels: {}, event_types: [], implemented_combos: {} });
|
|
mailer.testSmtp.mockResolvedValue({ success: true });
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('401 without a session cookie', async () => {
|
|
const res = await request(server).get('/api/notifications/preferences');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('200 preferences for an authenticated user', async () => {
|
|
const res = await request(server).get('/api/notifications/preferences').set('Cookie', sessionCookie(2));
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toMatchObject({ preferences: {} });
|
|
});
|
|
|
|
it('403 { error: Admin only } when a non-admin hits test-smtp', async () => {
|
|
const res = await request(server).post('/api/notifications/test-smtp').set('Cookie', sessionCookie(2)).send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body).toEqual({ error: 'Admin only' });
|
|
expect(mailer.testSmtp).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('200 test-smtp for an admin (stays 200, not 201)', async () => {
|
|
const res = await request(server).post('/api/notifications/test-smtp').set('Cookie', sessionCookie(1)).send({ email: 'x@y.z' });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual({ success: true });
|
|
});
|
|
|
|
it('200 unread-count from the real notifications table', async () => {
|
|
db.prepare('DELETE FROM notifications').run();
|
|
seedNotification(2);
|
|
seedNotification(2);
|
|
seedNotification(2, { is_read: 1 });
|
|
const res = await request(server).get('/api/notifications/in-app/unread-count').set('Cookie', sessionCookie(2));
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual({ count: 2 });
|
|
});
|
|
|
|
it('DELETE /in-app/all hits deleteAll, not the /:id handler', async () => {
|
|
db.prepare('DELETE FROM notifications').run();
|
|
for (let i = 0; i < 4; i++) seedNotification(2);
|
|
const other = seedNotification(1);
|
|
const res = await request(server).delete('/api/notifications/in-app/all').set('Cookie', sessionCookie(2));
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual({ success: true, count: 4 });
|
|
// Only user 2's rows are gone — the static route deleted per-recipient, it
|
|
// did not fall through to the /:id param handler (which would 400 on 'all').
|
|
const rows = db.prepare('SELECT id FROM notifications').all() as { id: number }[];
|
|
expect(rows.map(r => r.id)).toEqual([other]);
|
|
});
|
|
|
|
it('400 on a non-numeric in-app id', async () => {
|
|
const res = await request(server).put('/api/notifications/in-app/abc/read').set('Cookie', sessionCookie(2));
|
|
expect(res.status).toBe(400);
|
|
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
});
|
|
});
|