# What this PR does This PR moves phone notification logic into separate object PhoneBackend and introduces PhoneProvider interface to hide actual implementation of external phone services provider. It should allow add new phone providers just by implementing one class (See SimplePhoneProvider for example). # Why [Asterisk PR](https://github.com/grafana/oncall/pull/1282) showed that our phone notification system is not flexible. However this is one of the most frequent community questions - how to add "X" phone provider. Also, this refactoring move us one step closer to unifying all notification backends, since with PhoneBackend all phone notification logic is collected in one place and independent from concrete realisation. # Highligts 1. PhoneBackend object - contains all phone notifications business logic. 2. PhoneProvider - interface to external phone services provider. 3. TwilioPhoneProvider and SimplePhoneProvider - two examples of PhoneProvider implementation. 4. PhoneCallRecord and SMSRecord models. I introduced these models to keep phone notification limits logic decoupled from external providers. Existing TwilioPhoneCall and TwilioSMS objects will be migrated to the new table to not to reset limits counter. To be able to receive status callbacks and gather from Twilio TwilioPhoneCall and TwilioSMS still exists, but they are linked to PhoneCallRecord and SMSRecord via fk, to not to leat twilio logic into core code. --------- Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
103 lines
3.6 KiB
Python
103 lines
3.6 KiB
Python
from unittest import mock
|
|
|
|
import pytest
|
|
from django.urls import reverse
|
|
from django.utils.datastructures import MultiValueDict
|
|
from django.utils.http import urlencode
|
|
from rest_framework.test import APIClient
|
|
|
|
from apps.base.models import UserNotificationPolicy
|
|
from apps.twilioapp.models import TwilioSMS, TwilioSMSstatuses
|
|
|
|
|
|
@pytest.fixture
|
|
def make_twilio_sms(
|
|
make_organization_and_user,
|
|
make_alert_receive_channel,
|
|
make_user_notification_policy,
|
|
make_alert_group,
|
|
make_alert,
|
|
make_sms_record,
|
|
):
|
|
organization, user = make_organization_and_user()
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
make_alert(alert_group, raw_request_data="{}")
|
|
notification_policy = make_user_notification_policy(
|
|
user=user,
|
|
step=UserNotificationPolicy.Step.NOTIFY,
|
|
notify_by=UserNotificationPolicy.NotificationChannel.PHONE_CALL,
|
|
)
|
|
sms_record = make_sms_record(
|
|
receiver=user,
|
|
represents_alert_group=alert_group,
|
|
notification_policy=notification_policy,
|
|
)
|
|
return TwilioSMS.objects.create(sid="SMa12312312a123a123123c6dd2f1aee77", sms_record=sms_record)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_forbidden_requests(make_twilio_sms):
|
|
"""Tests check inaccessibility of twilio urls for unauthorized requests"""
|
|
twilio_sms = make_twilio_sms
|
|
|
|
# empty data case
|
|
data = {}
|
|
|
|
client = APIClient()
|
|
response = client.post(
|
|
path=reverse("twilioapp:sms_status_events"),
|
|
data=urlencode(MultiValueDict(data), doseq=True),
|
|
content_type="application/x-www-form-urlencoded",
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
assert response.data["detail"] == "You do not have permission to perform this action."
|
|
|
|
# wrong AccountSid data
|
|
data = {"MessageSid": twilio_sms.sid, "MessageStatus": "delivered", "AccountSid": "TopSecretAccountSid"}
|
|
|
|
response = client.post(
|
|
path=reverse("twilioapp:sms_status_events"),
|
|
data=urlencode(MultiValueDict(data), doseq=True),
|
|
content_type="application/x-www-form-urlencoded",
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
assert response.data["detail"] == "You do not have permission to perform this action."
|
|
|
|
# absent MessageSid data
|
|
data = {"MessageStatus": "delivered", "AccountSid": "TopSecretAccountSid"}
|
|
|
|
response = client.post(
|
|
path=reverse("twilioapp:sms_status_events"),
|
|
data=urlencode(MultiValueDict(data), doseq=True),
|
|
content_type="application/x-www-form-urlencoded",
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
assert response.data["detail"] == "You do not have permission to perform this action."
|
|
|
|
|
|
@mock.patch("apps.twilioapp.views.AllowOnlyTwilio.has_permission")
|
|
@pytest.mark.django_db
|
|
def test_update_status(mock_has_permission, mock_slack_api_call, make_twilio_sms):
|
|
"""The test for SMSMessage status update via api"""
|
|
twilio_sms = make_twilio_sms
|
|
mock_has_permission.return_value = True
|
|
for status in ["delivered", "failed", "undelivered"]:
|
|
data = {
|
|
"MessageSid": twilio_sms.sid,
|
|
"MessageStatus": status,
|
|
"AccountSid": "Because of mock_has_permission there are may be any value",
|
|
}
|
|
client = APIClient()
|
|
response = client.post(
|
|
path=reverse("twilioapp:sms_status_events"),
|
|
data=urlencode(MultiValueDict(data), doseq=True),
|
|
content_type="application/x-www-form-urlencoded",
|
|
)
|
|
assert response.status_code == 204
|
|
assert response.data == ""
|
|
twilio_sms.refresh_from_db()
|
|
assert twilio_sms.status == TwilioSMSstatuses.DETERMINANT[status]
|