-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(api-locking-mechanism): use cases
- Loading branch information
1 parent
123b412
commit b196a50
Showing
5 changed files
with
247 additions
and
5 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/api-locking-mechanism/__tests__/useCase/isEntryLockedUseCase.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/api-locking-mechanism/__tests__/useCase/unlockEntryRequestUseCase.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/api-locking-mechanism/__tests__/useCase/unlockEntryUseCase.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters