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"; import {SearchServiceAPI} from "../../../src/core/platform/services/search/api"; import Session from "../../../src/services/console/entities/session"; import EmailPusherAPI from "../../../src/core/platform/services/email-pusher/provider"; import { DocumentsService } from "../../../src/services/documents/services"; type TokenPayload = { sub: string; sid?: string; org?: { [companyId: string]: { role: string; }; }; }; export type User = { id: string; email?: string; first_name?: string; isWorkspaceModerator?: boolean; deleted?: boolean, marked_to_delete?: boolean; delete_process_started_epoch?: number, preferences?: { language?: string; }; }; export interface TestPlatform { currentUser: User; currentSession: string; platform: TdrivePlatform; workspace: Workspace; app: FastifyInstance; database: DatabaseServiceAPI; storage: StorageAPI; emailPusher: EmailPusherAPI; messageQueue: MessageQueueServiceAPI; authService: AuthServiceAPI; filesService: FileServiceImpl; documentService: DocumentsService; auth: { getJWTToken(payload?: TokenPayload): Promise; }; tearDown(): Promise; search: SearchServiceAPI; } export interface TestPlatformConfiguration { services: string[]; } let testPlatform: TestPlatform = null; export async function init( testConfig?: TestPlatformConfiguration, prePlatformStartCallback?: (fastify: FastifyInstance) => void, ): Promise { 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("webserver").getServer(); if (prePlatformStartCallback) { prePlatformStartCallback(app); } await platform.start(); const database = platform.getProvider("database"); await database.getConnector().drop(); const messageQueue = platform.getProvider("message-queue"); const auth = platform.getProvider("auth"); const storage: StorageAPI = platform.getProvider("storage"); const search: SearchServiceAPI = platform.getProvider("search"); const emailPusher: EmailPusherAPI = platform.getProvider("email-pusher"); testPlatform = { platform, app, messageQueue, database, storage, emailPusher, workspace: { company_id: "", workspace_id: "" }, currentUser: { id: "" }, currentSession: uuidv1(), authService: auth, filesService: globalResolver.services.files, documentService: globalResolver.services.documents.documents, auth: { getJWTToken, }, tearDown, search, }; } 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, sid: testPlatform.currentSession }, ): Promise { const sessionRepository = await testPlatform.database.getRepository("session", Session); if (!payload.sub) { payload.sub = testPlatform.currentUser.id; } if (!payload.sid) { payload.sid = testPlatform.currentSession; } let session = (await sessionRepository.find({ sub: payload.sub })).getEntities(); if (session.length == 0) { const session = new Session(); session.sid = payload.sid; session.sub = payload.sub; await sessionRepository.save(session); } if (testPlatform.currentUser.isWorkspaceModerator) { payload.org = {}; payload.org[testPlatform.workspace.company_id] = { role: "", }; } return testPlatform.authService.sign(payload); } async function tearDown(): Promise { if (testPlatform) { testPlatform.app.server.close(); //await testPlatform.messageQueue.stop(); } } return testPlatform; }