Refactor and write the tests of the 'createUser' resolver and the multi registration email

This commit is contained in:
Wolfgang Huß 2022-06-02 15:45:49 +02:00
parent 2785379c12
commit c7923e3122
11 changed files with 87 additions and 9 deletions

View File

@ -45,6 +45,8 @@ EMAIL_LINK_VERIFICATION=http://localhost/checkEmail/{optin}{code}
EMAIL_LINK_SETPASSWORD=http://localhost/reset-password/{optin} EMAIL_LINK_SETPASSWORD=http://localhost/reset-password/{optin}
EMAIL_LINK_FORGOTPASSWORD=http://localhost/forgot-password EMAIL_LINK_FORGOTPASSWORD=http://localhost/forgot-password
EMAIL_LINK_OVERVIEW=http://localhost/overview EMAIL_LINK_OVERVIEW=http://localhost/overview
EMAIL_LINK_SUPPORT=https://gradido.net/de/contact/
EMAIL_EMAIL_SUPPORT=support@gradido.net
EMAIL_CODE_VALID_TIME=1440 EMAIL_CODE_VALID_TIME=1440
EMAIL_CODE_REQUEST_TIME=10 EMAIL_CODE_REQUEST_TIME=10

View File

@ -42,7 +42,10 @@ EMAIL_SMTP_URL=$EMAIL_SMTP_URL
EMAIL_SMTP_PORT=587 EMAIL_SMTP_PORT=587
EMAIL_LINK_VERIFICATION=$EMAIL_LINK_VERIFICATION EMAIL_LINK_VERIFICATION=$EMAIL_LINK_VERIFICATION
EMAIL_LINK_SETPASSWORD=$EMAIL_LINK_SETPASSWORD EMAIL_LINK_SETPASSWORD=$EMAIL_LINK_SETPASSWORD
EMAIL_LINK_FORGOTPASSWORD=$EMAIL_LINK_FORGOTPASSWORD
EMAIL_LINK_OVERVIEW=$EMAIL_LINK_OVERVIEW EMAIL_LINK_OVERVIEW=$EMAIL_LINK_OVERVIEW
EMAIL_LINK_SUPPORT=$EMAIL_LINK_SUPPORT
EMAIL_EMAIL_SUPPORT=$EMAIL_EMAIL_SUPPORT
EMAIL_CODE_VALID_TIME=$EMAIL_CODE_VALID_TIME EMAIL_CODE_VALID_TIME=$EMAIL_CODE_VALID_TIME
EMAIL_CODE_REQUEST_TIME=$EMAIL_CODE_REQUEST_TIME EMAIL_CODE_REQUEST_TIME=$EMAIL_CODE_REQUEST_TIME

View File

@ -77,6 +77,8 @@ const email = {
EMAIL_LINK_FORGOTPASSWORD: EMAIL_LINK_FORGOTPASSWORD:
process.env.EMAIL_LINK_FORGOTPASSWORD || 'http://localhost/forgot-password', process.env.EMAIL_LINK_FORGOTPASSWORD || 'http://localhost/forgot-password',
EMAIL_LINK_OVERVIEW: process.env.EMAIL_LINK_OVERVIEW || 'http://localhost/overview', EMAIL_LINK_OVERVIEW: process.env.EMAIL_LINK_OVERVIEW || 'http://localhost/overview',
EMAIL_LINK_SUPPORT: process.env.EMAIL_LINK_SUPPORT || 'https://gradido.net/de/contact/',
EMAIL_EMAIL_SUPPORT: process.env.EMAIL_EMAIL_SUPPORT || 'support@gradido.net',
// time in minutes a optin code is valid // time in minutes a optin code is valid
EMAIL_CODE_VALID_TIME: process.env.EMAIL_CODE_VALID_TIME EMAIL_CODE_VALID_TIME: process.env.EMAIL_CODE_VALID_TIME
? parseInt(process.env.EMAIL_CODE_VALID_TIME) || 1440 ? parseInt(process.env.EMAIL_CODE_VALID_TIME) || 1440

View File

@ -11,6 +11,7 @@ import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
import { User } from '@entity/User' import { User } from '@entity/User'
import CONFIG from '@/config' import CONFIG from '@/config'
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail' import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
import { sendAccountMultiRegistrationEmail } from '@/mailer/sendAccountMultiRegistrationEmail'
import { sendResetPasswordEmail } from '@/mailer/sendResetPasswordEmail' import { sendResetPasswordEmail } from '@/mailer/sendResetPasswordEmail'
import { printTimeDuration, activationLink } from './UserResolver' import { printTimeDuration, activationLink } from './UserResolver'
@ -25,6 +26,13 @@ jest.mock('@/mailer/sendAccountActivationEmail', () => {
} }
}) })
jest.mock('@/mailer/sendAccountMultiRegistrationEmail', () => {
return {
__esModule: true,
sendAccountMultiRegistrationEmail: jest.fn(),
}
})
jest.mock('@/mailer/sendResetPasswordEmail', () => { jest.mock('@/mailer/sendResetPasswordEmail', () => {
return { return {
__esModule: true, __esModule: true,
@ -151,14 +159,33 @@ describe('UserResolver', () => {
}) })
describe('email already exists', () => { describe('email already exists', () => {
it('throws and logs an error', async () => { let mutation: any
const mutation = await mutate({ mutation: createUser, variables }) beforeAll(async () => {
mutation = await mutate({ mutation: createUser, variables })
})
it('logs an info', async () => {
expect(logger.info).toBeCalledWith('User already exists with this email=peter@lustig.de')
})
it('sends an account multi registration email', () => {
expect(sendAccountMultiRegistrationEmail).toBeCalledWith({
firstName: 'Peter',
lastName: 'Lustig',
email: 'peter@lustig.de',
})
})
it('results with partly faked user with random "id"', async () => {
expect(mutation).toEqual( expect(mutation).toEqual(
expect.objectContaining({ expect.objectContaining({
errors: [new GraphQLError('User already exists.')], data: {
createUser: {
id: expect.any(Number),
},
},
}), }),
) )
expect(logger.error).toBeCalledWith('User already exists with this email=peter@lustig.de')
}) })
}) })

View File

@ -338,6 +338,7 @@ export class UserResolver {
// Wolle: throw new Error(`User already exists.`) // Wolle: throw new Error(`User already exists.`)
// send mail even CC to support // send mail even CC to support
// respond with fake user_id? // respond with fake user_id?
dbUser.id = sodium.randombytes_random() % (2048 * 16)
dbUser.email = email dbUser.email = email
dbUser.firstName = firstName dbUser.firstName = firstName
dbUser.lastName = lastName dbUser.lastName = lastName
@ -349,7 +350,6 @@ export class UserResolver {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const emailSent = await sendAccountMultiRegistrationEmail({ const emailSent = await sendAccountMultiRegistrationEmail({
link: CONFIG.EMAIL_LINK_FORGOTPASSWORD,
firstName, firstName,
lastName, lastName,
email, email,
@ -361,6 +361,8 @@ export class UserResolver {
logger.debug(`Email not send!`) logger.debug(`Email not send!`)
} }
logger.info('createUser() faked and send multi registration mail...') logger.info('createUser() faked and send multi registration mail...')
// Wolle:
// console.log('dbUser: ', dbUser)
} else { } else {
// Wolle: const passphrase = PassphraseGenerate() // Wolle: const passphrase = PassphraseGenerate()
// const keyPair = KeyPairEd25519Create(passphrase) // return pub, priv Key // const keyPair = KeyPairEd25519Create(passphrase) // return pub, priv Key

View File

@ -0,0 +1,31 @@
import { sendAccountMultiRegistrationEmail } from './sendAccountMultiRegistrationEmail'
import { sendEMail } from './sendEMail'
jest.mock('./sendEMail', () => {
return {
__esModule: true,
sendEMail: jest.fn(),
}
})
describe('sendAccountMultiRegistrationEmail', () => {
beforeEach(async () => {
await sendAccountMultiRegistrationEmail({
firstName: 'Peter',
lastName: 'Lustig',
email: 'peter@lustig.de',
})
})
it('calls sendEMail', () => {
expect(sendEMail).toBeCalledWith({
to: `Peter Lustig <peter@lustig.de>`,
cc: 'support@gradido.net',
subject: 'Gradido: Erneuter Registrierungsversuch mit deiner E-Mail',
text:
expect.stringContaining('Hallo Peter Lustig') &&
expect.stringContaining('http://localhost/forgot-password') &&
expect.stringContaining('https://gradido.net/de/contact/'),
})
})
})

View File

@ -3,17 +3,18 @@ import { accountMultiRegistration } from './text/accountMultiRegistration'
import CONFIG from '@/config' import CONFIG from '@/config'
export const sendAccountMultiRegistrationEmail = (data: { export const sendAccountMultiRegistrationEmail = (data: {
link: string
firstName: string firstName: string
lastName: string lastName: string
email: string email: string
}): Promise<boolean> => { }): Promise<boolean> => {
return sendEMail({ return sendEMail({
to: `${data.firstName} ${data.lastName} <${data.email}>`, to: `${data.firstName} ${data.lastName} <${data.email}>`,
cc: CONFIG.EMAIL_EMAIL_SUPPORT,
subject: accountMultiRegistration.de.subject, subject: accountMultiRegistration.de.subject,
text: accountMultiRegistration.de.text({ text: accountMultiRegistration.de.text({
...data, ...data,
resendLink: CONFIG.EMAIL_LINK_FORGOTPASSWORD, resendLink: CONFIG.EMAIL_LINK_FORGOTPASSWORD,
supportLink: CONFIG.EMAIL_LINK_SUPPORT,
}), }),
}) })
} }

View File

@ -31,6 +31,7 @@ describe('sendEMail', () => {
beforeEach(async () => { beforeEach(async () => {
result = await sendEMail({ result = await sendEMail({
to: 'receiver@mail.org', to: 'receiver@mail.org',
cc: 'support@gradido.net',
subject: 'Subject', subject: 'Subject',
text: 'Text text text', text: 'Text text text',
}) })
@ -50,6 +51,7 @@ describe('sendEMail', () => {
CONFIG.EMAIL = true CONFIG.EMAIL = true
result = await sendEMail({ result = await sendEMail({
to: 'receiver@mail.org', to: 'receiver@mail.org',
cc: 'support@gradido.net',
subject: 'Subject', subject: 'Subject',
text: 'Text text text', text: 'Text text text',
}) })
@ -72,6 +74,7 @@ describe('sendEMail', () => {
expect((createTransport as jest.Mock).mock.results[0].value.sendMail).toBeCalledWith({ expect((createTransport as jest.Mock).mock.results[0].value.sendMail).toBeCalledWith({
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`, from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
to: 'receiver@mail.org', to: 'receiver@mail.org',
cc: 'support@gradido.net',
subject: 'Subject', subject: 'Subject',
text: 'Text text text', text: 'Text text text',
}) })

View File

@ -5,10 +5,15 @@ import CONFIG from '@/config'
export const sendEMail = async (emailDef: { export const sendEMail = async (emailDef: {
to: string to: string
cc?: string
subject: string subject: string
text: string text: string
}): Promise<boolean> => { }): Promise<boolean> => {
logger.info(`send Email: to=${emailDef.to}, subject=${emailDef.subject}, text=${emailDef.text}`) logger.info(
`send Email: to=${emailDef.to}` +
(emailDef.cc ? `, cc=${emailDef.cc}` : '') +
`, subject=${emailDef.subject}, text=${emailDef.text}`,
)
if (!CONFIG.EMAIL) { if (!CONFIG.EMAIL) {
logger.info(`Emails are disabled via config...`) logger.info(`Emails are disabled via config...`)

View File

@ -2,11 +2,11 @@ export const accountMultiRegistration = {
de: { de: {
subject: 'Gradido: Erneuter Registrierungsversuch mit deiner E-Mail', subject: 'Gradido: Erneuter Registrierungsversuch mit deiner E-Mail',
text: (data: { text: (data: {
link: string // Wolle: support link?
firstName: string firstName: string
lastName: string lastName: string
email: string email: string
resendLink: string resendLink: string
supportLink: string
}): string => }): string =>
`Hallo ${data.firstName} ${data.lastName}, `Hallo ${data.firstName} ${data.lastName},
@ -18,7 +18,7 @@ ${data.resendLink}
oder kopiere den obigen Link in dein Browserfenster. oder kopiere den obigen Link in dein Browserfenster.
Wenn du nicht derjenige bist, der sich versucht hat erneut zu registrieren, wende dich bitte an unseren support: Wenn du nicht derjenige bist, der sich versucht hat erneut zu registrieren, wende dich bitte an unseren support:
${data.link} ${data.supportLink}
Mit freundlichen Grüßen, Mit freundlichen Grüßen,
dein Gradido-Team`, dein Gradido-Team`,

View File

@ -47,6 +47,8 @@ EMAIL_LINK_VERIFICATION=https://stage1.gradido.net/checkEmail/{optin}{code}
EMAIL_LINK_SETPASSWORD=https://stage1.gradido.net/reset-password/{optin} EMAIL_LINK_SETPASSWORD=https://stage1.gradido.net/reset-password/{optin}
EMAIL_LINK_FORGOTPASSWORD=https://stage1.gradido.net/forgot-password EMAIL_LINK_FORGOTPASSWORD=https://stage1.gradido.net/forgot-password
EMAIL_LINK_OVERVIEW=https://stage1.gradido.net/overview EMAIL_LINK_OVERVIEW=https://stage1.gradido.net/overview
EMAIL_LINK_SUPPORT=https://gradido.net/de/contact/
EMAIL_EMAIL_SUPPORT=support@gradido.net
EMAIL_CODE_VALID_TIME=1440 EMAIL_CODE_VALID_TIME=1440
EMAIL_CODE_REQUEST_TIME=10 EMAIL_CODE_REQUEST_TIME=10