|
| 1 | +import logging |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from notification.models import EmailNotificationHistory, EmailNotificationHistorySentTo, EmailNotificationTemplate |
| 6 | +from notification.models.base import NotificationStatus |
| 7 | +from notification.tasks import send_notification_to_recipient |
| 8 | +from user.models import UserExt |
| 9 | + |
| 10 | +LABEL = EmailNotificationHistorySentTo._meta.label_lower |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def system_user(db): |
| 15 | + return UserExt.get_system_user() |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def email_template(system_user): |
| 20 | + return EmailNotificationTemplate.objects.create( |
| 21 | + code="t", |
| 22 | + title="t", |
| 23 | + sent_from="from@example.com", |
| 24 | + data='{"title":"hi","from_":"f","send_to":"r","body":"b"}', |
| 25 | + created_by=system_user, |
| 26 | + updated_by=system_user, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def sent_to(email_template): |
| 32 | + history = EmailNotificationHistory.objects.create_for_recipients( |
| 33 | + template=email_template, |
| 34 | + recipients=[{"recipient": "to@example.com"}], |
| 35 | + ) |
| 36 | + return history.sent_to_list.get() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.django_db |
| 40 | +def test_task_sends_created_row(sent_to): |
| 41 | + with patch.object(EmailNotificationHistory, "client") as mock_client: |
| 42 | + send_notification_to_recipient(LABEL, sent_to.id) |
| 43 | + mock_client.send_message.assert_called_once() |
| 44 | + sent_to.refresh_from_db() |
| 45 | + assert sent_to.status == NotificationStatus.SENT |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.django_db |
| 49 | +def test_task_resends_failed_row(sent_to): |
| 50 | + sent_to.status = NotificationStatus.FAILED |
| 51 | + sent_to.save(update_fields=["status"]) |
| 52 | + with patch.object(EmailNotificationHistory, "client"): |
| 53 | + send_notification_to_recipient(LABEL, sent_to.id) |
| 54 | + sent_to.refresh_from_db() |
| 55 | + assert sent_to.status == NotificationStatus.SENT |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.django_db |
| 59 | +def test_task_skips_sending_row_to_prevent_duplicate_send(sent_to): |
| 60 | + # 동시성/재배달 가드 — SENDING 중인 row를 다른 워커가 받아도 외부 호출이 일어나지 않아야 함. |
| 61 | + sent_to.status = NotificationStatus.SENDING |
| 62 | + sent_to.save(update_fields=["status"]) |
| 63 | + with patch.object(EmailNotificationHistory, "client") as mock_client: |
| 64 | + send_notification_to_recipient(LABEL, sent_to.id) |
| 65 | + mock_client.send_message.assert_not_called() |
| 66 | + sent_to.refresh_from_db() |
| 67 | + assert sent_to.status == NotificationStatus.SENDING |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.django_db |
| 71 | +def test_task_skips_sent_row_to_prevent_duplicate_send(sent_to): |
| 72 | + sent_to.status = NotificationStatus.SENT |
| 73 | + sent_to.save(update_fields=["status"]) |
| 74 | + with patch.object(EmailNotificationHistory, "client") as mock_client: |
| 75 | + send_notification_to_recipient(LABEL, sent_to.id) |
| 76 | + mock_client.send_message.assert_not_called() |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.django_db |
| 80 | +def test_task_marks_failed_and_propagates_external_failure(sent_to): |
| 81 | + with patch.object(EmailNotificationHistory, "client") as mock_client: |
| 82 | + mock_client.send_message.side_effect = RuntimeError("api down") |
| 83 | + with pytest.raises(RuntimeError, match="api down"): |
| 84 | + send_notification_to_recipient(LABEL, sent_to.id) |
| 85 | + sent_to.refresh_from_db() |
| 86 | + assert sent_to.status == NotificationStatus.FAILED |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.django_db |
| 90 | +def test_task_logs_unexpected_error_when_inner_save_fails(sent_to, caplog): |
| 91 | + # send() 내부 try가 못 잡는 경로(첫 status save 실패) — task가 "Batch send unexpected error"로 추가 로깅. |
| 92 | + with patch.object(EmailNotificationHistorySentTo, "save", side_effect=RuntimeError("db down")): |
| 93 | + with caplog.at_level(logging.ERROR, logger="slack_logger"): |
| 94 | + with pytest.raises(RuntimeError, match="db down"): |
| 95 | + send_notification_to_recipient(LABEL, sent_to.id) |
| 96 | + records = [r for r in caplog.records if "Batch send unexpected" in r.getMessage()] |
| 97 | + assert len(records) == 1 |
| 98 | + assert records[0].exc_info is not None |
0 commit comments