Skip to content

Commit

Permalink
Add platform tests (#192)
Browse files Browse the repository at this point in the history
* Add platform tests

* Fix tests

* Some fixes
  • Loading branch information
imsitnikov authored Oct 7, 2024
1 parent e998b80 commit d5d08aa
Show file tree
Hide file tree
Showing 68 changed files with 8,096 additions and 179 deletions.
2 changes: 2 additions & 0 deletions api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export {
RETURN_FAVORITES_COLUMNS,
RETURN_NAVIGATION_COLUMNS,
INTER_TENANT_GET_ENTRIES_SCHEMA,
AUTHORIZATION_HEADER,
DL_AUTH_HEADER_KEY,
} from '../src/const';
1 change: 1 addition & 0 deletions api/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export {
WorkbookRole,
Permissions as WorkbookPermissions,
} from '../src/entities/workbook';
export {getMockedOperation} from '../src/entities/utils';
2 changes: 1 addition & 1 deletion api/tests/auth.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from '../../src/tests/int/auth';
export * from '../../src/tests/int/env/platform/auth';
2 changes: 1 addition & 1 deletion api/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from '../../src/tests/int/helpers';
export * from '../../src/tests/int/env/platform/helpers';
1 change: 1 addition & 0 deletions api/tests/mocks/auth-zitadel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../../src/tests/int/mocks/auth-zitadel';
2 changes: 1 addition & 1 deletion api/tests/roles.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from '../../src/tests/int/roles';
export * from '../../src/tests/int/env/platform/roles';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"dev": "node ./scripts/setup-dev-env.js opensource && npm run clear:dist && APP_DEV_MODE=1 tsc-watch --onSuccess 'node dist/server'",
"test:unit": "jest -c './jest/unit/jest.config.js'",
"build:test:unit": "npm run build && npm run test:unit",
"test:int": "ENV=int-testing node ./scripts/setup-dev-env.js opensource && JEST_TESTCONTAINERS_CONFIG_PATH='./jest/int/testcontainers-config.js' NODE_TLS_REJECT_UNAUTHORIZED=0 jest -c './jest/int/jest.config.js' --testMatch '**/dist/server/tests/int/suites/**/(*.)+(test|spec).[tj]s' --detectOpenHandles",
"test:int": "ENV=int-testing node ./scripts/setup-dev-env.js opensource && JEST_TESTCONTAINERS_CONFIG_PATH='./jest/int/testcontainers-config.js' NODE_TLS_REJECT_UNAUTHORIZED=0 jest -c './jest/int/jest.config.js' --testMatch '**/dist/server/tests/int/env/opensource/suites/**/(*.)+(test|spec).[tj]s' --detectOpenHandles",
"build:test:int": "npm run build && npm run test:int",
"test": "npm run build && npm run test:unit && npm run test:int",
"db:migrate": "node dist/server/db/scripts/migrate.js",
Expand Down
1 change: 1 addition & 0 deletions src/components/middlewares/auth-zitadel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const authZitadel = async (req: Request, res: Response, next: NextFunctio
r2.active &&
(r2.username === ZitadelServiceUser.charts || r2.username === ZitadelServiceUser.bi)
) {
res.locals.userToken = authToken;
res.locals.userId = r1.userId;
res.locals.login = r1.username;
res.locals.serviceUser = r2.username;
Expand Down
2 changes: 2 additions & 0 deletions src/components/middlewares/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const ctx = async (req: Request, res: Response, next: NextFunction) => {
const {
tenantId,
workbookId,
userToken,
userId,
login,
isPrivateRoute = false,
Expand All @@ -26,6 +27,7 @@ export const ctx = async (req: Request, res: Response, next: NextFunction) => {
requestId: req.id,
tenantId,
workbookId,
userToken,
user,
isPrivateRoute,
dlContext,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/registry/common/entities/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {CollectionConstructor, CollectionInstance} from './types';
import {CollectionPermission, Permissions} from '../../../../entities/collection/types';
import {US_ERRORS} from '../../../../const';
import {ZitadelUserRole} from '../../../../types/zitadel';
import {getMockedOperation} from '../utils';
import {getMockedOperation} from '../../../../entities/utils';
import Utils from '../../../../utils';

export const Collection: CollectionConstructor = class Collection implements CollectionInstance {
Expand Down
2 changes: 1 addition & 1 deletion src/registry/common/entities/workbook/workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {WorkbookConstructor, WorkbookInstance} from './types';
import {Permissions, WorkbookPermission} from '../../../../entities/workbook/types';
import {US_ERRORS} from '../../../../const';
import {ZitadelUserRole} from '../../../../types/zitadel';
import {getMockedOperation} from '../utils';
import {getMockedOperation} from '../../../../entities/utils';
import Utils from '../../../../utils';

export const Workbook: WorkbookConstructor<WorkbookInstance> = class Workbook
Expand Down
81 changes: 6 additions & 75 deletions src/tests/int/auth.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import request from 'supertest';
import {
AUTHORIZATION_HEADER,
DL_AUTH_HEADER_KEY,
US_ERRORS,
US_MASTER_TOKEN_HEADER,
} from '../../const';
import {testUserId, testUserLogin} from './constants';
import {OpensourceRole} from './roles';
import {US_ERRORS, US_MASTER_TOKEN_HEADER} from '../../const';
import usApp from '../..';
import {ZitadelUserRole} from '../../types/zitadel';
import {CollectionPermission} from '../../entities/collection/types';
import {WorkbookPermission} from '../../entities/workbook/types';
import {ResourceType} from '../../entities/types';

export type CommonAuthArgs = {
userId?: string;
login?: string;
};

export {US_ERRORS};

Expand All @@ -21,69 +15,6 @@ export const appConfig = usApp.config;
export const testTenantId = 'common';
export const testProjectId = null;

export const getCollectionBinding = (
collectionId: string,
permission: `${CollectionPermission}`,
) => {
return {
id: collectionId,
type: ResourceType.Collection as const,
permission,
};
};

export const getWorkbookBinding = (workbookId: string, permission: `${WorkbookPermission}`) => {
return {
id: workbookId,
type: ResourceType.Workbook as const,
permission,
};
};

export type AccessBinding = ReturnType<typeof getCollectionBinding | typeof getWorkbookBinding>;

export type AuthArgs = {
userId?: string;
login?: string;
role?: OpensourceRole;
accessBindings?: AccessBinding[];
};

export const auth = (req: request.Test, args: AuthArgs = {}) => {
const {
userId = testUserId,
login = testUserLogin,
role = OpensourceRole.Viewer,
accessBindings = [],
} = args;

let zitadelRole: ZitadelUserRole;

switch (role) {
case OpensourceRole.Admin:
zitadelRole = ZitadelUserRole.Admin;
break;
case OpensourceRole.Editor:
zitadelRole = ZitadelUserRole.Editor;
break;
default:
zitadelRole = ZitadelUserRole.Viewer;
break;
}

req.set(
AUTHORIZATION_HEADER,
`${DL_AUTH_HEADER_KEY} ${JSON.stringify({
userId,
login,
role: zitadelRole,
accessBindings,
})}`,
);

return req;
};

export const authMasterToken = (req: request.Test) => {
const token = process.env.MASTER_TOKEN ?? '';
req.set(US_MASTER_TOKEN_HEADER, token);
Expand Down
2 changes: 0 additions & 2 deletions src/tests/int/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ export const testUserId = 'test-user-id';
export const testUserLogin = 'test-user-login';

export const testOtherUserId = 'test-other-user-id';

export const ZITADEL_USER_ROLE_HEADER = 'zitadel-user-role';
41 changes: 41 additions & 0 deletions src/tests/int/env/opensource/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import request from 'supertest';
import type {CommonAuthArgs} from '../../auth';
import {testUserId, testUserLogin} from '../../constants';
import {AUTHORIZATION_HEADER, DL_AUTH_HEADER_KEY} from '../../../../const';
import {OpensourceRole} from './roles';
import {ZitadelUserRole} from '../../../../types/zitadel';

export {US_ERRORS, app, appConfig, testTenantId, testProjectId, authMasterToken} from '../../auth';

export type AuthArgs = CommonAuthArgs & {
role?: OpensourceRole;
};

export const auth = (req: request.Test, args: AuthArgs = {}) => {
const {userId = testUserId, login = testUserLogin, role = OpensourceRole.Viewer} = args;

let zitadelRole: ZitadelUserRole;

switch (role) {
case OpensourceRole.Admin:
zitadelRole = ZitadelUserRole.Admin;
break;
case OpensourceRole.Editor:
zitadelRole = ZitadelUserRole.Editor;
break;
default:
zitadelRole = ZitadelUserRole.Viewer;
break;
}

req.set(
AUTHORIZATION_HEADER,
`${DL_AUTH_HEADER_KEY} ${JSON.stringify({
userId,
login,
role: zitadelRole,
})}`,
);

return req;
};
Loading

0 comments on commit d5d08aa

Please sign in to comment.