diff --git a/django_twilio/utils.py b/django_twilio/utils.py index c90510b..3e2fc0b 100644 --- a/django_twilio/utils.py +++ b/django_twilio/utils.py @@ -1,6 +1,10 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, absolute_import +from twilio.twiml.messaging_response import Message + +from django_twilio.request import decompose + """ Useful utility functions. """ @@ -72,8 +76,14 @@ def get_blacklisted_response(request): frm = data['From'] caller = Caller.objects.get(phone_number=frm) if caller.blacklisted: - r = VoiceResponse() - r.reject() + twilio_request = decompose(request) + if twilio_request.type == 'voice': + r = VoiceResponse() + r.reject() + else: + # SMS does not allow to selectively reject SMS. So, we respond with nothing, and twilio + # does not forward the message back to the sender. + r = Message() return HttpResponse(str(r), content_type='application/xml') except Exception: pass diff --git a/test_project/test_app/decorators.py b/test_project/test_app/decorators.py index 185863f..d60086c 100644 --- a/test_project/test_app/decorators.py +++ b/test_project/test_app/decorators.py @@ -9,6 +9,7 @@ from django.test import Client, TestCase from django.test.utils import override_settings from django_dynamic_fixture import G +from twilio.twiml.messaging_response import Message from twilio.twiml.voice_response import VoiceResponse from django_twilio.models import Caller @@ -171,8 +172,25 @@ def tst_form_field_no_caller_class_view(self): def test_blacklist_works(self): with override_settings(DEBUG=False): - request = self.factory.post(self.str_uri, {'From': '+13333333333'}) + request = self.factory.post(self.str_uri, {'From': str(self.blocked_caller.phone_number)}) response = str_view(request) + r = Message() + self.assertEqual( + response.content, + str(r).encode('utf-8'), + ) + with override_settings(DEBUG=True): + request = self.factory.post(self.str_uri, {'From': str(self.blocked_caller.phone_number)}) + response = str_view(request) + r = Message() + self.assertEqual( + response.content, + str(r).encode('utf-8'), + ) + with override_settings(DEBUG=False): + request = self.factory.post(self.verb_uri, {'From': str(self.blocked_caller.phone_number), + 'callsid': 'some-call-sid', }) + response = verb_view(request) r = VoiceResponse() r.reject() self.assertEqual( @@ -180,8 +198,9 @@ def test_blacklist_works(self): str(r).encode('utf-8'), ) with override_settings(DEBUG=True): - request = self.factory.post(self.str_uri, {'From': '+13333333333'}) - response = str_view(request) + request = self.factory.post(self.verb_uri, {'From': str(self.blocked_caller.phone_number), + 'callsid': 'some-call-sid', }) + response = verb_view(request) r = VoiceResponse() r.reject() self.assertEqual( @@ -191,8 +210,27 @@ def test_blacklist_works(self): def test_black_list_works_class_view(self): with override_settings(DEBUG=False): - request = self.factory.post(self.str_class_uri, {'From': '+13333333333'}) + request = self.factory.post(self.str_class_uri, {'From': str(self.blocked_caller.phone_number)}) response = StrView.as_view()(request) + r = Message() + self.assertEqual( + response.content, + str(r).encode('utf-8'), + ) + with override_settings(DEBUG=True): + request = self.factory.post(self.str_class_uri, {'From': str(self.blocked_caller.phone_number)}) + response = StrView.as_view()(request) + r = Message() + self.assertEqual( + response.content, + str(r).encode('utf-8'), + ) + + def test_black_list_works_verb_class_view(self): + with override_settings(DEBUG=False): + request = self.factory.post(self.verb_class_uri, {'From': str(self.blocked_caller.phone_number), + 'callsid': 'some-call-sid', }) + response = VerbView.as_view()(request) r = VoiceResponse() r.reject() self.assertEqual( @@ -200,8 +238,9 @@ def test_black_list_works_class_view(self): str(r).encode('utf-8'), ) with override_settings(DEBUG=True): - request = self.factory.post(self.str_class_uri, {'From': '+13333333333'}) - response = StrView.as_view()(request) + request = self.factory.post(self.verb_class_uri, {'From': str(self.blocked_caller.phone_number), + 'callsid': 'some-call-sid', }) + response = VerbView.as_view()(request) r = VoiceResponse() r.reject() self.assertEqual(