From 2d634b4111cc789e80cfd0bf1546887c03b1b9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Thu, 24 Nov 2022 22:10:07 +0100 Subject: [PATCH] Test 'sendResetPasswordEmail' --- backend/src/emails/sendEmailVariants.test.ts | 77 ++++++++++++++++++- .../src/graphql/resolver/UserResolver.test.ts | 18 ++--- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/backend/src/emails/sendEmailVariants.test.ts b/backend/src/emails/sendEmailVariants.test.ts index 666fea994..dd5bb68e9 100644 --- a/backend/src/emails/sendEmailVariants.test.ts +++ b/backend/src/emails/sendEmailVariants.test.ts @@ -10,6 +10,7 @@ import { sendAccountMultiRegistrationEmail, sendContributionConfirmedEmail, sendContributionRejectedEmail, + sendResetPasswordEmail, } from './sendEmailVariants' import { sendEmailTranslated } from './sendEmailTranslated' @@ -319,7 +320,7 @@ describe('sendEmailVariants', () => { ) expect(result.originalMessage.html).toContain('Hello Peter Lustig') expect(result.originalMessage.html).toContain( - 'your public good contribution “My contribution.” has just been confirmed by Bibi Bloxberg and credited to your Gradido account.', + 'Your public good contribution “My contribution.” has just been confirmed by Bibi Bloxberg and credited to your Gradido account.', ) expect(result.originalMessage.html).toContain('Amount: 23.54 GDD') expect(result.originalMessage.html).toContain( @@ -389,7 +390,7 @@ describe('sendEmailVariants', () => { ) expect(result.originalMessage.html).toContain('Hello Peter Lustig') expect(result.originalMessage.html).toContain( - 'your public good contribution “My contribution.” was rejected by Bibi Bloxberg.', + 'Your public good contribution “My contribution.” was rejected by Bibi Bloxberg.', ) expect(result.originalMessage.html).toContain( 'To see your common good contributions and related messages, go to the “Community” menu in your Gradido account and click on the “My contributions to the common good” tab!', @@ -402,4 +403,76 @@ describe('sendEmailVariants', () => { }) }) }) + + describe('sendResetPasswordEmail', () => { + beforeAll(async () => { + result = await sendResetPasswordEmail({ + firstName: 'Peter', + lastName: 'Lustig', + email: 'peter@lustig.de', + language: 'en', + resetLink: 'http://localhost/reset-password/3762660021544901417', + timeDurationObject: { hours: 23, minutes: 30 }, + }) + }) + + describe('calls "sendEmailTranslated"', () => { + it('with expected parameters', () => { + expect(sendEmailTranslated).toBeCalledWith({ + receiver: { + to: 'Peter Lustig ', + }, + template: 'resetPassword', + locals: { + firstName: 'Peter', + lastName: 'Lustig', + locale: 'en', + resetLink: 'http://localhost/reset-password/3762660021544901417', + timeDurationObject: { hours: 23, minutes: 30 }, + resendLink: CONFIG.EMAIL_LINK_FORGOTPASSWORD, + }, + }) + }) + + it('has expected result', () => { + expect(result).toMatchObject({ + envelope: { + from: 'info@gradido.net', + to: ['peter@lustig.de'], + }, + message: expect.any(String), + originalMessage: expect.objectContaining({ + to: 'Peter Lustig ', + from: 'Gradido (nicht antworten) ', + attachments: [], + subject: 'Gradido: Reset password', + html: expect.any(String), + text: expect.stringContaining('GRADIDO: RESET PASSWORD'), + }), + }) + expect(result.originalMessage.html).toContain('') + expect(result.originalMessage.html).toContain('') + expect(result.originalMessage.html).toContain('Gradido: Reset password') + expect(result.originalMessage.html).toContain('>Gradido: Reset password') + expect(result.originalMessage.html).toContain('Hello Peter Lustig') + expect(result.originalMessage.html).toContain( + 'You, or someone else, requested a password reset for this account.', + ) + expect(result.originalMessage.html).toContain('If it was you, please click on the link:') + expect(result.originalMessage.html).toContain( + 'http://localhost/reset-password/3762660021544901417', + ) + expect(result.originalMessage.html).toContain( + 'or copy the link above into your browser window.', + ) + expect(result.originalMessage.html).toContain( + 'The link has a validity of 23 hours and 30 minutes. If the validity of the link has already expired, you can have a new link sent to you here by entering your email address:', + ) + expect(result.originalMessage.html).toContain( + `${CONFIG.EMAIL_LINK_FORGOTPASSWORD}`, + ) + expect(result.originalMessage.html).toContain('Kind regards,
your Gradido team') + }) + }) + }) }) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 2be2361e9..ee88e58cc 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -23,8 +23,8 @@ import CONFIG from '@/config' import { sendAccountActivationEmail, sendAccountMultiRegistrationEmail, + sendResetPasswordEmail, } from '@/emails/sendEmailVariants' -import { sendResetPasswordEmail } from '@/mailer/sendResetPasswordEmail' import { activationLink } from './UserResolver' import { contributionLinkFactory } from '@/seeds/factory/contributionLink' import { transactionLinkFactory } from '@/seeds/factory/transactionLink' @@ -53,13 +53,7 @@ jest.mock('@/emails/sendEmailVariants', () => { sendAccountMultiRegistrationEmail: jest.fn((a) => originalModule.sendAccountMultiRegistrationEmail(a), ), - } -}) - -jest.mock('@/mailer/sendResetPasswordEmail', () => { - return { - __esModule: true, - sendResetPasswordEmail: jest.fn(), + sendResetPasswordEmail: jest.fn((a) => originalModule.sendResetPasswordEmail(a)), } }) @@ -857,11 +851,15 @@ describe('UserResolver', () => { it('sends reset password email', () => { expect(sendResetPasswordEmail).toBeCalledWith({ - link: activationLink(emailContact.emailVerificationCode), firstName: 'Bibi', lastName: 'Bloxberg', email: 'bibi@bloxberg.de', - duration: expect.any(String), + language: 'de', + resetLink: activationLink(emailContact.emailVerificationCode), + timeDurationObject: expect.objectContaining({ + hours: expect.any(Number), + minutes: expect.any(Number), + }), }) })