From 2e77bdf11e325935bb6be0ebc5cfef5cfb8f08f7 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Mon, 29 Apr 2024 14:38:35 -0400 Subject: [PATCH] test: add test email adapter, use for all tests by default (#6120) --- packages/payload/src/config/schema.ts | 2 +- test/buildConfigWithDefaults.ts | 19 ++------------ test/testEmailAdapter.ts | 38 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 test/testEmailAdapter.ts diff --git a/packages/payload/src/config/schema.ts b/packages/payload/src/config/schema.ts index 570599fba..f8245e0b3 100644 --- a/packages/payload/src/config/schema.ts +++ b/packages/payload/src/config/schema.ts @@ -101,7 +101,7 @@ export default joi.object({ validate: joi.func().required(), }) .unknown(), - email: joi.object(), + email: joi.alternatives().try(joi.object(), joi.func()), endpoints: endpointsSchema, globals: joi.array(), graphQL: joi.object().keys({ diff --git a/test/buildConfigWithDefaults.ts b/test/buildConfigWithDefaults.ts index dfd9a630e..3447a17bf 100644 --- a/test/buildConfigWithDefaults.ts +++ b/test/buildConfigWithDefaults.ts @@ -34,6 +34,7 @@ import sharp from 'sharp' import { reInitEndpoint } from './helpers/reInit.js' import { localAPIEndpoint } from './helpers/sdk/endpoint.js' +import { testEmailAdapter } from './testEmailAdapter.js' // process.env.PAYLOAD_DATABASE = 'postgres' export async function buildConfigWithDefaults( @@ -74,23 +75,7 @@ export async function buildConfigWithDefaults( const config: Config = { db: databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongodb'], secret: 'TEST_SECRET', - //editor: slateEditor({}), - // editor: slateEditor({ - // admin: { - // upload: { - // collections: { - // media: { - // fields: [ - // { - // name: 'alt', - // type: 'text', - // }, - // ], - // }, - // }, - // }, - // }, - // }), + email: testEmailAdapter, endpoints: [localAPIEndpoint, reInitEndpoint], editor: lexicalEditor({ features: [ diff --git a/test/testEmailAdapter.ts b/test/testEmailAdapter.ts new file mode 100644 index 000000000..a8460fadf --- /dev/null +++ b/test/testEmailAdapter.ts @@ -0,0 +1,38 @@ +import type { EmailAdapter, SendEmailOptions } from 'payload/config' + +/** + * Logs all emails to stdout + */ +export const testEmailAdapter: EmailAdapter = ({ payload }) => ({ + name: 'test-email-adapter', + defaultFromAddress: 'dev@payloadcms.com', + defaultFromName: 'Payload Test', + sendEmail: async (message) => { + const stringifiedTo = getStringifiedToAddress(message) + const res = `Test email to: '${stringifiedTo}', Subject: '${message.subject}'` + payload.logger.info({ msg: res, content: message }) + return Promise.resolve() + }, +}) + +export function getStringifiedToAddress(message: SendEmailOptions): string | undefined { + let stringifiedTo: string | undefined + + if (typeof message.to === 'string') { + stringifiedTo = message.to + } else if (Array.isArray(message.to)) { + stringifiedTo = message.to + .map((to) => { + if (typeof to === 'string') { + return to + } else if (to.address) { + return to.address + } + return '' + }) + .join(', ') + } else if (message.to.address) { + stringifiedTo = message.to.address + } + return stringifiedTo +}