diff --git a/packages/core/src/routes/connector.ts b/packages/core/src/routes/connector.ts index a7ed428ff..b1b99af43 100644 --- a/packages/core/src/routes/connector.ts +++ b/packages/core/src/routes/connector.ts @@ -208,12 +208,14 @@ export default function connectorRoutes(router: T) { const { type, validateConfig, metadata: originalMetadata } = await getLogtoConnectorById(id); assertThat( - originalMetadata.isStandard !== true || metadata?.target === originalMetadata.target, + originalMetadata.isStandard !== true || + !metadata || + metadata.target === originalMetadata.target, 'connector.can_not_modify_target' ); assertThat( - originalMetadata.isStandard === true || metadata === undefined, + originalMetadata.isStandard === true || !metadata, 'connector.cannot_overwrite_metadata_for_non_standard_connector' ); diff --git a/packages/core/src/routes/connector.update.test.ts b/packages/core/src/routes/connector.update.test.ts index e91c8af84..3a46d022d 100644 --- a/packages/core/src/routes/connector.update.test.ts +++ b/packages/core/src/routes/connector.update.test.ts @@ -108,7 +108,54 @@ describe('connector PATCH routes', () => { expect(response).toHaveProperty('statusCode', 400); }); - it('successfully updates connector configs', async () => { + it('throws when updates non-standard connector metadata', async () => { + getLogtoConnectors.mockResolvedValue([ + { + dbEntry: mockConnector, + metadata: { ...mockMetadata }, + type: ConnectorType.Social, + ...mockLogtoConnector, + }, + ]); + const response = await connectorRequest.patch('/connectors/id').send({ + metadata: { + target: 'connector', + name: { en: 'connector_name', fr: 'connector_name' }, + logo: 'new_logo.png', + }, + }); + expect(response).toHaveProperty('statusCode', 400); + }); + + it('successfully updates connector config', async () => { + getLogtoConnectors.mockResolvedValue([ + { + dbEntry: mockConnector, + metadata: { ...mockMetadata, isStandard: true }, + type: ConnectorType.Social, + ...mockLogtoConnector, + }, + ]); + updateConnector.mockResolvedValueOnce({ + ...mockConnector, + config: { cliend_id: 'client_id', client_secret: 'client_secret' }, + }); + const response = await connectorRequest.patch('/connectors/id').send({ + config: { cliend_id: 'client_id', client_secret: 'client_secret' }, + }); + expect(response).toHaveProperty('statusCode', 200); + expect(updateConnector).toHaveBeenCalledWith( + expect.objectContaining({ + where: { id: 'id' }, + set: { + config: { cliend_id: 'client_id', client_secret: 'client_secret' }, + }, + jsonbMode: 'replace', + }) + ); + }); + + it('successfully updates connector config and metadata', async () => { getLogtoConnectors.mockResolvedValue([ { dbEntry: mockConnector, diff --git a/packages/integration-tests/src/tests/api/connector.test.ts b/packages/integration-tests/src/tests/api/connector.test.ts index 33ad7b12d..ea2d711ff 100644 --- a/packages/integration-tests/src/tests/api/connector.test.ts +++ b/packages/integration-tests/src/tests/api/connector.test.ts @@ -77,9 +77,7 @@ test('connector set-up flow', async () => { connectorId: mockStandardEmailConnectorId, metadata: { target: 'mock-standard-mail' }, }); - await updateConnectorConfig(id, mockStandardEmailConnectorConfig, { - target: 'mock-standard-mail', - }); + await updateConnectorConfig(id, mockStandardEmailConnectorConfig); connectorIdMap.set(mockStandardEmailConnectorId, id); const currentConnectors = await listConnectors(); expect(