Skip to content

Commit

Permalink
Merge pull request #75 from MeasureAuthoringTool/MAT-6744/addCheckUse…
Browse files Browse the repository at this point in the history
…rCanDeleteUtilFunction

MAt-6744 added check user can delete function
  • Loading branch information
sb-prateekkeerthi authored Mar 12, 2024
2 parents 0125ef2 + d3f2bf0 commit 8130cc4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/madie-madie-util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -33,6 +34,7 @@ export {
useOrganizationApi,
useDocumentTitle,
checkUserCanEdit,
checkUserCanDelete,
useFeatureFlags,
getOidFromString,
};
40 changes: 40 additions & 0 deletions src/util/useCheckCanDelete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 and draft is true", () => {
const canEdit = useCheckUserCanDelete(JANE_DOE, true);
expect(canEdit).toBeTruthy();
});

it("should return false 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 true when user name and createdBy are the same ", () => {
const canEdit = useCheckUserCanDelete(JANE_DOE);
expect(canEdit).toBeTruthy();
});
});
17 changes: 17 additions & 0 deletions src/util/useCheckCanDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import useOktaTokens from "../hooks/useOktaTokens";

const useCheckUserCanDelete = (
user: string,
draft: boolean = true
): boolean => {
const { getUserName } = useOktaTokens();
const loggedInUser = getUserName();
// versioned measures/libraries can never be deleted.
if (!draft) {
return false;
}

return user?.toLowerCase() === loggedInUser?.toLowerCase();
};

export default useCheckUserCanDelete;
2 changes: 1 addition & 1 deletion src/util/useCheckCanEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 8130cc4

Please sign in to comment.