From 3cd4badcdb5abd81a5e2452d8a91ec2ce3cf8070 Mon Sep 17 00:00:00 2001 From: Jason Held Date: Tue, 1 May 2018 19:27:55 -0400 Subject: [PATCH 1/3] Bugfix: Reject for Voice, blank Message for others. (#137) --- django_twilio/utils.py | 14 ++++++-- test_project/test_app/decorators.py | 51 +++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 8 deletions(-) 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( From a4114ad1a37743dbedf0235dee0f5ff737e3be2b Mon Sep 17 00:00:00 2001 From: Jason Held Date: Sat, 4 Aug 2018 13:08:58 -0400 Subject: [PATCH 2/3] update version to 0.9.1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 127d745..09068a7 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ # Basic package information: name='django-twilio', - version='0.9.0', + version='0.9.1', packages=find_packages(), # Packaging options: From 11b0b1d8aaa9dc2a3f5381436dbdfb39e6e5d3bc Mon Sep 17 00:00:00 2001 From: Jason Held Date: Sat, 4 Aug 2018 13:17:37 -0400 Subject: [PATCH 3/3] pip no longer support py 3.3, so travis can't test it. --- .travis.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 68cdcc2..afdb3d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: python python: - '2.7' - - '3.3' - '3.4' - '3.5' - '3.6' @@ -30,14 +29,6 @@ matrix: env: DJANGO="django>=2.0,<2.1" - python: "pypy" env: DJANGO="django>=2.0,<2.1" - - python: "3.3" - env: DJANGO="django>=1.9,<1.10" - - python: "3.3" - env: DJANGO="django>=1.10,<1.11" - - python: "3.3" - env: DJANGO="django>=1.11,<2" - - python: "3.3" - env: DJANGO="django>=2.0,<2.1" - python: "3.6" env: DJANGO="django>=1.8,<1.9" - python: "3.6"