From 4bbdc61da29cc0c1d49638a628b2a558ae55d563 Mon Sep 17 00:00:00 2001 From: Yasiu1376 Date: Mon, 30 Oct 2023 12:06:21 +0330 Subject: [PATCH] CFE-3 implementation permission --- src/layouts/DashboardLayout.jsx | 9 ++++--- src/lib/app/hooks/useNotification.jsx | 4 ++- src/middlewares/AppPermission.jsx | 8 ++++++ .../__test__/AppPermission.test.js | 26 +++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/middlewares/AppPermission.jsx create mode 100644 src/middlewares/__test__/AppPermission.test.js diff --git a/src/layouts/DashboardLayout.jsx b/src/layouts/DashboardLayout.jsx index 3c62a77..72344d6 100644 --- a/src/layouts/DashboardLayout.jsx +++ b/src/layouts/DashboardLayout.jsx @@ -1,12 +1,15 @@ -import CallWidget from "src/components/layouts/Dashboard/CallWidget"; import Dashboard from "src/components/layouts/Dashboard"; +import AppPermission from "@/middlewares/AppPermission"; +import CallWidget from "@/components/layouts/Dashboard/CallWidget"; const DashboardLayout = (props) => { - + const permissions = ["NEXT_PUBLIC_HAS_WIDGET"]; return ( <> - + + + ); }; diff --git a/src/lib/app/hooks/useNotification.jsx b/src/lib/app/hooks/useNotification.jsx index 6453ee8..6e80bca 100644 --- a/src/lib/app/hooks/useNotification.jsx +++ b/src/lib/app/hooks/useNotification.jsx @@ -1,7 +1,9 @@ import useSWR from 'swr' import {GET_SIDEBAR_NOTIFICATION} from "@/core/data/apiRoutes"; import useRequest from "@/lib/app/hooks/useRequest"; -const isNotificationEnabled = process.env.NEXT_HAS_NOTIFICATION === "true"; +const GLOBAL_HAS_VALUE = process.env.NEXT_PUBLIC_HAS_VALUE; +const hasPermissionsValue = GLOBAL_HAS_VALUE ? JSON.parse(GLOBAL_HAS_VALUE) : []; +const isNotificationEnabled = hasPermissionsValue.includes("NEXT_PUBLIC_HAS_NOTIFICATION"); const useNotification = () => { diff --git a/src/middlewares/AppPermission.jsx b/src/middlewares/AppPermission.jsx new file mode 100644 index 0000000..8f93bd3 --- /dev/null +++ b/src/middlewares/AppPermission.jsx @@ -0,0 +1,8 @@ +const GLOBAL_HAS_VALUE = process.env.NEXT_PUBLIC_HAS_VALUE; +const hasPermissionsValue = GLOBAL_HAS_VALUE ? JSON.parse(GLOBAL_HAS_VALUE) : []; + +const AppPermission = ({ children , permissions }) => { + return permissions.some(permission => hasPermissionsValue.includes(permission)) ? <>{children} : null; +}; + +export default AppPermission; diff --git a/src/middlewares/__test__/AppPermission.test.js b/src/middlewares/__test__/AppPermission.test.js new file mode 100644 index 0000000..82377ba --- /dev/null +++ b/src/middlewares/__test__/AppPermission.test.js @@ -0,0 +1,26 @@ +import {render, screen, waitFor} from '@testing-library/react'; +import MockAppWithProviders from "../../../mocks/AppWithProvider"; +import AppPermission from "@/middlewares/AppPermission"; + +describe('AppPermission From middlewares', () => { + describe('Behavior', () => { + it('show related child if permission exist', async () => { + + const requiredPermissions = ["NEXT_PUBLIC_HAS_WIDGET"]; + render({'children'}); + + await waitFor(()=>{ + expect(screen.queryByText('children')).toBeInTheDocument(); + }) + }); + it('do not show related child if permission does not exist', async () => { + + const requiredPermissions = ["MISSING_PERMISSION"]; + render(); + + await waitFor(() => { + expect(screen.queryByText('children')).not.toBeInTheDocument(); + }); + }); + }); +});