feat(admin): rebuild admin area around the Article domain
The admin area was managing a Hacker News-style Item model that the
public site never consumed, so the homepage always fell back to sample
data. Rebuild the admin area around the real public content model.
Backend:
- Remove the Item model, schema and admin API
- Admin API now manages articles at /api/v1/admin/articles (CRUD) with
/api/v1/admin/authors and /api/v1/admin/categories reference data
- Add public read-only article endpoints (/api/v1/articles,
/api/v1/articles/{id}): no auth, published-only, newest-first
- Rewrite Article schemas; ArticleRead embeds author and category
- Add idempotent seed_content.py dev seeder (3 authors, 4 categories,
6 published articles)
Frontend:
- Admin UI rewritten around articles: AdminDashboard list, new
EditArticlePage (create/edit, author/category selects, published
toggle, Lexical editor, debounced auto-save)
- Replace AdminItem types and api client with the article domain
- Update App routes, AdminLayout sidebar, DeleteConfirmation, Lexical
editor labels and Home/client comments to article terminology
Tests:
- Rewrite admin unit tests and the Playwright admin e2e spec for the
article domain; add test_articles.py for the public endpoints and
seeder
This commit is contained in:
29 files changed
+2465
-1658
No files matched your search
@@ -4,24 +4,28 @@
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
adminCreateItem,
|
||||
adminDeleteItem,
|
||||
adminGetItem,
|
||||
adminListItems,
|
||||
adminUpdateItem,
|
||||
adminCreateArticle,
|
||||
adminDeleteArticle,
|
||||
adminGetArticle,
|
||||
adminListArticles,
|
||||
adminListAuthors,
|
||||
adminListCategories,
|
||||
adminUpdateArticle,
|
||||
} from '../../api/admin';
|
||||
import { ApiError, TOKEN_KEY } from '../../api/client';
|
||||
import type { AdminItemsPage } from '../../types/admin';
|
||||
import type { AdminArticlesPage } from '../../types/admin';
|
||||
|
||||
const SAMPLE_PAGE: AdminItemsPage = {
|
||||
items: [
|
||||
const SAMPLE_PAGE: AdminArticlesPage = {
|
||||
articles: [
|
||||
{
|
||||
id: 1,
|
||||
title: 'First story',
|
||||
url: 'https://example.com/first',
|
||||
points: 3,
|
||||
username: 'alice',
|
||||
content: '<p>Hello</p>',
|
||||
author_id: 7,
|
||||
category_id: 1,
|
||||
author: { id: 7, name: 'Ada Lovelace', email: 'ada@example.com' },
|
||||
category: { id: 1, name: 'Technology', slug: 'technology' },
|
||||
published: true,
|
||||
created_at: '2024-05-01T12:00:00Z',
|
||||
},
|
||||
],
|
||||
@@ -50,11 +54,11 @@ describe('admin API client', () => {
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
});
|
||||
|
||||
it('listItems builds the query string from all parameters', async () => {
|
||||
it('listArticles builds the query string from all parameters', async () => {
|
||||
localStorage.setItem(TOKEN_KEY, 'admin-token');
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, SAMPLE_PAGE));
|
||||
|
||||
const res = await adminListItems({
|
||||
const res = await adminListArticles({
|
||||
page: 2,
|
||||
pageSize: 50,
|
||||
search: ' breaking news ',
|
||||
@@ -66,7 +70,7 @@ describe('admin API client', () => {
|
||||
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
// URLSearchParams encodes spaces as `+`.
|
||||
expect(url).toBe(
|
||||
'/api/v1/admin/items?page=2&page_size=50&search=breaking+news&sort_by=title&sort_order=asc',
|
||||
'/api/v1/admin/articles?page=2&page_size=50&search=breaking+news&sort_by=title&sort_order=asc',
|
||||
);
|
||||
expect(init.method).toBeUndefined();
|
||||
// The JWT is attached as a Bearer token.
|
||||
@@ -75,10 +79,10 @@ describe('admin API client', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('listItems omits the search parameter when empty', async () => {
|
||||
it('listArticles omits the search parameter when empty', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, SAMPLE_PAGE));
|
||||
|
||||
await adminListItems({
|
||||
await adminListArticles({
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
search: ' ',
|
||||
@@ -88,79 +92,101 @@ describe('admin API client', () => {
|
||||
|
||||
const [url] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe(
|
||||
'/api/v1/admin/items?page=1&page_size=20&sort_by=created_at&sort_order=desc',
|
||||
'/api/v1/admin/articles?page=1&page_size=20&sort_by=created_at&sort_order=desc',
|
||||
);
|
||||
});
|
||||
|
||||
it('getItem requests the item by id', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, SAMPLE_PAGE.items[0]));
|
||||
it('getArticle requests the article by id', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, SAMPLE_PAGE.articles[0]));
|
||||
|
||||
await adminGetItem(7);
|
||||
await adminGetArticle(7);
|
||||
|
||||
const [url] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe('/api/v1/admin/items/7');
|
||||
expect(url).toBe('/api/v1/admin/articles/7');
|
||||
});
|
||||
|
||||
it('createItem POSTs the payload', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(201, SAMPLE_PAGE.items[0]));
|
||||
it('createArticle POSTs the payload', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(201, SAMPLE_PAGE.articles[0]));
|
||||
|
||||
await adminCreateItem({
|
||||
await adminCreateArticle({
|
||||
title: 'New story',
|
||||
url: '',
|
||||
points: 0,
|
||||
username: 'root',
|
||||
content: '<p>Body</p>',
|
||||
author_id: 7,
|
||||
category_id: 2,
|
||||
published: false,
|
||||
});
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe('/api/v1/admin/items');
|
||||
expect(url).toBe('/api/v1/admin/articles');
|
||||
expect(init.method).toBe('POST');
|
||||
expect(JSON.parse(init.body as string)).toEqual({
|
||||
title: 'New story',
|
||||
url: '',
|
||||
points: 0,
|
||||
username: 'root',
|
||||
content: '<p>Body</p>',
|
||||
author_id: 7,
|
||||
category_id: 2,
|
||||
published: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('updateItem PUTs the payload to the item route', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, SAMPLE_PAGE.items[0]));
|
||||
it('updateArticle PUTs the payload to the article route', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, SAMPLE_PAGE.articles[0]));
|
||||
|
||||
await adminUpdateItem(7, {
|
||||
await adminUpdateArticle(7, {
|
||||
title: 'Updated',
|
||||
url: 'https://example.com/new',
|
||||
points: 9,
|
||||
username: 'root',
|
||||
content: '',
|
||||
content: '<p>Body</p>',
|
||||
author_id: 8,
|
||||
category_id: 1,
|
||||
published: true,
|
||||
});
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe('/api/v1/admin/items/7');
|
||||
expect(url).toBe('/api/v1/admin/articles/7');
|
||||
expect(init.method).toBe('PUT');
|
||||
});
|
||||
|
||||
it('deleteItem sends DELETE and resolves to undefined on 204', async () => {
|
||||
it('deleteArticle sends DELETE and resolves to undefined on 204', async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 204,
|
||||
statusText: 'No Content',
|
||||
} as Response);
|
||||
|
||||
await expect(adminDeleteItem(7)).resolves.toBeUndefined();
|
||||
await expect(adminDeleteArticle(7)).resolves.toBeUndefined();
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe('/api/v1/admin/items/7');
|
||||
expect(url).toBe('/api/v1/admin/articles/7');
|
||||
expect(init.method).toBe('DELETE');
|
||||
});
|
||||
|
||||
it('listAuthors requests the author reference list', async () => {
|
||||
const authors = [{ id: 1, name: 'Ada Lovelace', email: 'ada@example.com', bio: null }];
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, authors));
|
||||
|
||||
const res = await adminListAuthors();
|
||||
|
||||
expect(res).toEqual(authors);
|
||||
const [url] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe('/api/v1/admin/authors');
|
||||
});
|
||||
|
||||
it('listCategories requests the category reference list', async () => {
|
||||
const categories = [{ id: 1, name: 'Technology', slug: 'technology' }];
|
||||
fetchMock.mockResolvedValue(jsonResponse(200, categories));
|
||||
|
||||
const res = await adminListCategories();
|
||||
|
||||
expect(res).toEqual(categories);
|
||||
const [url] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe('/api/v1/admin/categories');
|
||||
});
|
||||
|
||||
it('surfaces the backend error for non-admin access (403)', async () => {
|
||||
localStorage.setItem(TOKEN_KEY, 'non-admin-token');
|
||||
fetchMock.mockResolvedValue(
|
||||
jsonResponse(403, { detail: 'Admin access required', code: 'forbidden' }),
|
||||
);
|
||||
|
||||
const err = await adminListItems({
|
||||
const err = await adminListArticles({
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
search: '',
|
||||
|
||||
Reference in new issue
Block a user