2022-07-21 04:22:16 -05:00
|
|
|
import { ConnectorType } from '@logto/schemas';
|
2022-07-21 06:12:44 -05:00
|
|
|
import { HTTPError } from 'got';
|
2022-07-21 04:22:16 -05:00
|
|
|
|
2022-07-26 03:00:12 -05:00
|
|
|
import {
|
2022-07-26 03:12:34 -05:00
|
|
|
mockEmailConnectorConfig,
|
2022-07-29 05:43:10 -05:00
|
|
|
mockEmailConnectorId,
|
|
|
|
mockSmsConnectorConfig,
|
|
|
|
mockSmsConnectorId,
|
2022-08-01 20:44:35 -05:00
|
|
|
mockSocialConnectorConfig,
|
|
|
|
mockSocialConnectorId,
|
2022-07-26 22:23:10 -05:00
|
|
|
} from '@/__mocks__/connectors-mock';
|
|
|
|
import {
|
|
|
|
disableConnector,
|
|
|
|
enableConnector,
|
|
|
|
getConnector,
|
|
|
|
listConnectors,
|
2022-07-26 22:33:32 -05:00
|
|
|
sendEmailTestMessage,
|
|
|
|
sendSmsTestMessage,
|
2022-07-26 22:23:10 -05:00
|
|
|
updateConnectorConfig,
|
|
|
|
} from '@/api/connector';
|
2022-07-21 04:38:27 -05:00
|
|
|
|
2022-07-27 02:12:49 -05:00
|
|
|
/*
|
|
|
|
* We'd better only use mock connectors in integration tests.
|
|
|
|
* Since we will refactor connectors soon, keep using some real connectors
|
|
|
|
* for testing updating configs and enabling/disabling for now.
|
|
|
|
*/
|
|
|
|
test('connector set-up flow', async () => {
|
2022-07-21 04:22:16 -05:00
|
|
|
/*
|
2022-07-27 02:12:49 -05:00
|
|
|
* Set up social/SMS/email connectors
|
2022-07-21 04:38:27 -05:00
|
|
|
*/
|
2022-07-29 05:43:10 -05:00
|
|
|
await Promise.all(
|
|
|
|
[
|
2022-09-05 04:49:11 -05:00
|
|
|
{ id: mockSmsConnectorId, config: mockSmsConnectorConfig },
|
|
|
|
{ id: mockEmailConnectorId, config: mockEmailConnectorConfig },
|
2022-08-01 20:44:35 -05:00
|
|
|
{ id: mockSocialConnectorId, config: mockSocialConnectorConfig },
|
2022-07-29 05:43:10 -05:00
|
|
|
].map(async ({ id, config }) => {
|
|
|
|
const updatedConnector = await updateConnectorConfig(id, config);
|
|
|
|
expect(updatedConnector.config).toEqual(config);
|
|
|
|
const enabledConnector = await enableConnector(id);
|
|
|
|
expect(enabledConnector.enabled).toBeTruthy();
|
|
|
|
|
|
|
|
// The result of getting a connector should be same as the result of updating a connector above.
|
|
|
|
const connector = await getConnector(id);
|
|
|
|
expect(connector.enabled).toBeTruthy();
|
|
|
|
expect(connector.config).toEqual(config);
|
|
|
|
})
|
|
|
|
);
|
2022-07-21 04:38:27 -05:00
|
|
|
|
2022-07-21 06:12:44 -05:00
|
|
|
/*
|
|
|
|
* It should update the connector config successfully when it is valid; otherwise, it should fail.
|
|
|
|
* We will test updating to the invalid connector config, that is the case not covered above.
|
|
|
|
*/
|
|
|
|
await expect(
|
2022-09-05 04:49:11 -05:00
|
|
|
updateConnectorConfig(mockSocialConnectorId, mockSmsConnectorConfig)
|
2022-07-21 06:12:44 -05:00
|
|
|
).rejects.toThrow(HTTPError);
|
|
|
|
// To confirm the failed updating request above did not modify the original config,
|
2022-08-01 20:44:35 -05:00
|
|
|
// we check: the mock connector config should stay the same.
|
|
|
|
const mockSocialConnector = await getConnector(mockSocialConnectorId);
|
|
|
|
expect(mockSocialConnector.config).toEqual(mockSocialConnectorConfig);
|
2022-07-27 02:12:49 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Change to another SMS/Email connector
|
|
|
|
*/
|
2022-07-29 05:43:10 -05:00
|
|
|
await Promise.all(
|
|
|
|
[
|
2022-08-31 23:21:42 -05:00
|
|
|
{ id: mockSmsConnectorId, config: mockSmsConnectorConfig, type: ConnectorType.Sms },
|
2022-07-29 05:43:10 -05:00
|
|
|
{ id: mockEmailConnectorId, config: mockEmailConnectorConfig, type: ConnectorType.Email },
|
|
|
|
].map(async ({ id, config, type }) => {
|
|
|
|
const updatedConnector = await updateConnectorConfig(id, config);
|
|
|
|
expect(updatedConnector.config).toEqual(config);
|
|
|
|
const enabledConnector = await enableConnector(id);
|
|
|
|
expect(enabledConnector.enabled).toBeTruthy();
|
|
|
|
|
|
|
|
// There should be exactly one enabled SMS/email connector after changing to another SMS/email connector.
|
|
|
|
const connectorsAfterChanging = await listConnectors();
|
|
|
|
const enabledConnectors = connectorsAfterChanging.filter(
|
|
|
|
(connector) => connector.type === type && connector.enabled
|
|
|
|
);
|
|
|
|
expect(enabledConnectors.length).toEqual(1);
|
|
|
|
expect(enabledConnectors[0]?.id).toEqual(id);
|
|
|
|
})
|
|
|
|
);
|
2022-07-21 06:12:44 -05:00
|
|
|
|
2022-07-21 07:51:48 -05:00
|
|
|
/*
|
|
|
|
* Delete (i.e. disable) a connector
|
|
|
|
*
|
|
|
|
* We have not provided the API to delete a connector for now.
|
|
|
|
* Deleting a connector using Admin Console means disabling a connector using Management API.
|
|
|
|
*/
|
2022-07-26 03:12:34 -05:00
|
|
|
const disabledMockEmailConnector = await disableConnector(mockEmailConnectorId);
|
|
|
|
expect(disabledMockEmailConnector.enabled).toBeFalsy();
|
|
|
|
const mockEmailConnector = await getConnector(mockEmailConnectorId);
|
|
|
|
expect(mockEmailConnector.enabled).toBeFalsy();
|
2022-07-21 07:51:48 -05:00
|
|
|
|
2022-07-21 23:37:25 -05:00
|
|
|
/**
|
|
|
|
* List connectors after manually setting up connectors.
|
|
|
|
* The result of listing connectors should be same as the result of updating connectors above.
|
|
|
|
*/
|
2022-07-23 00:04:39 -05:00
|
|
|
expect(await listConnectors()).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
2022-09-05 04:49:11 -05:00
|
|
|
id: mockEmailConnectorId,
|
|
|
|
config: mockEmailConnectorConfig,
|
2022-07-23 00:04:39 -05:00
|
|
|
enabled: false,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
2022-07-26 03:12:34 -05:00
|
|
|
id: mockSmsConnectorId,
|
|
|
|
config: mockSmsConnectorConfig,
|
2022-07-23 00:04:39 -05:00
|
|
|
enabled: true,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
2022-09-05 04:49:11 -05:00
|
|
|
id: mockSocialConnectorId,
|
|
|
|
config: mockSocialConnectorConfig,
|
|
|
|
enabled: true,
|
2022-07-23 00:04:39 -05:00
|
|
|
}),
|
|
|
|
])
|
2022-07-21 23:37:25 -05:00
|
|
|
);
|
2022-07-26 22:33:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('send SMS/email test message', () => {
|
|
|
|
const phone = '8612345678901';
|
|
|
|
const email = 'test@example.com';
|
2022-07-21 23:37:25 -05:00
|
|
|
|
2022-07-26 22:33:32 -05:00
|
|
|
it('should send the test message successfully when the config is valid', async () => {
|
|
|
|
await expect(
|
|
|
|
sendSmsTestMessage(mockSmsConnectorId, phone, mockSmsConnectorConfig)
|
|
|
|
).resolves.not.toThrow();
|
|
|
|
await expect(
|
|
|
|
sendEmailTestMessage(mockEmailConnectorId, email, mockEmailConnectorConfig)
|
|
|
|
).resolves.not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to send the test message when the config is invalid', async () => {
|
|
|
|
await expect(sendSmsTestMessage(mockSmsConnectorId, phone, {})).rejects.toThrow(HTTPError);
|
|
|
|
await expect(sendEmailTestMessage(mockEmailConnectorId, email, {})).rejects.toThrow(HTTPError);
|
|
|
|
});
|
2022-07-21 01:21:05 -05:00
|
|
|
});
|