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:
34
apps/events/tests/unit/test_admin_actions.py
Normal file
34
apps/events/tests/unit/test_admin_actions.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from django.contrib import admin
|
||||
|
||||
from apps.events.admin import EventAdmin
|
||||
from apps.events.models import Event
|
||||
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def test_event_admin_formats_empty_price_and_capacity():
|
||||
model_admin = EventAdmin(Event, admin.site)
|
||||
event = SimpleNamespace(price=None, capacity=None, current_attendees_count=3, is_registration_open=True)
|
||||
assert model_admin.price_display(event) == "رایگان"
|
||||
assert model_admin.capacity_display(event) == "نامحدود"
|
||||
assert model_admin.attendees_display(event) == 3
|
||||
assert model_admin.is_registration_open_display(event)
|
||||
|
||||
|
||||
def test_event_admin_status_actions_update_selection(mocker):
|
||||
model_admin = EventAdmin(Event, admin.site)
|
||||
mocker.patch.object(model_admin, "message_user")
|
||||
queryset = mocker.Mock()
|
||||
queryset.count.return_value = 2
|
||||
for action, status in (
|
||||
(model_admin.make_published, Event.StatusChoices.PUBLISHED),
|
||||
(model_admin.make_draft, Event.StatusChoices.DRAFT),
|
||||
(model_admin.make_cancelled, Event.StatusChoices.CANCELLED),
|
||||
(model_admin.make_completed, Event.StatusChoices.COMPLETED),
|
||||
):
|
||||
action(SimpleNamespace(), queryset)
|
||||
queryset.update.assert_called_with(status=status)
|
||||
72
apps/events/tests/unit/test_event_notifications.py
Normal file
72
apps/events/tests/unit/test_event_notifications.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import pytest
|
||||
from types import SimpleNamespace
|
||||
from datetime import timedelta
|
||||
from django.contrib import admin
|
||||
from django.utils import timezone
|
||||
from apps.events.admin import EventAdmin, RegistrationAdmin
|
||||
from apps.events.models import Event, Registration
|
||||
from apps.events.tasks import send_registration_cancellation_notification, send_registration_confirmation_notification
|
||||
from apps.users.models import User
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestAutomaticRegistrationNotificationTests:
|
||||
|
||||
def test_confirmation_delivers_in_app_with_event_link(self, mocker):
|
||||
registrations = mocker.patch('apps.events.tasks.Registration.objects')
|
||||
notify_user = mocker.patch('apps.events.tasks.notify_user')
|
||||
registration = SimpleNamespace(user_id=7, event_id=12, ticket_id='ticket', event=SimpleNamespace(title='Workshop', slug='workshop'))
|
||||
registrations.select_related.return_value.get.return_value = registration
|
||||
result = send_registration_confirmation_notification.run('1')
|
||||
assert result == {'sent': True}
|
||||
assert notify_user.call_args.args[0] == 7
|
||||
assert notify_user.call_args.args[1]['action_url'] == '/events/workshop'
|
||||
|
||||
def test_cancellation_delivers_in_app_and_optional_sms(self, mocker):
|
||||
registrations = mocker.patch('apps.events.tasks.Registration.objects')
|
||||
notify_user = mocker.patch('apps.events.tasks.notify_user')
|
||||
send_sms = mocker.patch('apps.events.tasks.send_critical_sms.delay')
|
||||
registrations.select_related.return_value.get.return_value = SimpleNamespace(user_id=7, event_id=12, user=SimpleNamespace(mobile='09191110002', is_mobile_verified=True), event=SimpleNamespace(title='Workshop', slug='workshop'))
|
||||
assert send_registration_cancellation_notification.run('1') == {'sent': True}
|
||||
assert notify_user.call_args.args[1]['type'] == 'event_cancellation'
|
||||
send_sms.assert_called_once_with('09191110002', 'event_cancellation', 'Workshop')
|
||||
|
||||
def test_cancellation_skips_sms_for_unverified_mobile(self, mocker):
|
||||
registrations = mocker.patch('apps.events.tasks.Registration.objects')
|
||||
mocker.patch('apps.events.tasks.notify_user')
|
||||
send_sms = mocker.patch('apps.events.tasks.send_critical_sms.delay')
|
||||
registrations.select_related.return_value.get.return_value = SimpleNamespace(
|
||||
user_id=7, event_id=12,
|
||||
user=SimpleNamespace(mobile='09191110002', is_mobile_verified=False),
|
||||
event=SimpleNamespace(title='Workshop', slug='workshop'),
|
||||
)
|
||||
assert send_registration_cancellation_notification.run('1') == {'sent': True}
|
||||
send_sms.assert_not_called()
|
||||
|
||||
def test_confirmation_failure_requests_retry(self, mocker):
|
||||
registrations = mocker.patch('apps.events.tasks.Registration.objects')
|
||||
registrations.select_related.return_value.get.side_effect = Registration.DoesNotExist()
|
||||
retry = mocker.patch.object(
|
||||
send_registration_confirmation_notification, 'retry', side_effect=RuntimeError('retry requested')
|
||||
)
|
||||
with pytest.raises(RuntimeError, match='retry requested'):
|
||||
send_registration_confirmation_notification.run('missing')
|
||||
assert retry.call_args.kwargs['countdown'] == 60
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestManualActionRemovalTests:
|
||||
|
||||
def test_event_and_registration_admin_have_no_manual_send_actions(self):
|
||||
assert not EventAdmin.__dict__.get('actions_row')
|
||||
assert not RegistrationAdmin.__dict__.get('actions_row')
|
||||
|
||||
def test_bulk_status_actions_save_each_registration_for_automatic_notification(self, mocker):
|
||||
model_admin = RegistrationAdmin(Registration, admin.site)
|
||||
registration = mocker.Mock()
|
||||
mocker.patch.object(model_admin, 'message_user')
|
||||
model_admin.confirm_registrations(SimpleNamespace(), [registration])
|
||||
assert registration.status == Registration.StatusChoices.CONFIRMED
|
||||
registration.save.assert_called_with(update_fields=['status'])
|
||||
registration.reset_mock()
|
||||
model_admin.cancel_registrations(SimpleNamespace(), [registration])
|
||||
assert registration.status == Registration.StatusChoices.CANCELLED
|
||||
registration.save.assert_called_with(update_fields=['status'])
|
||||
@@ -1,93 +0,0 @@
|
||||
from types import SimpleNamespace
|
||||
from unittest import mock
|
||||
from datetime import timedelta
|
||||
|
||||
from django.contrib import admin
|
||||
from django.test import SimpleTestCase, TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.events.admin import EventAdmin, RegistrationAdmin
|
||||
from apps.events.models import Event, Registration
|
||||
from apps.events.tasks import (
|
||||
send_registration_cancellation_notification,
|
||||
send_registration_confirmation_notification,
|
||||
)
|
||||
from apps.users.models import User
|
||||
|
||||
|
||||
class AutomaticRegistrationNotificationTests(SimpleTestCase):
|
||||
@mock.patch("apps.events.tasks.notify_user")
|
||||
@mock.patch("apps.events.tasks.Registration.objects")
|
||||
def test_confirmation_delivers_in_app_with_event_link(self, registrations, notify_user):
|
||||
registration = SimpleNamespace(
|
||||
user_id=7,
|
||||
event_id=12,
|
||||
ticket_id="ticket",
|
||||
event=SimpleNamespace(title="Workshop", slug="workshop"),
|
||||
)
|
||||
registrations.select_related.return_value.get.return_value = registration
|
||||
result = send_registration_confirmation_notification.run("1")
|
||||
self.assertEqual(result, {"sent": True})
|
||||
self.assertEqual(notify_user.call_args.args[0], 7)
|
||||
self.assertEqual(notify_user.call_args.args[1]["action_url"], "/events/workshop")
|
||||
|
||||
@mock.patch("apps.events.tasks.send_critical_sms.delay")
|
||||
@mock.patch("apps.events.tasks.notify_user")
|
||||
@mock.patch("apps.events.tasks.Registration.objects")
|
||||
def test_cancellation_delivers_in_app_and_optional_sms(self, registrations, notify_user, send_sms):
|
||||
registrations.select_related.return_value.get.return_value = SimpleNamespace(
|
||||
user_id=7,
|
||||
event_id=12,
|
||||
user=SimpleNamespace(mobile="09191110002", is_mobile_verified=True),
|
||||
event=SimpleNamespace(title="Workshop", slug="workshop"),
|
||||
)
|
||||
self.assertEqual(send_registration_cancellation_notification.run("1"), {"sent": True})
|
||||
self.assertEqual(notify_user.call_args.args[1]["type"], "event_cancellation")
|
||||
send_sms.assert_called_once_with("09191110002", "event_cancellation", "Workshop")
|
||||
|
||||
|
||||
class ManualActionRemovalTests(SimpleTestCase):
|
||||
def test_event_and_registration_admin_have_no_manual_send_actions(self):
|
||||
self.assertFalse(EventAdmin.__dict__.get("actions_row"))
|
||||
self.assertFalse(RegistrationAdmin.__dict__.get("actions_row"))
|
||||
|
||||
def test_bulk_status_actions_save_each_registration_for_automatic_notification(self):
|
||||
model_admin = RegistrationAdmin(Registration, admin.site)
|
||||
registration = mock.Mock()
|
||||
with mock.patch.object(model_admin, "message_user"):
|
||||
model_admin.confirm_registrations(SimpleNamespace(), [registration])
|
||||
self.assertEqual(registration.status, Registration.StatusChoices.CONFIRMED)
|
||||
registration.save.assert_called_with(update_fields=["status"])
|
||||
registration.reset_mock()
|
||||
model_admin.cancel_registrations(SimpleNamespace(), [registration])
|
||||
self.assertEqual(registration.status, Registration.StatusChoices.CANCELLED)
|
||||
registration.save.assert_called_with(update_fields=["status"])
|
||||
|
||||
|
||||
class EventModelTests(TestCase):
|
||||
def setUp(self):
|
||||
now = timezone.now()
|
||||
self.event = Event.objects.create(
|
||||
title="Workshop", description="**Important** details", start_time=now + timedelta(days=1),
|
||||
end_time=now + timedelta(days=1, hours=2), capacity=1,
|
||||
registration_start_date=now - timedelta(days=1),
|
||||
registration_end_date=now + timedelta(hours=1),
|
||||
)
|
||||
self.user = User.objects.create_user(username="attendee", mobile="09191110003", password="password")
|
||||
|
||||
def test_markdown_description_and_registration_window_remain_available(self):
|
||||
self.assertIn("<strong>Important</strong>", self.event.description_html)
|
||||
self.assertTrue(self.event.is_registration_open)
|
||||
self.event.registration_end_date = timezone.now() - timedelta(minutes=1)
|
||||
self.assertFalse(self.event.is_registration_open)
|
||||
|
||||
@mock.patch("apps.events.tasks.send_registration_confirmation_notification.delay")
|
||||
def test_capacity_counts_confirmed_registration(self, delay):
|
||||
Registration.objects.create(event=self.event, user=self.user, status=Registration.StatusChoices.CONFIRMED)
|
||||
self.assertEqual(self.event.current_attendees_count, 1)
|
||||
self.assertFalse(self.event.has_available_slots)
|
||||
delay.assert_called_once()
|
||||
|
||||
def test_unlimited_capacity_allows_registration(self):
|
||||
self.event.capacity = None
|
||||
self.assertTrue(self.event.has_available_slots)
|
||||
Reference in New Issue
Block a user