mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-21 22:59:07 +00:00
Refactor Project and User middleware tests; update method names for clarity and improve mock implementations
This commit is contained in:
parent
bf01322fbd
commit
a10a83ad32
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user