Skip to content

Commit

Permalink
test(api-locking-mechanism): use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Mar 27, 2024
1 parent 123b412 commit b196a50
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { IsEntryLockedUseCase } from "~/useCases/IsEntryLocked/IsEntryLockedUseCase";
import { WebinyError } from "@webiny/error";
import { ILockingMechanismLockRecord } from "~/types";
import { NotFoundError } from "@webiny/handler-graphql";

describe("is entry locked use case", () => {
it("should return false if lock record is not found - object param", async () => {
const useCase = new IsEntryLockedUseCase({
getLockRecordUseCase: {
async execute() {
throw new NotFoundError();
}
}
});

const result = await useCase.execute({
id: "aTestId#0001",
type: "aTestType"
});

expect(result).toBe(false);
});

it("should return false if lock record is not locked", async () => {
const useCase = new IsEntryLockedUseCase({
getLockRecordUseCase: {
async execute() {
return {
lockedOn: new Date("2020-01-01")
} as unknown as ILockingMechanismLockRecord;
}
}
});

const result = await useCase.execute({
id: "aTestId#0001",
type: "aTestType"
});

expect(result).toBe(false);
});

it("should throw an error on error in getLockRecordUseCase", async () => {
expect.assertions(1);

const useCase = new IsEntryLockedUseCase({
getLockRecordUseCase: {
async execute() {
throw new WebinyError("Testing error.", "TESTING_ERROR");
}
}
});

try {
await useCase.execute({
id: "aTestId#0001",
type: "aTestType"
});
} catch (ex) {
expect(ex).toEqual(new WebinyError("Testing error.", "TESTING_ERROR"));
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LockEntryUseCase } from "~/useCases/LockEntryUseCase/LockEntryUseCase";
import { WebinyError } from "@webiny/error";
import { IIsEntryLockedUseCase } from "~/abstractions/IsEntryLocked";
import { ILockingMechanismModelManager } from "~/types";
import { NotFoundError } from "@webiny/handler-graphql";

describe("lock entry use case", () => {
it("should throw an error on isEntryLockedUseCase.execute", async () => {
Expand All @@ -25,4 +26,41 @@ describe("lock entry use case", () => {
expect(ex).toEqual(new WebinyError("Trying out an error", "TRYING_OUT_ERROR", {}));
}
});

it("should throw an error on creating a lock record", async () => {
expect.assertions(1);
const useCase = new LockEntryUseCase({
isEntryLockedUseCase: {
execute: async () => {
throw new NotFoundError();
}
} as unknown as IIsEntryLockedUseCase,
getManager: async () => {
return {
create: async () => {
throw new WebinyError(
"Trying out an error on manager.create.",
"TRYING_OUT_ERROR",
{}
);
}
} as unknown as ILockingMechanismModelManager;
}
});

try {
await useCase.execute({
id: "id1",
type: "aType"
});
} catch (ex) {
expect(ex).toEqual(
new WebinyError(
"Could not lock entry: Trying out an error on manager.create.",
"TRYING_OUT_ERROR",
{}
)
);
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { UnlockEntryRequestUseCase } from "~/useCases/UnlockRequestUseCase/UnlockEntryRequestUseCase";
import {
IGetLockRecordUseCase,
IGetLockRecordUseCaseExecuteParams
} from "~/abstractions/IGetLockRecordUseCase";
import { getSecurityIdentity } from "~tests/helpers/identity";
import { ILockingMechanismModelManager } from "~/types";
import { WebinyError } from "@webiny/error";

describe("unlock entry request use case", () => {
it("should throw an error on missing lock record", async () => {
expect.assertions(1);
const useCase = new UnlockEntryRequestUseCase({
getLockRecordUseCase: {
execute: async () => {
return null;
}
} as unknown as IGetLockRecordUseCase,
getIdentity: getSecurityIdentity,
getManager: async () => {
return {} as unknown as ILockingMechanismModelManager;
}
});

try {
await useCase.execute({ id: "id", type: "type" });
} catch (ex) {
expect(ex).toEqual(
new WebinyError("Entry is not locked.", "ENTRY_NOT_LOCKED", {
id: "id",
type: "type"
})
);
}
});

it("should throw an error if current user did not start the unlock request", async () => {
expect.assertions(1);
const useCase = new UnlockEntryRequestUseCase({
getLockRecordUseCase: {
execute: async () => {
return {
getUnlockRequested() {
return {
createdBy: {
id: "other-user-id"
}
};
}
};
}
} as unknown as IGetLockRecordUseCase,
getIdentity: getSecurityIdentity,
getManager: async () => {
return {} as unknown as ILockingMechanismModelManager;
}
});

try {
await useCase.execute({ id: "id", type: "type" });
} catch (ex) {
expect(ex).toEqual(
new WebinyError("Unlock request already sent.", "UNLOCK_REQUEST_ALREADY_SENT", {
id: "id",
type: "type",
identity: {
id: "other-user-id"
}
})
);
}
});

it("should return the lock record if unlock request was already approved", async () => {
expect.assertions(1);
const useCase = new UnlockEntryRequestUseCase({
getLockRecordUseCase: {
execute: async (params: IGetLockRecordUseCaseExecuteParams) => {
return {
id: typeof params === "object" ? params.id : params,
getUnlockRequested() {
return {
createdBy: getSecurityIdentity()
};
},
getUnlockApproved() {
return {};
},
getUnlockDenied() {
return null;
}
};
}
} as unknown as IGetLockRecordUseCase,
getIdentity: getSecurityIdentity,
getManager: async () => {
return {} as unknown as ILockingMechanismModelManager;
}
});

const result = await useCase.execute({ id: "aTestIdValue#0001", type: "type" });
expect(result.id).toEqual("wby-lm-aTestIdValue");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { UnlockEntryUseCase } from "~/useCases/UnlockEntryUseCase/UnlockEntryUseCase";
import { IGetLockRecordUseCase } from "~/abstractions/IGetLockRecordUseCase";
import { WebinyError } from "@webiny/error";

describe("unlock entry use case", () => {
it("should throw an error on unlocking an entry", async () => {
expect.assertions(1);

const useCase = new UnlockEntryUseCase({
getLockRecordUseCase: {
execute: async () => {
return {};
}
} as unknown as IGetLockRecordUseCase,
async getManager() {
throw new WebinyError("Testing error.", "TESTING_ERROR");
}
});

try {
await useCase.execute({ id: "id", type: "type" });
} catch (ex) {
expect(ex).toEqual(
new WebinyError("Could not unlock entry: Testing error.", "UNLOCK_ENTRY_ERROR")
);
}
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { IIsEntryLockedUseCase } from "~/abstractions/IsEntryLocked";
import {
IIsEntryLockedUseCase,
IIsEntryLockedUseCaseExecuteParams
} from "~/abstractions/IsEntryLocked";
import { IGetLockRecordUseCase } from "~/abstractions/IGetLockRecordUseCase";
import { ILockingMechanismIsLockedParams, ILockingMechanismLockRecord } from "~/types";
import { ILockingMechanismLockRecord } from "~/types";
import { createLockRecordDatabaseId } from "~/utils/lockRecordDatabaseId";
import { NotFoundError } from "@webiny/handler-graphql";

const defaultTimeoutInSeconds = 600;
/**
* In milliseconds.
*/
const getTimeout = () => {
const userDefined = parseInt(process.env.WEBINY_RECORD_LOCK_TIMEOUT || "600");
return (isNaN(userDefined) ? 600 : userDefined) * 1000;
const userDefined = process.env.WEBINY_RECORD_LOCK_TIMEOUT
? parseInt(process.env.WEBINY_RECORD_LOCK_TIMEOUT)
: undefined;
if (!userDefined || isNaN(userDefined) || userDefined <= 0) {
return defaultTimeoutInSeconds * 1000;
}
return userDefined * 1000;
};

export interface IIsEntryLockedParams {
Expand All @@ -23,7 +32,7 @@ export class IsEntryLockedUseCase implements IIsEntryLockedUseCase {
this.getLockRecordUseCase = params.getLockRecordUseCase;
}

public async execute(params: ILockingMechanismIsLockedParams): Promise<boolean> {
public async execute(params: IIsEntryLockedUseCaseExecuteParams): Promise<boolean> {
const id = createLockRecordDatabaseId(params.id);
try {
const result = await this.getLockRecordUseCase.execute(id);
Expand Down

0 comments on commit b196a50

Please sign in to comment.