fd03040d73
* #23 Fix throw error when file doesn't exist Run all the tests in once without special runner Add positive scenario Move coverage to the backend build workflow
134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
import { resolve as pathResolve } from "path";
|
|
import { v1 as uuidv1 } from "uuid";
|
|
import { FastifyInstance } from "fastify";
|
|
import { TdrivePlatform, TdrivePlatformConfiguration } from "../../../src/core/platform/platform";
|
|
import WebServerAPI from "../../../src/core/platform/services/webserver/provider";
|
|
import { DatabaseServiceAPI } from "../../../src/core/platform/services/database/api";
|
|
import AuthServiceAPI from "../../../src/core/platform/services/auth/provider";
|
|
import { Workspace } from "../../../src/utils/types";
|
|
import { MessageQueueServiceAPI } from "../../../src/core/platform/services/message-queue/api";
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
import config from "config";
|
|
import globalResolver from "../../../src/services/global-resolver";
|
|
import {FileServiceImpl} from "../../../src/services/files/services";
|
|
import StorageAPI from "../../../src/core/platform/services/storage/provider";
|
|
|
|
type TokenPayload = {
|
|
sub: string;
|
|
org?: {
|
|
[companyId: string]: {
|
|
role: string;
|
|
};
|
|
};
|
|
};
|
|
|
|
type User = {
|
|
id: string;
|
|
isWorkspaceModerator?: boolean;
|
|
};
|
|
|
|
export interface TestPlatform {
|
|
currentUser: User;
|
|
platform: TdrivePlatform;
|
|
workspace: Workspace;
|
|
app: FastifyInstance;
|
|
database: DatabaseServiceAPI;
|
|
storage: StorageAPI;
|
|
messageQueue: MessageQueueServiceAPI;
|
|
authService: AuthServiceAPI;
|
|
filesService: FileServiceImpl;
|
|
auth: {
|
|
getJWTToken(payload?: TokenPayload): Promise<string>;
|
|
};
|
|
tearDown(): Promise<void>;
|
|
}
|
|
|
|
export interface TestPlatformConfiguration {
|
|
services: string[];
|
|
}
|
|
|
|
let testPlatform: TestPlatform = null;
|
|
|
|
export async function init(
|
|
testConfig?: TestPlatformConfiguration,
|
|
prePlatformStartCallback?: (fastify: FastifyInstance) => void,
|
|
): Promise<TestPlatform> {
|
|
if (!testPlatform) {
|
|
const configuration: TdrivePlatformConfiguration = {
|
|
services: config.get("services"),
|
|
servicesPath: pathResolve(__dirname, "../../../src/services/"),
|
|
};
|
|
const platform = new TdrivePlatform(configuration);
|
|
await platform.init();
|
|
await globalResolver.doInit(platform);
|
|
|
|
const app = platform.getProvider<WebServerAPI>("webserver").getServer();
|
|
|
|
if (prePlatformStartCallback) {
|
|
prePlatformStartCallback(app);
|
|
}
|
|
|
|
await platform.start();
|
|
|
|
const database = platform.getProvider<DatabaseServiceAPI>("database");
|
|
await database.getConnector().drop();
|
|
const messageQueue = platform.getProvider<MessageQueueServiceAPI>("message-queue");
|
|
const auth = platform.getProvider<AuthServiceAPI>("auth");
|
|
const storage: StorageAPI = platform.getProvider<StorageAPI>("storage");
|
|
|
|
testPlatform = {
|
|
platform,
|
|
app,
|
|
messageQueue,
|
|
database,
|
|
storage,
|
|
workspace: { company_id: "", workspace_id: "" },
|
|
currentUser: { id: "" },
|
|
authService: auth,
|
|
filesService: globalResolver.services.files,
|
|
auth: {
|
|
getJWTToken,
|
|
},
|
|
tearDown,
|
|
};
|
|
}
|
|
|
|
testPlatform.app.server.close();
|
|
|
|
testPlatform.currentUser = { id: uuidv1() };
|
|
testPlatform.workspace = {
|
|
company_id: uuidv1(),
|
|
workspace_id: uuidv1(),
|
|
};
|
|
|
|
testPlatform.app.server.listen(3000);
|
|
//await testPlatform.messageQueue.start();
|
|
|
|
async function getJWTToken(
|
|
payload: TokenPayload = { sub: testPlatform.currentUser.id },
|
|
): Promise<string> {
|
|
if (!payload.sub) {
|
|
payload.sub = testPlatform.currentUser.id;
|
|
}
|
|
|
|
if (testPlatform .currentUser.isWorkspaceModerator) {
|
|
payload.org = {};
|
|
payload.org[testPlatform.workspace.company_id] = {
|
|
role: "",
|
|
};
|
|
}
|
|
|
|
return testPlatform.authService.sign(payload);
|
|
}
|
|
|
|
async function tearDown(): Promise<void> {
|
|
if (testPlatform) {
|
|
testPlatform.app.server.close();
|
|
//await testPlatform.messageQueue.stop();
|
|
}
|
|
}
|
|
|
|
return testPlatform;
|
|
}
|