mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
fix(notifications): link admin emails to admin notification settings
This commit is contained in:
@@ -880,6 +880,11 @@ describe('useAdmin', () => {
|
||||
expect(result.current.activeTab).toBe('audit');
|
||||
});
|
||||
|
||||
it('opens the notifications tab from the admin email manage link', async () => {
|
||||
const { result } = await mountAdmin('/admin?tab=notifications');
|
||||
expect(result.current.activeTab).toBe('notifications');
|
||||
});
|
||||
|
||||
/*
|
||||
* A tab id with no panel behind it — a typo, or a link from an install that
|
||||
* has tabs this one does not — used to open the page on an empty content
|
||||
|
||||
@@ -51,7 +51,8 @@ export function buildBuiltinChannels({ mailer, webhook, ntfy, push }: BuiltinCha
|
||||
async sendToUser(userId, msg) {
|
||||
const email = await mailer.getUserEmail(userId);
|
||||
if (!email) return false;
|
||||
return mailer.sendEmail(email, msg.title, msg.body, userId, msg.navigateTarget);
|
||||
const manageTarget = ADMIN_SCOPED_EVENTS.has(msg.event) ? '/admin?tab=notifications' : undefined;
|
||||
return mailer.sendEmail(email, msg.title, msg.body, userId, msg.navigateTarget, manageTarget);
|
||||
},
|
||||
async test(userId) {
|
||||
const email = await mailer.getUserEmail(userId);
|
||||
|
||||
@@ -37,11 +37,12 @@ export function buildEmailHtml(
|
||||
lang: string,
|
||||
navigateTarget?: string,
|
||||
rawBody = false,
|
||||
manageTarget = '/settings?tab=notifications',
|
||||
): string {
|
||||
const s = I18N[lang] || I18N.en;
|
||||
const appUrl = getAppUrl();
|
||||
const ctaHref = escapeHtml(navigateTarget ? `${appUrl}${navigateTarget}` : appUrl || '');
|
||||
const manageHref = escapeHtml(`${appUrl}/settings?tab=notifications`);
|
||||
const manageHref = escapeHtml(`${appUrl}${manageTarget}`);
|
||||
const safeSubject = escapeHtml(subject);
|
||||
const safeBody = rawBody ? body : escapeHtml(body);
|
||||
|
||||
|
||||
@@ -213,6 +213,7 @@ export class MailerService {
|
||||
body: string,
|
||||
userId?: number,
|
||||
navigateTarget?: string,
|
||||
manageTarget?: string,
|
||||
): Promise<boolean> {
|
||||
const config = await this.getSmtpConfig();
|
||||
if (!config) return false;
|
||||
@@ -227,7 +228,7 @@ export class MailerService {
|
||||
to,
|
||||
subject: `TREK — ${subject}`,
|
||||
text: body,
|
||||
html: buildEmailHtml(subject, body, lang, navigateTarget),
|
||||
html: buildEmailHtml(subject, body, lang, navigateTarget, false, manageTarget),
|
||||
attachments: [emailLogoAttachment()],
|
||||
});
|
||||
logInfo(`Email sent to=${to} subject="${subject}"`);
|
||||
|
||||
@@ -260,6 +260,13 @@ describe('buildEmailHtml', () => {
|
||||
expect(html).toContain('Hello world, this is the body!');
|
||||
});
|
||||
|
||||
it('keeps the email shell fluid on narrow screens', () => {
|
||||
const html = buildEmailHtml('Subject', 'Body', 'en');
|
||||
expect(html).toContain('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
|
||||
expect(html).toContain('width="100%"');
|
||||
expect(html).toContain('max-width: 480px');
|
||||
});
|
||||
|
||||
it('uses English i18n strings for lang=en', () => {
|
||||
const html = buildEmailHtml('Subject', 'Body', 'en');
|
||||
expect(html).toContain('notifications enabled in TREK');
|
||||
@@ -272,6 +279,13 @@ describe('buildEmailHtml', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('links admin-scoped notification preferences to the admin notifications tab', () => {
|
||||
const html = buildEmailHtml('Subject', 'Body', 'en', undefined, false, '/admin?tab=notifications');
|
||||
expect(html).toMatch(
|
||||
/<a href="https?:\/\/[^"]+\/admin\?tab=notifications"[^>]*>Manage preferences in Settings<\/a>/,
|
||||
);
|
||||
});
|
||||
|
||||
it('uses German i18n strings for lang=de', () => {
|
||||
const html = buildEmailHtml('Subject', 'Body', 'de');
|
||||
expect(html).toContain('TREK aktiviert');
|
||||
@@ -293,6 +307,43 @@ describe('buildEmailHtml', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('built-in email manage link scope', () => {
|
||||
it('uses the admin tab for admin-scoped events and the user tab otherwise', async () => {
|
||||
const mailer = {
|
||||
getUserEmail: vi.fn(async () => 'person@example.com'),
|
||||
sendEmail: vi.fn(async () => true),
|
||||
};
|
||||
const emailChannel = buildBuiltinChannels({
|
||||
mailer: mailer as never,
|
||||
webhook: {} as never,
|
||||
ntfy: {} as never,
|
||||
push: {} as never,
|
||||
}).find((channel) => channel.id === 'email')!;
|
||||
|
||||
await emailChannel.sendToUser(1, { event: 'version_available', title: 'Update', body: 'New version' });
|
||||
await emailChannel.sendToUser(1, { event: 'trip_invite', title: 'Invite', body: 'Join trip' });
|
||||
|
||||
expect(mailer.sendEmail).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'person@example.com',
|
||||
'Update',
|
||||
'New version',
|
||||
1,
|
||||
undefined,
|
||||
'/admin?tab=notifications',
|
||||
);
|
||||
expect(mailer.sendEmail).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'person@example.com',
|
||||
'Invite',
|
||||
'Join trip',
|
||||
1,
|
||||
undefined,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ── SEC: XSS escaping in buildEmailHtml ──────────────────────────────────────
|
||||
|
||||
describe('buildEmailHtml XSS prevention (SEC-016)', () => {
|
||||
|
||||
Reference in New Issue
Block a user