diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index aaff81270..601b43778 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -60,6 +60,7 @@ jobs: mv tests /tmp/tests cd /tmp/tests pnpm i + pnpm prepack # Setup environment - name: Start Postgres (Linux) @@ -89,6 +90,7 @@ jobs: run: node . --from-root --all-yes & working-directory: logto/packages/core env: + INTEGRATION_TEST: true NODE_ENV: production DB_URL_DEFAULT: postgres://postgres:postgres@localhost:5432 @@ -101,5 +103,4 @@ jobs: cd /tmp/tests/packages/integration-tests pnpm start env: - NODE_ENV: integration-test LOGTO_URL: http://localhost:3001 diff --git a/packages/core/src/env-set/index.ts b/packages/core/src/env-set/index.ts index 538fbea43..42afb4395 100644 --- a/packages/core/src/env-set/index.ts +++ b/packages/core/src/env-set/index.ts @@ -18,6 +18,7 @@ export enum MountedApps { const loadEnvValues = async () => { const isProduction = getEnv('NODE_ENV') === 'production'; const isTest = getEnv('NODE_ENV') === 'test'; + const isIntegrationTest = isTrue(getEnv('INTEGRATION_TEST')); const isHttpsEnabled = Boolean(process.env.HTTPS_CERT_PATH && process.env.HTTPS_KEY_PATH); const port = Number(getEnv('PORT', '3001')); const localhostUrl = `${isHttpsEnabled ? 'https' : 'http'}://localhost:${port}`; @@ -25,6 +26,7 @@ const loadEnvValues = async () => { return Object.freeze({ isTest, + isIntegrationTest, isProduction, isHttpsEnabled, httpsCert: process.env.HTTPS_CERT_PATH, diff --git a/packages/core/src/middleware/koa-auth.test.ts b/packages/core/src/middleware/koa-auth.test.ts index 5a0904557..fd5b01f33 100644 --- a/packages/core/src/middleware/koa-auth.test.ts +++ b/packages/core/src/middleware/koa-auth.test.ts @@ -44,7 +44,7 @@ describe('koaAuth middleware', () => { jest.resetModules(); }); - it('should read DEVELOPMENT_USER_ID from env variable first if not production', async () => { + it('should read DEVELOPMENT_USER_ID from env variable first if not production and not integration test', async () => { const spy = jest .spyOn(envSet, 'values', 'get') .mockReturnValue({ ...envSet.values, developmentUserId: 'foo' }); @@ -55,7 +55,7 @@ describe('koaAuth middleware', () => { spy.mockRestore(); }); - it('should read `development-user-id` from headers if not production', async () => { + it('should read `development-user-id` from headers if not production and not integration test', async () => { const mockCtx = { ...ctx, request: { @@ -68,6 +68,41 @@ describe('koaAuth middleware', () => { expect(mockCtx.auth).toEqual('foo'); }); + it('should read DEVELOPMENT_USER_ID from env variable first if is in production and integration test', async () => { + const spy = jest.spyOn(envSet, 'values', 'get').mockReturnValue({ + ...envSet.values, + developmentUserId: 'foo', + isProduction: true, + isIntegrationTest: true, + }); + + await koaAuth()(ctx, next); + expect(ctx.auth).toEqual('foo'); + + spy.mockRestore(); + }); + + it('should read `development-user-id` from headers if is in production and integration test', async () => { + const spy = jest.spyOn(envSet, 'values', 'get').mockReturnValue({ + ...envSet.values, + isProduction: true, + isIntegrationTest: true, + }); + + const mockCtx = { + ...ctx, + request: { + ...ctx.request, + headers: { ...ctx.request.headers, 'development-user-id': 'foo' }, + }, + }; + + await koaAuth()(mockCtx, next); + expect(mockCtx.auth).toEqual('foo'); + + spy.mockRestore(); + }); + it('should set user auth with given sub returned from accessToken', async () => { ctx.request = { ...ctx.request, diff --git a/packages/core/src/middleware/koa-auth.ts b/packages/core/src/middleware/koa-auth.ts index 9014f8677..9d745adf0 100644 --- a/packages/core/src/middleware/koa-auth.ts +++ b/packages/core/src/middleware/koa-auth.ts @@ -40,10 +40,10 @@ type UserInfo = { }; const getUserInfoFromRequest = async (request: Request): Promise => { - const { isProduction, developmentUserId, oidc } = envSet.values; + const { isProduction, isIntegrationTest, developmentUserId, oidc } = envSet.values; const userId = developmentUserId || request.headers['development-user-id']?.toString(); - if (!isProduction && userId) { + if ((!isProduction || isIntegrationTest) && userId) { return { sub: userId, roleNames: [UserRole.Admin] }; }