|
| 1 | +import datetime |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from manage_breast_screening.notifications.management.commands.helpers.routing_plan import ( |
| 7 | + RoutingPlan, |
| 8 | +) |
| 9 | +from manage_breast_screening.notifications.management.commands.send_message_batch import ( |
| 10 | + TZ_INFO, |
| 11 | + Command, |
| 12 | +) |
| 13 | +from manage_breast_screening.notifications.models import ( |
| 14 | + Appointment, |
| 15 | + Message, |
| 16 | + MessageBatchStatusChoices, |
| 17 | +) |
| 18 | +from manage_breast_screening.notifications.tests.factories import AppointmentFactory |
| 19 | + |
| 20 | + |
| 21 | +@patch("manage_breast_screening.notifications.services.api_client.jwt.encode") |
| 22 | +@pytest.mark.integration |
| 23 | +class TestSendMessageBatch: |
| 24 | + @pytest.fixture(autouse=True) |
| 25 | + def setup(self, monkeypatch): |
| 26 | + monkeypatch.setenv( |
| 27 | + "API_MESSAGE_BATCH_URL", "http://localhost:8888/message/batch" |
| 28 | + ) |
| 29 | + monkeypatch.setenv("OAUTH_TOKEN_URL", "http://localhost:8888/token") |
| 30 | + monkeypatch.setenv("OAUTH_API_KEY", "a1b2c3d4") |
| 31 | + monkeypatch.setenv("OAUTH_API_KID", "test-1") |
| 32 | + monkeypatch.setenv("PRIVATE_KEY", "test-key") |
| 33 | + monkeypatch.setenv("CMAPI_CONSUMER_KEY", "consumer-key") |
| 34 | + |
| 35 | + @pytest.mark.django_db |
| 36 | + def test_message_batch_is_sent_for_all_routing_plans( |
| 37 | + self, mock_jwt_encode, monkeypatch |
| 38 | + ): |
| 39 | + for routing_plan in RoutingPlan.all(): |
| 40 | + for episode_type in routing_plan.episode_types: |
| 41 | + AppointmentFactory( |
| 42 | + starts_at=datetime.datetime.now(tz=TZ_INFO), |
| 43 | + episode_type=episode_type, |
| 44 | + status="B", |
| 45 | + ) |
| 46 | + |
| 47 | + Command().handle() |
| 48 | + |
| 49 | + for routing_plan in RoutingPlan.all(): |
| 50 | + for episode_type in routing_plan.episode_types: |
| 51 | + appointment = Appointment.objects.filter( |
| 52 | + episode_type=episode_type |
| 53 | + ).first() |
| 54 | + message = Message.objects.filter(appointment=appointment).first() |
| 55 | + assert message.batch.status == MessageBatchStatusChoices.SENT.value |
0 commit comments