From 7953392dd0dcb9fc9ce5ad4ab09e6dd34d0487ea Mon Sep 17 00:00:00 2001 From: Prateek Keerthi Date: Fri, 8 Mar 2024 17:01:36 -0500 Subject: [PATCH 1/5] MAt-6744 added check user can delete function --- src/madie-madie-util.tsx | 2 + src/util/useCheckCanDelete.test.ts | 75 ++++++++++++++++++++++++++++++ src/util/useCheckCanDelete.ts | 17 +++++++ src/util/useCheckCanEdit.ts | 2 +- 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/util/useCheckCanDelete.test.ts create mode 100644 src/util/useCheckCanDelete.ts diff --git a/src/madie-madie-util.tsx b/src/madie-madie-util.tsx index efd5fee..c2ba49c 100644 --- a/src/madie-madie-util.tsx +++ b/src/madie-madie-util.tsx @@ -17,6 +17,7 @@ import { default as useTerminologyServiceApi } from "./api/useTerminologyService import { default as useOrganizationApi } from "./api/useOrganizationApi"; import { useDocumentTitle } from "./hooks/useDocumentTitle"; import { default as checkUserCanEdit } from "./util/useCheckCanEdit"; +import { default as checkUserCanDelete } from "./util/useCheckCanDelete"; import { useFeatureFlags } from "./hooks/useFeatureFlags"; import { getOidFromString } from "./util/terminologyUtils"; @@ -33,6 +34,7 @@ export { useOrganizationApi, useDocumentTitle, checkUserCanEdit, + checkUserCanDelete, useFeatureFlags, getOidFromString, }; diff --git a/src/util/useCheckCanDelete.test.ts b/src/util/useCheckCanDelete.test.ts new file mode 100644 index 0000000..96614aa --- /dev/null +++ b/src/util/useCheckCanDelete.test.ts @@ -0,0 +1,75 @@ +import * as React from "react"; +import useOktaTokens from "../hooks/useOktaTokens"; +import useCheckUserCanDelete from "./useCheckCanDelete"; + +const JANE_DOE = "Jane doe"; +const JOHN_DOE = "john doe"; + +jest.mock("../hooks/useOktaTokens", () => + jest.fn(() => ({ + getUserName: jest.fn(), + })) +); + +describe("Check user canEdit", () => { + beforeEach(() => { + jest.resetAllMocks(); + (useOktaTokens as jest.Mock).mockImplementation(() => ({ + getUserName: () => JANE_DOE, + })); + }); + + it("should return true when user name and createdBy are the same", () => { + const canEdit = useCheckUserCanDelete(JANE_DOE, true); + expect(canEdit).toBeTruthy(); + }); + + it("should return true when user name and createdBy are the same", () => { + const canEdit = useCheckUserCanDelete(JANE_DOE, true); + expect(canEdit).toBeTruthy(); + }); + + it("should return false when user name and createdBy are same but it is not draft", () => { + const canEdit = useCheckUserCanDelete(JANE_DOE, false); + expect(canEdit).not.toBeTruthy(); + }); + + // it("should return undefined when user name and createdBy are not the same", () => { + // const canEdit = useCheckUserCanEdit("anotherU$er", true); + // expect(canEdit).not.toBeTruthy(); + // }); + + // it("should return true when measure is shared with the same user", () => { + // const canEdit = useCheckUserCanEdit( + // "anotherU$er", // nosec + // [{ userId: JANE_DOE, roles: ["SHARED_WITH"] }], + // true + // ); + // expect(canEdit).toBeTruthy(); + // }); + + // it("should return true when measure is shared with the same user with no version status supplied", () => { + // const canEdit = useCheckUserCanEdit("anotherU$er", [ + // { userId: JANE_DOE, roles: ["SHARED_WITH"] }, + // ]); + // expect(canEdit).toBeTruthy(); + // }); + + // it("should return undefined when measure is shared with a different user", () => { + // const canEdit = useCheckUserCanEdit( + // "anotherU$er", + // [{ userId: JOHN_DOE, roles: ["SHARED_WITH"] }], + // true + // ); + // expect(canEdit).not.toBeTruthy(); + // }); + + // it("should return false when measure is versioned greater than 0", () => { + // const canEdit = useCheckUserCanEdit( + // "anotherU$er", + // [{ userId: JOHN_DOE, roles: ["SHARED_WITH"] }], + // false + // ); + // expect(canEdit).not.toBeTruthy(); + // }); +}); diff --git a/src/util/useCheckCanDelete.ts b/src/util/useCheckCanDelete.ts new file mode 100644 index 0000000..d620cb8 --- /dev/null +++ b/src/util/useCheckCanDelete.ts @@ -0,0 +1,17 @@ +import useOktaTokens from "../hooks/useOktaTokens"; + +const useCheckUserCanDelete = ( + createdBy: string, + draft: boolean = true +): boolean => { + const { getUserName } = useOktaTokens(); + const userName = getUserName(); + // versioned measures/libraries can never be deleted . + if (!draft) { + return false; + } + + return createdBy?.toLowerCase() === userName?.toLowerCase(); +}; + +export default useCheckUserCanDelete; diff --git a/src/util/useCheckCanEdit.ts b/src/util/useCheckCanEdit.ts index be3de9d..b754d63 100644 --- a/src/util/useCheckCanEdit.ts +++ b/src/util/useCheckCanEdit.ts @@ -8,7 +8,7 @@ const useCheckUserCanEdit = ( ): boolean => { const { getUserName } = useOktaTokens(); const userName = getUserName(); - // versioned measures are always uneditable. + // versioned measures/libraries are always uneditable. if (!draft) { return false; } From 98eba63fa6f4c22ebad2945e7ba8afdff8447190 Mon Sep 17 00:00:00 2001 From: Prateek Keerthi Date: Fri, 8 Mar 2024 17:08:00 -0500 Subject: [PATCH 2/5] MAt-6744 added test case --- src/util/useCheckCanDelete.test.ts | 45 ++---------------------------- 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/src/util/useCheckCanDelete.test.ts b/src/util/useCheckCanDelete.test.ts index 96614aa..997db38 100644 --- a/src/util/useCheckCanDelete.test.ts +++ b/src/util/useCheckCanDelete.test.ts @@ -24,52 +24,13 @@ describe("Check user canEdit", () => { expect(canEdit).toBeTruthy(); }); - it("should return true when user name and createdBy are the same", () => { - const canEdit = useCheckUserCanDelete(JANE_DOE, true); - expect(canEdit).toBeTruthy(); + it("should return true when user name and createdBy are not the same", () => { + const canEdit = useCheckUserCanDelete(JOHN_DOE, true); + expect(canEdit).not.toBeTruthy(); }); it("should return false when user name and createdBy are same but it is not draft", () => { const canEdit = useCheckUserCanDelete(JANE_DOE, false); expect(canEdit).not.toBeTruthy(); }); - - // it("should return undefined when user name and createdBy are not the same", () => { - // const canEdit = useCheckUserCanEdit("anotherU$er", true); - // expect(canEdit).not.toBeTruthy(); - // }); - - // it("should return true when measure is shared with the same user", () => { - // const canEdit = useCheckUserCanEdit( - // "anotherU$er", // nosec - // [{ userId: JANE_DOE, roles: ["SHARED_WITH"] }], - // true - // ); - // expect(canEdit).toBeTruthy(); - // }); - - // it("should return true when measure is shared with the same user with no version status supplied", () => { - // const canEdit = useCheckUserCanEdit("anotherU$er", [ - // { userId: JANE_DOE, roles: ["SHARED_WITH"] }, - // ]); - // expect(canEdit).toBeTruthy(); - // }); - - // it("should return undefined when measure is shared with a different user", () => { - // const canEdit = useCheckUserCanEdit( - // "anotherU$er", - // [{ userId: JOHN_DOE, roles: ["SHARED_WITH"] }], - // true - // ); - // expect(canEdit).not.toBeTruthy(); - // }); - - // it("should return false when measure is versioned greater than 0", () => { - // const canEdit = useCheckUserCanEdit( - // "anotherU$er", - // [{ userId: JOHN_DOE, roles: ["SHARED_WITH"] }], - // false - // ); - // expect(canEdit).not.toBeTruthy(); - // }); }); From 20a6483f0a719c7cfe29b457930d9846b8d0b0a0 Mon Sep 17 00:00:00 2001 From: Prateek Keerthi Date: Fri, 8 Mar 2024 17:14:59 -0500 Subject: [PATCH 3/5] MAt-6744 added test case --- src/util/useCheckCanDelete.test.ts | 9 +++++++-- src/util/useCheckCanDelete.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/util/useCheckCanDelete.test.ts b/src/util/useCheckCanDelete.test.ts index 997db38..41bab19 100644 --- a/src/util/useCheckCanDelete.test.ts +++ b/src/util/useCheckCanDelete.test.ts @@ -19,12 +19,12 @@ describe("Check user canEdit", () => { })); }); - it("should return true when user name and createdBy are the same", () => { + it("should return true when user name and createdBy are the same and draft is true", () => { const canEdit = useCheckUserCanDelete(JANE_DOE, true); expect(canEdit).toBeTruthy(); }); - it("should return true when user name and createdBy are not the same", () => { + it("should return false when user name and createdBy are not the same", () => { const canEdit = useCheckUserCanDelete(JOHN_DOE, true); expect(canEdit).not.toBeTruthy(); }); @@ -33,4 +33,9 @@ describe("Check user canEdit", () => { const canEdit = useCheckUserCanDelete(JANE_DOE, false); expect(canEdit).not.toBeTruthy(); }); + + it("should return true when user name and createdBy are the same ", () => { + const canEdit = useCheckUserCanDelete(JANE_DOE); + expect(canEdit).toBeTruthy(); + }); }); diff --git a/src/util/useCheckCanDelete.ts b/src/util/useCheckCanDelete.ts index d620cb8..75e0cb6 100644 --- a/src/util/useCheckCanDelete.ts +++ b/src/util/useCheckCanDelete.ts @@ -6,7 +6,7 @@ const useCheckUserCanDelete = ( ): boolean => { const { getUserName } = useOktaTokens(); const userName = getUserName(); - // versioned measures/libraries can never be deleted . + // versioned measures/libraries can never be deleted. if (!draft) { return false; } From 14f5f839993c793fd46f2c045d71c81f1208a501 Mon Sep 17 00:00:00 2001 From: Prateek Keerthi Date: Fri, 8 Mar 2024 17:21:18 -0500 Subject: [PATCH 4/5] MAt-6744 fixed codacy issue --- src/util/useCheckCanDelete.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util/useCheckCanDelete.test.ts b/src/util/useCheckCanDelete.test.ts index 41bab19..1c9efdc 100644 --- a/src/util/useCheckCanDelete.test.ts +++ b/src/util/useCheckCanDelete.test.ts @@ -1,4 +1,3 @@ -import * as React from "react"; import useOktaTokens from "../hooks/useOktaTokens"; import useCheckUserCanDelete from "./useCheckCanDelete"; From d3f2bf02d195ca57a33b48723b04dfc67301377f Mon Sep 17 00:00:00 2001 From: Prateek Keerthi Date: Tue, 12 Mar 2024 09:28:02 -0500 Subject: [PATCH 5/5] MAT-6744 changed variable names --- src/util/useCheckCanDelete.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/useCheckCanDelete.ts b/src/util/useCheckCanDelete.ts index 75e0cb6..e473eb1 100644 --- a/src/util/useCheckCanDelete.ts +++ b/src/util/useCheckCanDelete.ts @@ -1,17 +1,17 @@ import useOktaTokens from "../hooks/useOktaTokens"; const useCheckUserCanDelete = ( - createdBy: string, + user: string, draft: boolean = true ): boolean => { const { getUserName } = useOktaTokens(); - const userName = getUserName(); + const loggedInUser = getUserName(); // versioned measures/libraries can never be deleted. if (!draft) { return false; } - return createdBy?.toLowerCase() === userName?.toLowerCase(); + return user?.toLowerCase() === loggedInUser?.toLowerCase(); }; export default useCheckUserCanDelete;