Skip to content

Commit

Permalink
Merge pull request #1063 from praekelt/feature/OPENHELPD-158-cast-smp…
Browse files Browse the repository at this point in the history
…p-data-coding-keys-to-ints

Cast smpp data coding keys to ints
  • Loading branch information
rudigiesler committed Jan 10, 2017
2 parents a679391 + 8b4086d commit fcb1fac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
16 changes: 13 additions & 3 deletions vumi/transports/smpp/processors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from vumi.config import (
Config, ConfigDict, ConfigRegex, ConfigText, ConfigInt, ConfigBool)
from vumi.errors import ConfigError
from vumi.message import TransportUserMessage
from vumi.transports.smpp.iprocessors import (
IDeliveryReportProcessor, IDeliverShortMessageProcessor,
Expand Down Expand Up @@ -209,8 +210,9 @@ class DeliverShortMessageProcessorConfig(Config):
"setting the default encoding (0), adding additional undefined "
"encodings (such as 4 or 8) or overriding encodings in cases where "
"the SMSC is violating the spec (which happens a lot). Keys should "
"be integers, values should be strings containing valid Python "
"character encoding names.", default={}, static=True)
"be something that can be cast to an integer, values should be strings"
"containing valid Python character encoding names.",
default={}, static=True)

allow_empty_messages = ConfigBool(
"If True, send on empty messages as an empty unicode string. "
Expand Down Expand Up @@ -268,7 +270,15 @@ def __init__(self, transport, config):
9: 'shift_jis',
10: 'iso2022_jp'
}
self.data_coding_map.update(self.config.data_coding_overrides)
try:
self.data_coding_map.update({
int(key): value for key, value in
self.config.data_coding_overrides.items()})
except ValueError as e:
raise ConfigError(
"data_coding_overrides keys must be castable to ints. "
"{}".format(e.message))

self.allow_empty_messages = self.config.allow_empty_messages

def dcs_decode(self, obj, data_coding):
Expand Down
63 changes: 63 additions & 0 deletions vumi/transports/smpp/processors/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from twisted.internet.defer import inlineCallbacks
from twisted.trial.unittest import FailTest

from vumi.errors import ConfigError
from vumi.tests.helpers import VumiTestCase
from vumi.transports.tests.helpers import TransportHelper
from vumi.transports.smpp.smpp_transport import SmppTransceiverTransport
from vumi.transports.smpp.tests.fake_smsc import FakeSMSC


class DefaultProcessorTestCase(VumiTestCase):
def setUp(self):
self.fake_smsc = FakeSMSC()
self.tx_helper = self.add_helper(
TransportHelper(SmppTransceiverTransport))

@inlineCallbacks
def test_data_coding_override_keys_ints(self):
"""
If the keys of the data coding overrides config dictionary are not
integers, they should be cast to integers.
"""
config = {
'system_id': 'foo',
'password': 'bar',
'twisted_endpoint': self.fake_smsc.endpoint,
'deliver_short_message_processor_config': {
'data_coding_overrides': {
'0': 'utf-8'
},
},
}
transport = yield self.tx_helper.get_transport(config)
self.assertEqual(
transport.deliver_sm_processor.data_coding_map.get(0), 'utf-8')

@inlineCallbacks
def test_data_coding_override_keys_invalid(self):
"""
If the keys of the data coding overrides config dictionary can not be
cast to integers, a config error with an appropriate message should
be raised.
"""
config = {
'system_id': 'foo',
'password': 'bar',
'twisted_endpoint': self.fake_smsc.endpoint,
'deliver_short_message_processor_config': {
'data_coding_overrides': {
'not-an-int': 'utf-8'
},
},
}
try:
yield self.tx_helper.get_transport(config)
except ConfigError as e:
self.assertEqual(
str(e),
"data_coding_overrides keys must be castable to ints. "
"invalid literal for int() with base 10: 'not-an-int'"
)
else:
raise FailTest("Expected ConfigError to be raised")
5 changes: 4 additions & 1 deletion vumi/transports/smpp/smpp_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ class SmppTransceiverTransport(Transport):
bind_type = 'TRX'
clock = reactor
start_message_consumer = False
service = None
redis = None

@property
def throttled(self):
Expand Down Expand Up @@ -293,7 +295,8 @@ def start_service(self):
def teardown_transport(self):
if self.service:
yield self.service.stopService()
yield self.redis._close()
if self.redis:
yield self.redis._close()

def _check_address_valid(self, message, field):
try:
Expand Down

0 comments on commit fcb1fac

Please sign in to comment.