Split backend tests into unit and integration suites, add isolated fixtures and coverage enforcement, and update CI and test documentation. Cover current authentication, content, notification, payment, media, and admin flows; fix behaviors revealed by the new tests. Validate both suites and the per-module gate against local PostgreSQL.
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from apps.blog.permissions import (
|
|
_has_any_perm,
|
|
can_access_blog_admin,
|
|
can_edit_post,
|
|
can_manage_post_assets,
|
|
can_moderate_blog_comments,
|
|
can_review_blog_posts,
|
|
can_write_blog_posts,
|
|
)
|
|
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
def user(*permissions, staff=False, superuser=False, authenticated=True):
|
|
return SimpleNamespace(
|
|
id=1,
|
|
is_authenticated=authenticated,
|
|
is_staff=staff,
|
|
is_superuser=superuser,
|
|
has_perm=lambda permission: permission in permissions,
|
|
)
|
|
|
|
|
|
def test_anonymous_user_has_no_blog_permissions():
|
|
visitor = user(authenticated=False)
|
|
post = SimpleNamespace(author_id=1, status="draft")
|
|
assert not can_access_blog_admin(visitor)
|
|
assert not can_write_blog_posts(visitor)
|
|
assert not can_review_blog_posts(visitor)
|
|
assert not can_moderate_blog_comments(visitor)
|
|
assert not can_edit_post(visitor, post)
|
|
assert not can_manage_post_assets(visitor, post)
|
|
|
|
|
|
def test_staff_can_manage_posts_and_assets():
|
|
staff = user(staff=True)
|
|
post = SimpleNamespace(author_id=2, status="archived")
|
|
assert can_access_blog_admin(staff)
|
|
assert can_edit_post(staff, post)
|
|
assert can_manage_post_assets(staff, post)
|
|
|
|
|
|
def test_author_can_edit_draft_but_not_archived_post():
|
|
author = user("blog.add_post", "blog.upload_blog_asset")
|
|
draft = SimpleNamespace(author_id=1, status="draft")
|
|
archived = SimpleNamespace(author_id=1, status="archived")
|
|
assert can_edit_post(author, draft)
|
|
assert can_manage_post_assets(author, draft)
|
|
assert not can_edit_post(author, archived)
|
|
|
|
|
|
def test_review_and_moderation_require_their_permissions():
|
|
reviewer = user("blog.review_blog_post")
|
|
moderator = user("blog.moderate_blog_comment")
|
|
assert can_review_blog_posts(reviewer)
|
|
assert not can_moderate_blog_comments(reviewer)
|
|
assert can_moderate_blog_comments(moderator)
|
|
|
|
|
|
def test_permission_set_helper_checks_authentication_and_membership():
|
|
assert not _has_any_perm(None, {"blog.add_post"})
|
|
assert not _has_any_perm(user(authenticated=False), {"blog.add_post"})
|
|
assert _has_any_perm(user("blog.add_post"), {"blog.add_post"})
|
|
assert _has_any_perm(user(superuser=True), {"blog.add_post"})
|