0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00
logto/packages/connector-aliyun-dm/src/index.test.ts
Darcy Ye 3925424316
refactor(connectors): use zod guard instead of got generic and elaborate error info (#1078)
* feat(alipay): use zod parse instead of got response generic

* feat(alipay): fix expires_in and re_expires_in type

* feat(alipay): extract errorHandler function to map HTTP error to connector error

* feat(alipay-native): refactor as alipay

* feat(aliyun-dm): use zod parser for type guard and extract error handler

* feat(aliyun-sms): extract error handler

* feat(facebook): use zod parser

* feat(github): use zod parser

* feat(google): use zod parser

* feat(wechat-web): use zod parser and wrap error handler

* feat(wechat-native): use zod parser and wrap error handler
2022-06-10 10:41:45 +08:00

68 lines
1.8 KiB
TypeScript

import { GetConnectorConfig } from '@logto/connector-types';
import AliyunDmConnector from '.';
import { mockedConfig } from './mock';
import { singleSendMail } from './single-send-mail';
import { AliyunDmConfig } from './types';
const getConnectorConfig = jest.fn() as GetConnectorConfig<AliyunDmConfig>;
const aliyunDmMethods = new AliyunDmConnector(getConnectorConfig);
jest.mock('./single-send-mail', () => {
return {
singleSendMail: jest.fn(() => {
return {
body: JSON.stringify({ EnvId: 'env-id', RequestId: 'request-id' }),
statusCode: 200,
};
}),
};
});
beforeAll(() => {
jest.spyOn(aliyunDmMethods, 'getConfig').mockResolvedValue(mockedConfig);
});
describe('validateConfig()', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should pass on valid config', async () => {
await expect(
aliyunDmMethods.validateConfig({
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
accountName: 'accountName',
templates: [],
})
).resolves.not.toThrow();
});
it('throws if config is invalid', async () => {
await expect(aliyunDmMethods.validateConfig({})).rejects.toThrow();
});
});
describe('sendMessage()', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should call singleSendMail() and replace code in content', async () => {
await aliyunDmMethods.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(
aliyunDmMethods.sendMessage('to@email.com', 'Register', { code: '1234' })
).rejects.toThrow();
});
});