Adds auth pages
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import { renderApp, mockAuthenticated, mockGuest } from '@/test/utils';
|
||||
import { renderApp, renderWithProviders, mockAuthenticated, mockGuest } from '@/test/utils';
|
||||
import { server } from '@/mocks/server';
|
||||
import { setupUninitializedHandlers } from '@/mocks/index';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
import { RoleGuard } from '@/components/auth/RoleGuard';
|
||||
|
||||
describe('Route guards (BR-U1-05, BR-U1-06)', () => {
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('ProtectedRoute (BR-U1-05, BR-U1-06)', () => {
|
||||
it('redirects an unauthenticated user from a protected route to /login', async () => {
|
||||
mockGuest();
|
||||
renderApp('/dashboard');
|
||||
@@ -26,3 +34,54 @@ describe('Route guards (BR-U1-05, BR-U1-06)', () => {
|
||||
expect(screen.queryByTestId('login-form-submit-button')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('InitGuard (BR-U2-09, BR-U2-10)', () => {
|
||||
it('redirects any route to /setup when system is not initialized', async () => {
|
||||
server.use(...setupUninitializedHandlers);
|
||||
mockGuest();
|
||||
renderApp('/login');
|
||||
|
||||
// InitGuard should redirect /login → /setup when not initialized.
|
||||
expect(await screen.findByTestId('setup-submit-button')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('login-form-submit-button')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('allows /setup to render when system is not initialized', async () => {
|
||||
server.use(...setupUninitializedHandlers);
|
||||
mockGuest();
|
||||
renderApp('/setup');
|
||||
|
||||
expect(await screen.findByTestId('setup-submit-button')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('redirects /setup to /login when system is already initialized', async () => {
|
||||
// Default handlers return { initialized: true }.
|
||||
mockGuest();
|
||||
renderApp('/setup');
|
||||
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('setup-submit-button')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('RoleGuard (BR-U2-14–BR-U2-20)', () => {
|
||||
it('renders children when the user has a permitted role', () => {
|
||||
const { getByText } = renderWithProviders(
|
||||
<RoleGuard allowedRoles={['Owner', 'Administrator']}>
|
||||
<p>Protected content</p>
|
||||
</RoleGuard>,
|
||||
);
|
||||
// mockAuthenticated sets role to 'Owner' via makeAuthResponse — see fixtures.
|
||||
expect(getByText('Protected content')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders inline Access Denied when role is insufficient', () => {
|
||||
// renderWithProviders uses the guest (no user) state — no role at all.
|
||||
const { getByTestId } = renderWithProviders(
|
||||
<RoleGuard allowedRoles={['Owner']}>
|
||||
<p>Should not appear</p>
|
||||
</RoleGuard>,
|
||||
);
|
||||
expect(getByTestId('access-denied-message')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user