From a10a83ad32cce8cfdac8b273c41d7bab42f5ac35 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Thu, 26 Sep 2024 16:15:07 +0100 Subject: [PATCH] Refactor Project and User middleware tests; update method names for clarity and improve mock implementations --- .../Server/Middleware/ProjectAuthorization.test.ts | 5 +++-- .../Tests/Server/Middleware/UserAuthorization.test.ts | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Common/Tests/Server/Middleware/ProjectAuthorization.test.ts b/Common/Tests/Server/Middleware/ProjectAuthorization.test.ts index 7ffd1e8e7f..63af9d8d3b 100644 --- a/Common/Tests/Server/Middleware/ProjectAuthorization.test.ts +++ b/Common/Tests/Server/Middleware/ProjectAuthorization.test.ts @@ -18,6 +18,7 @@ import { describe, expect, afterEach, jest } from "@jest/globals"; import getJestMockFunction from "Common/Tests/MockType"; import { getJestSpyOn } from "Common/Tests/Spy"; import { TestDatabaseMock } from "../TestingUtils/__mocks__/TestDatabase.mock"; +import APIKeyAccessPermission from "../../../Server/Utils/APIKey/AccessPermission"; jest.mock("../../../Server/Services/ApiKeyService"); jest.mock("../../../Server/Services/AccessTokenService"); @@ -250,7 +251,7 @@ describe("ProjectMiddleware", () => { test("should call Response.sendErrorResponse when apiKeyModel is not null but getApiTenantAccessPermission returned null", async () => { jest.spyOn(ApiKeyService, "findOneBy").mockResolvedValue(mockedApiModel); const spyGetApiTenantAccessPermission: jest.SpyInstance = getJestSpyOn( - AccessTokenService, + APIKeyAccessPermission, "getApiTenantAccessPermission", ).mockImplementationOnce(getJestMockFunction().mockResolvedValue(null)); @@ -272,7 +273,7 @@ describe("ProjectMiddleware", () => { {} as UserTenantAccessPermission; jest.spyOn(ApiKeyService, "findOneBy").mockResolvedValue(mockedApiModel); const spyGetApiTenantAccessPermission: jest.SpyInstance = getJestSpyOn( - AccessTokenService, + APIKeyAccessPermission, "getApiTenantAccessPermission", ).mockResolvedValue(mockedUserTenantAccessPermission); diff --git a/Common/Tests/Server/Middleware/UserAuthorization.test.ts b/Common/Tests/Server/Middleware/UserAuthorization.test.ts index 5f957e4010..76529d1dfe 100644 --- a/Common/Tests/Server/Middleware/UserAuthorization.test.ts +++ b/Common/Tests/Server/Middleware/UserAuthorization.test.ts @@ -57,7 +57,7 @@ describe("UserMiddleware", () => { jest.clearAllMocks(); }); - describe("getAccessToken", () => { + describe("getAccessTokenFromExpressRequest", () => { test("should return access token when authorization token is passed in the cookie", () => { const req: ExpressRequest = { cookies: { "user-token": mockedAccessToken }, @@ -281,7 +281,7 @@ describe("UserMiddleware", () => { projectId, ); getJestSpyOn(ProjectMiddleware, "hasApiKey").mockReturnValue(false); - getJestSpyOn(UserMiddleware, "getAccessToken").mockReturnValue( + getJestSpyOn(UserMiddleware, "getAccessTokenFromExpressRequest").mockReturnValue( mockedAccessToken, ); getJestSpyOn(JSONWebToken, "decode").mockReturnValue(jwtTokenData); @@ -304,7 +304,7 @@ describe("UserMiddleware", () => { const spyGetAccessToken: jest.SpyInstance = getJestSpyOn( UserMiddleware, - "getAccessToken", + "getAccessTokenFromExpressRequest", ); await UserMiddleware.getUserMiddleware(req, res, next); @@ -315,10 +315,10 @@ describe("UserMiddleware", () => { expect(spyGetAccessToken).not.toHaveBeenCalled(); }); - test("should call function 'next' and return, when getAccessToken returns a null value", async () => { + test("should call function 'next' and return, when getAccessTokenFromExpressRequest returns a null value", async () => { const spyGetAccessToken: jest.SpyInstance = getJestSpyOn( UserMiddleware, - "getAccessToken", + "getAccessTokenFromExpressRequest", ).mockReturnValueOnce(undefined); await UserMiddleware.getUserMiddleware(req, res, next);