From 0916d1bb49f5fcd96437cb20d309636faa0be2c3 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Sat, 15 Jun 2024 08:04:20 +0200 Subject: [PATCH] test: check reporting user sends an email (#55166) --- .github/workflows/e2e-playwright.yml | 3 ++- api/src/routes/user.test.ts | 2 +- api/src/routes/user.ts | 2 +- e2e/report-user.spec.ts | 18 +++++++++++++++++ e2e/utils/mailhog.ts | 30 ++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 e2e/utils/mailhog.ts diff --git a/.github/workflows/e2e-playwright.yml b/.github/workflows/e2e-playwright.yml index 39c2e3e6273..58424b0330b 100644 --- a/.github/workflows/e2e-playwright.yml +++ b/.github/workflows/e2e-playwright.yml @@ -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 diff --git a/api/src/routes/user.test.ts b/api/src/routes/user.test.ts index bcf9dfac8d8..0dc768d9cb2 100644 --- a/api/src/routes/user.test.ts +++ b/api/src/routes/user.test.ts @@ -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, diff --git a/api/src/routes/user.ts b/api/src/routes/user.ts index 9e9d4a9e992..0b1ef61146c 100644 --- a/api/src/routes/user.ts +++ b/api/src/routes/user.ts @@ -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) }); diff --git a/e2e/report-user.spec.ts b/e2e/report-user.spec.ts index 6569e2cd75b..12c47f4c5c2 100644 --- a/e2e/report-user.spec.ts +++ b/e2e/report-user.spec.ts @@ -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(); }); diff --git a/e2e/utils/mailhog.ts b/e2e/utils/mailhog.ts new file mode 100644 index 00000000000..e9365d04c0a --- /dev/null +++ b/e2e/utils/mailhog.ts @@ -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 => { + const res = await fetch(`http://${host}:8025/api/v2/messages`); + return res.json() as Promise; +}; + +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' + }); +};