diff --git a/packages/core/src/connectors/aliyun-dm/index.test.ts b/packages/core/src/connectors/aliyun-dm/index.test.ts new file mode 100644 index 000000000..7552f485c --- /dev/null +++ b/packages/core/src/connectors/aliyun-dm/index.test.ts @@ -0,0 +1,49 @@ +import { sendMessage, validateConfig } from '.'; +import { singleSendMail } from './single-send-mail'; + +jest.mock('./single-send-mail'); +jest.mock('../utilities', () => ({ + getConnectorConfig: async () => ({ + accessKeyId: 'accessKeyId', + accessKeySecret: 'accessKeySecret', + accountName: 'accountName', + templates: [ + { + usageType: 'SignIn', + content: 'Your code is {{code}}, {{code}} is your code', + subject: 'subject', + }, + ], + }), +})); + +describe('validateConfig()', () => { + it('should pass on valid config', async () => { + await expect( + validateConfig({ + accessKeyId: 'accessKeyId', + accessKeySecret: 'accessKeySecret', + accountName: 'accountName', + templates: [], + }) + ).resolves.not.toThrow(); + }); + it('throws if config is invalid', async () => { + await expect(validateConfig({})).rejects.toThrow(); + }); +}); + +describe('sendMessage()', () => { + it('should call singleSendMail() and replace code in content', async () => { + await sendMessage('to@email.com', 'SignIn', { code: '1234' }); + expect(singleSendMail).toHaveBeenCalledWith( + expect.objectContaining({ + HtmlBody: 'Your code is 1234, 1234 is your code', + }), + expect.anything() + ); + }); + it('throws if template is missing', async () => { + await expect(sendMessage('to@email.com', 'Register', { code: '1234' })).rejects.toThrow(); + }); +}); diff --git a/packages/core/src/connectors/aliyun-dm/index.ts b/packages/core/src/connectors/aliyun-dm/index.ts index 1920853d4..569710f97 100644 --- a/packages/core/src/connectors/aliyun-dm/index.ts +++ b/packages/core/src/connectors/aliyun-dm/index.ts @@ -45,10 +45,6 @@ const configGuard = z.object({ }); export const validateConfig: ValidateConfig = async (config: unknown) => { - if (!config) { - throw new ConnectorError(ConnectorErrorCodes.InvalidConfig, 'Missing config'); - } - const result = configGuard.safeParse(config); if (!result.success) { @@ -82,7 +78,7 @@ export const sendMessage: EmailSendMessageFunction = async (address, type, data) Subject: template.subject, HtmlBody: typeof data.code === 'string' - ? template.content.replaceAll('{{code}}', data.code) + ? template.content.replace(/{{code}}/g, data.code) : template.content, }, accessKeySecret