Skip to content

Commit

Permalink
Merge pull request #450 from rapidsms/feature/vumi-allow-from-address…
Browse files Browse the repository at this point in the history
…-configuration

Allow from address configuration with Vumi
  • Loading branch information
vkurup committed Jan 13, 2015
2 parents 346dd92 + ca5ca9e commit 7e9c49e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rapidsms/backends/vumi/outgoing.py
Expand Up @@ -30,6 +30,10 @@ def prepare_request(self, id_, text, identities, context):
'metadata': {'rapidsms_msg_id': id_}})
if len(identities) == 1 and 'external_id' in context:
payload['in_reply_to'] = context['external_id']
# add endpoint and/or from_addr, if provided
for key in ['endpoint', 'from_addr']:
if key in context:
payload[key] = context[key]
if self.sendsms_user and self.sendsms_pass:
kwargs['auth'] = (self.sendsms_user, self.sendsms_pass)
kwargs['data'] = json.dumps(payload)
Expand Down
28 changes: 28 additions & 0 deletions rapidsms/backends/vumi/tests.py
@@ -1,4 +1,5 @@
import json
from mock import patch

from django.test import TestCase
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -181,6 +182,33 @@ def test_message_id_in_metadata(self):
data = json.loads(kwargs['data'])
self.assertIn(message.id, data.get('metadata', {}).values())

def test_from_addr_and_endpoint_in_payload(self):
"""Make sure that we include from_addr or endpoint if provided, but only those keys"""
message = self.create_outgoing_message()
config = {"sendsms_url": "http://example.com"}
backend = VumiBackend(None, "vumi", **config)
context = {'from_addr': '5551212',
'endpoint': '12345',
'other': 'not included'}
kwargs = backend.prepare_request(message.id, message.text,
[message.connections[0].identity], context)
data = json.loads(kwargs['data'])
self.assertEqual(context['from_addr'], data['from_addr'])
self.assertEqual(context['endpoint'], data['endpoint'])
self.assertNotIn('other', data)

def test_send(self):
"""Test successful send."""
message = self.create_outgoing_message()
config = {"sendsms_url": "http://example.com"}
backend = VumiBackend(None, "vumi", **config)
kwargs = backend.prepare_request(message.id, message.text,
[message.connections[0].identity], {})
with patch('rapidsms.backends.vumi.outgoing.requests.post') as mock_post:
backend.send(message.id, message.text,
[message.connections[0].identity], {})
mock_post.assert_called_once_with(**kwargs)

def test_auth(self):
"""Vumi backend shold use basic authentication if given user/pass."""
message = self.create_outgoing_message()
Expand Down

0 comments on commit 7e9c49e

Please sign in to comment.