Skip to content

Commit

Permalink
fix address if asked
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed Mar 31, 2017
1 parent 389701f commit 5d1ffe3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
11 changes: 10 additions & 1 deletion vumi/transports/dmark/dmark_ussd.py
Expand Up @@ -6,7 +6,7 @@
from twisted.web import http

from vumi.components.session import SessionManager
from vumi.config import ConfigDict, ConfigInt
from vumi.config import ConfigDict, ConfigInt, ConfigBool
from vumi.message import TransportUserMessage
from vumi.transports.httprpc import HttpRpcTransport

Expand All @@ -19,6 +19,10 @@ class DmarkUssdTransportConfig(HttpRpcTransport.CONFIG_CLASS):
" expires.",
default=600, static=True)

fix_to_addr = ConfigBool(
"Whether or not to ensure that the to_addr is always starting with a "
"* and ending with a #", default=False, static=True)

redis_manager = ConfigDict(
"Redis client configuration.", default={}, static=True)

Expand Down Expand Up @@ -150,7 +154,12 @@ def handle_raw_inbound_message(self, request_id, request):
type='request_parsed',
message='Request parsed',)

config = self.get_static_config()
to_addr = values["ussdServiceCode"]
if (config.fix_to_addr
and not to_addr.startswith('*')
and not to_addr.endswith('#')):
to_addr = '*%s#' % (to_addr,)
from_addr = values["msisdn"]
content = values["ussdRequestString"]
session_event = yield self.session_event_for_transaction(
Expand Down
90 changes: 69 additions & 21 deletions vumi/transports/dmark/tests/test_dmark_ussd.py
Expand Up @@ -15,7 +15,7 @@
from vumi.transports.httprpc.tests.helpers import HttpRpcTransportHelper


class TestDmarkUssdTransport(VumiTestCase):
class DmarkTestMixin(object):

_transaction_id = u'transaction-123'
_to_addr = '*121#'
Expand All @@ -30,26 +30,6 @@ class TestDmarkUssdTransport(VumiTestCase):
'response': 'false',
}

@inlineCallbacks
def setUp(self):
self.clock = Clock()
self.patch(DmarkUssdTransport, 'get_clock', lambda _: self.clock)

self.config = {
'web_port': 0,
'web_path': '/api/v1/dmark/ussd/',
'publish_status': True,
}
self.tx_helper = self.add_helper(
HttpRpcTransportHelper(DmarkUssdTransport,
request_defaults=self._request_defaults))
self.transport = yield self.tx_helper.get_transport(self.config)
self.session_manager = self.transport.session_manager
self.transport_url = self.transport.get_transport_url(
self.config['web_path'])
yield self.session_manager.redis._purge_all() # just in case
self.session_timestamps = {}

@inlineCallbacks
def mk_session(self, transaction_id=_transaction_id):
yield self.session_manager.create_session(
Expand Down Expand Up @@ -87,6 +67,29 @@ def assert_nack(self, nack, reply, reason):
self.assertEqual(nack.payload['user_message_id'], reply['message_id'])
self.assertEqual(nack.payload['nack_reason'], reason)


class TestDmarkUssdTransport(DmarkTestMixin, VumiTestCase):

@inlineCallbacks
def setUp(self):
self.clock = Clock()
self.patch(DmarkUssdTransport, 'get_clock', lambda _: self.clock)

self.config = {
'web_port': 0,
'web_path': '/api/v1/dmark/ussd/',
'publish_status': True,
}
self.tx_helper = self.add_helper(
HttpRpcTransportHelper(DmarkUssdTransport,
request_defaults=self._request_defaults))
self.transport = yield self.tx_helper.get_transport(self.config)
self.session_manager = self.transport.session_manager
self.transport_url = self.transport.get_transport_url(
self.config['web_path'])
yield self.session_manager.redis._purge_all() # just in case
self.session_timestamps = {}

@inlineCallbacks
def test_inbound_begin(self):
user_content = "SHOULD BE IGNORED"
Expand Down Expand Up @@ -430,3 +433,48 @@ def test_status_down_timeout(self):
self.assertEqual(status['details'], {
'response_time': self.transport.request_timeout + 0.1,
})


class TestDmarkUssdAddressFixingTransport(DmarkTestMixin, VumiTestCase):

@inlineCallbacks
def setUp(self):
self.clock = Clock()
self.patch(DmarkUssdTransport, 'get_clock', lambda _: self.clock)

self.config = {
'web_port': 0,
'web_path': '/api/v1/dmark/ussd/',
'publish_status': True,
'fix_to_addr': True
}
self.tx_helper = self.add_helper(
HttpRpcTransportHelper(DmarkUssdTransport,
request_defaults=self._request_defaults))
self.transport = yield self.tx_helper.get_transport(self.config)
self.session_manager = self.transport.session_manager
self.transport_url = self.transport.get_transport_url(
self.config['web_path'])
yield self.session_manager.redis._purge_all() # just in case
self.session_timestamps = {}

@inlineCallbacks
def test_inbound_malformed_to_addr(self):
d = self.tx_helper.mk_request(ussdRequestString='',
ussdServiceCode='128')
[msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
self.assert_inbound_message(
msg,
session_event=TransportUserMessage.SESSION_NEW,
to_addr='*128#',
content=None)

reply_content = "We are the Knights Who Say ... Ni!"
reply = msg.reply(reply_content)
self.tx_helper.dispatch_outbound(reply)
response = yield d
self.assertEqual(json.loads(response.delivered_body), {
"responseString": reply_content,
"action": "request",
})
self.assertEqual(response.code, 200)

0 comments on commit 5d1ffe3

Please sign in to comment.