test: check reporting user sends an email (#55166)

This commit is contained in:
Oliver Eyton-Williams
2024-06-15 08:04:20 +02:00
committed by GitHub
parent eb84dce6ca
commit 0916d1bb49
5 changed files with 52 additions and 3 deletions

View File

@@ -89,7 +89,8 @@ jobs:
mailhog:
image: mailhog/mailhog
ports:
- 1025:1025
- 1025:1025 # SMTP server (listens for emails)
- 8025:8025 # HTTP server (so we can make requests to the api)
steps:
- name: Set Action Environment Variables

View File

@@ -769,7 +769,7 @@ describe('userRoutes', () => {
from: 'team@freecodecamp.org',
to: 'support@freecodecamp.org',
cc: 'foo@bar.com',
subject: "Abuse Report: Reporting darth-vader's profile",
subject: "Abuse Report : Reporting darth-vader's profile.",
text: `
Hello Team,

View File

@@ -250,7 +250,7 @@ export const userRoutes: FastifyPluginCallbackTypebox = (
from: 'team@freecodecamp.org',
to: 'support@freecodecamp.org',
cc: user.email,
subject: `Abuse Report: Reporting ${username}'s profile`,
subject: `Abuse Report : Reporting ${username}'s profile.`,
text: generateReportEmail(user, username, report)
});

View File

@@ -1,9 +1,19 @@
import { test, expect } from '@playwright/test';
import {
deleteAllEmails,
getAllEmails,
getFirstEmail,
getSubject
} from './utils/mailhog';
test.use({ storageState: 'playwright/.auth/certified-user.json' });
// To run this test you will need to run the email server.
// To do this: https://contribute.freecodecamp.org/#/how-to-catch-outgoing-emails-locally?id=using-mailhog
test.beforeEach(async () => {
await deleteAllEmails();
});
test('should be possible to report a user from their profile page', async ({
page
}) => {
@@ -28,4 +38,12 @@ test('should be possible to report a user from their profile page', async ({
await expect(page.getByTestId('flash-message')).toContainText(
'A report was sent to the team with foo@bar.com in copy'
);
await expect(async () => {
const emails = await getAllEmails();
expect(emails.items).toHaveLength(1);
expect(getSubject(getFirstEmail(emails))).toBe(
"Abuse Report : Reporting twaha's profile."
);
}).toPass();
});

30
e2e/utils/mailhog.ts Normal file
View File

@@ -0,0 +1,30 @@
type Email = {
Content: { Headers: { Subject: string[] } };
};
type AllEmails = {
items: Email[];
};
const host = process.env.MAILHOG_HOST || 'localhost';
export const getAllEmails = async (): Promise<AllEmails> => {
const res = await fetch(`http://${host}:8025/api/v2/messages`);
return res.json() as Promise<AllEmails>;
};
export const getFirstEmail = (allEmails: { items: Email[] }) => {
return allEmails.items[0];
};
export const getSubject = (email: {
Content: { Headers: { Subject: string[] } };
}) => {
return email.Content.Headers.Subject[0];
};
export const deleteAllEmails = async () => {
await fetch(`http://${host}:8025/api/v1/messages`, {
method: 'DELETE'
});
};