Refactor Project and User middleware tests; update method names for clarity and improve mock implementations

This commit is contained in:
Simon Larsen 2024-09-26 16:15:07 +01:00
parent bf01322fbd
commit a10a83ad32
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
2 changed files with 8 additions and 7 deletions

View File

@ -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);

View File

@ -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);