A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
test(email): cover template dispatch and bounce routing
Hampusb15b706c7b76
diff --git a/fluxer_api/pkgs/email/src/DomainRoutedEmailProvider.test.ts b/fluxer_api/pkgs/email/src/DomainRoutedEmailProvider.test.tsindex 08a9ad5ca..18722b375 100644--- a/fluxer_api/pkgs/email/src/DomainRoutedEmailProvider.test.ts+++ b/fluxer_api/pkgs/email/src/DomainRoutedEmailProvider.test.ts@@ -49,15 +49,21 @@ describe('DomainRoutedEmailProvider', () => {fromEmail: '[email protected]',fromName: 'Example',appBaseUrl: 'https://example.com',+ apiBaseUrl: 'https://api.example.com',appealsEmail: null,+ privacyEmail: null,safetyEmail: null,supportEmail: null,productName: 'Example',+ marketingBaseUrl: 'https://example.com',+ staticCdnBaseUrl: 'https://static.example.com',+ legalAddress: null,+ brandLogo: false,},new DomainRoutedEmailProvider(primary, fallback, ['gmail.com']),{isEmailBounced: async () => true},);- expect(await service.sendPasswordResetEmail('[email protected]', 'user', 'token')).toBe(false);+ expect(await service.send('password_reset', '[email protected]', {resetToken: 'token'})).toBe(false);expect(primary.sendEmail).not.toHaveBeenCalled();expect(fallback.sendEmail).not.toHaveBeenCalled();});diff --git a/fluxer_api/pkgs/email/src/EmailService.test.ts b/fluxer_api/pkgs/email/src/EmailService.test.tsnew file mode 100644index 000000000..5ade02926--- /dev/null+++ b/fluxer_api/pkgs/email/src/EmailService.test.ts@@ -0,0 +1,88 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import type {EmailConfig, EmailMessage, IEmailProvider} from '@pkgs/email/src/EmailProviderTypes';+import {EmailService} from '@pkgs/email/src/EmailService';+import {describe, expect, it} from 'vitest';++const CONFIG: EmailConfig = {+ enabled: true,+ fromEmail: '[email protected]',+ fromName: 'Fluxer',+ appBaseUrl: 'https://example.com',+ apiBaseUrl: 'https://api.example.com',+ appealsEmail: '[email protected]',+ privacyEmail: '[email protected]',+ safetyEmail: '[email protected]',+ supportEmail: '[email protected]',+ productName: 'Fluxer',+ marketingBaseUrl: 'https://example.com',+ staticCdnBaseUrl: 'https://static.example.com',+ legalAddress: 'Fluxer Platform AB, c/o Embassy House, Östgötagatan 12, 116 25 Stockholm, Sweden',+ brandLogo: true,+};++function createCapturingServiceFor(config: EmailConfig): {service: EmailService; sent: Array<EmailMessage>} {+ const sent: Array<EmailMessage> = [];+ const provider: IEmailProvider = {+ sendEmail: async (message) => {+ sent.push(message);+ return true;+ },+ };+ return {service: new EmailService(config, provider), sent};+}++const SALE_PARAMS = {+ checkoutUrl: 'https://web.fluxer.app/channels/@operator-pass',+ priceFormatted: '$199',+ priceRisesAt: new Date('2027-01-02T12:00:00Z'),+ unsubscribeUrl: 'https://api.fluxer.app/operator-pass/waitlist/unsubscribe?token=a.b',+ unsubscribePageUrl: 'https://fluxer.app/pass/unsubscribe?token=a.b',+};++describe('EmailService Operator Pass emails', () => {+ it('escapes variables in the HTML part and keeps them raw in plaintext', async () => {+ const {service, sent} = createCapturingService();+ await service.send(+ 'operator_pass_sale_open',+ '[email protected]',+ {...SALE_PARAMS, priceFormatted: `<b>"it's & $1"</b>`},+ 'en-US',+ );+ expect(sent[0].text).toContain(`<b>"it's & $1"</b>`);+ const html = sent[0].html ?? '';+ expect(html).toContain('<b>"it's & $1"</b>');+ expect(html).not.toContain('<b>"');+ });++ it('falls back to plaintext only when a link is not https', async () => {+ const {service, sent} = createCapturingService();+ await expect(+ service.send(+ 'operator_pass_sale_open',+ '[email protected]',+ {...SALE_PARAMS, checkoutUrl: 'javascript:alert(1)'},+ 'en-US',+ ),+ ).resolves.toBe(true);+ expect(sent[0].text).toContain('javascript:alert(1)');+ expect(sent[0]).not.toHaveProperty('html');+ });+});++describe('EmailService transactional and notice emails', () => {+ it('keeps the HTML part on a plain-http instance whose links stay on its own origins', async () => {+ const {service, sent} = createCapturingServiceFor({+ ...CONFIG,+ appBaseUrl: 'http://chat.example.org',+ apiBaseUrl: 'http://chat.example.org/api',+ marketingBaseUrl: 'http://chat.example.org',+ staticCdnBaseUrl: 'http://static.chat.example.org:8080',+ });+ await service.send('password_reset', '[email protected]', {resetToken: 'tok.abc'}, 'en-US');+ const html = sent[0].html ?? '';+ expect(html).toContain('href="http://chat.example.org/reset#token=tok.abc"');+ expect(html).toContain('src="http://static.chat.example.org:8080/email/brand/v1/logo-color.png"');+ expect(html).not.toContain('%%');+ });+});