From 57a9cfa6d35e0f6defe882c2542b21dbc71dc455 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Thu, 7 Mar 2024 15:06:50 +0800 Subject: [PATCH] fix(core): fix rebase --- packages/core/src/queries/logto-config.ts | 47 ++++---------- .../core/src/routes/logto-config.openapi.json | 23 +------ packages/core/src/routes/logto-config.test.ts | 9 --- packages/core/src/routes/logto-config.ts | 22 +++---- .../integration-tests/src/api/logto-config.ts | 9 +-- .../src/tests/api/logto-config.test.ts | 65 +++++-------------- 6 files changed, 44 insertions(+), 131 deletions(-) diff --git a/packages/core/src/queries/logto-config.ts b/packages/core/src/queries/logto-config.ts index 4d051bce6..da6aaea3f 100644 --- a/packages/core/src/queries/logto-config.ts +++ b/packages/core/src/queries/logto-config.ts @@ -1,5 +1,5 @@ import { - jwtCustomizerConfigGuard, + type jwtCustomizerConfigGuard, LogtoTenantConfigKey, LogtoConfigs, type AdminConsoleData, @@ -12,9 +12,7 @@ import { import { convertToIdentifiers } from '@logto/shared'; import type { CommonQueryMethods } from 'slonik'; import { sql } from 'slonik'; -import { z } from 'zod'; - -import RequestError from '#src/errors/RequestError/index.js'; +import { type z } from 'zod'; import { DeletionError } from '#src/errors/SlonikError/index.js'; @@ -47,6 +45,17 @@ export const createLogtoConfigQueries = (pool: CommonQueryMethods) => { where ${fields.key} in (${sql.join(keys, sql`,`)}) `); + const deleteRowByKey = async (key: LogtoConfigKey) => { + const { rowCount } = await pool.query(sql` + delete from ${table} + where ${fields.key}=${key} + `); + + if (rowCount < 1) { + throw new DeletionError(LogtoConfigs.table, key); + } + }; + const updateOidcConfigsByKey = async (key: LogtoOidcConfigKey, value: OidcConfigKey[]) => pool.query(sql` update ${table} @@ -71,34 +80,7 @@ export const createLogtoConfigQueries = (pool: CommonQueryMethods) => { ` ); - const getJwtCustomizer = async (key: T) => { - const { rows } = await getRowsByKeys([key]); - - // If the record does not exist (`rows` is empty) - if (rows.length === 0) { - throw new RequestError({ - code: 'entity.not_exists', - name: table, - id: key, - status: 404, - }); - } - - return z.object({ value: jwtCustomizerConfigGuard[key] }).parse(rows[0]); - }; - - const deleteJwtCustomizer = async (key: T) => { - const { rowCount } = await pool.query( - sql` - delete from ${table} - where ${fields.key} = ${key} - ` - ); - - if (rowCount < 1) { - throw new DeletionError(LogtoConfigs.table, key); - } - }; + const deleteJwtCustomizer = async (key: T) => deleteRowByKey(key); return { getAdminConsoleConfig, @@ -107,7 +89,6 @@ export const createLogtoConfigQueries = (pool: CommonQueryMethods) => { getRowsByKeys, updateOidcConfigsByKey, upsertJwtCustomizer, - getJwtCustomizer, deleteJwtCustomizer, }; }; diff --git a/packages/core/src/routes/logto-config.openapi.json b/packages/core/src/routes/logto-config.openapi.json index 983f3cab7..64fd081b7 100644 --- a/packages/core/src/routes/logto-config.openapi.json +++ b/packages/core/src/routes/logto-config.openapi.json @@ -169,33 +169,14 @@ } } }, - "get": { - "summary": "Get JWT customizer", - "description": "Get the JWT customizer for the given token type.", - "parameters": [ - { - "in": "path", - "name": "tokenType", - "description": "The token type to get the JWT customizer for." - } - ], - "responses": { - "200": { - "description": "The JWT customizer." - }, - "404": { - "description": "The JWT customizer does not exist." - } - } - }, "delete": { "summary": "Delete JWT customizer", "description": "Delete the JWT customizer for the given token type.", "parameters": [ { "in": "path", - "name": "tokenType", - "description": "The token type to delete the JWT customizer for." + "name": "tokenTypePath", + "description": "The token type path to delete the JWT customizer for." } ], "responses": { diff --git a/packages/core/src/routes/logto-config.test.ts b/packages/core/src/routes/logto-config.test.ts index d9b146931..4f4eebbf6 100644 --- a/packages/core/src/routes/logto-config.test.ts +++ b/packages/core/src/routes/logto-config.test.ts @@ -277,15 +277,6 @@ describe('configs routes', () => { expect(response.body).toEqual(mockJwtCustomizerConfigForAccessToken.value); }); - it('GET /configs/jwt-customizer/:tokenType should return the record', async () => { - logtoConfigLibraries.getJwtCustomizer.mockResolvedValueOnce( - mockJwtCustomizerConfigForAccessToken - ); - const response = await routeRequester.get('/configs/jwt-customizer/access-token'); - expect(response.status).toEqual(200); - expect(response.body).toEqual(mockJwtCustomizerConfigForAccessToken.value); - }); - it('DELETE /configs/jwt-customizer/:tokenType should delete the record', async () => { const response = await routeRequester.delete('/configs/jwt-customizer/client-credentials'); expect(logtoConfigQueries.deleteJwtCustomizer).toHaveBeenCalledWith( diff --git a/packages/core/src/routes/logto-config.ts b/packages/core/src/routes/logto-config.ts index bb258949a..335bbc0aa 100644 --- a/packages/core/src/routes/logto-config.ts +++ b/packages/core/src/routes/logto-config.ts @@ -81,22 +81,14 @@ const getRedactedOidcKeyResponse = async ( export default function logtoConfigRoutes( ...[router, { queries, logtoConfigs, invalidateCache }]: RouterInitArgs ) { -<<<<<<< HEAD - const { getAdminConsoleConfig, getRowsByKeys, updateAdminConsoleConfig, updateOidcConfigsByKey } = - queries.logtoConfigs; - const { getOidcConfigs, upsertJwtCustomizer, getJwtCustomizer } = logtoConfigs; -======= const { getAdminConsoleConfig, getRowsByKeys, - insertJwtCustomizer, updateAdminConsoleConfig, updateOidcConfigsByKey, - getJwtCustomizer, deleteJwtCustomizer, } = queries.logtoConfigs; - const { getOidcConfigs } = logtoConfigs; ->>>>>>> 8086c9bc6 (feat(core): add GET /configs/jwt-customizer API) + const { getOidcConfigs, upsertJwtCustomizer, getJwtCustomizer } = logtoConfigs; router.get( '/configs/admin-console', @@ -278,19 +270,23 @@ export default function logtoConfigRoutes( ); router.delete( - '/configs/jwt-customizer/:tokenType', + '/configs/jwt-customizer/:tokenTypePath', koaGuard({ params: z.object({ - tokenType: z.nativeEnum(LogtoJwtTokenKeyType), + tokenTypePath: z.nativeEnum(LogtoJwtTokenPath), }), status: [204, 404], }), async (ctx, next) => { const { - params: { tokenType }, + params: { tokenTypePath }, } = ctx.guard; - await deleteJwtCustomizer(getLogtoJwtTokenKey(tokenType)); + await deleteJwtCustomizer( + tokenTypePath === LogtoJwtTokenPath.AccessToken + ? LogtoJwtTokenKey.AccessToken + : LogtoJwtTokenKey.ClientCredentials + ); ctx.status = 204; return next(); } diff --git a/packages/integration-tests/src/api/logto-config.ts b/packages/integration-tests/src/api/logto-config.ts index 44fe3981f..a657c39e6 100644 --- a/packages/integration-tests/src/api/logto-config.ts +++ b/packages/integration-tests/src/api/logto-config.ts @@ -46,10 +46,5 @@ export const getJwtCustomizer = async (keyTypePath: 'access-token' | 'client-cre .get(`configs/jwt-customizer/${keyTypePath}`) .json(); -export const getJwtCustomizer = async (keyType: LogtoJwtTokenKeyType) => - authedAdminApi - .get(`configs/jwt-customizer/${keyType}`) - .json(); - -export const deleteJwtCustomizer = async (keyType: LogtoJwtTokenKeyType) => - authedAdminApi.delete(`configs/jwt-customizer/${keyType}`); +export const deleteJwtCustomizer = async (keyTypePath: 'access-token' | 'client-credentials') => + authedAdminApi.delete(`configs/jwt-customizer/${keyTypePath}`); diff --git a/packages/integration-tests/src/tests/api/logto-config.test.ts b/packages/integration-tests/src/tests/api/logto-config.test.ts index 261f1332c..05aaea519 100644 --- a/packages/integration-tests/src/tests/api/logto-config.test.ts +++ b/packages/integration-tests/src/tests/api/logto-config.test.ts @@ -3,7 +3,6 @@ import { type AdminConsoleData, LogtoOidcConfigKeyType, } from '@logto/schemas'; -import { HTTPError } from 'got'; import { deleteOidcKey, @@ -128,7 +127,7 @@ describe('admin console sign-in experience', () => { expect(privateKeys2[1]?.id).toBe(privateKeys[0]?.id); }); - it('should successfully POST/GET/DELETE a JWT customizer (access token)', async () => { + it('should successfully PUT/GET/DELETE a JWT customizer (access token)', async () => { const accessTokenJwtCustomizerPayload = { script: '', envVars: {}, @@ -140,25 +139,15 @@ describe('admin console sign-in experience', () => { }, }; - const accessToken = await upsertJwtCustomizer('access-token', accessTokenJwtCustomizerPayload); - /** - * The 'access-token' JWT customizer has not been created, as a result: - * - GET request should fail with a 404 error - * - DELETE request should fail with a 404 error - */ await expectRejects(getJwtCustomizer('access-token'), { code: 'entity.not_exists', statusCode: 404, }); - const response = await deleteJwtCustomizer('access-token').catch( - (error: unknown) => error - ); - expect(response instanceof HTTPError && response.response.statusCode === 404).toBe(true); - - const accessToken = await insertJwtCustomizer( - 'access-token', - accessTokenJwtCustomizerPayload - ); + await expectRejects(deleteJwtCustomizer('access-token'), { + code: 'entity.not_found', + statusCode: 404, + }); + const accessToken = await upsertJwtCustomizer('access-token', accessTokenJwtCustomizerPayload); expect(accessToken).toMatchObject(accessTokenJwtCustomizerPayload); const newAccessTokenJwtCustomizerPayload = { ...accessTokenJwtCustomizerPayload, @@ -172,44 +161,30 @@ describe('admin console sign-in experience', () => { await expect(getJwtCustomizer('access-token')).resolves.toMatchObject( newAccessTokenJwtCustomizerPayload ); - - /** - * The 'access-token' JWT customizer has been created, as a result: - * - DELETE request should succeed - * - GET request should fail with a 404 error after the successful DELETE request - */ await expect(deleteJwtCustomizer('access-token')).resolves.not.toThrow(); await expectRejects(getJwtCustomizer('access-token'), { - code: 'entity.not_found', + code: 'entity.not_exists', statusCode: 404, }); }); - it('should successfully POST/GET/DELETE a JWT customizer (client credentials)', async () => { + it('should successfully PUT/GET/DELETE a JWT customizer (client credentials)', async () => { const clientCredentialsJwtCustomizerPayload = { script: '', envVars: {}, contextSample: {}, }; - const clientCredentials = await upsertJwtCustomizer( - 'client-credentials', - /** - * The 'client-credentials' JWT customizer has not been created, as a result: - * - GET request should fail with a 404 error - * - DELETE request should fail with a 404 error - */ await expectRejects(getJwtCustomizer('client-credentials'), { code: 'entity.not_exists', statusCode: 404, }); - const response = await deleteJwtCustomizer(LogtoJwtTokenKeyType.ClientCredentials).catch( - (error: unknown) => error - ); - expect(response instanceof HTTPError && response.response.statusCode === 404).toBe(true); - - const clientCredentials = await insertJwtCustomizer( - LogtoJwtTokenKeyType.ClientCredentials, + await expectRejects(deleteJwtCustomizer('client-credentials'), { + code: 'entity.not_found', + statusCode: 404, + }); + const clientCredentials = await upsertJwtCustomizer( + 'client-credentials', clientCredentialsJwtCustomizerPayload ); expect(clientCredentials).toMatchObject(clientCredentialsJwtCustomizerPayload); @@ -225,15 +200,9 @@ describe('admin console sign-in experience', () => { await expect(getJwtCustomizer('client-credentials')).resolves.toMatchObject( newClientCredentialsJwtCustomizerPayload ); - - /** - * The 'client-credentials' JWT customizer has been created, as a result: - * - DELETE request should succeed - * - GET request should fail with a 404 error after the successful DELETE request - */ - await deleteJwtCustomizer(LogtoJwtTokenKeyType.ClientCredentials); - await expectRejects(getJwtCustomizer(LogtoJwtTokenKeyType.ClientCredentials), { - code: 'entity.not_found', + await expect(deleteJwtCustomizer('client-credentials')).resolves.not.toThrow(); + await expectRejects(getJwtCustomizer('client-credentials'), { + code: 'entity.not_exists', statusCode: 404, }); });