Test 'sendEmailTranslated'

This commit is contained in:
Wolfgang Huß 2022-11-14 09:36:43 +01:00
parent 2df19ab362
commit c187b33a4d
6 changed files with 124 additions and 31 deletions

View File

@ -0,0 +1,112 @@
import { createTransport } from 'nodemailer'
import { logger, i18n } from '@test/testSetup'
import CONFIG from '@/config'
import { sendEmailTranslated } from './sendEmailTranslated'
CONFIG.EMAIL = false
CONFIG.EMAIL_SMTP_URL = 'EMAIL_SMTP_URL'
CONFIG.EMAIL_SMTP_PORT = '1234'
CONFIG.EMAIL_USERNAME = 'user'
CONFIG.EMAIL_PASSWORD = 'pwd'
jest.mock('nodemailer', () => {
return {
__esModule: true,
createTransport: jest.fn(() => {
return {
sendMail: jest.fn(() => {
return {
messageId: 'message',
}
}),
}
}),
}
})
describe('sendEmailTranslated', () => {
let result: Record<string, unknown> | null
describe('config email is false', () => {
beforeEach(async () => {
result = await sendEmailTranslated({
receiver: {
to: 'receiver@mail.org',
cc: 'support@gradido.net',
},
template: 'accountMultiRegistration',
locals: {
locale: 'en',
},
})
})
it('logs warning', () => {
expect(logger.info).toBeCalledWith('Emails are disabled via config...')
})
it('returns false', () => {
expect(result).toBeFalsy()
})
})
describe('config email is true', () => {
beforeEach(async () => {
CONFIG.EMAIL = true
result = await sendEmailTranslated({
receiver: {
to: 'receiver@mail.org',
cc: 'support@gradido.net',
},
template: 'accountMultiRegistration',
locals: {
locale: 'en',
},
})
})
it('calls the transporter', () => {
expect(createTransport).toBeCalledWith({
host: 'EMAIL_SMTP_URL',
port: 1234,
secure: false,
requireTLS: true,
auth: {
user: 'user',
pass: 'pwd',
},
})
})
it('has "sendEmailTranslated" with result not(!) "null"', () => {
expect(result).toBeTruthy()
})
it('has "sendEmailTranslated" with result', () => {
expect(result).toMatchObject({
envelope: {
from: 'info@gradido.net',
to: ['stage1@gradido.net', 'support@gradido.net'],
},
message: expect.any(String),
originalMessage: expect.objectContaining({
to: 'stage1@gradido.net',
cc: 'support@gradido.net',
from: 'Gradido (nicht antworten) <info@gradido.net>',
attachments: [],
subject: 'Gradido: Try To Register Again With Your Email',
html: expect.stringContaining('Gradido: Try To Register Again With Your Email'),
text: expect.stringContaining('GRADIDO: TRY TO REGISTER AGAIN WITH YOUR EMAIL'),
}),
})
})
it.skip('calls "i18n.setLocale" with "en"', () => {
expect(i18n.setLocale).toBeCalledWith('en')
})
it.skip('calls "i18n.__" for translation', () => {
expect(i18n.__).toBeCalled()
})
})
})

View File

@ -13,16 +13,15 @@ export const sendEmailTranslated = async (params: {
}
template: string
locals: Record<string, string>
}): Promise<boolean> => {
// Wolle: test this …
}): Promise<Record<string, unknown> | null> => {
let resultSend: Record<string, unknown> | null = null
// TODO: test the calling order of 'i18n.setLocale' for example: language of logging 'en', language of email receiver 'es', reset language of current user 'de'
// because language of receiver can differ from language of current user who triggers the sending
const rememberLocaleToRestore = i18n.getLocale()
// Wolle:
// console.log('sendEmailTranslated i18n.getLocale, incoming from user: ', i18n.getLocale())
i18n.setLocale('en') // for logging
// Wolle:
// console.log('sendEmailTranslated i18n.getLocale, logging: ', i18n.getLocale())
logger.info(
`send Email: language=${params.locals.locale} to=${params.receiver.to}` +
(params.receiver.cc ? `, cc=${params.receiver.cc}` : '') +
@ -31,7 +30,7 @@ export const sendEmailTranslated = async (params: {
if (!CONFIG.EMAIL) {
logger.info(`Emails are disabled via config...`)
return false
return null
}
if (CONFIG.EMAIL_TEST_MODUS) {
logger.info(
@ -51,28 +50,17 @@ export const sendEmailTranslated = async (params: {
})
i18n.setLocale(params.locals.locale) // for email
// Wolle:
// console.log('sendEmailTranslated i18n.getLocale, email: ', i18n.getLocale())
// TESTING: see 'README.md'
const email = new Email({
message: {
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
},
// uncomment below to send emails in development/test env:
// send: true,
transport,
// uncomment below to open send emails in the browser
// preview: {
// open: {
// app: 'firefox',
// wait: false,
// },
// },
// Wolle
preview: false,
// i18n, // is only needed if you don't install i18n
})
// TESTING: see 'README.md'
// ATTENTION: await is needed, because otherwise on send the email gets send in the language of the current user, because below the language gets reset
await email
.send({
@ -80,7 +68,8 @@ export const sendEmailTranslated = async (params: {
message: params.receiver,
locals: params.locals, // the 'locale' in here seems not to be used by 'email-template', because it doesn't work if the language isn't set before by 'i18n.setLocale'
})
.then((result: unknown) => {
.then((result: Record<string, unknown>) => {
resultSend = result
logger.info('Send email successfully !!!')
logger.info('Result: ', result)
})
@ -89,10 +78,7 @@ export const sendEmailTranslated = async (params: {
throw new Error('Error sending notification email!')
})
// Wolle: !!! if we do this without an await on send the email gets send in the language from the current user !!!
i18n.setLocale(rememberLocaleToRestore)
// Wolle:
// console.log('sendEmailTranslated i18n.getLocale, reset: ', i18n.getLocale())
return true
return resultSend
}

View File

@ -71,7 +71,6 @@ let mutate: any, query: any, con: any
let testEnv: any
beforeAll(async () => {
// Wolle: console.log('beforeAll localization: ', localization)
testEnv = await testEnvironment(logger, localization)
mutate = testEnv.mutate
query = testEnv.query

View File

@ -2,7 +2,6 @@ import path from 'path'
import { backendLogger } from './logger'
import i18n from 'i18n'
// Wolle: console.log('i18n backend/src/server/localization.ts: ', i18n)
i18n.configure({
locales: ['en', 'de'],
defaultLocale: 'en',
@ -25,6 +24,5 @@ i18n.configure({
disable: false,
},
})
// Wolle: i18n.setLocale('en')
export { i18n }

View File

@ -26,7 +26,6 @@ export const cleanDB = async () => {
}
export const testEnvironment = async (logger?: any, localization?: any) => {
// Wolle: console.log('testEnvironment localization: ', localization)
const server = await createServer(context, logger, localization)
const con = server.con
const testClient = createTestClient(server.apollo)

View File

@ -1,5 +1,4 @@
import { backendLogger as logger } from '@/server/logger'
// Wolle: import i18n from 'i18n'
import { i18n } from '@/server/localization'
jest.setTimeout(1000000)
@ -29,10 +28,10 @@ jest.mock('@/server/localization', () => {
i18n: {
init: jest.fn(),
// configure: jest.fn(),
// __: jest.fn(),
// setLocale: jest.fn(),
},
}
})
// Wolle: console.log('testSetup.js i18n: ', i18n)
export { logger, i18n }