Skip to content

Commit c798618

Browse files
Merge pull request #183 from ManoManoTech/test/improve-raid-forms-coverage
test: improve raid/forms.py coverage from 96.8% to 98.4%
2 parents bbded4e + 151d81c commit c798618

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

tests/test_raid/test_raid_forms.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,58 @@ def test_get_internal_alert_conversations_normal_impact_incidents(self):
553553

554554
# Then
555555
assert conversation in result
556+
557+
558+
@pytest.mark.django_db
559+
class TestAlertSlackNewJiraTicketSlackApiError:
560+
"""Test SlackApiError handling in alert_slack_new_jira_ticket."""
561+
562+
@pytest.fixture(autouse=True)
563+
def setup(self):
564+
"""Set up test fixtures."""
565+
self.user = UserFactory()
566+
self.jira_user = JiraUser.objects.create(id="jira-slack-error", user=self.user)
567+
self.jira_ticket = JiraTicket.objects.create(
568+
id=88888,
569+
key="SLACK-888",
570+
summary="Slack error ticket",
571+
reporter=self.jira_user,
572+
)
573+
574+
@patch("firefighter.raid.forms.get_partner_alert_conversations")
575+
@patch("firefighter.raid.forms.get_internal_alert_conversations")
576+
@patch("firefighter.raid.forms.SlackMessageRaidCreatedIssue")
577+
def test_alert_slack_new_jira_ticket_slack_api_error_on_channel_send(
578+
self, mock_message_class, mock_get_internal, mock_get_partner, caplog
579+
):
580+
"""Test SlackApiError when sending to channel - should log exception and continue."""
581+
# Given: Create a conversation that will fail to send
582+
channel = Conversation.objects.create(
583+
channel_id="C_FAIL_TEST",
584+
name="fail-channel-test",
585+
tag="raid_alert__test_fail",
586+
)
587+
mock_get_internal.return_value = Conversation.objects.filter(id=channel.id)
588+
mock_get_partner.return_value = Conversation.objects.none()
589+
590+
# Mock message
591+
mock_message = Mock()
592+
mock_message_class.return_value = mock_message
593+
594+
# Mock channel.send_message_and_save to raise SlackApiError
595+
with patch.object(
596+
Conversation,
597+
"send_message_and_save",
598+
side_effect=SlackApiError(
599+
message="channel_not_found",
600+
response={"error": "channel_not_found"}
601+
)
602+
):
603+
# When
604+
alert_slack_new_jira_ticket(self.jira_ticket)
605+
606+
# Then
607+
# Should log exception about not being able to send
608+
assert "Couldn't send message to channel" in caplog.text
609+
assert str(self.jira_ticket.id) in caplog.text
610+
# Function should continue and not raise

0 commit comments

Comments
 (0)