feat: add admin area with item CRUD, Lexical editor and access control (M6)
Backend: - Item model (title, url, points, username, content, created_at) - is_admin flag on User + startup migration for pre-existing DBs - get_current_admin dependency (401 without/invalid token, 403 for non-admins); server-side check on every admin endpoint - /api/v1/admin/items CRUD: pagination, case-insensitive title/url search, sort by title/created_at/points, in-memory rate limiting (1000 req/60s per admin -> 429) - seed_admin.py (ADMIN_USERNAME/ADMIN_PASSWORD), documented in backend/.env.example Frontend: - Auth context exposes is_admin; RequireAdmin route guard (redirects signed-out users, renders 403 page for non-admins) - AdminLayout with responsive dark sidebar; Admin link in header for admins only - AdminDashboard: paginated table (page size 10/20/50), 300ms debounced search, sortable title/points/created_at columns, edit/delete row actions - EditItemPage: create + edit with client-side validation and a Lexical (0.25) WYSIWYG editor (bold/italic/underline, H1-H6, lists, code blocks, links, undo/redo), optional debounced auto-save - DeleteConfirmation modal with warning icon and explicit "Yes, delete" confirmation - Unit tests (34) and mocked E2E workflow tests (10) All acceptance criteria for milestones/m6-setup-admin-area-prompt.md verified: 108 backend, 87 frontend unit and 29 E2E tests pass.
This commit is contained in:
1 parent
b186d0f797
commit
b440809548
38 files changed
+5415
-50
No files matched your search
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Tests for the RequireAdmin route guard (M6): signed-out users are
|
||||
* redirected to /login, authenticated non-admins see the 403 page, and
|
||||
* admins get the protected content.
|
||||
*/
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { Mock } from 'vitest';
|
||||
import { TOKEN_KEY, me as clientMe } from '../../api/client';
|
||||
import { AuthProvider, RequireAdmin } from '../../auth';
|
||||
|
||||
vi.mock('../../api/client', () => ({
|
||||
TOKEN_KEY: 'AUTH_TOKEN',
|
||||
login: vi.fn(),
|
||||
register: vi.fn(),
|
||||
me: vi.fn(),
|
||||
logout: vi.fn(),
|
||||
}));
|
||||
|
||||
const routes = (
|
||||
<Routes>
|
||||
<Route path="/login" element={<div>Login page</div>} />
|
||||
<Route
|
||||
path="/admin"
|
||||
element={
|
||||
<RequireAdmin>
|
||||
<div data-testid="admin-content">Admin dashboard</div>
|
||||
</RequireAdmin>
|
||||
}
|
||||
/>
|
||||
</Routes>
|
||||
);
|
||||
|
||||
function renderAt(path: string) {
|
||||
return render(
|
||||
<MemoryRouter
|
||||
initialEntries={[path]}
|
||||
future={{ v7_startTransition: true, v7_relativeSplatPath: true }}
|
||||
>
|
||||
<AuthProvider>{routes}</AuthProvider>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
}
|
||||
|
||||
describe('RequireAdmin', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('redirects signed-out users to /login', async () => {
|
||||
renderAt('/admin');
|
||||
expect(await screen.findByText('Login page')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the 403 page for an authenticated non-admin', async () => {
|
||||
(clientMe as Mock).mockResolvedValue({ username: 'alice', is_admin: false });
|
||||
localStorage.setItem(TOKEN_KEY, 'token');
|
||||
|
||||
renderAt('/admin');
|
||||
|
||||
expect(await screen.findByTestId('forbidden-page')).toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { level: 1, name: '403' })).toBeInTheDocument();
|
||||
expect(screen.getByRole('alert')).toHaveTextContent(/admin area/i);
|
||||
expect(screen.queryByTestId('admin-content')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('treats a missing is_admin flag as non-admin (defensive)', async () => {
|
||||
(clientMe as Mock).mockResolvedValue({ username: 'alice' });
|
||||
localStorage.setItem(TOKEN_KEY, 'token');
|
||||
|
||||
renderAt('/admin');
|
||||
|
||||
expect(await screen.findByTestId('forbidden-page')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the protected content for an admin', async () => {
|
||||
(clientMe as Mock).mockResolvedValue({ username: 'root', is_admin: true });
|
||||
localStorage.setItem(TOKEN_KEY, 'token');
|
||||
|
||||
renderAt('/admin');
|
||||
|
||||
expect(await screen.findByTestId('admin-content')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('forbidden-page')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user