mirror of
https://github.com/logto-io/logto.git
synced 2025-01-27 21:39:16 -05:00
test(core): aliyun-dm (#295)
This commit is contained in:
parent
9dca1e8083
commit
7af6973079
2 changed files with 50 additions and 5 deletions
49
packages/core/src/connectors/aliyun-dm/index.test.ts
Normal file
49
packages/core/src/connectors/aliyun-dm/index.test.ts
Normal file
|
@ -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();
|
||||
});
|
||||
});
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue