mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
feat(core): support integration test env config (#1619)
This commit is contained in:
parent
4ce6bd8cf5
commit
708523ed52
4 changed files with 43 additions and 5 deletions
3
.github/workflows/integration-test.yml
vendored
3
.github/workflows/integration-test.yml
vendored
|
@ -60,6 +60,7 @@ jobs:
|
||||||
mv tests /tmp/tests
|
mv tests /tmp/tests
|
||||||
cd /tmp/tests
|
cd /tmp/tests
|
||||||
pnpm i
|
pnpm i
|
||||||
|
pnpm prepack
|
||||||
|
|
||||||
# Setup environment
|
# Setup environment
|
||||||
- name: Start Postgres (Linux)
|
- name: Start Postgres (Linux)
|
||||||
|
@ -89,6 +90,7 @@ jobs:
|
||||||
run: node . --from-root --all-yes &
|
run: node . --from-root --all-yes &
|
||||||
working-directory: logto/packages/core
|
working-directory: logto/packages/core
|
||||||
env:
|
env:
|
||||||
|
INTEGRATION_TEST: true
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DB_URL_DEFAULT: postgres://postgres:postgres@localhost:5432
|
DB_URL_DEFAULT: postgres://postgres:postgres@localhost:5432
|
||||||
|
|
||||||
|
@ -101,5 +103,4 @@ jobs:
|
||||||
cd /tmp/tests/packages/integration-tests
|
cd /tmp/tests/packages/integration-tests
|
||||||
pnpm start
|
pnpm start
|
||||||
env:
|
env:
|
||||||
NODE_ENV: integration-test
|
|
||||||
LOGTO_URL: http://localhost:3001
|
LOGTO_URL: http://localhost:3001
|
||||||
|
|
|
@ -18,6 +18,7 @@ export enum MountedApps {
|
||||||
const loadEnvValues = async () => {
|
const loadEnvValues = async () => {
|
||||||
const isProduction = getEnv('NODE_ENV') === 'production';
|
const isProduction = getEnv('NODE_ENV') === 'production';
|
||||||
const isTest = getEnv('NODE_ENV') === 'test';
|
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 isHttpsEnabled = Boolean(process.env.HTTPS_CERT_PATH && process.env.HTTPS_KEY_PATH);
|
||||||
const port = Number(getEnv('PORT', '3001'));
|
const port = Number(getEnv('PORT', '3001'));
|
||||||
const localhostUrl = `${isHttpsEnabled ? 'https' : 'http'}://localhost:${port}`;
|
const localhostUrl = `${isHttpsEnabled ? 'https' : 'http'}://localhost:${port}`;
|
||||||
|
@ -25,6 +26,7 @@ const loadEnvValues = async () => {
|
||||||
|
|
||||||
return Object.freeze({
|
return Object.freeze({
|
||||||
isTest,
|
isTest,
|
||||||
|
isIntegrationTest,
|
||||||
isProduction,
|
isProduction,
|
||||||
isHttpsEnabled,
|
isHttpsEnabled,
|
||||||
httpsCert: process.env.HTTPS_CERT_PATH,
|
httpsCert: process.env.HTTPS_CERT_PATH,
|
||||||
|
|
|
@ -44,7 +44,7 @@ describe('koaAuth middleware', () => {
|
||||||
jest.resetModules();
|
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
|
const spy = jest
|
||||||
.spyOn(envSet, 'values', 'get')
|
.spyOn(envSet, 'values', 'get')
|
||||||
.mockReturnValue({ ...envSet.values, developmentUserId: 'foo' });
|
.mockReturnValue({ ...envSet.values, developmentUserId: 'foo' });
|
||||||
|
@ -55,7 +55,7 @@ describe('koaAuth middleware', () => {
|
||||||
spy.mockRestore();
|
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 = {
|
const mockCtx = {
|
||||||
...ctx,
|
...ctx,
|
||||||
request: {
|
request: {
|
||||||
|
@ -68,6 +68,41 @@ describe('koaAuth middleware', () => {
|
||||||
expect(mockCtx.auth).toEqual('foo');
|
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 () => {
|
it('should set user auth with given sub returned from accessToken', async () => {
|
||||||
ctx.request = {
|
ctx.request = {
|
||||||
...ctx.request,
|
...ctx.request,
|
||||||
|
|
|
@ -40,10 +40,10 @@ type UserInfo = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const getUserInfoFromRequest = async (request: Request): Promise<UserInfo> => {
|
const getUserInfoFromRequest = async (request: Request): Promise<UserInfo> => {
|
||||||
const { isProduction, developmentUserId, oidc } = envSet.values;
|
const { isProduction, isIntegrationTest, developmentUserId, oidc } = envSet.values;
|
||||||
const userId = developmentUserId || request.headers['development-user-id']?.toString();
|
const userId = developmentUserId || request.headers['development-user-id']?.toString();
|
||||||
|
|
||||||
if (!isProduction && userId) {
|
if ((!isProduction || isIntegrationTest) && userId) {
|
||||||
return { sub: userId, roleNames: [UserRole.Admin] };
|
return { sub: userId, roleNames: [UserRole.Admin] };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue