Files
guilan-ace-backend/apps/content/tests/integration/test_seed_mock_content_command.py

68 lines
2.6 KiB
Python

from io import StringIO
import pytest
from django.contrib.admin.models import CHANGE, LogEntry
from django.contrib.contenttypes.models import ContentType
from django.core.management import call_command
from apps.content.current import AboutContent, HomeContent, ListingContent
from apps.users.models import User
pytestmark = [pytest.mark.django_db, pytest.mark.integration]
def test_command_replaces_unedited_migration_copy_with_mock_content_idempotently():
call_command("seed_mock_content", stdout=StringIO())
home = HomeContent.objects.get(pk=1)
about = AboutContent.objects.get(pk=1)
events = ListingContent.objects.get(pk="events")
blog = ListingContent.objects.get(pk="blog")
assert home.hero_heading == "عنوان نمونه صفحه اصلی"
assert about.title == "عنوان نمونه صفحه درباره ما"
assert about.items.filter(kind="mission").exists()
assert about.items.filter(kind="faq").exists()
assert not about.items.filter(kind__in=["committees", "affiliations"]).exists()
assert not about.items.filter(title="انجمن علمی مهندسی کامپیوتر دانشکده‌ی فنی").exists()
assert events.title == "عنوان نمونه رویدادها"
assert blog.title == "عنوان نمونه بلاگ"
call_command("seed_mock_content", stdout=StringIO())
assert HomeContent.objects.get(pk=1).hero_heading == "عنوان نمونه صفحه اصلی"
def test_command_preserves_content_with_an_admin_change_log():
editor = User.objects.create_user(
username="mock-editor",
mobile="09120000001",
password="TestPass123!",
is_staff=True,
)
home = HomeContent.objects.get(pk=1)
home.hero_heading = "Admin's real heading"
home.save(update_fields=["hero_heading"])
LogEntry.objects.create(
user=editor,
content_type=ContentType.objects.get_for_model(HomeContent),
object_id="1",
object_repr=str(home),
action_flag=CHANGE,
change_message="Changed through admin",
)
call_command("seed_mock_content", stdout=StringIO())
assert HomeContent.objects.get(pk=1).hero_heading == "Admin's real heading"
assert ListingContent.objects.get(slug="events").title == "عنوان نمونه رویدادها"
def test_dry_run_does_not_change_seeded_content():
output = StringIO()
call_command("seed_mock_content", dry_run=True, stdout=output)
assert HomeContent.objects.get(pk=1).hero_heading == "انجمن علمی کامپیوتر دانشگاه گیلان"
assert "dry run" in output.getvalue().lower()