Adds profile and settings pages
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { renderApp, mockGuest } from '@/test/utils';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('AccessDeniedPage', () => {
|
||||
it('renders the 403 title and message', async () => {
|
||||
renderApp('/403');
|
||||
|
||||
expect(await screen.findByTestId('access-denied-title', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
expect(screen.getByTestId('access-denied-message')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the back to dashboard button', async () => {
|
||||
renderApp('/403');
|
||||
|
||||
expect(await screen.findByTestId('access-denied-back-button', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('navigates to login when back button is clicked as guest', async () => {
|
||||
mockGuest();
|
||||
renderApp('/403');
|
||||
|
||||
const button = await screen.findByTestId('access-denied-back-button', {}, { timeout: 5000 });
|
||||
await userEvent.click(button);
|
||||
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ShieldOff } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useNavigate } from '@tanstack/react-router';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
export function AccessDeniedPage() {
|
||||
const { t } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center py-16 text-center space-y-4">
|
||||
<ShieldOff className="size-12 text-destructive" />
|
||||
<h1 className="text-2xl font-semibold" data-testid="access-denied-title">
|
||||
{t('error.403.title')}
|
||||
</h1>
|
||||
<p className="text-muted-foreground max-w-sm" data-testid="access-denied-message">
|
||||
{t('error.403.message')}
|
||||
</p>
|
||||
<Button
|
||||
onClick={() => void navigate({ to: '/dashboard' })}
|
||||
data-testid="access-denied-back-button"
|
||||
>
|
||||
{t('error.backToDashboard')}
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import { renderApp, mockAuthenticated, mockGuest } from '@/test/utils';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('CmsPage', () => {
|
||||
it('renders the CMS page title and placeholder', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/cms');
|
||||
|
||||
expect(await screen.findByTestId('cms-title', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
expect(screen.getByTestId('cms-placeholder')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('redirects unauthenticated users to login', async () => {
|
||||
mockGuest();
|
||||
renderApp('/cms');
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,17 @@
|
||||
import { LayoutGrid } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
/** Placeholder; CMS management (Owner-only, US-18/US-20) arrives in a later unit. */
|
||||
export function CmsPage() {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="flex flex-col items-center justify-center py-16 text-center space-y-4">
|
||||
<LayoutGrid className="size-12 text-muted-foreground" />
|
||||
<h1 className="text-2xl font-semibold" data-testid="cms-title">
|
||||
{t('nav.cms')}
|
||||
{t('cms.title')}
|
||||
</h1>
|
||||
<p className="text-muted-foreground">Coming soon.</p>
|
||||
<p className="text-muted-foreground max-w-sm" data-testid="cms-placeholder">
|
||||
{t('cms.description')}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { renderApp, mockGuest } from '@/test/utils';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('NotFoundPage', () => {
|
||||
it('renders for an unknown route', async () => {
|
||||
renderApp('/this-route-does-not-exist');
|
||||
|
||||
expect(await screen.findByTestId('not-found-title', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
expect(screen.getByTestId('not-found-message')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the back to dashboard button', async () => {
|
||||
renderApp('/some/nonexistent/path');
|
||||
|
||||
expect(await screen.findByTestId('not-found-back-button', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('navigates to login when back button is clicked as guest', async () => {
|
||||
mockGuest();
|
||||
renderApp('/this-does-not-exist');
|
||||
|
||||
const button = await screen.findByTestId('not-found-back-button', {}, { timeout: 5000 });
|
||||
await userEvent.click(button);
|
||||
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { FileQuestion } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useNavigate } from '@tanstack/react-router';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
export function NotFoundPage() {
|
||||
const { t } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center py-16 text-center space-y-4">
|
||||
<FileQuestion className="size-12 text-muted-foreground" />
|
||||
<h1 className="text-2xl font-semibold" data-testid="not-found-title">
|
||||
{t('error.404.title')}
|
||||
</h1>
|
||||
<p className="text-muted-foreground max-w-sm" data-testid="not-found-message">
|
||||
{t('error.404.message')}
|
||||
</p>
|
||||
<Button
|
||||
onClick={() => void navigate({ to: '/dashboard' })}
|
||||
data-testid="not-found-back-button"
|
||||
>
|
||||
{t('error.backToDashboard')}
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { renderApp, mockAuthenticated, mockGuest } from '@/test/utils';
|
||||
import { server } from '@/mocks/server';
|
||||
import { API_BASE } from '@/mocks/auth/fixtures';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('ProfilePage', () => {
|
||||
it('renders the page title and profile form', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
expect(await screen.findByTestId('profile-title', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
expect(screen.getByTestId('profile-name-input')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('profile-email-input')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('profile-role-badge')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('pre-fills name and email from authenticated user', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
const nameInput = await screen.findByTestId<HTMLInputElement>('profile-name-input');
|
||||
const emailInput = screen.getByTestId<HTMLInputElement>('profile-email-input');
|
||||
|
||||
expect(nameInput.value).toBe('Test Owner');
|
||||
expect(emailInput.value).toBe('owner@example.com');
|
||||
});
|
||||
|
||||
it('save button is disabled when form is pristine', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
await screen.findByTestId('profile-name-input');
|
||||
expect(screen.getByTestId('profile-save-button')).toBeDisabled();
|
||||
});
|
||||
|
||||
it('save button enables after editing a field', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
const nameInput = await screen.findByTestId('profile-name-input');
|
||||
await userEvent.clear(nameInput);
|
||||
await userEvent.type(nameInput, 'Updated Name');
|
||||
|
||||
expect(screen.getByTestId('profile-save-button')).not.toBeDisabled();
|
||||
});
|
||||
|
||||
it('saves profile successfully and shows success toast', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
const nameInput = await screen.findByTestId('profile-name-input');
|
||||
await userEvent.clear(nameInput);
|
||||
await userEvent.type(nameInput, 'New Name');
|
||||
await userEvent.click(screen.getByTestId('profile-save-button'));
|
||||
|
||||
expect(await screen.findByText(/profile updated/i, {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows validation error for invalid email', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
// Type invalid email — this makes form dirty and triggers validation on submit
|
||||
const emailInput = await screen.findByTestId('profile-email-input');
|
||||
await userEvent.tripleClick(emailInput);
|
||||
await userEvent.type(emailInput, 'not-an-email');
|
||||
|
||||
const saveButton = screen.getByTestId('profile-save-button');
|
||||
expect(saveButton).not.toBeDisabled();
|
||||
await userEvent.click(saveButton);
|
||||
|
||||
expect(await screen.findByTestId('profile-email-error')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('opens change password dialog when button is clicked', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
await screen.findByTestId('change-password-button');
|
||||
await userEvent.click(screen.getByTestId('change-password-button'));
|
||||
|
||||
expect(await screen.findByTestId('change-password-dialog')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error in dialog when current password is wrong', async () => {
|
||||
server.use(
|
||||
http.post(`${API_BASE}/api/v1/Auth/change-password`, () =>
|
||||
HttpResponse.json({ title: 'Bad Request' }, { status: 400 }),
|
||||
),
|
||||
);
|
||||
mockAuthenticated();
|
||||
renderApp('/profile');
|
||||
|
||||
await screen.findByTestId('change-password-button');
|
||||
await userEvent.click(screen.getByTestId('change-password-button'));
|
||||
|
||||
await screen.findByTestId('change-password-dialog');
|
||||
// PasswordField auto-generates testid as ${id}-input
|
||||
await userEvent.type(screen.getByTestId('currentPassword-input'), 'WrongPass1!');
|
||||
await userEvent.type(screen.getByTestId('newPassword-input'), 'NewPassword1!');
|
||||
await userEvent.type(screen.getByTestId('confirmPassword-input'), 'NewPassword1!');
|
||||
await userEvent.click(screen.getByTestId('change-password-submit'));
|
||||
|
||||
expect(await screen.findByTestId('current-password-error')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('redirects unauthenticated users to login', async () => {
|
||||
mockGuest();
|
||||
renderApp('/profile');
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,287 @@
|
||||
import { useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { z } from 'zod';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { toast } from 'sonner';
|
||||
import { useAuth } from '@/contexts/auth-context';
|
||||
import { useUpdateProfile, useChangePassword } from '@/api/useProfile';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { RoleBadge } from '@/components/shared/RoleBadge';
|
||||
import { PasswordField } from '@/components/ui/PasswordField';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import { ProblemDetailsError } from '@/lib/api-client';
|
||||
|
||||
const profileSchema = z.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
email: z.string().email('Enter a valid email address'),
|
||||
});
|
||||
|
||||
const changePasswordSchema = z
|
||||
.object({
|
||||
currentPassword: z.string().min(1, 'Required'),
|
||||
newPassword: z
|
||||
.string()
|
||||
.min(8, 'At least 8 characters')
|
||||
.regex(/[A-Z]/, 'At least 1 uppercase letter')
|
||||
.regex(/[a-z]/, 'At least 1 lowercase letter')
|
||||
.regex(/[0-9]/, 'At least 1 digit')
|
||||
.regex(/[^a-zA-Z0-9]/, 'At least 1 special character'),
|
||||
confirmPassword: z.string().min(1, 'Required'),
|
||||
})
|
||||
.refine((d) => d.newPassword === d.confirmPassword, {
|
||||
message: 'Passwords do not match',
|
||||
path: ['confirmPassword'],
|
||||
});
|
||||
|
||||
type ProfileFormData = z.infer<typeof profileSchema>;
|
||||
type ChangePasswordFormData = z.infer<typeof changePasswordSchema>;
|
||||
|
||||
function ChangePasswordDialog({
|
||||
open,
|
||||
onClose,
|
||||
}: {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const changePassword = useChangePassword();
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
reset,
|
||||
setError,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm<ChangePasswordFormData>({
|
||||
resolver: zodResolver(changePasswordSchema),
|
||||
});
|
||||
|
||||
const onSubmit = async (data: ChangePasswordFormData) => {
|
||||
try {
|
||||
await changePassword.mutateAsync({
|
||||
currentPassword: data.currentPassword,
|
||||
newPassword: data.newPassword,
|
||||
});
|
||||
toast.success(t('profile.changePasswordDialog.success'));
|
||||
reset();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
if (err instanceof ProblemDetailsError && err.status === 400) {
|
||||
setError('currentPassword', {
|
||||
message: t('profile.changePasswordDialog.errorCurrent'),
|
||||
});
|
||||
} else {
|
||||
setError('root', { message: t('errors.generic') });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
reset();
|
||||
onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={(o) => { if (!o) handleClose(); }}>
|
||||
<DialogContent data-testid="change-password-dialog">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t('profile.changePasswordDialog.title')}</DialogTitle>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
{errors.root && (
|
||||
<p className="text-sm text-destructive" data-testid="change-password-error">
|
||||
{errors.root.message}
|
||||
</p>
|
||||
)}
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="currentPassword">
|
||||
{t('profile.changePasswordDialog.current')}
|
||||
</Label>
|
||||
<PasswordField
|
||||
id="currentPassword"
|
||||
data-testid="current-password-input"
|
||||
{...register('currentPassword')}
|
||||
/>
|
||||
{errors.currentPassword && (
|
||||
<p className="text-sm text-destructive" data-testid="current-password-error">
|
||||
{errors.currentPassword.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="newPassword">
|
||||
{t('profile.changePasswordDialog.new')}
|
||||
</Label>
|
||||
<PasswordField
|
||||
id="newPassword"
|
||||
data-testid="new-password-input"
|
||||
{...register('newPassword')}
|
||||
/>
|
||||
{errors.newPassword && (
|
||||
<p className="text-sm text-destructive" data-testid="new-password-error">
|
||||
{errors.newPassword.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="confirmPassword">
|
||||
{t('profile.changePasswordDialog.confirm')}
|
||||
</Label>
|
||||
<PasswordField
|
||||
id="confirmPassword"
|
||||
data-testid="confirm-password-input"
|
||||
{...register('confirmPassword')}
|
||||
/>
|
||||
{errors.confirmPassword && (
|
||||
<p className="text-sm text-destructive" data-testid="confirm-password-error">
|
||||
{errors.confirmPassword.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex justify-end gap-2 pt-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={handleClose}
|
||||
data-testid="change-password-cancel"
|
||||
>
|
||||
{t('common.cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
data-testid="change-password-submit"
|
||||
>
|
||||
{isSubmitting
|
||||
? t('profile.changePasswordDialog.submitting')
|
||||
: t('profile.changePasswordDialog.submit')}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export function ProfilePage() {
|
||||
const { t } = useTranslation();
|
||||
const { user } = useAuth();
|
||||
const updateProfile = useUpdateProfile();
|
||||
const [passwordDialogOpen, setPasswordDialogOpen] = useState(false);
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors, isSubmitting, isDirty },
|
||||
} = useForm<ProfileFormData>({
|
||||
resolver: zodResolver(profileSchema),
|
||||
defaultValues: {
|
||||
name: user?.name ?? '',
|
||||
email: user?.email ?? '',
|
||||
},
|
||||
});
|
||||
|
||||
const onSubmit = async (data: ProfileFormData) => {
|
||||
try {
|
||||
await updateProfile.mutateAsync(data);
|
||||
toast.success(t('profile.saveSuccess'));
|
||||
} catch (err) {
|
||||
if (err instanceof ProblemDetailsError && err.status === 400) {
|
||||
toast.error(t('errors.generic'));
|
||||
} else {
|
||||
toast.error(t('errors.generic'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="space-y-6 max-w-lg">
|
||||
<h1 className="text-2xl font-semibold" data-testid="profile-title">
|
||||
{t('nav.profile')}
|
||||
{t('profile.title')}
|
||||
</h1>
|
||||
<p className="text-muted-foreground">Coming soon.</p>
|
||||
|
||||
{/* Profile info card */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">{t('profile.title')}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="name">{t('profile.name')}</Label>
|
||||
<Input
|
||||
id="name"
|
||||
data-testid="profile-name-input"
|
||||
{...register('name')}
|
||||
/>
|
||||
{errors.name && (
|
||||
<p className="text-sm text-destructive" data-testid="profile-name-error">
|
||||
{errors.name.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="email">{t('profile.email')}</Label>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
data-testid="profile-email-input"
|
||||
{...register('email')}
|
||||
/>
|
||||
{errors.email && (
|
||||
<p className="text-sm text-destructive" data-testid="profile-email-error">
|
||||
{errors.email.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<Label>{t('profile.role')}</Label>
|
||||
<div>
|
||||
{user?.role
|
||||
? <RoleBadge role={user.role} data-testid="profile-role-badge" />
|
||||
: <span data-testid="profile-role-badge">—</span>}
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={isSubmitting || !isDirty}
|
||||
data-testid="profile-save-button"
|
||||
>
|
||||
{isSubmitting ? t('profile.saving') : t('profile.save')}
|
||||
</Button>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Security card */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">Security</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setPasswordDialogOpen(true)}
|
||||
data-testid="change-password-button"
|
||||
>
|
||||
{t('profile.changePassword')}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<ChangePasswordDialog
|
||||
open={passwordDialogOpen}
|
||||
onClose={() => setPasswordDialogOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { renderApp, mockAuthenticated, mockGuest } from '@/test/utils';
|
||||
import { server } from '@/mocks/server';
|
||||
import { API_BASE } from '@/mocks/auth/fixtures';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('SettingsPage', () => {
|
||||
it('renders the page title', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
expect(await screen.findByTestId('settings-title', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the current availability status badge', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
expect(await screen.findByTestId('availability-badge', {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders all three availability mode buttons', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('availability-mode-selector', {}, { timeout: 5000 });
|
||||
expect(screen.getByTestId('mode-option-Available')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('mode-option-Maintenance')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('mode-option-NotAvailable')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('selecting a mode highlights that mode button', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('availability-mode-selector', {}, { timeout: 5000 });
|
||||
const maintenanceBtn = screen.getByTestId('mode-option-Maintenance');
|
||||
await userEvent.click(maintenanceBtn);
|
||||
|
||||
expect(maintenanceBtn.className).toContain('bg-primary');
|
||||
});
|
||||
|
||||
it('saves availability and shows success toast', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('availability-save-button', {}, { timeout: 5000 });
|
||||
await userEvent.click(screen.getByTestId('availability-save-button'));
|
||||
|
||||
expect(await screen.findByText(/availability updated/i, {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error toast when save fails', async () => {
|
||||
server.use(
|
||||
http.post(`${API_BASE}/api/v1/Availability/admin/status`, () =>
|
||||
HttpResponse.json({ title: 'Error' }, { status: 500 }),
|
||||
),
|
||||
);
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('availability-save-button', {}, { timeout: 5000 });
|
||||
await userEvent.click(screen.getByTestId('availability-save-button'));
|
||||
|
||||
expect(await screen.findByText(/something went wrong/i, {}, { timeout: 5000 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders all placeholder sections', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('settings-title', {}, { timeout: 5000 });
|
||||
expect(screen.getByTestId('placeholder-settings.modules.title')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('placeholder-settings.systemConfig.title')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('placeholder-settings.branding.title')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('redirects unauthenticated users to login', async () => {
|
||||
mockGuest();
|
||||
renderApp('/settings');
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,138 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { toast } from 'sonner';
|
||||
import { Lock } from 'lucide-react';
|
||||
import { useAvailabilityStatus, useUpdateAvailability } from '@/api/useAvailability';
|
||||
import { AvailabilityStatusBadge } from '@/components/shared/AvailabilityStatusBadge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import type { AvailabilityStatus } from '@/api/types';
|
||||
|
||||
function PlaceholderCard({ titleKey, comingSoonKey }: { titleKey: string; comingSoonKey: string }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<Card className="opacity-60">
|
||||
<CardHeader className="flex flex-row items-center gap-2">
|
||||
<Lock className="size-4 text-muted-foreground" />
|
||||
<CardTitle className="text-base">{t(titleKey)}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-sm text-muted-foreground" data-testid={`placeholder-${titleKey}`}>
|
||||
{t(comingSoonKey)}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export function SettingsPage() {
|
||||
const { t } = useTranslation();
|
||||
const { data: availability, isLoading } = useAvailabilityStatus();
|
||||
const updateAvailability = useUpdateAvailability();
|
||||
|
||||
const [selectedMode, setSelectedMode] = useState<AvailabilityStatus>('Available');
|
||||
const [reason, setReason] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
if (availability) {
|
||||
setSelectedMode(availability.status);
|
||||
setReason(availability.message ?? '');
|
||||
}
|
||||
}, [availability]);
|
||||
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
await updateAvailability.mutateAsync({ newStatus: selectedMode, reason });
|
||||
toast.success(t('settings.availability.saveSuccess'));
|
||||
} catch {
|
||||
toast.error(t('errors.generic'));
|
||||
}
|
||||
};
|
||||
|
||||
const modes: AvailabilityStatus[] = ['Available', 'Maintenance', 'NotAvailable'];
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="space-y-6 max-w-xl">
|
||||
<h1 className="text-2xl font-semibold" data-testid="settings-title">
|
||||
{t('nav.settings')}
|
||||
{t('settings.title')}
|
||||
</h1>
|
||||
<p className="text-muted-foreground">Coming soon.</p>
|
||||
|
||||
{/* Availability section */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">{t('settings.availability.title')}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{isLoading ? (
|
||||
<p className="text-sm text-muted-foreground">{t('common.loading')}</p>
|
||||
) : availability ? (
|
||||
<AvailabilityStatusBadge
|
||||
status={availability.status}
|
||||
message={availability.message}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>{t('settings.availability.mode')}</Label>
|
||||
<div className="flex gap-2 flex-wrap" data-testid="availability-mode-selector">
|
||||
{modes.map((mode) => (
|
||||
<button
|
||||
key={mode}
|
||||
type="button"
|
||||
onClick={() => setSelectedMode(mode)}
|
||||
data-testid={`mode-option-${mode}`}
|
||||
className={`rounded-full border px-4 py-1.5 text-sm font-medium transition-colors ${
|
||||
selectedMode === mode
|
||||
? 'border-primary bg-primary text-primary-foreground'
|
||||
: 'border-input bg-background hover:bg-accent'
|
||||
}`}
|
||||
>
|
||||
{t(`settings.availability.modes.${mode}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="availability-reason">
|
||||
{t('settings.availability.reason')}
|
||||
</Label>
|
||||
<textarea
|
||||
id="availability-reason"
|
||||
rows={2}
|
||||
value={reason}
|
||||
onChange={(e) => setReason(e.target.value)}
|
||||
data-testid="availability-reason-input"
|
||||
className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring"
|
||||
placeholder={t('settings.availability.reason')}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={updateAvailability.isPending}
|
||||
data-testid="availability-save-button"
|
||||
>
|
||||
{updateAvailability.isPending
|
||||
? t('settings.availability.saving')
|
||||
: t('settings.availability.save')}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<PlaceholderCard
|
||||
titleKey="settings.modules.title"
|
||||
comingSoonKey="settings.modules.comingSoon"
|
||||
/>
|
||||
<PlaceholderCard
|
||||
titleKey="settings.systemConfig.title"
|
||||
comingSoonKey="settings.systemConfig.comingSoon"
|
||||
/>
|
||||
<PlaceholderCard
|
||||
titleKey="settings.branding.title"
|
||||
comingSoonKey="settings.branding.comingSoon"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -33,18 +33,13 @@ import {
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { InviteUserDialog } from '@/components/users/InviteUserDialog';
|
||||
import { DeleteUserDialog } from '@/components/users/DeleteUserDialog';
|
||||
import { RoleBadge } from '@/components/shared/RoleBadge';
|
||||
import { useUsers, useChangeRole, useSetUserActive, useDeleteUser } from '@/api/useUsers';
|
||||
import { useAuth } from '@/contexts/auth-context';
|
||||
import { Trans } from 'react-i18next';
|
||||
import { ProblemDetailsError } from '@/lib/api-client';
|
||||
import type { UserRole, UserListItem } from '@/api/types';
|
||||
|
||||
function roleBadgeVariant(role: UserRole) {
|
||||
if (role === 'Owner') return 'default';
|
||||
if (role === 'Administrator') return 'secondary';
|
||||
return 'outline';
|
||||
}
|
||||
|
||||
function statusBadgeVariant(user: UserListItem) {
|
||||
if (user.invitationPending) return 'outline';
|
||||
if (!user.isActive) return 'destructive';
|
||||
@@ -248,9 +243,7 @@ export function UsersPage() {
|
||||
<TableCell className="font-medium">{user.name}</TableCell>
|
||||
<TableCell>{user.email}</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={roleBadgeVariant(user.role)}>
|
||||
{t(`users.roles.${user.role}`)}
|
||||
</Badge>
|
||||
<RoleBadge role={user.role} />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={statusBadgeVariant(user)}>
|
||||
|
||||
Reference in New Issue
Block a user