From 74cf40c441ecde6a2b8761c7cc52f9c50802ec85 Mon Sep 17 00:00:00 2001 From: Daniel Tom Date: Fri, 22 Nov 2024 15:05:59 +0100 Subject: [PATCH] Wip --- src/__mocks__/ucHandlers.ts | 39 +++++++++++++++++-- .../hooks/useGetCurrentUser.test.tsx | 37 ++++++++++++++++++ src/components/userInformation.tsx | 17 ++++++++ src/hooks/user.ts | 28 +++++++++++++ src/widgets/catalogTreeWidget.tsx | 9 +++++ 5 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/__tests__/hooks/useGetCurrentUser.test.tsx create mode 100644 src/components/userInformation.tsx create mode 100644 src/hooks/user.ts diff --git a/src/__mocks__/ucHandlers.ts b/src/__mocks__/ucHandlers.ts index e583747..bdb6c9e 100644 --- a/src/__mocks__/ucHandlers.ts +++ b/src/__mocks__/ucHandlers.ts @@ -1,5 +1,5 @@ import { http, HttpResponse } from 'msw'; -import { UC_API_PREFIX } from '../utils/constants'; +import { UC_API_PREFIX, UC_AUTH_API_PREFIX } from '../utils/constants'; export const ucHandlers = [ // Handler for catalogs @@ -118,5 +118,38 @@ export const ucHandlers = [ 'Content-Type, Authorization, x-interceptors-internal-request-id' } }); - }) -]; + }), + + // Handler for user + http.get(`http://localhost:8080${UC_AUTH_API_PREFIX}/scim2/Users/self`, () => { + return new HttpResponse( + JSON.stringify({ + id: '1', + userName: 'testuser', + displayName: 'Test User', + emails: [{ value: 'testuser@example.com' }], + photos: [{ value: 'http://example.com/photo.jpg' }] + }), + { + status: 200, + headers: { + 'Access-Control-Allow-Origin': 'http://localhost' + } + } + ); + }), + + // Handler for OPTIONS user requests + http.options(`http://localhost:8080${UC_AUTH_API_PREFIX}/scim2/Users/self`, () => { + return new HttpResponse(null, { + status: 204, + headers: { + 'Access-Control-Allow-Origin': 'http://localhost', + 'Access-Control-Allow-Methods': + 'GET, POST, PUT, DELETE, OPTIONS, PATCH', + 'Access-Control-Allow-Headers': + 'Content-Type, Authorization, x-interceptors-internal-request-id' + } + }); + }), +]; \ No newline at end of file diff --git a/src/__tests__/hooks/useGetCurrentUser.test.tsx b/src/__tests__/hooks/useGetCurrentUser.test.tsx new file mode 100644 index 0000000..9ef358d --- /dev/null +++ b/src/__tests__/hooks/useGetCurrentUser.test.tsx @@ -0,0 +1,37 @@ +import React from 'react'; +import { renderHook, waitFor } from '@testing-library/react'; +import { useGetCurrentUser } from '../../hooks/user'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { ClientContext, getClient } from '../../context/client'; + + +const queryClient = new QueryClient(); + +describe('useGetCurrentUser', () => { + it('fetches and returns the current user', async () => { + const user = { + id: '1', + userName: 'testuser', + displayName: 'Test User', + emails: [{ value: 'testuser@example.com' }], + photos: [{ value: 'http://example.com/photo.jpg' }] + }; + + const apiClient = getClient('http://localhost:8080', 'not-set'); + const { result } = renderHook(() => useGetCurrentUser("valid-token"), { + wrapper: ({ children }) => ( + + + {children} + + + ) + }); + + await waitFor(() => + expect(result.current.data).toEqual( +{"displayName": "Test User", "emails": [{"value": "testuser@example.com"}], "id": "1", "photos": [{"value": "http://example.com/photo.jpg"}], "userName": "testuser"} + ) + ); + }); +}); \ No newline at end of file diff --git a/src/components/userInformation.tsx b/src/components/userInformation.tsx new file mode 100644 index 0000000..82d1dee --- /dev/null +++ b/src/components/userInformation.tsx @@ -0,0 +1,17 @@ +import React from 'react'; + +interface UserInformationProps { + onBack: () => void; +} + +export const UserInformation: React.FC = ({ onBack }) => { + return ( +
+ +
+

User Information

+ {/* Render user information here */} +
+
+ ); +}; \ No newline at end of file diff --git a/src/hooks/user.ts b/src/hooks/user.ts new file mode 100644 index 0000000..b2fd639 --- /dev/null +++ b/src/hooks/user.ts @@ -0,0 +1,28 @@ +import { useQuery } from '@tanstack/react-query'; +import { UC_AUTH_API_PREFIX } from '../utils/constants'; +import { ClientContext } from '../context/client'; +import { useContext } from 'react'; + +export interface UserInterface { + id: string; + userName: string; + displayName: string; + emails: any; + photos: any; +} + +export function useGetCurrentUser(access_token: string) { + const apiClient = useContext(ClientContext); + return useQuery({ + queryKey: ['getUser', access_token], + queryFn: async () => { + return apiClient + .get(`${UC_AUTH_API_PREFIX}/scim2/Users/self`, { + }) + .then((response) => response.data) + .catch((e) => { + throw new Error(`Failed to fetch user. Error ${e}`); + }); + }, + }); +} \ No newline at end of file diff --git a/src/widgets/catalogTreeWidget.tsx b/src/widgets/catalogTreeWidget.tsx index 5a6b42d..d1ef989 100644 --- a/src/widgets/catalogTreeWidget.tsx +++ b/src/widgets/catalogTreeWidget.tsx @@ -59,6 +59,15 @@ export const CatalogTreeWidgetComponent: React.FC<{ updateToken: (token: string) => void; }> = ({ googleAuthEnabled, googleClientId, setAuthenticated, updateToken }) => { const loginWithToken = useLoginWithToken(); + const [view, setView] = useState<'catalog' | 'user'>('catalog'); + + const handleUserButtonClick = () => { + setView('user'); + }; + + const handleBackButtonClick = () => { + setView('catalog'); + }; React.useEffect(() => { const authCookie = Cookies.get('authenticated');