Skip to content

Commit

Permalink
Merge pull request #169 from praekelt/feature/MOMZA-836-create-messag…
Browse files Browse the repository at this point in the history
…e-endpoints

Fix sending reply from destination
  • Loading branch information
erikh360 committed Jan 24, 2018
2 parents d4dd6cf + 6ec9218 commit 3a832c0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
17 changes: 13 additions & 4 deletions junebug/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def send_message(self, request, body, channel_id):
self.redis, self.config, channel_id, self.service, self.plugins)

if (channel.has_destination):
msg = yield self.send_messsage_on_channel(channel_id, body)
msg = yield self.send_message_on_channel(channel_id, body)

returnValue(response(
request, 'message submitted', msg, code=http.CREATED))
Expand Down Expand Up @@ -600,7 +600,15 @@ def send_destination_message(
channel_id = yield router.router_worker.get_destination_channel(
destination_id, body)

msg = yield self.send_messsage_on_channel(channel_id, body)
in_msg = None
if 'reply_to' in body:
in_msg = yield self.inbounds.load_vumi_message(
destination_id, body['reply_to'])

msg = yield self.send_message_on_channel(
channel_id, body, in_msg)

self.outbounds.store_message(destination_id, msg)

returnValue(response(
request, 'message submitted', msg, code=http.CREATED))
Expand Down Expand Up @@ -639,7 +647,7 @@ def get_message_events(self, request, location_id, message_id):
})

@inlineCallbacks
def send_messsage_on_channel(self, channel_id, body):
def send_message_on_channel(self, channel_id, body, in_msg=None):
if 'to' not in body and 'reply_to' not in body:
raise ApiUsageError(
'Either "to" or "reply_to" must be specified')
Expand All @@ -650,7 +658,8 @@ def send_messsage_on_channel(self, channel_id, body):
if 'reply_to' in body:
msg = yield channel.send_reply_message(
self.message_sender, self.outbounds, self.inbounds, body,
allow_expired_replies=self.config.allow_expired_replies)
allow_expired_replies=self.config.allow_expired_replies,
in_msg=in_msg)
else:
msg = yield channel.send_message(
self.message_sender, self.outbounds, body)
Expand Down
5 changes: 3 additions & 2 deletions junebug/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ def send_message(self, sender, outbounds, msg):

@inlineCallbacks
def send_reply_message(self, sender, outbounds, inbounds, msg,
allow_expired_replies=False):
allow_expired_replies=False, in_msg=None):
'''Sends a reply message.'''
in_msg = yield inbounds.load_vumi_message(self.id, msg['reply_to'])
if not in_msg:
in_msg = yield inbounds.load_vumi_message(self.id, msg['reply_to'])
# NOTE: If we have a `reply_to` that cannot be found but also are
# given a `to` and the config says we can send expired
# replies then pop the `reply_to` from the message
Expand Down
58 changes: 58 additions & 0 deletions junebug/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,64 @@ def test_send_destination_message(self):
'test-channel', message['message_id'])
self.assertEqual(event_url, None)

@inlineCallbacks
def test_send_destination_message_reply(self):
'''Sending a reply message should fetch the relevant inbound message,
use it to construct a reply message, and place the reply message on the
queue for the channel'''
properties = self.create_channel_properties()
config = yield self.create_channel_config()
redis = yield self.get_redis()
channel = Channel(redis, config, properties, id='test-channel')
yield channel.save()
yield channel.start(self.service)

router_config = self.create_router_config()
resp = yield self.post('/routers/', router_config)
router_id = (yield resp.json())['result']['id']

dest_config = self.create_destination_config(
config={'channel': 'test-channel'})
resp = yield self.post(
'/routers/{}/destinations/'.format(router_id), dest_config)
destination_id = (yield resp.json())['result']['id']

in_msg = TransportUserMessage(
from_addr='+2789',
to_addr='+1234',
transport_name='test-channel',
transport_type='_',
transport_metadata={'foo': 'bar'})

yield self.api.inbounds.store_vumi_message(destination_id, in_msg)
expected = in_msg.reply(content='testcontent')
expected = api_from_message(expected)

resp = yield self.post(
'/routers/{}/destinations/{}/messages/'.format(
router_id, destination_id),
{'reply_to': in_msg['message_id'], 'content': 'testcontent'})

yield self.assert_response(
resp, http.CREATED,
'message submitted',
omit(expected, 'timestamp', 'message_id'),
ignore=['timestamp', 'message_id'])

[message] = self.get_dispatched_messages('test-channel.outbound')
message_id = (yield resp.json())['result']['message_id']
self.assertEqual(message['message_id'], message_id)

event_url = yield self.api.outbounds.load_event_url(
'test-channel', message['message_id'])
self.assertEqual(event_url, None)

# router_worker = self.api.service.namedServices[router_id]
stored_message = yield self.api.outbounds.load_message(
destination_id, message['message_id'])

self.assertEqual(api_from_message(message), stored_message)

@inlineCallbacks
def test_get_destination_message_invalid_router(self):
resp = yield self.get(
Expand Down

0 comments on commit 3a832c0

Please sign in to comment.