diff --git a/packages/core/jest.config.ts b/packages/core/jest.config.ts index 739dc928c..0a8c2a835 100644 --- a/packages/core/jest.config.ts +++ b/packages/core/jest.config.ts @@ -3,7 +3,6 @@ import { merge, Config } from '@silverhand/jest-config'; const config: Config.InitialOptions = merge({ testPathIgnorePatterns: ['/core/connectors/'], setupFilesAfterEnv: ['jest-matcher-specific-error', './jest.setup.ts'], - globalSetup: './jest.global-setup.ts', }); export default config; diff --git a/packages/core/jest.global-setup.ts b/packages/core/jest.global-setup.ts deleted file mode 100644 index ded7947fe..000000000 --- a/packages/core/jest.global-setup.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Generate private key for tests - */ -import { generateKeyPairSync } from 'crypto'; -import { writeFileSync } from 'fs'; - -export const privateKeyPath = 'oidc-private-key.test.pem'; - -const globalSetup = () => { - const { privateKey } = generateKeyPairSync('rsa', { - modulusLength: 4096, - publicKeyEncoding: { - type: 'spki', - format: 'pem', - }, - privateKeyEncoding: { - type: 'pkcs8', - format: 'pem', - }, - }); - - writeFileSync(privateKeyPath, privateKey); -}; - -export default globalSetup; diff --git a/packages/core/jest.setup.ts b/packages/core/jest.setup.ts index ead6df860..fc6b478c4 100644 --- a/packages/core/jest.setup.ts +++ b/packages/core/jest.setup.ts @@ -4,14 +4,10 @@ import envSet from '@/env-set'; -import { privateKeyPath } from './jest.global-setup'; +jest.mock('@/lib/logto-config'); +jest.mock('@/env-set/check-alteration-state'); // eslint-disable-next-line unicorn/prefer-top-level-await (async () => { - process.env = { - ...process.env, - OIDC_PRIVATE_KEY_PATHS: privateKeyPath, - OIDC_COOKIE_KEYS: '["LOGTOSEKRIT1"]', - }; await envSet.load(); })(); diff --git a/packages/core/src/env-set/__mocks__/check-alteration-state.ts b/packages/core/src/env-set/__mocks__/check-alteration-state.ts new file mode 100644 index 000000000..a6b386678 --- /dev/null +++ b/packages/core/src/env-set/__mocks__/check-alteration-state.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line @typescript-eslint/no-empty-function +export const checkAlterationState = async () => {}; diff --git a/packages/core/src/lib/__mocks__/logto-config.ts b/packages/core/src/lib/__mocks__/logto-config.ts new file mode 100644 index 000000000..9b5a47459 --- /dev/null +++ b/packages/core/src/lib/__mocks__/logto-config.ts @@ -0,0 +1,21 @@ +import { generateKeyPairSync } from 'crypto'; + +import { LogtoOidcConfigKey, LogtoOidcConfigType } from '@logto/schemas'; + +const { privateKey } = generateKeyPairSync('rsa', { + modulusLength: 4096, + publicKeyEncoding: { + type: 'spki', + format: 'pem', + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem', + }, +}); + +export const getOidcConfigs = async (): Promise => ({ + [LogtoOidcConfigKey.PrivateKeys]: [privateKey], + [LogtoOidcConfigKey.CookieKeys]: ['LOGTOSEKRIT1'], + [LogtoOidcConfigKey.RefreshTokenReuseInterval]: 3, +}); diff --git a/packages/core/src/lib/logto-config.ts b/packages/core/src/lib/logto-config.ts index 583c5ea09..56e516024 100644 --- a/packages/core/src/lib/logto-config.ts +++ b/packages/core/src/lib/logto-config.ts @@ -1,10 +1,12 @@ import { getRowsByKeys } from '@logto/cli/lib/queries/logto-config'; -import { logtoOidcConfigGuard, LogtoOidcConfigKey } from '@logto/schemas'; +import { logtoOidcConfigGuard, LogtoOidcConfigKey, LogtoOidcConfigType } from '@logto/schemas'; import chalk from 'chalk'; import { DatabasePool, DatabaseTransactionConnection } from 'slonik'; import { z, ZodError } from 'zod'; -export const getOidcConfigs = async (pool: DatabasePool | DatabaseTransactionConnection) => { +export const getOidcConfigs = async ( + pool: DatabasePool | DatabaseTransactionConnection +): Promise => { try { const { rows } = await getRowsByKeys(pool, Object.values(LogtoOidcConfigKey)); diff --git a/packages/core/tsconfig.build.json b/packages/core/tsconfig.build.json index 474bcd74a..988844c2c 100644 --- a/packages/core/tsconfig.build.json +++ b/packages/core/tsconfig.build.json @@ -5,6 +5,6 @@ ], "exclude": [ "src/**/*.test.ts", - "src/__mocks__/", + "src/**/__mocks__/", ] }