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 {
|
|
|
|
facebookConnectorId,
|
|
|
|
facebookConnectorConfig,
|
|
|
|
aliyunSmsConnectorId,
|
|
|
|
aliyunSmsConnectorConfig,
|
|
|
|
aliyunEmailConnectorId,
|
|
|
|
aliyunEmailConnectorConfig,
|
2022-07-26 03:12:34 -05:00
|
|
|
mockSmsConnectorId,
|
|
|
|
mockSmsConnectorConfig,
|
|
|
|
mockEmailConnectorId,
|
|
|
|
mockEmailConnectorConfig,
|
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-21 01:21:05 -05:00
|
|
|
test('connector flow', async () => {
|
2022-07-21 04:22:16 -05:00
|
|
|
/*
|
|
|
|
* List connectors after initializing a new Logto instance
|
|
|
|
*/
|
2022-07-21 01:54:11 -05:00
|
|
|
const allConnectors = await listConnectors();
|
2022-07-21 01:21:05 -05:00
|
|
|
|
|
|
|
// There should be no connectors, or all connectors should be disabled.
|
2022-07-21 01:54:11 -05:00
|
|
|
for (const connectorDto of allConnectors) {
|
2022-07-21 01:21:05 -05:00
|
|
|
expect(connectorDto.enabled).toBeFalsy();
|
|
|
|
}
|
|
|
|
|
2022-07-21 04:22:16 -05:00
|
|
|
/*
|
|
|
|
* Set up a social connector
|
|
|
|
*/
|
2022-07-21 01:54:11 -05:00
|
|
|
const updatedFacebookConnector = await updateConnectorConfig(
|
|
|
|
facebookConnectorId,
|
|
|
|
facebookConnectorConfig
|
|
|
|
);
|
|
|
|
expect(updatedFacebookConnector.config).toEqual(facebookConnectorConfig);
|
|
|
|
const enabledFacebookConnector = await enableConnector(facebookConnectorId);
|
|
|
|
expect(enabledFacebookConnector.enabled).toBeTruthy();
|
|
|
|
|
|
|
|
// The result of getting a connector should be same as the result of updating a connector above.
|
|
|
|
const facebookConnector = await getConnector(facebookConnectorId);
|
|
|
|
expect(facebookConnector.enabled).toBeTruthy();
|
|
|
|
expect(facebookConnector.config).toEqual(facebookConnectorConfig);
|
|
|
|
|
2022-07-21 04:22:16 -05:00
|
|
|
/*
|
|
|
|
* Set up an SMS connector
|
|
|
|
*/
|
|
|
|
const updatedAliyunSmsConnector = await updateConnectorConfig(
|
|
|
|
aliyunSmsConnectorId,
|
|
|
|
aliyunSmsConnectorConfig
|
|
|
|
);
|
|
|
|
expect(updatedAliyunSmsConnector.config).toEqual(aliyunSmsConnectorConfig);
|
|
|
|
const enabledAliyunSmsConnector = await enableConnector(aliyunSmsConnectorId);
|
|
|
|
expect(enabledAliyunSmsConnector.enabled).toBeTruthy();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change to another SMS connector
|
|
|
|
*/
|
2022-07-26 03:12:34 -05:00
|
|
|
const updatedMockSmsConnector = await updateConnectorConfig(
|
|
|
|
mockSmsConnectorId,
|
|
|
|
mockSmsConnectorConfig
|
2022-07-21 04:22:16 -05:00
|
|
|
);
|
2022-07-26 03:12:34 -05:00
|
|
|
expect(updatedMockSmsConnector.config).toEqual(mockSmsConnectorConfig);
|
|
|
|
const enabledMockSmsConnector = await enableConnector(mockSmsConnectorId);
|
|
|
|
expect(enabledMockSmsConnector.enabled).toBeTruthy();
|
2022-07-21 04:22:16 -05:00
|
|
|
|
|
|
|
// There should be exactly one enabled SMS connector after changing to another SMS connector.
|
|
|
|
const connectorsAfterChangingSmsConnector = await listConnectors();
|
|
|
|
const enabledSmsConnectors = connectorsAfterChangingSmsConnector.filter(
|
|
|
|
(connector) => connector.type === ConnectorType.SMS && connector.enabled
|
|
|
|
);
|
|
|
|
expect(enabledSmsConnectors.length).toEqual(1);
|
2022-07-26 03:12:34 -05:00
|
|
|
expect(enabledSmsConnectors[0]?.id).toEqual(mockSmsConnectorId);
|
2022-07-21 04:22:16 -05:00
|
|
|
|
2022-07-21 04:38:27 -05:00
|
|
|
/*
|
|
|
|
* Set up an email connector
|
|
|
|
*/
|
|
|
|
const updatedAliyunEmailConnector = await updateConnectorConfig(
|
|
|
|
aliyunEmailConnectorId,
|
|
|
|
aliyunEmailConnectorConfig
|
|
|
|
);
|
|
|
|
expect(updatedAliyunEmailConnector.config).toEqual(aliyunEmailConnectorConfig);
|
|
|
|
const enabledAliyunEmailConnector = await enableConnector(aliyunEmailConnectorId);
|
|
|
|
expect(enabledAliyunEmailConnector.enabled).toBeTruthy();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change to another email connector
|
|
|
|
*/
|
2022-07-26 03:12:34 -05:00
|
|
|
const updatedMockEmailConnector = await updateConnectorConfig(
|
|
|
|
mockEmailConnectorId,
|
|
|
|
mockEmailConnectorConfig
|
2022-07-21 04:38:27 -05:00
|
|
|
);
|
2022-07-26 03:12:34 -05:00
|
|
|
expect(updatedMockEmailConnector.config).toEqual(mockEmailConnectorConfig);
|
|
|
|
const enabledMockEmailConnector = await enableConnector(mockEmailConnectorId);
|
|
|
|
expect(enabledMockEmailConnector.enabled).toBeTruthy();
|
2022-07-21 04:38:27 -05:00
|
|
|
|
|
|
|
// There should be exactly one enabled email connector after changing to another email connector.
|
|
|
|
const connectorsAfterChangingEmailConnector = await listConnectors();
|
|
|
|
const enabledEmailConnector = connectorsAfterChangingEmailConnector.filter(
|
|
|
|
(connector) => connector.type === ConnectorType.Email && connector.enabled
|
|
|
|
);
|
|
|
|
expect(enabledEmailConnector.length).toEqual(1);
|
2022-07-26 03:12:34 -05:00
|
|
|
expect(enabledEmailConnector[0]?.id).toEqual(mockEmailConnectorId);
|
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-07-26 03:12:34 -05:00
|
|
|
updateConnectorConfig(aliyunEmailConnectorId, mockEmailConnectorConfig)
|
2022-07-21 06:12:44 -05:00
|
|
|
).rejects.toThrow(HTTPError);
|
|
|
|
// To confirm the failed updating request above did not modify the original config,
|
|
|
|
// we check: the Aliyun email connector config should stay the same.
|
|
|
|
const aliyunEmailConnector = await getConnector(aliyunEmailConnectorId);
|
|
|
|
expect(aliyunEmailConnector.config).toEqual(aliyunEmailConnectorConfig);
|
|
|
|
|
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({
|
|
|
|
id: facebookConnectorId,
|
|
|
|
config: facebookConnectorConfig,
|
|
|
|
enabled: true,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
|
|
|
id: aliyunSmsConnectorId,
|
|
|
|
config: aliyunSmsConnectorConfig,
|
|
|
|
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({
|
|
|
|
id: aliyunEmailConnectorId,
|
|
|
|
config: aliyunEmailConnectorConfig,
|
|
|
|
enabled: false,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
2022-07-26 03:12:34 -05:00
|
|
|
id: mockEmailConnectorId,
|
|
|
|
config: mockEmailConnectorConfig,
|
2022-07-23 00:04:39 -05:00
|
|
|
enabled: false,
|
|
|
|
}),
|
|
|
|
])
|
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
|
|
|
});
|
2022-07-26 22:33:32 -05:00
|
|
|
|
|
|
|
// Next up: refactor connectors.test.ts
|