Skip to content

Commit

Permalink
Refactor Project and User middleware tests; update method names for c…
Browse files Browse the repository at this point in the history
…larity and improve mock implementations
  • Loading branch information
simlarsen committed Sep 26, 2024
1 parent bf01322 commit a10a83a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Common/Tests/Server/Middleware/ProjectAuthorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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));

Expand All @@ -272,7 +273,7 @@ describe("ProjectMiddleware", () => {
{} as UserTenantAccessPermission;
jest.spyOn(ApiKeyService, "findOneBy").mockResolvedValue(mockedApiModel);
const spyGetApiTenantAccessPermission: jest.SpyInstance = getJestSpyOn(
AccessTokenService,
APIKeyAccessPermission,
"getApiTenantAccessPermission",
).mockResolvedValue(mockedUserTenantAccessPermission);

Expand Down
10 changes: 5 additions & 5 deletions Common/Tests/Server/Middleware/UserAuthorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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);
Expand All @@ -304,7 +304,7 @@ describe("UserMiddleware", () => {

const spyGetAccessToken: jest.SpyInstance = getJestSpyOn(
UserMiddleware,
"getAccessToken",
"getAccessTokenFromExpressRequest",
);

await UserMiddleware.getUserMiddleware(req, res, next);
Expand All @@ -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);
Expand Down

0 comments on commit a10a83a

Please sign in to comment.