0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-17 22:31:28 -05:00

feat(core): support integration test env config (#1619)

This commit is contained in:
Xiao Yijun 2022-07-20 14:21:13 +08:00 committed by GitHub
parent 4ce6bd8cf5
commit 708523ed52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 5 deletions

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -40,10 +40,10 @@ type 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();
if (!isProduction && userId) {
if ((!isProduction || isIntegrationTest) && userId) {
return { sub: userId, roleNames: [UserRole.Admin] };
}