|
| 1 | +import uuid |
| 2 | +from datetime import datetime, timedelta |
| 3 | +from unittest.mock import ANY, MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | + |
| 8 | +from manage_breast_screening.notifications.api_client import ApiClient |
| 9 | +from manage_breast_screening.notifications.management.commands.send_message_batch import ( |
| 10 | + TZ_INFO, |
| 11 | + Command, |
| 12 | + CommandError, |
| 13 | +) |
| 14 | +from manage_breast_screening.notifications.models import Message, MessageBatch |
| 15 | +from manage_breast_screening.notifications.tests.factories import AppointmentFactory |
| 16 | + |
| 17 | + |
| 18 | +@patch.object( |
| 19 | + ApiClient, "send_message_batch", return_value=MagicMock(spec=requests.Response) |
| 20 | +) |
| 21 | +class TestSendMessageBatch: |
| 22 | + @pytest.fixture |
| 23 | + def routing_plan_id(self): |
| 24 | + return str(uuid.uuid4()) |
| 25 | + |
| 26 | + @pytest.mark.django_db |
| 27 | + def test_handle_with_a_batch_to_send( |
| 28 | + self, mock_send_message_batch, routing_plan_id |
| 29 | + ): |
| 30 | + """Test sending message batch with valid Appointment data""" |
| 31 | + mock_send_message_batch.return_value.status_code = 201 |
| 32 | + |
| 33 | + appointment = AppointmentFactory( |
| 34 | + starts_at=datetime.today().replace(tzinfo=TZ_INFO) |
| 35 | + ) |
| 36 | + |
| 37 | + subject = Command() |
| 38 | + mock_mark_batch_as_sent = MagicMock() |
| 39 | + subject.mark_batch_as_sent = mock_mark_batch_as_sent |
| 40 | + |
| 41 | + subject.handle(**{"routing_plan_id": routing_plan_id}) |
| 42 | + |
| 43 | + message_batches = MessageBatch.objects.filter(routing_plan_id=routing_plan_id) |
| 44 | + assert message_batches.count() == 1 |
| 45 | + assert ( |
| 46 | + Message.objects.filter( |
| 47 | + appointment=appointment, batch=message_batches[0] |
| 48 | + ).count() |
| 49 | + == 1 |
| 50 | + ) |
| 51 | + mock_mark_batch_as_sent.assert_called_once_with(message_batches[0], ANY) |
| 52 | + |
| 53 | + @pytest.mark.django_db |
| 54 | + def test_handle_with_nothing_to_send( |
| 55 | + self, mock_send_message_batch, routing_plan_id |
| 56 | + ): |
| 57 | + """Test that no MessageBatch or Message records are created when no appointments need notifications""" |
| 58 | + Command().handle(**{"routing_plan_id": routing_plan_id}) |
| 59 | + |
| 60 | + assert MessageBatch.objects.count() == 0 |
| 61 | + assert Message.objects.count() == 0 |
| 62 | + |
| 63 | + @pytest.mark.django_db |
| 64 | + def test_handle_with_appointments_inside_schedule_window( |
| 65 | + self, mock_send_message_batch, routing_plan_id |
| 66 | + ): |
| 67 | + """Test that appointments with date inside the schedule period are notified""" |
| 68 | + mock_send_message_batch.return_value.status_code = 201 |
| 69 | + appointment = AppointmentFactory( |
| 70 | + starts_at=datetime.now().replace(tzinfo=TZ_INFO) |
| 71 | + + timedelta(weeks=4, days=4) |
| 72 | + ) |
| 73 | + |
| 74 | + subject = Command() |
| 75 | + mock_mark_batch_as_sent = MagicMock() |
| 76 | + subject.mark_batch_as_sent = mock_mark_batch_as_sent |
| 77 | + |
| 78 | + subject.handle(**{"routing_plan_id": routing_plan_id}) |
| 79 | + |
| 80 | + message_batches = MessageBatch.objects.filter(routing_plan_id=routing_plan_id) |
| 81 | + assert message_batches.count() == 1 |
| 82 | + assert ( |
| 83 | + Message.objects.filter( |
| 84 | + appointment=appointment, batch=message_batches[0] |
| 85 | + ).count() |
| 86 | + == 1 |
| 87 | + ) |
| 88 | + mock_mark_batch_as_sent.assert_called_once_with(message_batches[0], ANY) |
| 89 | + |
| 90 | + @pytest.mark.django_db |
| 91 | + def test_handle_with_appointments_outside_schedule_window( |
| 92 | + self, mock_send_message_batch, routing_plan_id |
| 93 | + ): |
| 94 | + """Test that appointments with date inside the schedule period are notified""" |
| 95 | + AppointmentFactory( |
| 96 | + starts_at=datetime.now().replace(tzinfo=TZ_INFO) |
| 97 | + + timedelta(weeks=4, days=5) |
| 98 | + ) |
| 99 | + |
| 100 | + subject = Command() |
| 101 | + mock_mark_batch_as_sent = MagicMock() |
| 102 | + subject.mark_batch_as_sent = mock_mark_batch_as_sent |
| 103 | + |
| 104 | + subject.handle(**{"routing_plan_id": routing_plan_id}) |
| 105 | + |
| 106 | + assert MessageBatch.objects.count() == 0 |
| 107 | + assert Message.objects.count() == 0 |
| 108 | + mock_mark_batch_as_sent.assert_not_called |
| 109 | + |
| 110 | + @pytest.mark.django_db |
| 111 | + def test_handle_with_failing_notifications( |
| 112 | + self, mock_send_message_batch, routing_plan_id |
| 113 | + ): |
| 114 | + """Test that message batches which fail to send are marked correctly""" |
| 115 | + mock_send_message_batch.return_value.status_code = 400 |
| 116 | + appointment = AppointmentFactory( |
| 117 | + starts_at=datetime.now().replace(tzinfo=TZ_INFO) |
| 118 | + + timedelta(weeks=4, days=4) |
| 119 | + ) |
| 120 | + |
| 121 | + Command().handle(**{"routing_plan_id": routing_plan_id}) |
| 122 | + |
| 123 | + message_batches = MessageBatch.objects.filter(routing_plan_id=routing_plan_id) |
| 124 | + assert message_batches.count() == 1 |
| 125 | + assert message_batches[0].status == "failed" |
| 126 | + messages = Message.objects.filter( |
| 127 | + appointment=appointment, batch=message_batches[0] |
| 128 | + ) |
| 129 | + assert messages.count() == 1 |
| 130 | + assert messages[0].status == "failed" |
| 131 | + |
| 132 | + def test_handle_with_error(self, mock_send_message_batch, routing_plan_id): |
| 133 | + """Test that errors are caught and raised as CommandErrors""" |
| 134 | + with pytest.raises(CommandError): |
| 135 | + Command().handle() |
0 commit comments