A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
test(api/reports): exercise report SMTP failures
Hampus1f8c02199d10
diff --git a/fluxer_api/src/api/report/tests/DsaReportEmailSendFailure.test.ts b/fluxer_api/src/api/report/tests/DsaReportEmailSendFailure.test.tsindex ba61a03f2..cf49ce179 100644--- a/fluxer_api/src/api/report/tests/DsaReportEmailSendFailure.test.ts+++ b/fluxer_api/src/api/report/tests/DsaReportEmailSendFailure.test.ts@@ -1,47 +1,194 @@// SPDX-License-Identifier: AGPL-3.0-or-later-import {clearTestEmails, createUniqueEmail, findLastTestEmail, listTestEmails} from '@app/api/auth/tests/AuthTestUtils';+import {createReportID} from '@app/api/BrandedTypes';+import {getConfig} from '@app/api/Config';+import {sendChannelMessage, setupTestGuildWithMembers} from '@app/api/channel/tests/ChannelTestUtils';+import {getEmailService, getInstanceConfigRepository} from '@app/api/middleware/ServiceSingletons';+import {getReportFlowResponse} from '@app/api/report/flows/ReportFlowRegistry';import {ReportRepository} from '@app/api/report/ReportRepository';import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';+import {createRecordingSmtpServer} from '@app/api/test/RecordingSmtpServer';import {HTTP_STATUS} from '@app/api/test/TestConstants';import {createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';+import {EmailDeliveryModes} from '@fluxer/constants/src/AccountConfigConstants';import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';-import {TestEmailService} from '@pkgs/email/src/TestEmailService';-import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest';+import {SmtpTlsModes} from '@fluxer/constants/src/SmtpConstants';+import {afterEach, beforeEach, describe, expect, test} from 'vitest';-describe('DSA verification email delivery result on the route', () => {+const TEST_LOCALE = 'en-US';++interface ReportTarget {+ guildId: string;+ channelId: string;+ messageId: string;+ authorId: string;+}++async function configureSmtp(port: number) {+ await getInstanceConfigRepository().setInstanceIntegrationsConfig({+ email: {+ delivery: EmailDeliveryModes.SMTP,+ from_email: '[email protected]',+ from_name: 'Report Team',+ smtp: {host: '127.0.0.1', port, username: '', password: '', tls_mode: SmtpTlsModes.NONE},+ },+ });+}++function verificationCode(body: string): string {+ const match = /(?:^|\r\n)([A-Z0-9]{4}-[A-Z0-9]{4})(?:\r\n|$)/.exec(body);+ if (!match) throw new Error('The delivered DSA email has no verification code');+ return match[1];+}++describe('DSA email delivery through SMTP', () => {let harness: ApiTestHarness;+ const servers = new Set<Awaited<ReturnType<typeof createRecordingSmtpServer>>>();beforeEach(async () => {- harness = await createApiTestHarness();+ harness = await createApiTestHarness({search: 'enabled'});+ const config = getConfig();+ const previousTestMode = config.dev.testModeEnabled;+ try {+ config.dev.testModeEnabled = false;+ getEmailService();+ } finally {+ config.dev.testModeEnabled = previousTestMode;+ }});afterEach(async () => {- vi.restoreAllMocks();- await harness?.shutdown();+ try {+ await Promise.all([...servers].map((smtp) => smtp.close()));+ } finally {+ servers.clear();+ await harness?.shutdown();+ }});test('the route answers 503 and the next send goes through right away', async () => {- await clearTestEmails(harness);- const email = createUniqueEmail('dsa-send-failure');- const send = vi.spyOn(TestEmailService.prototype, 'sendDsaReportVerificationCode').mockResolvedValueOnce(false);+ const smtp = await createRecordingSmtpServer({firstDelivery: 'reject'});+ servers.add(smtp);+ await configureSmtp(smtp.port);+ const email = '[email protected]';await createBuilderWithoutAuth(harness).post('/reports/dsa/email/send')- .body({email})+ .body({email, locale: TEST_LOCALE}).expect(HTTP_STATUS.SERVICE_UNAVAILABLE, APIErrorCodes.SERVICE_UNAVAILABLE).execute();- expect(send).toHaveBeenCalledTimes(1);- expect(await new ReportRepository().getDsaEmailVerification(email.toLowerCase())).toBeNull();+ expect(smtp.attempts).toHaveLength(1);+ expect(smtp.attempts[0]).toMatchObject({from: '<[email protected]>', to: '<[email protected]>'});+ expect(smtp.attempts[0].body).toContain('Use this code to send your Digital Services Act report to Fluxer.');+ expect(smtp.delivered).toEqual([]);+ expect(await new ReportRepository().getDsaEmailVerification(email)).toBeNull();await createBuilderWithoutAuth(harness).post('/reports/dsa/email/send')- .body({email})+ .body({email, locale: TEST_LOCALE})+ .expect(HTTP_STATUS.OK)+ .execute();+ expect(smtp.attempts).toHaveLength(2);+ expect(smtp.delivered).toHaveLength(1);+ expect(smtp.delivered[0]).toMatchObject({from: '<[email protected]>', to: '<[email protected]>'});+ expect(smtp.delivered[0].body).toContain('This code expires in 10 minutes.');+ await createBuilderWithoutAuth(harness)+ .post('/reports/dsa/email/verify')+ .body({email, code: verificationCode(smtp.delivered[0].body)}).expect(HTTP_STATUS.OK).execute();- const code = findLastTestEmail(await listTestEmails(harness), 'dsa_report_verification')?.metadata.code;+ expect(await new ReportRepository().getDsaEmailVerification(email)).toBeNull();+ });++ test.each<{+ kind: 'message' | 'guild';+ receipt: string;+ request: (target: ReportTarget) => Record<string, unknown>;+ reportedUserId: (target: ReportTarget) => bigint | null;+ }>([+ {+ kind: 'message',+ receipt: 'We received your Digital Services Act report about a message on Fluxer.',+ request: (target) => ({+ message_link: `https://web.fluxer.app/channels/${target.guildId}/${target.channelId}/${target.messageId}`,+ steps: [+ {screen_id: 'root_message', option_id: 'abuse'},+ {screen_id: 'abuse', option_id: 'hate'},+ {screen_id: 'hate', option_id: 'hate_incitement'},+ ],+ additional_info: 'This message calls for violence against a group of people.',+ }),+ reportedUserId: (target) => BigInt(target.authorId),+ },+ {+ kind: 'guild',+ receipt: 'We received your Digital Services Act report about a community on Fluxer.',+ request: (target) => ({+ guild_id: target.guildId,+ steps: [+ {screen_id: 'community_parts', item_ids: ['activity']},+ {screen_id: 'root_guild', option_id: 'abuse'},+ {screen_id: 'abuse_guild', option_id: 'raid'},+ ],+ additional_info: 'This community organizes raids on other communities.',+ }),+ reportedUserId: () => null,+ },+ ])('a $kind notice is filed when SMTP refuses its receipt', async ({kind, receipt, request, reportedUserId}) => {+ const verificationSmtp = await createRecordingSmtpServer({firstDelivery: 'hold'});+ servers.add(verificationSmtp);+ verificationSmtp.release();+ await configureSmtp(verificationSmtp.port);+ const {members, guild, systemChannel} = await setupTestGuildWithMembers(harness, 1);+ const message = await sendChannelMessage(harness, members[0].token, systemChannel.id, 'Reported on the DSA form');+ const email = '[email protected]';await createBuilderWithoutAuth(harness)+ .post('/reports/dsa/email/send')+ .body({email, locale: TEST_LOCALE})+ .expect(HTTP_STATUS.OK)+ .execute();+ const verificationEmails = verificationSmtp.delivered.filter(+ (mail) => mail.to === '<[email protected]>',+ );+ expect(verificationEmails).toHaveLength(1);+ const {ticket} = await createBuilderWithoutAuth<{ticket: string}>(harness).post('/reports/dsa/email/verify')- .body({email, code})+ .body({email, code: verificationCode(verificationEmails[0].body)})+ .expect(HTTP_STATUS.OK)+ .execute();+ const receiptSmtp = await createRecordingSmtpServer({firstDelivery: 'reject'});+ servers.add(receiptSmtp);+ await configureSmtp(receiptSmtp.port);+ const target = {+ guildId: guild.id,+ channelId: systemChannel.id,+ messageId: message.id,+ authorId: members[0].userId,+ };+ const body = {+ ticket,+ report_type: kind,+ revision_hash: getReportFlowResponse(kind, 'dsa', TEST_LOCALE).revision_hash,+ good_faith_confirmed: true,+ reporter_full_legal_name: 'Jane Doe',+ reporter_country_of_residence: 'DE',+ ...request(target),+ };+ const result = await createBuilderWithoutAuth<{report_id: string; status: string}>(harness)+ .post('/reports/dsa')+ .body(body).expect(HTTP_STATUS.OK).execute();+ expect(result.status).toBe('pending');+ const stored = await new ReportRepository().getReport(createReportID(BigInt(result.report_id)));+ expect(stored?.reporterEmail).toBe('[email protected]');+ expect(stored?.reportedGuildId?.toString()).toBe(guild.id);+ expect(stored?.reportedUserId).toBe(reportedUserId(target));+ expect(receiptSmtp.attempts).toHaveLength(1);+ expect(receiptSmtp.attempts[0]).toMatchObject({+ from: '<[email protected]>',+ to: '<[email protected]>',+ });+ expect(receiptSmtp.attempts[0].body).toContain(receipt);+ expect(receiptSmtp.attempts[0].body).toContain(result.report_id);+ expect(receiptSmtp.delivered).toEqual([]);});});diff --git a/fluxer_api/src/api/report/tests/ReportFlowDsa.test.ts b/fluxer_api/src/api/report/tests/ReportFlowDsa.test.tsindex c28f1d9b0..3ecc342d0 100644--- a/fluxer_api/src/api/report/tests/ReportFlowDsa.test.ts+++ b/fluxer_api/src/api/report/tests/ReportFlowDsa.test.ts@@ -7,7 +7,7 @@ import {findLastTestEmail,listTestEmails,} from '@app/api/auth/tests/AuthTestUtils';-import {createReportID} from '@app/api/BrandedTypes';+import {createReportID, createUserID} from '@app/api/BrandedTypes';import {Config} from '@app/api/Config';import {loadFixture, sendMessageWithAttachments} from '@app/api/channel/tests/AttachmentTestUtils';import {@@ -20,24 +20,25 @@ import {sendChannelMessage,setupTestGuildWithMembers,} from '@app/api/channel/tests/ChannelTestUtils';+import {deleteOneOrMany} from '@app/api/database/CassandraQueryExecution';import {resetActivityEventsForTests, startActivityEvents} from '@app/api/infrastructure/activity/ActivityEvents';import type {ActivityPublisher} from '@app/api/infrastructure/activity/ActivitySpool';import {phraseBlocklistCache} from '@app/api/middleware/PhraseBlocklistCache';import {urlBlocklistCache} from '@app/api/middleware/UrlBlocklistCache';-import {- getReportFlowVariant,- type ReportFlowStepInput,- resolveReportFlowAnswers,-} from '@app/api/report/flows/ReportFlowRegistry';+import {getReportFlowResponse, resolveReportFlowAnswers} from '@app/api/report/flows/ReportFlowRegistry';import {ReportRepository} from '@app/api/report/ReportRepository';+import {Users} from '@app/api/Tables';import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';import {HTTP_STATUS, TEST_IDS} from '@app/api/test/TestConstants';import {createBuilder, createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';import {UserRepository} from '@app/api/user/repositories/UserRepository';import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';import {Permissions} from '@fluxer/constants/src/ChannelConstants';-import type {ReportFlowResponse, ReportFlowTargetType} from '@fluxer/schema/src/domains/report/ReportFlowSchemas';-import {TestEmailService} from '@pkgs/email/src/TestEmailService';+import type {+ ReportFlowResponse,+ ReportFlowStep,+ ReportFlowTargetType,+} from '@fluxer/schema/src/domains/report/ReportFlowSchemas';import {MockKVProvider} from '@pkgs/kv_client/src/testing/MockKVProvider';import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest';@@ -58,26 +59,26 @@ interface Ticket {ticket: string;}-const HATE_WALK: ReadonlyArray<ReportFlowStepInput> = [+const HATE_WALK: ReadonlyArray<ReportFlowStep> = [{screen_id: 'root_message', option_id: 'abuse'},{screen_id: 'abuse', option_id: 'hate'},{screen_id: 'hate', option_id: 'hate_incitement'},];-const HARASSMENT_WALK: ReadonlyArray<ReportFlowStepInput> = [+const HARASSMENT_WALK: ReadonlyArray<ReportFlowStep> = [{screen_id: 'root_message', option_id: 'abuse'},{screen_id: 'abuse', option_id: 'harassment'},{screen_id: 'harassment', option_id: 'harassment_direct'},];-const CSAM_WALK: ReadonlyArray<ReportFlowStepInput> = [+const CSAM_WALK: ReadonlyArray<ReportFlowStep> = [{screen_id: 'root_message', option_id: 'abuse'},{screen_id: 'abuse', option_id: 'sexual'},{screen_id: 'sexual', option_id: 'minor_sexual'},{screen_id: 'minor_sexual', option_id: 'csam'},];-const RAID_WALK: ReadonlyArray<ReportFlowStepInput> = [+const RAID_WALK: ReadonlyArray<ReportFlowStep> = [{screen_id: 'community_parts', item_ids: ['activity']},{screen_id: 'root_guild', option_id: 'abuse'},{screen_id: 'abuse_guild', option_id: 'raid'},@@ -92,10 +93,10 @@ class CapturingPublisher implements ActivityPublisher {}function dsaHash(target: ReportFlowTargetType): string {- return getReportFlowVariant(target, 'dsa').revisionHash;+ return getReportFlowResponse(target, 'dsa', 'en-US').revision_hash;}-function dsaSteps(target: ReportFlowTargetType, steps: ReadonlyArray<ReportFlowStepInput>) {+function dsaSteps(target: ReportFlowTargetType, steps: ReadonlyArray<ReportFlowStep>) {return resolveReportFlowAnswers({target, surface: 'dsa', revisionHash: dsaHash(target), steps}).steps;}@@ -139,7 +140,7 @@ async function setupMessageLink(harness: ApiTestHarness): Promise<{link: string;};}-function messageFlowBody(ticket: string, link: string, steps: ReadonlyArray<ReportFlowStepInput>, extra = {}) {+function messageFlowBody(ticket: string, link: string, steps: ReadonlyArray<ReportFlowStep>, extra = {}) {return {ticket,report_type: 'message',@@ -205,7 +206,6 @@ describe('DSA report flow', () => {});afterEach(async () => {- vi.restoreAllMocks();resetActivityEventsForTests();await harness?.shutdown();});@@ -371,7 +371,7 @@ describe('DSA report flow', () => {.expect(HTTP_STATUS.BAD_REQUEST, APIErrorCodes.INVALID_REPORT_FLOW_ANSWERS).execute();expect(welfare.step_index).toBe(2);- const inAppHash = getReportFlowVariant('message', 'in_app').revisionHash;+ const inAppHash = getReportFlowResponse('message', 'in_app', 'en-US').revision_hash;await submitDsa(harness,messageFlowBody(ticket, target.link, [{screen_id: 'root_message', option_id: 'dislike'}], {@@ -413,6 +413,7 @@ describe('DSA report flow', () => {expect(event.data).toEqual({report_id: result.report_id,reporter_id: '0',+ reporter_tier: 'verified',category: 'raid_coordination',target_type: 'dsa',reported_user_id: null,@@ -475,25 +476,6 @@ describe('DSA report flow', () => {expect(await listReceipts(harness)).toEqual([]);});- test('a notice is filed even when its receipt cannot be sent', async () => {- const target = await setupMessageLink(harness);- const send = vi.spyOn(TestEmailService.prototype, 'sendReportReceivedEmail');- send.mockRejectedValueOnce(new Error('mail transport is down'));- const first = await issueTicket(harness);- const thrown = await submitDsa(harness, messageFlowBody(first.ticket, target.link, HATE_WALK))- .expect(HTTP_STATUS.OK)- .execute();- send.mockResolvedValueOnce(false);- const second = await issueTicket(harness);- const refused = await submitDsa(harness, guildFlowBody(second.ticket, target.guildId))- .expect(HTTP_STATUS.OK)- .execute();- expect(send).toHaveBeenCalledTimes(2);- expect((await readReport(thrown.report_id)).reporterEmail).toBe(first.email);- expect((await readReport(refused.report_id)).reporterEmail).toBe(second.email);- expect(await listReceipts(harness)).toEqual([]);- });-test('every message link that does not resolve gets the same answer and keeps the ticket', async () => {const {ticket} = await issueTicket(harness);const {owner, members, guild, systemChannel} = await setupTestGuildWithMembers(harness, 1);@@ -545,13 +527,20 @@ describe('DSA report flow', () => {harness,messageFlowBody(ticket, target.link.replace(/\d+$/, TEST_IDS.NONEXISTENT_MESSAGE), HATE_WALK),).executeRaw();- const findUnique = vi.spyOn(UserRepository.prototype, 'findUnique').mockResolvedValueOnce(null);- const {response, text} = await submitDsa(harness, messageFlowBody(ticket, target.link, HATE_WALK)).executeRaw();- expect(findUnique).toHaveBeenCalledTimes(1);+ const repository = new UserRepository();+ const authorId = createUserID(BigInt(target.authorId));+ const author = await repository.findUniqueAssert(authorId);+ await deleteOneOrMany(Users.deleteByPk({user_id: authorId}));+ expect(await repository.findUnique(authorId)).toBeNull();+ const {response, text, json} = await submitDsa<ErrorResponse>(+ harness,+ messageFlowBody(ticket, target.link, HATE_WALK),+ ).executeRaw();expect(response.status).toBe(HTTP_STATUS.NOT_FOUND);- expect((JSON.parse(text) as ErrorResponse).code).toBe(APIErrorCodes.UNKNOWN_MESSAGE);+ expect(json.code).toBe(APIErrorCodes.UNKNOWN_MESSAGE);expect(text).toBe(missing.text);expect(await countReports()).toBe(0);+ await repository.create(author.toRow());await submitDsa(harness, messageFlowBody(ticket, target.link, HATE_WALK)).expect(HTTP_STATUS.OK).execute();