2022-04-26 06:32:41 -05:00
|
|
|
import { GetConnectorConfig } from '@logto/connector-types';
|
|
|
|
|
2022-05-24 00:54:37 -05:00
|
|
|
import AliyunDmConnector from '.';
|
2022-04-26 06:32:41 -05:00
|
|
|
import { mockedConfig } from './mock';
|
|
|
|
import { singleSendMail } from './single-send-mail';
|
2022-05-13 01:22:37 -05:00
|
|
|
import { AliyunDmConfig } from './types';
|
2022-04-26 06:32:41 -05:00
|
|
|
|
|
|
|
const getConnectorConfig = jest.fn() as GetConnectorConfig<AliyunDmConfig>;
|
|
|
|
|
|
|
|
const aliyunDmMethods = new AliyunDmConnector(getConnectorConfig);
|
|
|
|
|
2022-06-09 21:41:45 -05:00
|
|
|
jest.mock('./single-send-mail', () => {
|
|
|
|
return {
|
|
|
|
singleSendMail: jest.fn(() => {
|
|
|
|
return {
|
|
|
|
body: JSON.stringify({ EnvId: 'env-id', RequestId: 'request-id' }),
|
|
|
|
statusCode: 200,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
2022-04-26 06:32:41 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|