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.
196 lines
6.9 KiB
Python
196 lines
6.9 KiB
Python
"""Tests for ORM model instantiation and database operations."""
|
|
|
|
import pytest
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from models import Article, Author, Category, Item, User, slugify
|
|
|
|
|
|
class TestAuthor:
|
|
def test_create_and_commit(self, session) -> None:
|
|
author = Author(
|
|
name="Ada Lovelace", email="ada@example.com", bio="First programmer."
|
|
)
|
|
session.add(author)
|
|
session.commit()
|
|
session.refresh(author)
|
|
assert author.id is not None
|
|
assert session.get(Author, author.id).email == "ada@example.com"
|
|
|
|
def test_unique_name_enforced(self, session) -> None:
|
|
session.add(Author(name="Grace Hopper", email="grace@example.com"))
|
|
session.commit()
|
|
session.add(Author(name="Grace Hopper", email="other@example.com"))
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
def test_unique_email_enforced(self, session) -> None:
|
|
session.add(Author(name="Grace Hopper", email="grace@example.com"))
|
|
session.commit()
|
|
session.add(Author(name="Another Person", email="grace@example.com"))
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
def test_bio_is_nullable(self, session) -> None:
|
|
author = Author(name="Alan Turing", email="alan@example.com")
|
|
session.add(author)
|
|
session.commit()
|
|
session.refresh(author)
|
|
assert author.bio is None
|
|
|
|
|
|
class TestUser:
|
|
def test_create_and_commit(self, session) -> None:
|
|
user = User(username="alice", password_hash="abc$def")
|
|
session.add(user)
|
|
session.commit()
|
|
session.refresh(user)
|
|
assert user.id is not None
|
|
assert user.created_at is not None
|
|
assert user.is_admin is False # default: no admin rights (M6)
|
|
assert session.get(User, user.id).username == "alice"
|
|
|
|
def test_is_admin_can_be_set(self, session) -> None:
|
|
user = User(username="root", password_hash="abc$def", is_admin=True)
|
|
session.add(user)
|
|
session.commit()
|
|
session.refresh(user)
|
|
assert user.is_admin is True
|
|
|
|
def test_unique_username_enforced(self, session) -> None:
|
|
session.add(User(username="alice", password_hash="abc$def"))
|
|
session.commit()
|
|
session.add(User(username="alice", password_hash="other$hash"))
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
|
|
class TestCategory:
|
|
def test_create_and_commit(self, session) -> None:
|
|
category = Category(name="Technology", slug="technology")
|
|
session.add(category)
|
|
session.commit()
|
|
session.refresh(category)
|
|
assert category.id is not None
|
|
assert session.get(Category, category.id).name == "Technology"
|
|
|
|
def test_unique_name_enforced(self, session) -> None:
|
|
session.add(Category(name="Science", slug="science"))
|
|
session.commit()
|
|
session.add(Category(name="Science", slug="science-2"))
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
def test_unique_slug_enforced(self, session) -> None:
|
|
session.add(Category(name="Science", slug="science"))
|
|
session.commit()
|
|
session.add(Category(name="Science News", slug="science"))
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
def test_slug_generation(self) -> None:
|
|
assert slugify("Technology & Science") == "technology-science"
|
|
assert slugify(" Sports ") == "sports"
|
|
assert slugify("Already-fine") == "already-fine"
|
|
assert slugify("Café News") == "cafe-news"
|
|
|
|
|
|
class TestArticle:
|
|
def _make_author_category(self, session) -> tuple[Author, Category]:
|
|
author = Author(name="Byline Writer", email="writer@example.com")
|
|
category = Category(name="World", slug="world")
|
|
session.add_all([author, category])
|
|
session.commit()
|
|
session.refresh(author)
|
|
session.refresh(category)
|
|
return author, category
|
|
|
|
def test_create_and_commit(self, session) -> None:
|
|
author, category = self._make_author_category(session)
|
|
article = Article(
|
|
title="First post",
|
|
content="Hello world.",
|
|
author_id=author.id,
|
|
category_id=category.id,
|
|
)
|
|
session.add(article)
|
|
session.commit()
|
|
session.refresh(article)
|
|
assert article.id is not None
|
|
assert article.created_at is not None
|
|
assert article.published is False
|
|
|
|
def test_foreign_key_relationships(self, session) -> None:
|
|
author, category = self._make_author_category(session)
|
|
article = Article(
|
|
title="Related",
|
|
content="Body text.",
|
|
author_id=author.id,
|
|
category_id=category.id,
|
|
)
|
|
session.add(article)
|
|
session.commit()
|
|
assert article.author.name == author.name
|
|
assert article.category.slug == "world"
|
|
assert article in author.articles
|
|
assert article in category.articles
|
|
|
|
def test_unique_title_enforced(self, session) -> None:
|
|
author, category = self._make_author_category(session)
|
|
session.add(
|
|
Article(title="Dup", content="one", author_id=author.id, category_id=category.id)
|
|
)
|
|
session.commit()
|
|
session.add(
|
|
Article(title="Dup", content="two", author_id=author.id, category_id=category.id)
|
|
)
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
def test_foreign_key_enforced(self, session) -> None:
|
|
author, category = self._make_author_category(session)
|
|
article = Article(
|
|
title="Orphan", content="No such author.", author_id=9999, category_id=category.id
|
|
)
|
|
session.add(article)
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|
|
session.rollback()
|
|
|
|
|
|
class TestItem:
|
|
def test_create_and_commit_defaults(self, session) -> None:
|
|
item = Item(title="Breaking: things happen")
|
|
session.add(item)
|
|
session.commit()
|
|
session.refresh(item)
|
|
assert item.id is not None
|
|
assert item.url == ""
|
|
assert item.points == 0
|
|
assert item.username == "anonymous"
|
|
assert item.content == ""
|
|
assert item.created_at is not None
|
|
|
|
def test_create_with_all_fields(self, session) -> None:
|
|
item = Item(
|
|
title="Show HN: a new tool",
|
|
url="https://example.com/show-hn",
|
|
points=42,
|
|
username="alice",
|
|
content="<p>Hello <strong>world</strong></p>",
|
|
)
|
|
session.add(item)
|
|
session.commit()
|
|
session.refresh(item)
|
|
assert item.title == "Show HN: a new tool"
|
|
assert item.url == "https://example.com/show-hn"
|
|
assert item.points == 42
|
|
assert item.username == "alice"
|
|
assert item.content == "<p>Hello <strong>world</strong></p>"
|