0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

test(core): add UT for aliyun sms (#305)

* test(core): add UT for aliyun sms

* test(core): define some params as top-level consts
This commit is contained in:
Darcy Ye 2022-03-02 16:50:26 +08:00 committed by GitHub
parent 8ae42e9666
commit 5734333dea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 11 deletions

View file

@ -0,0 +1,61 @@
import { sendMessage, validateConfig } from '.';
import { sendSms } from './single-send-text';
const defaultConnectorConfig = {
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
signName: 'signName',
templates: [
{
usageType: 'SignIn',
code: 'code',
name: 'name',
content: 'content',
remark: 'remark',
},
],
};
const validConnectorConfig = {
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
signName: 'signName',
templates: [],
};
const phoneTest = '13012345678';
const codeTest = '1234';
jest.mock('./single-send-text');
jest.mock('../utilities', () => ({
getConnectorConfig: async () => defaultConnectorConfig,
}));
describe('validateConfig()', () => {
it('should pass on valid config', async () => {
await expect(validateConfig(validConnectorConfig)).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(phoneTest, 'SignIn', { code: codeTest });
const { templates, ...credentials } = defaultConnectorConfig;
expect(sendSms).toHaveBeenCalledWith(
expect.objectContaining({
AccessKeyId: credentials.accessKeyId,
PhoneNumbers: phoneTest,
SignName: credentials.signName,
TemplateCode: templates.find(({ usageType }) => usageType === 'SignIn')?.code,
TemplateParam: `{"code":"${codeTest}"}`,
}),
'accessKeySecret'
);
});
it('throws if template is missing', async () => {
await expect(sendMessage(phoneTest, 'Register', { code: codeTest })).rejects.toThrow();
});
});

View file

@ -54,7 +54,7 @@ enum SmsTemplateType {
const templateGuard = z.object({
type: z.nativeEnum(SmsTemplateType).default(2),
usageType: z.string(),
code: z.string().optional(),
code: z.string(),
name: z.string().min(1).max(30),
content: z.string().min(1).max(500),
remark: z.string(),
@ -64,7 +64,6 @@ const configGuard = z.object({
accessKeyId: z.string(),
accessKeySecret: z.string(),
signName: z.string(),
templateCode: z.string(),
templates: z.array(templateGuard),
});
@ -85,16 +84,11 @@ export type AliyunSmsConfig = z.infer<typeof configGuard>;
export const sendMessage: SmsSendMessageFunction = async (phone, type, { code }) => {
const config = await getConnectorConfig<AliyunSmsConfig>(metadata.id);
await validateConfig(config);
const { accessKeyId, accessKeySecret, signName, templateCode, templates } = config;
const template = templates.find(
({ code, usageType }) => code === templateCode && usageType === type
);
const { accessKeyId, accessKeySecret, signName, templates } = config;
const template = templates.find(({ usageType }) => usageType === type);
if (!template) {
throw new ConnectorError(
ConnectorErrorCodes.TemplateNotFound,
`Cannot find template code: ${templateCode}`
);
throw new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`);
}
return sendSms(
@ -102,7 +96,7 @@ export const sendMessage: SmsSendMessageFunction = async (phone, type, { code })
AccessKeyId: accessKeyId,
PhoneNumbers: phone,
SignName: signName,
TemplateCode: templateCode,
TemplateCode: template.code,
TemplateParam: JSON.stringify({ code }),
},
accessKeySecret