From 8b60fab8f025bc603e553b7e0496c524c560ea5a Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Tue, 2 Aug 2022 06:58:36 -0300 Subject: [PATCH] Disable gather digit step for cloud phone call (#308) --- engine/apps/twilioapp/models/phone_call.py | 2 +- .../apps/twilioapp/tests/test_phone_calls.py | 63 +++++++++++++++++++ engine/apps/twilioapp/twilio_client.py | 15 +++-- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/engine/apps/twilioapp/models/phone_call.py b/engine/apps/twilioapp/models/phone_call.py index 69893b8a..b0db9f91 100644 --- a/engine/apps/twilioapp/models/phone_call.py +++ b/engine/apps/twilioapp/models/phone_call.py @@ -251,7 +251,7 @@ class PhoneCall(models.Model): if phone_calls_left < 3: message_body += " {} phone calls left. Contact your admin.".format(phone_calls_left) - twilio_call = twilio_client.make_call(message_body, user.verified_phone_number) + twilio_call = twilio_client.make_call(message_body, user.verified_phone_number, grafana_cloud=grafana_cloud) if twilio_call.status and twilio_call.sid: phone_call.status = TwilioCallStatuses.DETERMINANT.get(twilio_call.status, None) phone_call.sid = twilio_call.sid diff --git a/engine/apps/twilioapp/tests/test_phone_calls.py b/engine/apps/twilioapp/tests/test_phone_calls.py index 22c64a3f..17ec3556 100644 --- a/engine/apps/twilioapp/tests/test_phone_calls.py +++ b/engine/apps/twilioapp/tests/test_phone_calls.py @@ -1,3 +1,4 @@ +import urllib from unittest import mock import pytest @@ -11,6 +12,13 @@ from rest_framework.test import APIClient from apps.base.models import UserNotificationPolicy from apps.twilioapp.constants import TwilioCallStatuses from apps.twilioapp.models import PhoneCall +from apps.twilioapp.utils import get_gather_message + + +class FakeTwilioCall: + def __init__(self): + self.sid = "123" + self.status = TwilioCallStatuses.COMPLETED @pytest.fixture @@ -268,3 +276,58 @@ def test_wrong_pressed_digit(mock_has_permission, mock_get_gather_url, phone_cal assert response.status_code == 200 assert "Wrong digit" in content + + +@mock.patch("apps.twilioapp.twilio_client.Client") +@pytest.mark.django_db +def test_make_cloud_phone_call_not_gathering_digit(mock_twilio_client, make_organization, make_user): + organization = make_organization() + user = make_user(organization=organization, _verified_phone_number="9999555") + mock_twilio_client.return_value.calls.create.return_value = FakeTwilioCall() + + PhoneCall.make_grafana_cloud_call(user, "the message") + + gather_message = urllib.parse.quote(get_gather_message()) + assert gather_message not in mock_twilio_client.return_value.calls.create.call_args.kwargs["url"] + + +@mock.patch("apps.twilioapp.twilio_client.Client") +@pytest.mark.django_db +def test_make_phone_call_gathering_digit( + mock_twilio_client, + make_organization, + make_user, + make_user_notification_policy, + make_alert_receive_channel, + make_alert_group, + make_alert, +): + organization = make_organization() + user = make_user(organization=organization, _verified_phone_number="9999555") + alert_receive_channel = make_alert_receive_channel(organization) + alert_group = make_alert_group(alert_receive_channel) + notification_policy = make_user_notification_policy( + user=user, + step=UserNotificationPolicy.Step.NOTIFY, + notify_by=UserNotificationPolicy.NotificationChannel.PHONE_CALL, + ) + make_alert( + alert_group, + raw_request_data={ + "status": "firing", + "labels": { + "alertname": "TestAlert", + "region": "eu-1", + }, + "annotations": {}, + "startsAt": "2018-12-25T15:47:47.377363608Z", + "endsAt": "0001-01-01T00:00:00Z", + "generatorURL": "", + }, + ) + mock_twilio_client.return_value.calls.create.return_value = FakeTwilioCall() + + PhoneCall.make_call(user, alert_group, notification_policy) + + gather_message = urllib.parse.quote(get_gather_message()) + assert gather_message in mock_twilio_client.return_value.calls.create.call_args.kwargs["url"] diff --git a/engine/apps/twilioapp/twilio_client.py b/engine/apps/twilioapp/twilio_client.py index b1e07e2a..007d9e72 100644 --- a/engine/apps/twilioapp/twilio_client.py +++ b/engine/apps/twilioapp/twilio_client.py @@ -126,19 +126,22 @@ class TwilioClient: ) self.make_call(message=message, to=to) - def make_call(self, message, to): + def make_call(self, message, to, grafana_cloud=False): try: start_message = message.replace('"', "") - twiml_query = urllib.parse.quote( + gather_message = ( ( - f"" - f"{start_message}" f'' f"{get_gather_message()}" f"" - f"" - ), + ) + if not grafana_cloud + else "" + ) + + twiml_query = urllib.parse.quote( + f"{start_message}{gather_message}", safe="", )