Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PUI] Set password #8770

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_ui_panels(self, request, context, **kwargs):
'key': 'dynamic-panel',
'title': 'Dynamic Panel',
'source': self.plugin_static_file('sample_panel.js'),
'icon': 'part',
'icon': 'ti:wave-saw-tool:outline',
'context': {
'version': INVENTREE_SW_VERSION,
'plugin_version': self.VERSION,
Expand All @@ -97,7 +97,7 @@ def get_ui_panels(self, request, context, **kwargs):
'key': 'part-panel',
'title': _('Part Panel'),
'source': self.plugin_static_file('sample_panel.js:renderPartPanel'),
'icon': 'part',
'icon': 'ti:package_outline',
'context': {'part_name': part.name if part else ''},
})

Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/enums/ApiEndpoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum ApiEndpoints {
user_simple_login = 'email/generate/',
user_reset = 'auth/password/reset/',
user_reset_set = 'auth/password/reset/confirm/',
user_change_password = 'auth/password/change/',
user_sso = 'auth/social/',
user_sso_remove = 'auth/social/:id/disconnect/',
user_emails = 'auth/emails/',
Expand Down
124 changes: 124 additions & 0 deletions src/frontend/src/pages/Auth/ChangePassword.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Trans, t } from '@lingui/macro';
import {
Button,
Center,
Container,
Divider,
Group,
Paper,
PasswordInput,
Stack,
Text
} from '@mantine/core';
import { useForm } from '@mantine/form';
import { notifications } from '@mantine/notifications';
import { useNavigate } from 'react-router-dom';

import { api } from '../../App';
import { StylishText } from '../../components/items/StylishText';
import { ProtectedRoute } from '../../components/nav/Layout';
import { LanguageContext } from '../../contexts/LanguageContext';
import { ApiEndpoints } from '../../enums/ApiEndpoints';
import { apiUrl } from '../../states/ApiState';
import { useUserState } from '../../states/UserState';

export default function Set_Password() {
const simpleForm = useForm({
initialValues: {
new_password1: '',
new_password2: ''
}
});

const user = useUserState();
const navigate = useNavigate();

function passwordError(values: any) {
let message: any =
values?.new_password2 ||
values?.new_password1 ||
values?.error ||
t`Password could not be changed`;

// If message is array
if (!Array.isArray(message)) {
message = [message];
}

message.forEach((msg: string) => {
notifications.show({
title: t`Error`,
message: msg,
color: 'red'
});
});
}

function handleSet() {
// Set password with call to backend
api
.post(apiUrl(ApiEndpoints.user_change_password), {
new_password1: simpleForm.values.new_password1,
new_password2: simpleForm.values.new_password2
})
.then((val) => {
if (val.status === 200) {
notifications.show({
title: t`Password Changed`,
message: t`The password was set successfully. You can now login with your new password`,
color: 'green',
autoClose: false
});
navigate('/login');
} else {
passwordError(val.data);
}
})
.catch((err) => {
passwordError(err.response.data);
});
}

return (
<LanguageContext>
<ProtectedRoute>
<Center mih='100vh'>
<Container w='md' miw={425}>
<Stack>
<StylishText size='xl'>{t`Reset Password`}</StylishText>
<Divider />
{user.username() && (
<Paper>
<Group>
<StylishText size='md'>{t`User`}</StylishText>
<Text>{user.username()}</Text>
</Group>
</Paper>
)}
<Divider />
<Stack gap='xs'>
<PasswordInput
required
aria-label='input-password-1'
label={t`New Password`}
description={t`Enter your new password`}
{...simpleForm.getInputProps('new_password1')}
/>
<PasswordInput
required
aria-label='input-password-2'
label={t`Confirm New Password`}
description={t`Confirm your new password`}
{...simpleForm.getInputProps('new_password2')}
/>
</Stack>
<Button type='submit' onClick={handleSet}>
<Trans>Confirm</Trans>
</Button>
</Stack>
</Container>
</Center>
</ProtectedRoute>
</LanguageContext>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { LanguageContext } from '../../contexts/LanguageContext';
import { ApiEndpoints } from '../../enums/ApiEndpoints';
import { apiUrl } from '../../states/ApiState';

export default function Set_Password() {
export default function ResetPassword() {
const simpleForm = useForm({ initialValues: { password: '' } });
const [searchParams] = useSearchParams();
const navigate = useNavigate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { Group, Stack, Table, Title } from '@mantine/core';
import { IconKey, IconUser } from '@tabler/icons-react';
import { useMemo } from 'react';

import { useNavigate } from 'react-router-dom';
import { YesNoUndefinedButton } from '../../../../components/buttons/YesNoButton';
import type { ApiFormFieldSet } from '../../../../components/forms/fields/ApiFormField';
import { ActionDropdown } from '../../../../components/items/ActionDropdown';
import { ApiEndpoints } from '../../../../enums/ApiEndpoints';
import { notYetImplemented } from '../../../../functions/notifications';
import { useEditApiFormModal } from '../../../../hooks/UseForm';
import { useUserState } from '../../../../states/UserState';

export function AccountDetailPanel() {
const navigate = useNavigate();

const [user, fetchUserState] = useUserState((state) => [
state.user,
state.fetchUserState
Expand Down Expand Up @@ -51,10 +53,12 @@ export function AccountDetailPanel() {
onClick: editUser.open
},
{
name: t`Set Password`,
name: t`Change Password`,
icon: <IconKey />,
tooltip: t`Set User Password`,
onClick: notYetImplemented
tooltip: t`Change User Password`,
onClick: () => {
navigate('/change-password');
}
}
]}
/>
Expand Down
12 changes: 9 additions & 3 deletions src/frontend/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,13 @@ export const Login = Loadable(lazy(() => import('./pages/Auth/Login')));
export const Logout = Loadable(lazy(() => import('./pages/Auth/Logout')));
export const Logged_In = Loadable(lazy(() => import('./pages/Auth/Logged-In')));
export const Reset = Loadable(lazy(() => import('./pages/Auth/Reset')));
export const Set_Password = Loadable(
lazy(() => import('./pages/Auth/Set-Password'))

export const ChangePassword = Loadable(
lazy(() => import('./pages/Auth/ChangePassword'))
);

export const ResetPassword = Loadable(
lazy(() => import('./pages/Auth/ResetPassword'))
);

// Routes
Expand Down Expand Up @@ -168,7 +173,8 @@ export const routes = (
<Route path='/logout' element={<Logout />} />,
<Route path='/logged-in' element={<Logged_In />} />
<Route path='/reset-password' element={<Reset />} />
<Route path='/set-password' element={<Set_Password />} />
<Route path='/set-password' element={<ResetPassword />} />
<Route path='/change-password' element={<ChangePassword />} />
</Route>
</Routes>
);
2 changes: 2 additions & 0 deletions src/frontend/tests/pages/pui_part.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from '../baseFixtures';

Check failure on line 1 in src/frontend/tests/pages/pui_part.spec.ts

View workflow job for this annotation

GitHub Actions / Tests - Platform UI

[chromium] › pages/pui_part.spec.ts:9:1 › Parts - Tabs

1) [chromium] › pages/pui_part.spec.ts:9:1 › Parts - Tabs ──────────────────────────────────────── Test timeout of 90000ms exceeded.

Check failure on line 1 in src/frontend/tests/pages/pui_part.spec.ts

View workflow job for this annotation

GitHub Actions / Tests - Platform UI

[chromium] › pages/pui_part.spec.ts:100:1 › Parts - Allocations

2) [chromium] › pages/pui_part.spec.ts:100:1 › Parts - Allocations ─────────────────────────────── Test timeout of 90000ms exceeded.

Check failure on line 1 in src/frontend/tests/pages/pui_part.spec.ts

View workflow job for this annotation

GitHub Actions / Tests - Platform UI

[chromium] › pages/pui_part.spec.ts:100:1 › Parts - Allocations

2) [chromium] › pages/pui_part.spec.ts:100:1 › Parts - Allocations ─────────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Test timeout of 90000ms exceeded.
import { baseUrl } from '../defaults';
import { getRowFromCell } from '../helpers';
import { doQuickLogin } from '../login';
Expand Down Expand Up @@ -27,7 +27,7 @@
await page.getByRole('tab', { name: 'Related Parts' }).click();

// Related Parts
await page.getByText('1551ACLR').click();

Check failure on line 30 in src/frontend/tests/pages/pui_part.spec.ts

View workflow job for this annotation

GitHub Actions / Tests - Platform UI

[chromium] › pages/pui_part.spec.ts:9:1 › Parts - Tabs

1) [chromium] › pages/pui_part.spec.ts:9:1 › Parts - Tabs ──────────────────────────────────────── Error: locator.click: Test timeout of 90000ms exceeded. Call log: - waiting for getByText('1551ACLR') 28 | 29 | // Related Parts > 30 | await page.getByText('1551ACLR').click(); | ^ 31 | await page.getByRole('tab', { name: 'Part Details' }).click(); 32 | await page.getByRole('tab', { name: 'Parameters' }).click(); 33 | await page at /home/runner/work/InvenTree/InvenTree/src/frontend/tests/pages/pui_part.spec.ts:30:36
await page.getByRole('tab', { name: 'Part Details' }).click();
await page.getByRole('tab', { name: 'Parameters' }).click();
await page
Expand Down Expand Up @@ -116,13 +116,15 @@
await page.getByText('5 / 109').waitFor();

// Navigate to the "Allocations" tab
await page.waitForTimeout(500);

await page.getByRole('tab', { name: 'Allocations' }).click();

await page.getByRole('button', { name: 'Build Order Allocations' }).waitFor();
await page.getByRole('button', { name: 'Sales Order Allocations' }).waitFor();

// Expected order reference values
await page.getByText('BO0001').waitFor();

Check failure on line 127 in src/frontend/tests/pages/pui_part.spec.ts

View workflow job for this annotation

GitHub Actions / Tests - Platform UI

[chromium] › pages/pui_part.spec.ts:100:1 › Parts - Allocations

2) [chromium] › pages/pui_part.spec.ts:100:1 › Parts - Allocations ─────────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: locator.waitFor: Test timeout of 90000ms exceeded. Call log: - waiting for getByText('BO0001') to be visible 125 | 126 | // Expected order reference values > 127 | await page.getByText('BO0001').waitFor(); | ^ 128 | await page.getByText('BO0016').waitFor(); 129 | await page.getByText('BO0019').waitFor(); 130 | await page.getByText('SO0008').waitFor(); at /home/runner/work/InvenTree/InvenTree/src/frontend/tests/pages/pui_part.spec.ts:127:34
await page.getByText('BO0016').waitFor();
await page.getByText('BO0019').waitFor();
await page.getByText('SO0008').waitFor();
Expand Down
34 changes: 34 additions & 0 deletions src/frontend/tests/pui_login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,37 @@ test('Login - Failures', async ({ page }) => {

await page.waitForTimeout(2500);
});

test('Login - Change Password', async ({ page }) => {
await doQuickLogin(page, 'noaccess', 'youshallnotpass');

// Navigate to the 'change password' page
await page.goto(`${baseUrl}/settings/user/account`);
await page.getByLabel('action-menu-user-actions').click();
await page.getByLabel('action-menu-user-actions-change-password').click();

// First attempt with some errors
await page.getByLabel('input-password-1').fill('12345');
await page.getByLabel('input-password-2').fill('54321');
await page.getByRole('button', { name: 'Confirm' }).click();
await page.getByText('The two password fields didn’t match').waitFor();

await page.getByLabel('input-password-2').fill('12345');
await page.getByRole('button', { name: 'Confirm' }).click();

await page.getByText('This password is too short').waitFor();
await page.getByText('This password is entirely numeric').waitFor();

await page.getByLabel('input-password-1').fill('youshallnotpass');
await page.getByLabel('input-password-2').fill('youshallnotpass');
await page.getByRole('button', { name: 'Confirm' }).click();

await page.getByText('Password Changed').waitFor();
await page.getByText('The password was set successfully').waitFor();

// Should have redirected to the index page
await page.waitForURL('**/platform/home**');
await page.getByText('InvenTree Demo Server - Norman Nothington');

await page.waitForTimeout(1000);
});
Loading