From aeb88a6a42432f3225905b9b3bcfe8eb79ef44b6 Mon Sep 17 00:00:00 2001 From: josephine-rutten Date: Thu, 21 Nov 2024 14:59:00 +0100 Subject: [PATCH 1/5] Added message to people without permissions and made sure permissions were deleted when logging out --- public/components/Callback.js | 17 +++++++++++++---- public/components/Callback.test.js | 20 ++++++++++++++++++++ public/contexts/AuthTokenContext.js | 8 ++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/public/components/Callback.js b/public/components/Callback.js index 849e8e8..47c5a72 100644 --- a/public/components/Callback.js +++ b/public/components/Callback.js @@ -17,7 +17,7 @@ function Callback() { const checkSuccess = useCallback(() => { if ( - (permissions || process.env.PERMISSIONS_DISABLED === "true") && + (permissions.length > 0 || process.env.PERMISSIONS_DISABLED === "true") && Object.hasOwn(localStorage, "token") ) { setInfoMessage( @@ -25,6 +25,10 @@ function Callback() { ); window.location.replace("/"); return true; + } else if (permissions.length == 0 ) { + setInfoMessage( + "You don't seem to have any permissions within this application. Please check with an admin if this is correct.", + ); } return false; }, [permissions]); @@ -36,9 +40,14 @@ function Callback() { } else { getData(`${process.env.API_URL}/api/v1.0/auth/permissions`, token) .then((data) => { - putPermissions(data); - setInfoMessage("You're logged in."); - window.location.replace("/"); + if (data.length > 0) { + putPermissions(data); + setInfoMessage("You're logged in."); + window.location.replace("/"); + } else { + putPermissions(data); + setInfoMessage("You don't seem to have any permissions within this application. Please check with an admin if this is correct."); + } }) .catch((e) => { setInfoMessage( diff --git a/public/components/Callback.test.js b/public/components/Callback.test.js index 4502332..0d15a54 100644 --- a/public/components/Callback.test.js +++ b/public/components/Callback.test.js @@ -79,4 +79,24 @@ describe("Callback Component", () => { screen.getByText("Something went wrong. Retry the login."), ).toBeInTheDocument(); }); + + test("displays no permissions message when user has no permissions", async () => { + getData.mockResolvedValueOnce([]); + + render(); + + await waitFor(() => { + expect(screen.getByText("You don't seem to have any permissions within this application. Please check with an admin if this is correct.")).toBeInTheDocument(); + }); + + await waitFor(() => { + expect(mockPutToken).toHaveBeenCalledWith("some-valid-token"); + }); + await waitFor(() => { + expect(mockSetUsername).toHaveBeenCalledWith("testuser"); + }); + await waitFor(() => { + expect(mockPutPermissions).toHaveBeenCalledWith([]); + }); + }); }); diff --git a/public/contexts/AuthTokenContext.js b/public/contexts/AuthTokenContext.js index 6e473e9..93105f1 100644 --- a/public/contexts/AuthTokenContext.js +++ b/public/contexts/AuthTokenContext.js @@ -29,6 +29,7 @@ export function AuthTokenProvider({ children }) { const [loggedIn, setLoggedIn] = useState(false); const [loginMessage, setLoginMessage] = useState(""); const [token, setToken] = useState(); + const [permissions, setPermissions] = useState(); const [tokenExpiry, setTokenExpiry] = useState(); const [tokenWillExpire, setTokenWillExpire] = useState(false); const [username, setUsername] = useState(); @@ -56,6 +57,11 @@ export function AuthTokenProvider({ children }) { localStorage.removeItem("token"); }, []); + const removePermissions = useCallback(() => { + setPermissions(null); + localStorage.removeItem("permissions"); + }, []); + // Only supposed to be used in Callback component. const putToken = useCallback( (newToken) => { @@ -122,6 +128,7 @@ export function AuthTokenProvider({ children }) { ); const logout = useCallback(() => { + removePermissions(); removeToken(); setUsername(""); setLoginMessage("You have been logged out"); @@ -177,6 +184,7 @@ export function AuthTokenProvider({ children }) { const setAuthStateOnLoad = () => { const tokenStored = localStorage.getItem("token"); if (storeValueIsUndefined(tokenStored)) { + removePermissions(); removeToken(); } else { setToken(tokenStored); From 61fb20f57a40f5a7201ccfa903df0aeb84fe3df7 Mon Sep 17 00:00:00 2001 From: josephine-rutten Date: Thu, 21 Nov 2024 16:10:04 +0100 Subject: [PATCH 2/5] run prettier --- public/components/Callback.js | 6 ++++-- public/components/Callback.test.js | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/public/components/Callback.js b/public/components/Callback.js index 47c5a72..037ca3e 100644 --- a/public/components/Callback.js +++ b/public/components/Callback.js @@ -25,7 +25,7 @@ function Callback() { ); window.location.replace("/"); return true; - } else if (permissions.length == 0 ) { + } else if (permissions.length == 0) { setInfoMessage( "You don't seem to have any permissions within this application. Please check with an admin if this is correct.", ); @@ -46,7 +46,9 @@ function Callback() { window.location.replace("/"); } else { putPermissions(data); - setInfoMessage("You don't seem to have any permissions within this application. Please check with an admin if this is correct."); + setInfoMessage( + "You don't seem to have any permissions within this application. Please check with an admin if this is correct.", + ); } }) .catch((e) => { diff --git a/public/components/Callback.test.js b/public/components/Callback.test.js index 0d15a54..5eb3eea 100644 --- a/public/components/Callback.test.js +++ b/public/components/Callback.test.js @@ -86,7 +86,11 @@ describe("Callback Component", () => { render(); await waitFor(() => { - expect(screen.getByText("You don't seem to have any permissions within this application. Please check with an admin if this is correct.")).toBeInTheDocument(); + expect( + screen.getByText( + "You don't seem to have any permissions within this application. Please check with an admin if this is correct.", + ), + ).toBeInTheDocument(); }); await waitFor(() => { From d0d67af4533216fc3de54c6bd4f71afcfa064538 Mon Sep 17 00:00:00 2001 From: josephine-rutten Date: Tue, 26 Nov 2024 10:35:59 +0100 Subject: [PATCH 3/5] Removed any reference to permissions from authcontext --- public/contexts/AuthTokenContext.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/public/contexts/AuthTokenContext.js b/public/contexts/AuthTokenContext.js index 93105f1..34830af 100644 --- a/public/contexts/AuthTokenContext.js +++ b/public/contexts/AuthTokenContext.js @@ -29,7 +29,6 @@ export function AuthTokenProvider({ children }) { const [loggedIn, setLoggedIn] = useState(false); const [loginMessage, setLoginMessage] = useState(""); const [token, setToken] = useState(); - const [permissions, setPermissions] = useState(); const [tokenExpiry, setTokenExpiry] = useState(); const [tokenWillExpire, setTokenWillExpire] = useState(false); const [username, setUsername] = useState(); @@ -57,10 +56,6 @@ export function AuthTokenProvider({ children }) { localStorage.removeItem("token"); }, []); - const removePermissions = useCallback(() => { - setPermissions(null); - localStorage.removeItem("permissions"); - }, []); // Only supposed to be used in Callback component. const putToken = useCallback( @@ -128,7 +123,6 @@ export function AuthTokenProvider({ children }) { ); const logout = useCallback(() => { - removePermissions(); removeToken(); setUsername(""); setLoginMessage("You have been logged out"); @@ -184,7 +178,6 @@ export function AuthTokenProvider({ children }) { const setAuthStateOnLoad = () => { const tokenStored = localStorage.getItem("token"); if (storeValueIsUndefined(tokenStored)) { - removePermissions(); removeToken(); } else { setToken(tokenStored); From a879e03414e23cc1aedb37faec48ff744fbcc926 Mon Sep 17 00:00:00 2001 From: josephine-rutten Date: Tue, 26 Nov 2024 10:36:59 +0100 Subject: [PATCH 4/5] small bug callback --- public/components/Callback.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/components/Callback.js b/public/components/Callback.js index 037ca3e..f90afa9 100644 --- a/public/components/Callback.js +++ b/public/components/Callback.js @@ -25,7 +25,7 @@ function Callback() { ); window.location.replace("/"); return true; - } else if (permissions.length == 0) { + } else if (permissions.length === 0) { setInfoMessage( "You don't seem to have any permissions within this application. Please check with an admin if this is correct.", ); From 3f17251f7fea61ef5e610251d44ebb512e51efb7 Mon Sep 17 00:00:00 2001 From: josephine-rutten Date: Tue, 26 Nov 2024 14:36:48 +0100 Subject: [PATCH 5/5] reformat --- public/contexts/AuthTokenContext.js | 1 - 1 file changed, 1 deletion(-) diff --git a/public/contexts/AuthTokenContext.js b/public/contexts/AuthTokenContext.js index 34830af..6e473e9 100644 --- a/public/contexts/AuthTokenContext.js +++ b/public/contexts/AuthTokenContext.js @@ -56,7 +56,6 @@ export function AuthTokenProvider({ children }) { localStorage.removeItem("token"); }, []); - // Only supposed to be used in Callback component. const putToken = useCallback( (newToken) => {