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-logto-email/src/index.test.ts

56 lines
1.9 KiB
TypeScript

import { got } from 'got';
import nock from 'nock';
import { VerificationCodeType } from '@logto/connector-kit';
import { emailEndpoint, usageEndpoint } from './constant.js';
import createConnector from './index.js';
const { jest } = import.meta;
const endpoint = 'http://localhost:3003';
const api = got.extend({ prefixUrl: endpoint });
const dropLeadingSlash = (path: string) => path.replace(/^\//, '');
const buildUrl = (path: string, endpoint: string) => new URL(`${endpoint}/api${path}`);
const getConfig = jest.fn().mockResolvedValue({});
const getCloudServiceClient = jest.fn().mockResolvedValue({
post: async (path: string, payload: { body: unknown }) => {
return api(dropLeadingSlash(path), {
method: 'POST',
json: payload.body,
});
},
get: async (path: string, payload: { search: Record<string, string> }) => {
return api(dropLeadingSlash(path), {
method: 'GET',
searchParams: payload.search,
}).json<{ count: number }>();
},
});
describe('sendMessage()', () => {
it('should send message successfully', async () => {
const url = buildUrl(emailEndpoint, endpoint);
nock(url.origin).post(url.pathname).reply(204);
const { sendMessage } = await createConnector({ getConfig, getCloudServiceClient });
await expect(
sendMessage({
to: 'wangsijie94@gmail.com',
type: VerificationCodeType.SignIn,
payload: { code: '1234' },
})
).resolves.not.toThrow();
});
it('should get usage successfully', async () => {
const date = new Date();
const url = buildUrl(usageEndpoint, endpoint);
nock(url.origin).get(url.pathname).query({ from: date.toISOString() }).reply(200, { count: 1 });
const connector = await createConnector({ getConfig, getCloudServiceClient });
expect(connector.getUsage).toBeDefined();
const usage = await connector.getUsage!(date);
expect(usage).toEqual(1);
});
});