0
Fork 0
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:
Wang Sijie 2022-03-01 11:04:20 +08:00 committed by GitHub
parent 9dca1e8083
commit 7af6973079
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 5 deletions

View 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();
});
});

View file

@ -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