0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(test): inline unnecessary functions for testing connector set-up flow (#1698)

This commit is contained in:
IceHe.Life 2022-07-29 18:43:10 +08:00 committed by GitHub
parent c76a0b8430
commit 71b8807404
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,16 +2,16 @@ import { ConnectorType } from '@logto/schemas';
import { HTTPError } from 'got';
import {
facebookConnectorId,
facebookConnectorConfig,
aliyunSmsConnectorId,
aliyunSmsConnectorConfig,
aliyunEmailConnectorId,
aliyunEmailConnectorConfig,
mockSmsConnectorId,
mockSmsConnectorConfig,
mockEmailConnectorId,
aliyunEmailConnectorId,
aliyunSmsConnectorConfig,
aliyunSmsConnectorId,
facebookConnectorConfig,
facebookConnectorId,
mockEmailConnectorConfig,
mockEmailConnectorId,
mockSmsConnectorConfig,
mockSmsConnectorId,
} from '@/__mocks__/connectors-mock';
import {
disableConnector,
@ -23,38 +23,6 @@ import {
updateConnectorConfig,
} from '@/api/connector';
const updateConfigAndEnableConnector = async (id: string, config: Record<string, unknown>) => {
const updatedConnector = await updateConnectorConfig(id, config);
expect(updatedConnector.config).toEqual(config);
const enabledConnector = await enableConnector(id);
expect(enabledConnector.enabled).toBeTruthy();
};
const setUpConnector = async (id: string, config: Record<string, unknown>) => {
await updateConfigAndEnableConnector(id, config);
// 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);
};
const changeToAnotherConnector = async (
id: string,
config: Record<string, unknown>,
connectorType: ConnectorType.SMS | ConnectorType.Email
) => {
await updateConfigAndEnableConnector(id, config);
// 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 === connectorType && connector.enabled
);
expect(enabledConnectors.length).toEqual(1);
expect(enabledConnectors[0]?.id).toEqual(id);
};
/*
* We'd better only use mock connectors in integration tests.
* Since we will refactor connectors soon, keep using some real connectors
@ -74,15 +42,23 @@ test('connector set-up flow', async () => {
/*
* Set up social/SMS/email connectors
*/
await expect(
Promise.all(
[
{ id: facebookConnectorId, config: facebookConnectorConfig },
{ id: aliyunSmsConnectorId, config: aliyunSmsConnectorConfig },
{ id: aliyunEmailConnectorId, config: aliyunEmailConnectorConfig },
].map(async ({ id, config }) => setUpConnector(id, config))
)
).resolves.not.toThrow();
await Promise.all(
[
{ id: facebookConnectorId, config: facebookConnectorConfig },
{ id: aliyunSmsConnectorId, config: aliyunSmsConnectorConfig },
{ id: aliyunEmailConnectorId, config: aliyunEmailConnectorConfig },
].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);
})
);
/*
* It should update the connector config successfully when it is valid; otherwise, it should fail.
@ -99,12 +75,25 @@ test('connector set-up flow', async () => {
/*
* Change to another SMS/Email connector
*/
await expect(
Promise.all([
changeToAnotherConnector(mockSmsConnectorId, mockSmsConnectorConfig, ConnectorType.SMS),
changeToAnotherConnector(mockEmailConnectorId, mockEmailConnectorConfig, ConnectorType.Email),
])
).resolves.not.toThrow();
await Promise.all(
[
{ id: mockSmsConnectorId, config: mockSmsConnectorConfig, type: ConnectorType.SMS },
{ 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);
})
);
/*
* Delete (i.e. disable) a connector