test: migrate backend suites to pytest with module coverage gate

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.
This commit is contained in:
2026-09-26 16:50:29 +03:30
parent 3ec28638d1
commit ff99130938
76 changed files with 3271 additions and 1357 deletions

View File

@@ -0,0 +1,33 @@
from types import SimpleNamespace
import pytest
from django.contrib import admin
from apps.blog.admin import CommentAdmin, PostAdmin
from apps.blog.models import Comment, Post
pytestmark = pytest.mark.unit
def test_blog_admin_preview_shortens_long_comment():
model_admin = CommentAdmin(Comment, admin.site)
assert model_admin.content_preview(SimpleNamespace(content="short")) == "short"
assert model_admin.content_preview(SimpleNamespace(content="x" * 51)) == "x" * 50 + "..."
def test_post_admin_actions_update_status_and_featured_flag(mocker):
model_admin = PostAdmin(Post, admin.site)
mocker.patch.object(model_admin, "message_user")
queryset = mocker.Mock()
queryset.count.return_value = 1
user = SimpleNamespace(id=1)
request = SimpleNamespace(user=user)
model_admin.submit_for_review(request, queryset)
queryset.update.assert_called_with(status="submitted")
model_admin.request_changes(request, queryset)
queryset.update.assert_called_with(status="changes_requested", reviewed_by=user)
model_admin.make_published(request, queryset)
queryset.update.assert_called_with(status="published", published_by=user)
model_admin.make_featured(request, queryset)
queryset.update.assert_called_with(is_featured=True)

View File

@@ -0,0 +1,69 @@
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"})

View File

@@ -0,0 +1,15 @@
import pytest
from apps.blog.management.commands.seed_blog_categories import console_safe as category_console_safe
from apps.blog.management.commands.seed_blog_mock_data import make_image_bytes, make_markdown
from apps.blog.management.commands.seed_blog_tags import console_safe as tag_console_safe
pytestmark = pytest.mark.unit
def test_seed_helpers_produce_readable_markdown_and_image():
assert "# Workshop" in make_markdown("Workshop")
assert make_image_bytes("Banner", 120, 80, (20, 30, 40)).startswith(b"\xff\xd8")
assert category_console_safe("دسته") == "دسته"
assert tag_console_safe("برچسب") == "برچسب"