Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
2.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {clearTestEmails, createUniqueEmail, listTestEmails} from '@app/api/auth/tests/AuthTestUtils';
import {ReportRepository} from '@app/api/report/ReportRepository';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
import {HTTP_STATUS} from '@app/api/test/TestConstants';
import {createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
import {afterEach, beforeEach, describe, expect, test} from 'vitest';

async function countVerificationEmailsTo(harness: ApiTestHarness, email: string): Promise<number> {
	const emails = await listTestEmails(harness);
	return emails.filter((sent) => sent.type === 'dsa_report_verification' && sent.to === email.toLowerCase()).length;
}

async function waitOutResendCooldown(email: string): Promise<void> {
	const repository = new ReportRepository();
	const row = await repository.getDsaEmailVerification(email.toLowerCase());
	if (row) {
		await repository.upsertDsaEmailVerification({...row, last_sent_at: new Date(Date.now() - 61_000)});
	}
}

describe('DSA report verification email recipient limit', () => {
	let harness: ApiTestHarness;
	beforeEach(async () => {
		harness = await createApiTestHarness();
	});
	afterEach(async () => {
		await harness?.shutdown();
	});

	test('limits verification emails per address regardless of letter case', async () => {
		await clearTestEmails(harness);
		const email = createUniqueEmail('dsa-recipient');
		for (let attempt = 0; attempt < 3; attempt++) {
			await createBuilderWithoutAuth(harness)
				.post('/reports/dsa/email/send')
				.body({email})
				.expect(HTTP_STATUS.OK)
				.execute();
			await waitOutResendCooldown(email);
		}
		await createBuilderWithoutAuth(harness)
			.post('/reports/dsa/email/send')
			.body({email: email.toUpperCase()})
			.expect(429)
			.execute();
		expect(await countVerificationEmailsTo(harness, email)).toBe(3);
	});

	test('keeps sending to other addresses after one address reaches its limit', async () => {
		await clearTestEmails(harness);
		const limited = createUniqueEmail('dsa-recipient');
		for (let attempt = 0; attempt < 3; attempt++) {
			await createBuilderWithoutAuth(harness)
				.post('/reports/dsa/email/send')
				.body({email: limited})
				.expect(HTTP_STATUS.OK)
				.execute();
			await waitOutResendCooldown(limited);
		}
		await createBuilderWithoutAuth(harness)
			.post('/reports/dsa/email/send')
			.body({email: limited})
			.expect(429)
			.execute();
		const other = createUniqueEmail('dsa-recipient');
		await createBuilderWithoutAuth(harness)
			.post('/reports/dsa/email/send')
			.body({email: other})
			.expect(HTTP_STATUS.OK)
			.execute();
		expect(await countVerificationEmailsTo(harness, other)).toBe(1);
	});
});