diff --git a/src/meshapi/tests/test_install_create_signals.py b/src/meshapi/tests/test_install_create_signals.py index 59b77f0d..1e9df8e6 100644 --- a/src/meshapi/tests/test_install_create_signals.py +++ b/src/meshapi/tests/test_install_create_signals.py @@ -159,3 +159,47 @@ def test_no_events_for_install_edit(self, request_mocker): install.save() self.assertEqual(len(request_mocker.request_history), 0) + + @patch( + "meshapi.util.events.join_requests_slack_channel.SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL", + "http://example.com/test-url-slack", + ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_NEW_TICKET_ENDPOINT", + "http://example.com/test-url-os-ticket", + ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_API_TOKEN", + "mock-token", + ) + @requests_mock.Mocker() + def test_many_retry_no_crash_on_integration_404(self, request_mocker): + request_mocker.post("http://example.com/test-url-slack", text="Not found", status_code=404) + request_mocker.post("http://example.com/test-url-os-ticket", text="Not found", status_code=404) + + enable_flag("INTEGRATION_ENABLED_SEND_JOIN_REQUEST_SLACK_MESSAGES") + enable_flag("INTEGRATION_ENABLED_CREATE_OSTICKET_TICKETS") + + install = Install(**self.sample_install_copy) + install.save() + + self.assertEqual( + len( + [ + request + for request in request_mocker.request_history + if request.url == "http://example.com/test-url-os-ticket" + ] + ), + 4, + ) + self.assertEqual( + len( + [ + request + for request in request_mocker.request_history + if request.url == "http://example.com/test-url-slack" + ] + ), + 4, + ) diff --git a/src/meshapi/util/events/join_requests_slack_channel.py b/src/meshapi/util/events/join_requests_slack_channel.py index 8c09acef..d2886308 100644 --- a/src/meshapi/util/events/join_requests_slack_channel.py +++ b/src/meshapi/util/events/join_requests_slack_channel.py @@ -1,5 +1,6 @@ import logging import os +import time import requests from django.db.models.base import ModelBase @@ -29,14 +30,22 @@ def send_join_request_slack_message(sender: ModelBase, instance: Install, create building_height = str(int(install.building.altitude)) + "m" if install.building.altitude else "Altitude not found" roof_access = "Roof access" if install.roof_access else "No roof access" - response = requests.post( - SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL, - json={ - "text": f"**\n" - f"{building_height} · {roof_access} · No LoS Data Available" - }, - ) + attempts = 0 + while attempts < 4: + attempts += 1 + response = requests.post( + SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL, + json={ + "text": f"**\n" + f"{building_height} · {roof_access} · No LoS Data Available" + }, + ) + + if response.status_code == 200: + break + + time.sleep(1) if response.status_code != 200: logging.error(