0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/connectors/connector-aliyun-sms/src/index.test.ts
Gao Sun 6b322a537c
refactor: add connector packages
the initial commit to move all connector packages to the main
repo.
2023-04-01 15:53:14 +08:00

87 lines
2.5 KiB
TypeScript

import { VerificationCodeType } from '@logto/connector-kit';
import { mockedConnectorConfig, phoneTest, codeTest } from './mock.js';
const { jest } = import.meta;
const getConfig = jest.fn().mockResolvedValue(mockedConnectorConfig);
const sendSms = jest.fn().mockResolvedValue({
body: JSON.stringify({ Code: 'OK', RequestId: 'request-id', Message: 'OK' }),
statusCode: 200,
});
jest.unstable_mockModule('./single-send-text.js', () => ({
sendSms,
}));
const { default: createConnector } = await import('./index.js');
describe('sendMessage()', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should call singleSendMail() and replace code in content', async () => {
const connector = await createConnector({ getConfig });
await connector.sendMessage({
to: phoneTest,
type: VerificationCodeType.SignIn,
payload: { code: codeTest },
});
expect(sendSms).toHaveBeenCalledWith(
expect.objectContaining({
AccessKeyId: mockedConnectorConfig.accessKeyId,
PhoneNumbers: phoneTest,
SignName: mockedConnectorConfig.signName,
TemplateCode: 'TemplateCode',
TemplateParam: `{"code":"${codeTest}"}`,
}),
mockedConnectorConfig.accessKeySecret
);
});
it('should use template per region', async () => {
const connector = await createConnector({ getConfig });
for (const to of [phoneTest, `86${phoneTest}`, `0086${phoneTest}`, `+86${phoneTest}`]) {
// eslint-disable-next-line no-await-in-loop
await connector.sendMessage({
to,
type: VerificationCodeType.Register,
payload: { code: codeTest },
});
expect(sendSms).toHaveBeenCalledWith(
expect.objectContaining({
TemplateCode: 'TemplateCode1',
}),
mockedConnectorConfig.accessKeySecret
);
sendSms.mockClear();
}
await connector.sendMessage({
to: '+1123123123',
type: VerificationCodeType.Register,
payload: { code: codeTest },
});
expect(sendSms).toHaveBeenCalledWith(
expect.objectContaining({
TemplateCode: 'TemplateCode2',
}),
mockedConnectorConfig.accessKeySecret
);
});
it('throws if template is missing', async () => {
const connector = await createConnector({ getConfig });
await expect(
connector.sendMessage({
to: phoneTest,
type: VerificationCodeType.Test,
payload: { code: codeTest },
})
).rejects.toThrow();
});
});