Skip to content

Commit

Permalink
endpoint rapidsmsrelay + test
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed Nov 16, 2013
1 parent 01363f0 commit fb079cc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
16 changes: 12 additions & 4 deletions vumi/application/rapidsms_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from vumi.application.base import ApplicationWorker
from vumi.persist.txredis_manager import TxRedisManager
from vumi.config import (
ConfigUrl, ConfigText, ConfigInt, ConfigDict, ConfigBool, ConfigContext)
ConfigUrl, ConfigText, ConfigInt, ConfigDict, ConfigBool, ConfigContext,
ConfigList)
from vumi.message import to_json, TransportUserMessage
from vumi.utils import http_request_full
from vumi.errors import ConfigError
Expand Down Expand Up @@ -128,6 +129,9 @@ class RapidSMSRelayConfig(ApplicationWorker.CONFIG_CLASS):
vumi_reply_timeout = ConfigInt(
"Number of seconds to keep original messages in redis so that"
" replies may be sent via `in_reply_to`.", default=10 * 60)
allowed_endpoints = ConfigList(
'List of allowed endpoints to be sending from', static=True,
required=True)

rapidsms_url = ConfigUrl("URL of the rapidsms http backend.")
rapidsms_username = ConfigText(
Expand All @@ -153,6 +157,8 @@ def validate_config(self):
self.supported_auth_methods = {
'basic': self.generate_basic_auth_headers,
}
config = self.get_static_config()
self.ALLOWED_ENDPOINTS = config.allowed_endpoints

def generate_basic_auth_headers(self, username, password):
credentials = ':'.join([username, password])
Expand Down Expand Up @@ -250,10 +256,10 @@ def _handle_reply_to(self, config, content, to_addrs, in_reply_to):
reply = yield self.reply_to(orig_msg, content)
returnValue([reply])

def _handle_send_to(self, config, content, to_addrs):
def _handle_send_to(self, config, content, to_addrs, endpoint):
sends = []
for to_addr in to_addrs:
sends.append(self.send_to(to_addr, content))
sends.append(self.send_to(to_addr, content, endpoint=endpoint))
d = DeferredList(sends, consumeErrors=True)
d.addCallback(lambda msgs: [msg[1] for msg in msgs if msg[0]])
return d
Expand All @@ -266,11 +272,13 @@ def handle_raw_outbound_message(self, request):
content = data['content']
to_addrs = data['to_addr']
in_reply_to = data.get('in_reply_to')
endpoint = data.get('endpoint')
if in_reply_to is not None:
msgs = yield self._handle_reply_to(config, content, to_addrs,
in_reply_to)
else:
msgs = yield self._handle_send_to(config, content, to_addrs)
msgs = yield self._handle_send_to(config, content, to_addrs,
endpoint)
returnValue(msgs)

@inlineCallbacks
Expand Down
30 changes: 30 additions & 0 deletions vumi/application/tests/test_rapidsms_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def setup_app(self, url, auth=None):
'rapidsms_password': 'password',
'vumi_username': vumi_username,
'vumi_password': vumi_password,
'allowed_endpoints': ['default', '10010', '10020'],
})

def get_response_msgs(self, response):
Expand Down Expand Up @@ -213,3 +214,32 @@ def test_rapidsms_relay_outbound_failed_authenticated(self):
}, auth=bad_auth)
self.assertEqual(response.code, 401)
self.assertEqual(response.delivered_body, "Unauthorized")

@inlineCallbacks
def test_rapidsms_relay_outbound_on_specific_endpoint(self):
yield self.setup_resource()
response = yield self._call_relay({
'to_addr': ['+123456'],
'content': u'foo',
'endpoint': '10010',
})
self._check_messages(response, [
{'to_addr': '+123456', 'content': u'foo'}])
[msg] = self.app_helper.get_dispatched_outbound()
self.assertEqual(msg['routing_metadata'], {
'endpoint_name': '10010',
})

@inlineCallbacks
def test_rapidsms_relay_outbound_on_default_endpoint(self):
yield self.setup_resource()
response = yield self._call_relay({
'to_addr': ['+123456'],
'content': u'foo',
})
self._check_messages(response, [
{'to_addr': '+123456', 'content': u'foo'}])
[msg] = self.app_helper.get_dispatched_outbound()
self.assertEqual(msg['routing_metadata'], {
'endpoint_name': 'default',
})

0 comments on commit fb079cc

Please sign in to comment.