Disable gather digit step for cloud phone call (#308)

This commit is contained in:
Matias Bordese 2022-08-02 06:58:36 -03:00 committed by GitHub
parent 8b553ce735
commit 8b60fab8f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 7 deletions

View file

@ -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

View file

@ -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"]

View file

@ -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"<Response>"
f"<Say>{start_message}</Say>"
f'<Gather numDigits="1" action="{get_gather_url()}" method="POST">'
f"<Say>{get_gather_message()}</Say>"
f"</Gather>"
f"</Response>"
),
)
if not grafana_cloud
else ""
)
twiml_query = urllib.parse.quote(
f"<Response><Say>{start_message}</Say>{gather_message}</Response>",
safe="",
)