Skip to content

Commit

Permalink
Bugfix: Reject for Voice, blank Message for others. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
jheld authored May 1, 2018
1 parent d3cd82e commit 3cd4bad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
14 changes: 12 additions & 2 deletions django_twilio/utils.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand Down Expand Up @@ -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
Expand Down
51 changes: 45 additions & 6 deletions test_project/test_app/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -171,17 +172,35 @@ 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(
response.content,
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(
Expand All @@ -191,17 +210,37 @@ 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(
response.content,
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(
Expand Down

0 comments on commit 3cd4bad

Please sign in to comment.