Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Add support for retrieving config for outbound messages from RapidSMS.
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgestar committed Jan 10, 2014
1 parent 6d0dd00 commit 78ea172
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
13 changes: 12 additions & 1 deletion go/apps/rapidsms/tests/test_vumi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from vumi.tests.utils import LogCatcher
from vumi.tests.helpers import VumiTestCase
from vumi.config import ConfigContext

from go.apps.tests.helpers import AppWorkerHelper
from go.apps.rapidsms.vumi_app import RapidSMSApplication, RapidSMSConfig
Expand Down Expand Up @@ -76,14 +77,24 @@ def test_teardown_application(self):
# TODO: check something?

@inlineCallbacks
def test_get_config(self):
def test_get_config_for_message(self):
app = yield self.app_helper.get_app_worker(self.APP_CONFIG)
conv = yield self.app_helper.create_conversation(
config=self.CONV_CONFIG, started=True)
msg = yield self.app_helper.make_stored_inbound(conv, "foo")
config = yield app.get_config(msg)
self.assertTrue(isinstance(config, RapidSMSConfig))

@inlineCallbacks
def test_get_config_for_username(self):
app = yield self.app_helper.get_app_worker(self.APP_CONFIG)
conv = yield self.app_helper.create_conversation(
config=self.CONV_CONFIG, started=True)
ctxt = ConfigContext(
username="%s:%s" % (conv.user_account.key, conv.key))
config = yield app.get_config(None, ctxt=ctxt)
self.assertTrue(isinstance(config, RapidSMSConfig))

@inlineCallbacks
def test_process_command_start(self):
yield self.app_helper.get_app_worker(self.APP_CONFIG)
Expand Down
28 changes: 25 additions & 3 deletions go/apps/rapidsms/vumi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"""Vumi Go application worker for RapidSMS."""

from twisted.internet.defer import inlineCallbacks
from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.application.rapidsms_relay import RapidSMSRelay
from vumi import log
Expand Down Expand Up @@ -41,8 +41,30 @@ def get_config_data_for_conversation(self, conversation):
dynamic_config["vumi_password"] = api_tokens[0] if api_tokens else None
return GoWorkerConfigData(self.config, dynamic_config)

def get_config(self, msg):
return self.get_message_config(msg)
@inlineCallbacks
def get_ctxt_config(self, ctxt):
username = getattr(ctxt, 'username', None)
if username is None:
raise ValueError("No username provided for retrieving"
" RapidSMS conversation.")
user_account_key, _, conversation_key = username.partition(":")
conv = yield self.get_conversation(user_account_key, conversation_key)
if conv is None:
log.warning("Cannot find conversation '%s' for user '%s'." % (
conversation_key, user_account_key))
raise ValueError("No conversation found for retrieiving"
" RapidSMS configuration.")
config = yield self.get_config_for_conversation(conv)
returnValue(config)

def get_config(self, msg, ctxt=None):
if msg is not None:
return self.get_message_config(msg)
elif ctxt is not None:
return self.get_ctxt_config(ctxt)
else:
raise ValueError("No msg or context provided for"
" retrieving a RapidSMS config.")

def process_command_start(self, user_account_key, conversation_key):
log.info("Starting RapidSMS conversation (key: %r)." %
Expand Down

0 comments on commit 78ea172

Please sign in to comment.