From 194ab67bb464fa431da766276ad9e3324ddc44b9 Mon Sep 17 00:00:00 2001 From: Ahmed Nour <125875466+ahmed-nour-fdc@users.noreply.github.com> Date: Fri, 16 Aug 2024 11:23:04 +0200 Subject: [PATCH] feat: Display logged-in user (#1879) * Display a welcome message for the logged-in user. * If the cookie `kuberpult.oauth` is not set, nothing is displayed. Ref: SRX-Y5QM5R --- .../components/TopAppBar/TopAppBar.test.tsx | 59 +++++++++++++++++++ .../src/ui/components/TopAppBar/TopAppBar.tsx | 18 +++++- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 services/frontend-service/src/ui/components/TopAppBar/TopAppBar.test.tsx diff --git a/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.test.tsx b/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.test.tsx new file mode 100644 index 000000000..84554e98e --- /dev/null +++ b/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.test.tsx @@ -0,0 +1,59 @@ +/*This file is part of kuberpult. + +Kuberpult is free software: you can redistribute it and/or modify +it under the terms of the Expat(MIT) License as published by +the Free Software Foundation. + +Kuberpult is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +MIT License for more details. + +You should have received a copy of the MIT License +along with kuberpult. If not, see . + +Copyright freiheit.com*/ +import React from 'react'; +import { render } from '@testing-library/react'; +import { TopAppBar } from './TopAppBar'; +import { MemoryRouter } from 'react-router-dom'; + +describe('TopAppBar', () => { + beforeEach(() => { + document.cookie = ''; + }); + interface dataEnvT { + name: string; + cookie: string; + welcomeMessage: string; + } + const sampleEnvData: dataEnvT[] = [ + { + name: 'no cookie', + cookie: '', + welcomeMessage: '', + }, + { + name: 'cookie defined with no user email', + cookie: 'kuberpult.oauth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MjA2MjE5OTc3Nzd9.p3ApN5elnhhRhrh7DCOF-9suPIXYC36Nycf0nHfxuf8', + welcomeMessage: 'Welcome, Guest!', + }, + { + name: 'cookie defined with a user email', + cookie: 'kuberpult.oauth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJlbWFpbCI6InRlc3QudGVzdEB0ZXN0LmNvbSJ9.wgoizCsQb6AuFsbINF5OHfmfipdj5MuEUwJjZaqsBOg', + welcomeMessage: 'Welcome, test.test@test.com!', + }, + ]; + describe.each(sampleEnvData)(`welcomeMessage`, (tc) => { + it(tc.name, () => { + document.cookie = tc.cookie; + const { container } = render( + + + + ); + const message = container.getElementsByClassName('welcome-message'); + tc.cookie === '' ? expect(message).toHaveLength(0) : expect(message[0].textContent).toBe(tc.welcomeMessage); + }); + }); +}); diff --git a/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.tsx b/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.tsx index 9a6ac41ad..8889e5573 100644 --- a/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.tsx +++ b/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.tsx @@ -13,6 +13,7 @@ You should have received a copy of the MIT License along with kuberpult. If not, see . Copyright freiheit.com*/ +import { jwtDecode } from 'jwt-decode'; import { Textfield } from '../textfield'; import React, { useCallback } from 'react'; import { SideBar } from '../SideBar/SideBar'; @@ -47,6 +48,12 @@ export const TopAppBar: React.FC = (props) => { const teamsParam = (params.get('teams') || '').split(',').filter((val) => val !== ''); const version = useKuberpultVersion() || '2.6.0'; + const cookieValue = document.cookie + .split('; ') + .find((row) => row.startsWith('kuberpult.oauth=')) + ?.split('=')[1]; + const decodedToken: any = cookieValue ? jwtDecode(cookieValue) : undefined; + const loggedInUser = decodedToken?.email || 'Guest'; const hideWithoutWarningsValue = hideWithoutWarnings(params); const allWarnings: Warning[] = useAllWarnings(); @@ -114,7 +121,15 @@ export const TopAppBar: React.FC = (props) => { ) : (
); - + const renderedUser = cookieValue ? ( +
+ + Welcome, {loggedInUser}! + +
+ ) : ( +
+ ); return (
@@ -127,6 +142,7 @@ export const TopAppBar: React.FC = (props) => { {renderedTeamsFilter} {renderedWarningsFilter} {renderedWarnings} + {renderedUser}
Planned Actions