"""Tests for WaitingListEntry functionality.""" from unittest import mock import pytest from django.contrib.sites.models import Site from django.urls import reverse from membership.models import WaitingListEntry class TestWaitingListEntryNotifications: """Tests for WaitingListEntry notification functionality.""" @pytest.mark.django_db() def test_matrix_notification_on_create(self, membership_type): """Test that a Matrix notification is sent when a new WaitingListEntry is created.""" # Explicitly patch notify_admins to ensure we're testing the right function with mock.patch("membership.models.notify_admins") as mock_notify: # Create a new WaitingListEntry entry = WaitingListEntry.objects.create( name="Test User", email="test@example.com", membership_type=membership_type, ) # Check that notify_admins was called mock_notify.assert_called_once() # Get the expected URL components base_domain = Site.objects.get_current() change_url = reverse("admin:membership_waitinglistentry_change", kwargs={"object_id": entry.pk}) expected_message = f"Membership application: https://{base_domain}{change_url}" # Verify the message content mock_notify.assert_called_with(expected_message) @pytest.mark.django_db() def test_no_matrix_notification_on_update(self, membership_type): """Test that no Matrix notification is sent when a WaitingListEntry is updated.""" # First create the entry entry = WaitingListEntry.objects.create( name="Test User", email="test@example.com", membership_type=membership_type, ) # Now update it with a patched notify_admins with mock.patch("membership.models.notify_admins") as mock_notify: entry.geography = "Some Location" entry.save() # Verify that notify_admins was not called mock_notify.assert_not_called() @pytest.mark.django_db() def test_message_format(self, membership_type): """Test that the message has the expected format with 'Membership application:' prefix.""" with mock.patch("membership.models.notify_admins") as mock_notify: # Create a new WaitingListEntry WaitingListEntry.objects.create( name="Test User", email="test@example.com", membership_type=membership_type, ) # Check the message format args, kwargs = mock_notify.call_args message = args[0] assert message.startswith("Membership application: ") assert "https://" in message