From d48b725f6a392acb757ba0fa3d0804e69d8673f3 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 27 Sep 2024 14:21:47 +0100 Subject: [PATCH] Refactor UserAuthorization and Realtime; update mock implementation and add validation for tenantId --- Common/Tests/Server/Middleware/UserAuthorization.test.ts | 3 ++- Common/UI/Utils/Realtime.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Common/Tests/Server/Middleware/UserAuthorization.test.ts b/Common/Tests/Server/Middleware/UserAuthorization.test.ts index 7445320b94..9060dfa87f 100644 --- a/Common/Tests/Server/Middleware/UserAuthorization.test.ts +++ b/Common/Tests/Server/Middleware/UserAuthorization.test.ts @@ -34,6 +34,7 @@ import { } from "@jest/globals"; import { getJestSpyOn } from "Common/Tests/Spy"; import getJestMockFunction from "Common/Tests/MockType"; +import UserPermissionUtil from "../../../Server/Utils/UserPermission/UserPermission"; jest.mock("../../../Server/Utils/Logger"); jest.mock("../../../Server/Middleware/ProjectAuthorization"); @@ -718,7 +719,7 @@ describe("UserMiddleware", () => { const spyGetDefaultUserTenantAccessPermission: jest.SpyInstance = getJestSpyOn( - AccessTokenService, + UserPermissionUtil, "getDefaultUserTenantAccessPermission", ).mockReturnValueOnce(mockedUserTenantAccessPermission); diff --git a/Common/UI/Utils/Realtime.ts b/Common/UI/Utils/Realtime.ts index b27cfb27e3..306d20c2a2 100644 --- a/Common/UI/Utils/Realtime.ts +++ b/Common/UI/Utils/Realtime.ts @@ -11,6 +11,7 @@ import SocketIO, { Socket } from "socket.io-client"; import ModelEventType from "../../Types/Realtime/ModelEventType"; import ListenToModelEventJSON from "../../Types/Realtime/ListenToModelEventJSON"; import EventName from "../../Types/Realtime/EventName"; +import BadDataException from "../../Types/Exception/BadDataException"; export interface ListenToModelEvent< Model extends AnalyticsBaseModel | BaseModel, @@ -42,6 +43,12 @@ export default abstract class Realtime { this.init(); } + if (!listenToModelEvent.tenantId) { + throw new BadDataException( + "TenantId is required to listen to model event.", + ); + } + const listenToModelEventJSON: ListenToModelEventJSON = { eventType: listenToModelEvent.eventType, modelType: DatabaseType.Database,