diff --git a/src/frontend/src/components/previews/ModelPreviewShim.ts b/src/frontend/src/components/previews/ModelPreviewShim.ts index 999d25c184..1e5d263f54 100644 --- a/src/frontend/src/components/previews/ModelPreviewShim.ts +++ b/src/frontend/src/components/previews/ModelPreviewShim.ts @@ -13,6 +13,7 @@ import { StockLocationTypePreviewComponent } from './models/StockLocationTypePre import { StockPreviewComponent } from './models/StockPreview'; import { SupplierPartPreviewComponent } from './models/SupplierPartPreview'; import { TransferOrderPreviewComponent } from './models/TransferOrderPreview'; +import { UserPreviewComponent } from './models/UserPreview'; registerModelPreviews({ part: PartPreviewComponent, @@ -28,5 +29,6 @@ registerModelPreviews({ salesorder: SalesOrderPreviewComponent, salesordershipment: SalesOrderShipmentPreviewComponent, returnorder: ReturnOrderPreviewComponent, - transferorder: TransferOrderPreviewComponent + transferorder: TransferOrderPreviewComponent, + user: UserPreviewComponent }); diff --git a/src/frontend/src/components/previews/models/UserPreview.tsx b/src/frontend/src/components/previews/models/UserPreview.tsx new file mode 100644 index 0000000000..fe2bbd1e12 --- /dev/null +++ b/src/frontend/src/components/previews/models/UserPreview.tsx @@ -0,0 +1,16 @@ +import type { PreviewType } from '@lib/types/Preview'; +import { t } from '@lingui/core/macro'; +import { UserDetailsPanel } from '../../../pages/core/UserDetailsPanel'; + +export function UserPreviewComponent({ + instance, + modelId +}: Readonly<{ + instance: any; + modelId: number; +}>): PreviewType { + return { + title: `${t`User`} - ${instance?.username ?? `#${modelId}`}`, + preview: + }; +} diff --git a/src/frontend/src/pages/core/UserDetail.tsx b/src/frontend/src/pages/core/UserDetail.tsx index 8a03af4e11..87ae565feb 100644 --- a/src/frontend/src/pages/core/UserDetail.tsx +++ b/src/frontend/src/pages/core/UserDetail.tsx @@ -2,20 +2,18 @@ import { ApiEndpoints } from '@lib/enums/ApiEndpoints'; import { ModelType } from '@lib/enums/ModelType'; import type { PanelType } from '@lib/types/Panel'; import { t } from '@lingui/core/macro'; -import { Badge, Group, Skeleton, Stack } from '@mantine/core'; +import { Badge, Skeleton, Stack } from '@mantine/core'; import { IconInfoCircle } from '@tabler/icons-react'; import { type ReactNode, useMemo } from 'react'; import { useParams } from 'react-router-dom'; -import type { DetailsField } from '../../components/details/Details'; -import { ItemDetailsGrid } from '../../components/details/ItemDetails'; import {} from '../../components/items/ActionDropdown'; import InstanceDetail from '../../components/nav/InstanceDetail'; import { PageDetail } from '../../components/nav/PageDetail'; import { PanelGroup } from '../../components/panels/PanelGroup'; import {} from '../../hooks/UseForm'; import { useInstance } from '../../hooks/UseInstance'; -import { useGlobalSettingsState } from '../../states/SettingsStates'; import { useUserState } from '../../states/UserState'; +import { UserDetailsPanel } from './UserDetailsPanel'; /** * Detail page for a single user @@ -24,161 +22,19 @@ export default function UserDetail() { const { id } = useParams(); const user = useUserState(); - const settings = useGlobalSettingsState(); const { instance, instanceQuery } = useInstance({ endpoint: ApiEndpoints.user_list, pk: id }); - const userGroups: any[] = useMemo(() => instance?.groups ?? [], [instance]); - const detailsPanel = useMemo(() => { if (instanceQuery.isFetching) { return ; } - const tl: DetailsField[] = [ - { - type: 'text', - name: 'username', - label: t`Username`, - icon: 'info', - copy: true - }, - { - type: 'text', - name: 'first_name', - label: t`First Name`, - icon: 'info', - copy: true, - hidden: !instance.first_name - }, - { - type: 'text', - name: 'last_name', - label: t`Last Name`, - icon: 'info', - copy: true, - hidden: !instance.last_name - }, - { - type: 'text', - name: 'email', - label: t`Email`, - copy: true, - hidden: !instance.email - } - ]; - - const tr: DetailsField[] = [ - { - type: 'boolean', - name: 'is_active', - label: t`Active`, - icon: 'info' - }, - { - type: 'boolean', - name: 'is_staff', - label: t`Administrator`, - icon: 'warning' - }, - { - type: 'boolean', - name: 'is_superuser', - label: t`Superuser`, - icon: 'warning' - }, - { - type: 'text', - name: 'groups', - label: t`Groups`, - icon: 'group', - copy: false, - hidden: !userGroups, - value_formatter: () => { - return ( - - {userGroups?.map((group) => ( - {group.name} - ))} - - ); - } - } - ]; - - const br: DetailsField[] = [ - { - type: 'text', - name: 'displayname', - label: t`Display Name`, - icon: 'user', - copy: true, - hidden: !instance.displayname - }, - { - type: 'text', - name: 'position', - label: t`Position`, - icon: 'info', - hidden: !instance.position - }, - - { - type: 'text', - name: 'contact', - label: t`Contact`, - icon: 'email', - copy: true, - hidden: !instance.contact - }, - { - type: 'text', - name: 'organisation', - label: t`Organisation`, - icon: 'info', - copy: true, - hidden: !instance.organisation - }, - { - type: 'text', - name: 'status', - label: t`Status`, - icon: 'note', - hidden: !instance.status - }, - { - type: 'text', - name: 'location', - label: t`Location`, - icon: 'location', - copy: true, - hidden: !instance.location - } - ]; - - const hasProfile = - instance.displayname || - instance.position || - instance.contact || - instance.organisation || - instance.status || - instance.location; - - return ( - - ); - }, [instance, userGroups, instanceQuery]); + return ; + }, [instance, instanceQuery]); const userPanels: PanelType[] = useMemo(() => { return [ diff --git a/src/frontend/src/pages/core/UserDetailsPanel.tsx b/src/frontend/src/pages/core/UserDetailsPanel.tsx new file mode 100644 index 0000000000..8c144ce28e --- /dev/null +++ b/src/frontend/src/pages/core/UserDetailsPanel.tsx @@ -0,0 +1,162 @@ +import { t } from '@lingui/core/macro'; +import { Badge, Group } from '@mantine/core'; +import { useMemo } from 'react'; + +import type { DetailsField } from '../../components/details/Details'; +import { ItemDetailsGrid } from '../../components/details/ItemDetails'; +import { useGlobalSettingsState } from '../../states/SettingsStates'; + +/** + * Details panel for a single user. + * + * Used on the user detail page, and in the user preview drawer. + */ +export function UserDetailsPanel({ instance }: Readonly<{ instance: any }>) { + const settings = useGlobalSettingsState(); + + const userGroups: any[] = useMemo(() => instance?.groups ?? [], [instance]); + + const tl: DetailsField[] = [ + { + type: 'text', + name: 'username', + label: t`Username`, + icon: 'info', + copy: true + }, + { + type: 'text', + name: 'first_name', + label: t`First Name`, + icon: 'info', + copy: true, + hidden: !instance?.first_name + }, + { + type: 'text', + name: 'last_name', + label: t`Last Name`, + icon: 'info', + copy: true, + hidden: !instance?.last_name + }, + { + type: 'text', + name: 'email', + label: t`Email`, + copy: true, + hidden: !instance?.email + } + ]; + + const tr: DetailsField[] = [ + { + type: 'boolean', + name: 'is_active', + label: t`Active`, + icon: 'info' + }, + { + type: 'boolean', + name: 'is_staff', + label: t`Administrator`, + icon: 'warning' + }, + { + type: 'boolean', + name: 'is_superuser', + label: t`Superuser`, + icon: 'warning' + }, + { + type: 'text', + name: 'groups', + label: t`Groups`, + icon: 'group', + copy: false, + hidden: !userGroups, + value_formatter: () => { + return ( + + {userGroups?.map((group) => ( + {group.name} + ))} + + ); + } + } + ]; + + // Profile information is provided as a nested object + const profile = instance?.profile ?? {}; + + const br: DetailsField[] = [ + { + type: 'text', + name: 'displayname', + label: t`Display Name`, + icon: 'user', + copy: true, + hidden: !profile.displayname + }, + { + type: 'text', + name: 'position', + label: t`Position`, + icon: 'info', + hidden: !profile.position + }, + + { + type: 'text', + name: 'contact', + label: t`Contact`, + icon: 'email', + copy: true, + hidden: !profile.contact + }, + { + type: 'text', + name: 'organisation', + label: t`Organisation`, + icon: 'info', + copy: true, + hidden: !profile.organisation + }, + { + type: 'text', + name: 'status', + label: t`Status`, + icon: 'note', + hidden: !profile.status + }, + { + type: 'text', + name: 'location', + label: t`Location`, + icon: 'location', + copy: true, + hidden: !profile.location + } + ]; + + const hasProfile = + profile.displayname || + profile.position || + profile.contact || + profile.organisation || + profile.status || + profile.location; + + return ( + + ); +} diff --git a/src/frontend/tests/pages/pui_core.spec.ts b/src/frontend/tests/pages/pui_core.spec.ts index cc7f12d0b2..5062ccfdd6 100644 --- a/src/frontend/tests/pages/pui_core.spec.ts +++ b/src/frontend/tests/pages/pui_core.spec.ts @@ -1,6 +1,9 @@ -import { test } from '../baseFixtures.js'; +import { createApi } from '../api.js'; +import { expect, generateUUID, test } from '../baseFixtures.js'; +import { stevenuser } from '../defaults.js'; import { loadTab, navigate } from '../helpers.js'; import { doCachedLogin } from '../login.js'; +import { setSettingState } from '../settings.js'; test('Core User/Group/Contact', async ({ browser }) => { const page = await doCachedLogin(browser); @@ -24,3 +27,83 @@ test('Core User/Group/Contact', async ({ browser }) => { await loadTab(page, 'Contacts'); await page.getByRole('cell', { name: 'Adrian Briggs' }).waitFor(); }); + +test('Core - User Profile', async ({ browser }) => { + const suffix = generateUUID().slice(0, 8); + const position = `Position ${suffix}`; + const location = `Location ${suffix}`; + + await setSettingState({ setting: 'DISPLAY_PROFILE_INFO', value: true }); + + // Find the primary key of the test user + const api = await createApi({ + username: stevenuser.username, + password: stevenuser.testcred + }); + const me = await (await api.get('user/me/')).json(); + + const page = await doCachedLogin(browser, { user: stevenuser }); + + // Edit the user profile via the account settings page + await navigate(page, 'settings/user/account'); + await page.getByText('Account Details').waitFor(); + await page + .getByRole('button', { name: 'action-button-edit-profile-' }) + .click(); + + await page.getByLabel('text-field-position').fill(position); + await page.getByLabel('text-field-location').fill(location); + await page.getByRole('button', { name: 'Submit' }).click(); + await page.getByText('Profile details updated').waitFor(); + + // The profile information is displayed on the user detail page + await navigate(page, `core/user/${me.pk}/`); + await page + .getByText(`User: ${stevenuser.username}`, { exact: true }) + .waitFor(); + await page.getByText('User Profile').waitFor(); + await page.getByText(position, { exact: true }).waitFor(); + await page.getByText(location, { exact: true }).waitFor(); + + // The user (and profile) is also shown in the preview drawer + let response = await api.patch('settings/user/ENABLE_PREVIEW_PANEL/', { + data: { value: true } + }); + expect(response.status()).toBe(200); + + // Reload the page (to fetch the updated user setting) + await navigate(page, '/core'); + await loadTab(page, 'Users'); + await page + .getByRole('cell', { name: stevenuser.username, exact: true }) + .click(); + await page.getByText(`User - ${stevenuser.username}`).waitFor(); + await expect(page.getByText('No preview available')).toHaveCount(0); + await page.getByText(position, { exact: true }).waitFor(); + await page.keyboard.press('Escape'); + + response = await api.patch('settings/user/ENABLE_PREVIEW_PANEL/', { + data: { value: false } + }); + expect(response.status()).toBe(200); + + // Disable the display of profile information + await navigate(page, `core/user/${me.pk}/`); + await setSettingState({ setting: 'DISPLAY_PROFILE_INFO', value: false }); + + await page.reload(); + await page + .getByText(`User: ${stevenuser.username}`, { exact: true }) + .waitFor(); + await page.getByText('User Information').waitFor(); + await expect(page.getByText('User Profile')).toHaveCount(0); + await expect(page.getByText(position, { exact: true })).toHaveCount(0); + + // Restore the original state + await setSettingState({ setting: 'DISPLAY_PROFILE_INFO', value: true }); + + response = await api.patch('user/me/profile/', { + data: { position: '', location: '' } + }); + expect(response.status()).toBe(200); +}); diff --git a/src/frontend/tests/pui_plugins.spec.ts b/src/frontend/tests/pui_plugins.spec.ts index 3e39d1e084..92a7360be2 100644 --- a/src/frontend/tests/pui_plugins.spec.ts +++ b/src/frontend/tests/pui_plugins.spec.ts @@ -297,22 +297,32 @@ test('Plugins - Creator', async ({ browser }) => { user: adminuser }); - // Ensure that the SampleIntegration plugin is enabled - await setPluginState({ - plugin: 'my-custom-plugin', - state: true - }); + // Enable the plugin, and wait for its static files to be served. + // The plugin is toggled off -> on each attempt, because the static file copy + // can be lost if activation races a concurrent registry reload (triggered by + // other tests toggling plugins in parallel). Re-activating an already active + // plugin is a no-op, so without the toggle a retry could never recover. + await expect(async () => { + await setPluginState({ + plugin: 'my-custom-plugin', + state: false + }); - // Allow time for installation of plugin static files, etc - await expect - .poll( - async () => - ( - await page.request.get('/static/plugins/my-custom-plugin/Panel.js') - ).status(), - { timeout: 2500 } - ) - .toBe(200); + await setPluginState({ + plugin: 'my-custom-plugin', + state: true + }); + + await expect + .poll( + async () => + ( + await page.request.get('/static/plugins/my-custom-plugin/Panel.js') + ).status(), + { timeout: 5000 } + ) + .toBe(200); + }).toPass({ timeout: 60000 }); await expect(async () => { await navigate(page, 'part/106/details/');