Admin: kreator usera - kolejnosc danych (imie, nazwisko, haslo + powtorzenie, e-mail, wiek) i walidacja zgodnosci hasel

This commit is contained in:
tisachi
2026-10-16 22:43:45 +02:00
parent 60a4ec705f
commit 2fbecb3093
3 changed files with 63 additions and 24 deletions

View File

@@ -87,10 +87,11 @@ export default function UserForm() {
const gyms: Gym[] = gymsData?.gyms ?? [];
const [form, setForm] = useState({
email: '',
password: '',
firstName: '',
lastName: '',
password: '',
password2: '',
email: '',
age: '',
isAdmin: false,
});
@@ -107,10 +108,11 @@ export default function UserForm() {
if (seededRef.current || !user) return;
seededRef.current = true;
setForm({
email: user.email,
password: '',
firstName: user.firstName ?? '',
lastName: user.lastName ?? '',
password: '',
password2: '',
email: user.email,
age: user.age?.toString() ?? '',
isAdmin: user.isAdmin,
});
@@ -148,6 +150,8 @@ export default function UserForm() {
if (!isEdit && form.password.length < 8) return 'Hasło musi mieć co najmniej 8 znaków.';
if (isEdit && form.password && form.password.length < 8)
return 'Nowe hasło musi mieć co najmniej 8 znaków.';
// w edycji oba pola mogą zostać puste (bez zmiany hasła); w tworzeniu muszą się zgadzać
if (form.password !== form.password2) return 'Podane hasła muszą być takie same.';
return '';
};
@@ -271,24 +275,6 @@ export default function UserForm() {
<H3 className="mb-0">Dane</H3>
</div>
<div className="grid grid-cols-1 gap-x-4 sm:grid-cols-2">
<Field label="E-mail">
<Input
type="email"
value={form.email}
onChange={(e) => setForm({ ...form, email: e.target.value })}
required
/>
</Field>
<Field label={isEdit ? 'Nowe hasło (opcjonalnie)' : 'Hasło (min. 8)'}>
<Input
type="password"
value={form.password}
onChange={(e) => setForm({ ...form, password: e.target.value })}
minLength={8}
required={!isEdit}
placeholder={isEdit ? '••••••••' : undefined}
/>
</Field>
<Field label="Imię">
<Input
value={form.firstName}
@@ -301,6 +287,36 @@ export default function UserForm() {
onChange={(e) => setForm({ ...form, lastName: e.target.value })}
/>
</Field>
<Field label={isEdit ? 'Nowe hasło (opcjonalnie)' : 'Hasło (min. 8)'}>
<Input
type="password"
autoComplete="new-password"
value={form.password}
onChange={(e) => setForm({ ...form, password: e.target.value })}
minLength={8}
required={!isEdit}
placeholder={isEdit ? '••••••••' : undefined}
/>
</Field>
<Field label={isEdit ? 'Powtórz nowe hasło' : 'Powtórz hasło'}>
<Input
type="password"
autoComplete="new-password"
value={form.password2}
onChange={(e) => setForm({ ...form, password2: e.target.value })}
minLength={8}
required={!isEdit}
placeholder={isEdit ? '••••••••' : undefined}
/>
</Field>
<Field label="E-mail">
<Input
type="email"
value={form.email}
onChange={(e) => setForm({ ...form, email: e.target.value })}
required
/>
</Field>
<Field label="Wiek">
<Input
type="number"

View File

@@ -176,6 +176,7 @@ describe('admin UserForm — kreator tworzenia', () => {
await user.type(container.querySelector('input[type="email"]')!, 'nowy3@test.local');
await user.type(container.querySelector('input[type="password"]')!, 'Password123!');
await user.type(container.querySelectorAll('input[type="password"]')[1], 'Password123!');
// wybierz siłownię (placeholder „Szukaj siłowni…”) i zaznacz rolę Trener
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
@@ -214,6 +215,7 @@ describe('admin UserForm — kreator tworzenia', () => {
const { container } = renderForm('/users/new');
await user.type(container.querySelector('input[type="email"]')!, 'nowy4@test.local');
await user.type(container.querySelector('input[type="password"]')!, 'Password123!');
await user.type(container.querySelectorAll('input[type="password"]')[1], 'Password123!');
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
await user.click(screen.getByRole('checkbox', { name: 'Właściciel — siłownia 1' }));
@@ -236,6 +238,7 @@ describe('admin UserForm — kreator tworzenia', () => {
const { container } = renderForm('/users/new');
await user.type(container.querySelector('input[type="email"]')!, 'nowy5@test.local');
await user.type(container.querySelector('input[type="password"]')!, 'Password123!');
await user.type(container.querySelectorAll('input[type="password"]')[1], 'Password123!');
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
await user.click(container.querySelector('input[role="combobox"]')!);
await user.click(await screen.findByRole('option', { name: /Steel Gym/ }));
@@ -248,6 +251,24 @@ describe('admin UserForm — kreator tworzenia', () => {
await user.click(within(dialog).getByRole('button', { name: 'Rozumiem' }));
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
it('blokuje zapis, gdy powtórzone hasło różni się od hasła', async () => {
const calls = mockRoutes([
{ match: '/admin/gyms', reply: () => ({ gyms: [GYM_A] }) },
{ method: 'POST', match: '/admin/users', reply: () => ({ user: CREATED }) },
]);
const user = userEvent.setup();
const { container } = renderForm('/users/new');
const pw = container.querySelectorAll('input[type="password"]');
await user.type(container.querySelector('input[type="email"]')!, 'nowy6@test.local');
await user.type(pw[0], 'Password123!');
await user.type(pw[1], 'InneHaslo123!');
await submitForm();
expect(await screen.findByText('Podane hasła muszą być takie same.')).toBeInTheDocument();
expect(calls.filter((c) => c.init?.method === 'POST')).toHaveLength(0);
});
});
describe('admin UserForm — kreator edycji', () => {

View File

@@ -242,7 +242,8 @@ test('menu kontekstowe formularza „Nowy użytkownik” ma Zapisz, który wysy
});
await page.locator('input[type="email"]').fill('nowy@fitos.pl');
await page.locator('input[type="password"]').fill('Password123!');
await page.locator('input[type="password"]').first().fill('Password123!');
await page.locator('input[type="password"]').nth(1).fill('Password123!');
const postPromise = page.waitForRequest(
(req) => req.method() === 'POST' && req.url().endsWith('/admin/users'),
@@ -261,7 +262,8 @@ test('kreator blokuje zapis niekompletnego członkostwa — modal „Nie można
await page.getByRole('link', { name: 'Użytkownicy' }).click();
await page.getByRole('button', { name: 'Nowy użytkownik' }).click();
await page.locator('input[type="email"]').fill('rola@fitos.pl');
await page.locator('input[type="password"]').fill('Password123!');
await page.locator('input[type="password"]').first().fill('Password123!');
await page.locator('input[type="password"]').nth(1).fill('Password123!');
// rola bez wybranej siłowni → zapis wzbroniony
await page.getByRole('button', { name: 'Dodaj pierwszą siłownię' }).click();