Skip to content

Commit

Permalink
Remove log checking from tests and add db fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland authored and lunkwill42 committed Sep 7, 2023
1 parent 07d2bf9 commit 76b67b0
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions tests/integration/models/alert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,30 @@ def test_sending_alert_to_alert_address_with_invalid_address_will_delete_alert_a


def test_sending_alert_via_blacklisted_sender_will_fail_but_not_delete_alert(
alert, alert_address, account_alert_queue, caplog
db, alert, alert_address, account_alert_queue
):
alert_address.address = "47474747"
alert_address.save()
alert_address.type.blacklisted_reason = "This has been blacklisted because of x."
alert_address.type.save()
with caplog.at_level(logging.DEBUG):
sent = account_alert_queue.send()
assert not sent
assert (
f"Not sending alert {alert.pk} to {alert_address.address} as handler "
f"{alert_address.type} is blacklisted: {alert_address.type.blacklisted_reason}"
in caplog.text
)
assert not account_alert_queue.send()
assert AlertQueue.objects.filter(pk=alert.pk).exists()


@patch("nav.alertengine.dispatchers.sms_dispatcher.Sms.send")
def test_error_when_sending_alert_will_blacklist_sender(
mocked_send_function, alert_address, account_alert_queue, caplog
mocked_send_function, db, alert_address, account_alert_queue
):
exception_reason = "Exception reason"
mocked_send_function.side_effect = ValueError(exception_reason)
alert_address.address = "47474747"
alert_address.save()

with caplog.at_level(logging.DEBUG):
sent = account_alert_queue.send()

assert not sent
assert (
f"Unhandled error from {alert_address.type} (the handler has been blacklisted)"
in caplog.text
)
assert not account_alert_queue.send()
assert alert_address.type.blacklisted_reason == exception_reason


def test_clearing_blacklisted_status_of_alert_senders_will_succeed(alert_sender):
def test_clearing_blacklisted_status_of_alert_senders_will_succeed(db, alert_sender):
alert_sender.blacklisted_reason = "This has been blacklisted because of x."
clear_blacklisted_status_of_alert_senders()
alert_sender.refresh_from_db()
Expand Down Expand Up @@ -154,7 +140,7 @@ def account_alert_queue(alert, alertsub):


@pytest.fixture
def alert_sender():
def alert_sender(db):
alert_sender = AlertSender.objects.get(name=AlertSender.SMS)
yield alert_sender
if alert_sender.pk:
Expand Down

0 comments on commit 76b67b0

Please sign in to comment.