diff --git a/src/main/webui/src/app/components/nav/NavHeader.jsx b/src/main/webui/src/app/components/nav/NavHeader.jsx index cebf52f..0af9645 100644 --- a/src/main/webui/src/app/components/nav/NavHeader.jsx +++ b/src/main/webui/src/app/components/nav/NavHeader.jsx @@ -14,19 +14,42 @@ * limitations under the License. */ -import React from 'react'; +import React, {useState, useEffect} from 'react'; import Container from 'react-bootstrap/Container'; import Nav from 'react-bootstrap/Nav'; import Navbar from 'react-bootstrap/Navbar'; import NavDropdown from 'react-bootstrap/NavDropdown'; import {LinkContainer} from 'react-router-bootstrap'; +import {IndyRest} from '#utils/RestClient'; +import {Utils} from '#utils/AppUtils'; + +const {authRes} = IndyRest; -// TODO: This is mock user login, need to implement later for real login -const isUserloggedIn = true; -const username = "mock"; // eslint-disable-next-line max-lines-per-function export default function NavHeader(){ + const [state, setState] = useState({ + isUserloggedIn: false, + userInfo: {} + }); + + useEffect(()=>{ + const fetchUserInfo = async () => { + const res = await authRes.getUserInfo(); + if (res.success){ + setState({ + isUserloggedIn: true, + userInfo: res.result + }); + }else{ + res.text().then(data => { + Utils.logMessage(`Failed to get user info. Error reason: ${res.status}->${data}`); + }); + } + }; + + fetchUserInfo(); + }, []); return ( @@ -59,9 +82,9 @@ export default function NavHeader(){ Diagnostics Bundle - { isUserloggedIn && + { state.isUserloggedIn && diff --git a/src/main/webui/src/app/components/nav/NavHeader.test.jsx b/src/main/webui/src/app/components/nav/NavHeader.test.jsx index e00622e..c7831f6 100644 --- a/src/main/webui/src/app/components/nav/NavHeader.test.jsx +++ b/src/main/webui/src/app/components/nav/NavHeader.test.jsx @@ -15,18 +15,28 @@ */ import React from "react"; -import {render, screen, cleanup} from '@testing-library/react'; +import {render, screen, cleanup, waitFor} from '@testing-library/react'; import userEvent from "@testing-library/user-event"; import {MemoryRouter} from 'react-router-dom'; import '@testing-library/jest-dom'; +import fetchMock from "fetch-mock"; import NavHeader from "./NavHeader.jsx"; +beforeEach(() => { + fetchMock.restore(); +}); + afterEach(() => { cleanup(); }); describe('Header tests', () => { it("Verify Header for elements existing", async ()=>{ + const mockUserInfo = { + userName: "test-user", + roles: ["admin", "power-user"] + }; + fetchMock.mock("/api/admin/auth/userinfo", {status: 200, body: JSON.stringify(mockUserInfo)}); const user = userEvent.setup(); render(); expect(screen.getByRole("link", {name: "Indy"})).toBeInTheDocument(); @@ -51,5 +61,10 @@ describe('Header tests', () => { await user.click(addonsButton); expect(screen.getByRole("link", {name: "Not-Found Cache"})).toBeInTheDocument(); expect(screen.getByRole("link", {name: "Delete Cache"})).toBeInTheDocument(); + + await waitFor(()=>{ + expect(screen.getByText(mockUserInfo.userName)).toBeInTheDocument(); + }); + }); }); diff --git a/src/main/webui/src/app/utils/RestClient.js b/src/main/webui/src/app/utils/RestClient.js index 89f64bc..0dced71 100644 --- a/src/main/webui/src/app/utils/RestClient.js +++ b/src/main/webui/src/app/utils/RestClient.js @@ -128,6 +128,12 @@ const IndyRest = { return {success: false, error: {status: response.status, message: response.statusText}}; } }, + authRes: { + getUserInfo: async () => { + const response = await jsonRest.get('/api/admin/auth/userinfo'); + return handleResponse(response); + } + }, nfcRes: { // TODO: not implemented. }, diff --git a/src/main/webui/src/server/app.js b/src/main/webui/src/server/app.js index b96d59e..7bbb5c5 100644 --- a/src/main/webui/src/server/app.js +++ b/src/main/webui/src/server/app.js @@ -149,3 +149,8 @@ app.put(`${STORE_API_BASE}/:packageType/:type/:name`, (req, res) => { } }); +app.get('/api/admin/auth/userinfo', (req, res) => { + const testUser = {userName: "indy", roles: ["admin", "power-user", "user"]}; + res.status(200).json(testUser); +}); +