def mk_msg(self, to_addr, tag=None, from_addr="12345"): msg = TransportUserMessage( to_addr=to_addr, from_addr=from_addr, transport_name="dummy_endpoint", transport_type="dummy_transport_type" ) if tag is not None: TaggingMiddleware.add_tag_to_msg(msg, tag) return msg
def test_deepupdate_with_recursion(self): self.mk_tagger() orig = {"a": {"b": "foo"}, "c": "bar"} new = {"a": {"b": "baz"}} new["a"]["d"] = new TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, new) self.assertEqual(orig, {"a": {"b": "baz"}, "c": "bar"})
def mk_msg(self, to_addr, tag=None, from_addr="12345"): msg = TransportUserMessage(to_addr=to_addr, from_addr=from_addr, transport_name="dummy_connector", transport_type="dummy_transport_type") if tag is not None: TaggingMiddleware.add_tag_to_msg(msg, tag) return msg
def test_deepupdate_with_recursion(self): self.mk_tagger() orig = {'a': {'b': "foo"}, 'c': "bar"} new = {'a': {'b': "baz"}} new['a']['d'] = new TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, new) self.assertEqual(orig, {'a': {'b': "baz"}, 'c': "bar"})
def test_deepupdate(self): orig = {'a': {'b': "foo"}, 'c': "bar"} TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, {'a': {'b': "baz"}, 'd': r'\g<0>!', 'e': 1}) self.assertEqual(orig, {'a': {'b': "baz"}, 'c': "bar", 'd': "foo!", 'e': 1})
def test_handle_inbound_with_tag(self): mw = yield self.setup_middleware() batch_id = yield self.store.batch_start([("pool", "tag")]) msg = self.mk_msg() TaggingMiddleware.add_tag_to_msg(msg, ["pool", "tag"]) response = yield mw.handle_inbound(msg, "dummy_connector") self.assertEqual(response, msg) yield self.assert_inbound_stored(msg, batch_id)
def test_handle_inbound_with_tag(self): batch_id = self.store.batch_start([("pool", "tag")]) msg = self.mk_msg() msg_id = msg['message_id'] TaggingMiddleware.add_tag_to_msg(msg, ["pool", "tag"]) response = self.mw.handle_inbound(msg, "dummy_endpoint") self.assertTrue(isinstance(response, TransportUserMessage)) self.assertEqual(self.store.get_inbound_message(msg_id), msg) self.assertEqual(self.store.batch_messages(batch_id), []) self.assertEqual(self.store.batch_replies(batch_id), [msg_id])
def test_add_tag_to_payload(self): payload = {} TaggingMiddleware.add_tag_to_payload(payload, ('pool', 'mytag')) self.assertEqual(payload, { 'helper_metadata': { 'tag': { 'tag': ['pool', 'mytag'], }, }, })
def send_keyword(self, mw, word, expected_response): msg = self.mw_helper.make_inbound( word, to_addr='*****@*****.**', from_addr='*****@*****.**') TaggingMiddleware.add_tag_to_msg(msg, ("pool", "tag1")) yield mw.handle_inbound(msg, 'dummy_endpoint') expected_response = dict(expected_response, tag={'tag': ['pool', 'tag1']}) # MessageMetadataHelper can add 'go' metadata and we want to ignore it. if 'go' in msg['helper_metadata']: expected_response['go'] = msg['helper_metadata']['go'] self.assertEqual(msg['helper_metadata'], expected_response)
def send_keyword(self, mw, word, expected_response): msg = self.mw_helper.make_inbound(word, to_addr='*****@*****.**', from_addr='*****@*****.**') TaggingMiddleware.add_tag_to_msg(msg, ("pool", "tag1")) yield mw.handle_inbound(msg, 'dummy_endpoint') expected_response = dict(expected_response, tag={'tag': ['pool', 'tag1']}) # MessageMetadataHelper can add 'go' metadata and we want to ignore it. if 'go' in msg['helper_metadata']: expected_response['go'] = msg['helper_metadata']['go'] self.assertEqual(msg['helper_metadata'], expected_response)
def mk_msg(self, to_addr, from_addr, session_event=SESSION_NEW, session_start=None, session_end=None, tag=None, transport_name='dummy_transport'): msg = TransportUserMessage( to_addr=to_addr, from_addr=from_addr, transport_name=transport_name, transport_type="dummy_transport_type", session_event=session_event) if tag is not None: TaggingMiddleware.add_tag_to_msg(msg, tag) if session_start is not None: self._set_metadata(msg, 'session_start', session_start) if session_end is not None: self._set_metadata(msg, 'session_end', session_end) return msg
def __init__(self, vumi_api, message): self.vumi_api = vumi_api self.message = message super(MessageMetadataHelper, self).__init__( message.get('helper_metadata', {})) # A place to store objects we don't want serialised. if not hasattr(message, '_store_objects'): message._store_objects = {} self._store_objects = message._store_objects # If we don't have a tag, we want to blow up early in some places. self.tag = TaggingMiddleware.map_msg_to_tag(message)
def __init__(self, vumi_api, message): self.vumi_api = vumi_api self.message = message super(MessageMetadataHelper, self).__init__(message.get('helper_metadata', {})) # A place to store objects we don't want serialised. if not hasattr(message, '_store_objects'): message._store_objects = {} self._store_objects = message._store_objects # If we don't have a tag, we want to blow up early in some places. self.tag = TaggingMiddleware.map_msg_to_tag(message)
def mk_msg(self, to_addr, from_addr, session_event=SESSION_NEW, session_start=None, session_end=None, tag=None, transport_name='dummy_transport'): msg = TransportUserMessage(to_addr=to_addr, from_addr=from_addr, transport_name=transport_name, transport_type="dummy_transport_type", session_event=session_event) if tag is not None: TaggingMiddleware.add_tag_to_msg(msg, tag) if session_start is not None: self._set_metadata(msg, 'session_start', session_start) if session_end is not None: self._set_metadata(msg, 'session_end', session_end) return msg
def get_tag(self, to_addr): msg = self.mk_msg(to_addr) msg = self.mw.handle_inbound(msg, "dummy_connector") return TaggingMiddleware.map_msg_to_tag(msg)
def mk_tagger(self, config=None): dummy_worker = object() if config is None: config = self.DEFAULT_CONFIG self.mw = TaggingMiddleware("dummy_tagger", config, dummy_worker) self.mw.setup_middleware()
def set_tag(self, tag): TaggingMiddleware.add_tag_to_msg(self.message, tag) self.tag = TaggingMiddleware.map_msg_to_tag(self.message)
def test_add_tag_to_msg(self): msg = self.mk_msg("123456") TaggingMiddleware.add_tag_to_msg(msg, ('pool', 'mytag')) self.assertEqual(msg['helper_metadata']['tag'], { 'tag': ['pool', 'mytag'], })
def test_add_tag_to_payload(self): payload = {} TaggingMiddleware.add_tag_to_payload(payload, ("pool", "mytag")) self.assertEqual(payload, {"helper_metadata": {"tag": {"tag": ["pool", "mytag"]}}})
def test_map_msg_to_tag(self): msg = self.mk_msg("123456") self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), None) msg["helper_metadata"]["tag"] = {"tag": ["pool", "mytag"]} self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), ("pool", "mytag"))
def test_map_msg_to_tag(self): msg = self.mk_msg("123456") self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), None) msg['helper_metadata']['tag'] = {'tag': ['pool', 'mytag']} self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), ("pool", "mytag"))
def handle_outbound(self, message, endpoint): tag = TaggingMiddleware.map_msg_to_tag(message) self.store.add_outbound_message(message, tag=tag) return message
def handle_inbound(self, message, connector_name): tag = TaggingMiddleware.map_msg_to_tag(message) yield self.store.add_inbound_message(message, tag=tag) returnValue(message)
def test_add_tag_to_msg(self): msg = self.mk_msg("123456") TaggingMiddleware.add_tag_to_msg(msg, ("pool", "mytag")) self.assertEqual(msg["helper_metadata"]["tag"], {"tag": ["pool", "mytag"]})
def handle_outbound(self, message, connector_name): tag = TaggingMiddleware.map_msg_to_tag(message) yield self.store.add_outbound_message(message, tag=tag) returnValue(message)
def get_tag(self, to_addr): msg = self.mk_msg(to_addr) msg = self.mw.handle_inbound(msg, "dummy_endpoint") return TaggingMiddleware.map_msg_to_tag(msg)
def test_deepupdate(self): orig = {"a": {"b": "foo"}, "c": "bar"} TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, {"a": {"b": "baz"}, "d": r"\g<0>!", "e": 1}) self.assertEqual(orig, {"a": {"b": "baz"}, "c": "bar", "d": "foo!", "e": 1})
class TestTaggingMiddleware(VumiTestCase): DEFAULT_CONFIG = { 'incoming': { 'addr_pattern': r'^\d+(\d{3})$', 'tagpool_template': r'pool1', 'tagname_template': r'mytag-\1', }, 'outgoing': { 'tagname_pattern': r'mytag-(\d{3})$', 'msg_template': { 'from_addr': r'1234*\1', }, }, } def mk_tagger(self, config=None): dummy_worker = object() if config is None: config = self.DEFAULT_CONFIG self.mw = TaggingMiddleware("dummy_tagger", config, dummy_worker) self.mw.setup_middleware() def mk_msg(self, to_addr, tag=None, from_addr="12345"): msg = TransportUserMessage(to_addr=to_addr, from_addr=from_addr, transport_name="dummy_connector", transport_type="dummy_transport_type") if tag is not None: TaggingMiddleware.add_tag_to_msg(msg, tag) return msg def get_tag(self, to_addr): msg = self.mk_msg(to_addr) msg = self.mw.handle_inbound(msg, "dummy_connector") return TaggingMiddleware.map_msg_to_tag(msg) def get_from_addr(self, to_addr, tag): msg = self.mk_msg(to_addr, tag, from_addr=None) msg = self.mw.handle_outbound(msg, "dummy_connector") return msg['from_addr'] def test_inbound_matching_to_addr(self): self.mk_tagger() self.assertEqual(self.get_tag("123456"), ("pool1", "mytag-456")) self.assertEqual(self.get_tag("1234"), ("pool1", "mytag-234")) def test_inbound_nonmatching_to_addr(self): self.mk_tagger() self.assertEqual(self.get_tag("a1234"), None) def test_inbound_nonmatching_to_addr_leaves_msg_unmodified(self): self.mk_tagger() tag = ("dont", "modify") orig_msg = self.mk_msg("a1234", tag=tag) msg = orig_msg.from_json(orig_msg.to_json()) msg = self.mw.handle_inbound(msg, "dummy_connector") self.assertEqual(msg, orig_msg) def test_inbound_none_to_addr(self): self.mk_tagger() self.assertEqual(self.get_tag(None), None) def test_outbound_matching_tag(self): self.mk_tagger() self.assertEqual(self.get_from_addr("111", ("pool1", "mytag-456")), "1234*456") self.assertEqual(self.get_from_addr("111", ("pool1", "mytag-789")), "1234*789") def test_outbound_nonmatching_tag(self): self.mk_tagger() self.assertEqual(self.get_from_addr("111", ("pool1", "othertag-456")), None) def test_outbound_nonmatching_tag_leaves_msg_unmodified(self): self.mk_tagger() orig_msg = self.mk_msg("a1234", tag=("pool1", "othertag-456")) msg = orig_msg.from_json(orig_msg.to_json()) msg = self.mw.handle_outbound(msg, "dummy_connector") for key in msg.payload.keys(): self.assertEqual(msg[key], orig_msg[key], "Key %r not equal" % key) self.assertEqual(msg, orig_msg) def test_outbound_no_tag(self): self.mk_tagger() self.assertEqual(self.get_from_addr("111", None), None) def test_deepupdate(self): orig = {'a': {'b': "foo"}, 'c': "bar"} TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, {'a': {'b': "baz"}, 'd': r'\g<0>!', 'e': 1}) self.assertEqual(orig, {'a': {'b': "baz"}, 'c': "bar", 'd': "foo!", 'e': 1}) def test_deepupdate_with_recursion(self): self.mk_tagger() orig = {'a': {'b': "foo"}, 'c': "bar"} new = {'a': {'b': "baz"}} new['a']['d'] = new TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, new) self.assertEqual(orig, {'a': {'b': "baz"}, 'c': "bar"}) def test_map_msg_to_tag(self): msg = self.mk_msg("123456") self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), None) msg['helper_metadata']['tag'] = {'tag': ['pool', 'mytag']} self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), ("pool", "mytag")) def test_add_tag_to_msg(self): msg = self.mk_msg("123456") TaggingMiddleware.add_tag_to_msg(msg, ('pool', 'mytag')) self.assertEqual(msg['helper_metadata']['tag'], { 'tag': ['pool', 'mytag'], }) def test_add_tag_to_payload(self): payload = {} TaggingMiddleware.add_tag_to_payload(payload, ('pool', 'mytag')) self.assertEqual(payload, { 'helper_metadata': { 'tag': { 'tag': ['pool', 'mytag'], }, }, })
class TaggingMiddlewareTestCase(TestCase): DEFAULT_CONFIG = { "incoming": {"addr_pattern": r"^\d+(\d{3})$", "tagpool_template": r"pool1", "tagname_template": r"mytag-\1"}, "outgoing": {"tagname_pattern": r"mytag-(\d{3})$", "msg_template": {"from_addr": r"1234*\1"}}, } def mk_tagger(self, config=None): dummy_worker = object() if config is None: config = self.DEFAULT_CONFIG self.mw = TaggingMiddleware("dummy_tagger", config, dummy_worker) self.mw.setup_middleware() def mk_msg(self, to_addr, tag=None, from_addr="12345"): msg = TransportUserMessage( to_addr=to_addr, from_addr=from_addr, transport_name="dummy_endpoint", transport_type="dummy_transport_type" ) if tag is not None: TaggingMiddleware.add_tag_to_msg(msg, tag) return msg def get_tag(self, to_addr): msg = self.mk_msg(to_addr) msg = self.mw.handle_inbound(msg, "dummy_endpoint") return TaggingMiddleware.map_msg_to_tag(msg) def get_from_addr(self, to_addr, tag): msg = self.mk_msg(to_addr, tag, from_addr=None) msg = self.mw.handle_outbound(msg, "dummy_endpoint") return msg["from_addr"] def test_inbound_matching_to_addr(self): self.mk_tagger() self.assertEqual(self.get_tag("123456"), ("pool1", "mytag-456")) self.assertEqual(self.get_tag("1234"), ("pool1", "mytag-234")) def test_inbound_nonmatching_to_addr(self): self.mk_tagger() self.assertEqual(self.get_tag("a1234"), None) def test_inbound_nonmatching_to_addr_leaves_msg_unmodified(self): self.mk_tagger() tag = ("dont", "modify") orig_msg = self.mk_msg("a1234", tag=tag) msg = orig_msg.from_json(orig_msg.to_json()) msg = self.mw.handle_inbound(msg, "dummy_endpoint") self.assertEqual(msg, orig_msg) def test_inbound_none_to_addr(self): self.mk_tagger() self.assertEqual(self.get_tag(None), None) def test_outbound_matching_tag(self): self.mk_tagger() self.assertEqual(self.get_from_addr("111", ("pool1", "mytag-456")), "1234*456") self.assertEqual(self.get_from_addr("111", ("pool1", "mytag-789")), "1234*789") def test_outbound_nonmatching_tag(self): self.mk_tagger() self.assertEqual(self.get_from_addr("111", ("pool1", "othertag-456")), None) def test_outbound_nonmatching_tag_leaves_msg_unmodified(self): self.mk_tagger() orig_msg = self.mk_msg("a1234", tag=("pool1", "othertag-456")) msg = orig_msg.from_json(orig_msg.to_json()) msg = self.mw.handle_outbound(msg, "dummy_endpoint") for key in msg.payload.keys(): self.assertEqual(msg[key], orig_msg[key], "Key %r not equal" % key) self.assertEqual(msg, orig_msg) def test_outbound_no_tag(self): self.mk_tagger() self.assertEqual(self.get_from_addr("111", None), None) def test_deepupdate(self): orig = {"a": {"b": "foo"}, "c": "bar"} TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, {"a": {"b": "baz"}, "d": r"\g<0>!", "e": 1}) self.assertEqual(orig, {"a": {"b": "baz"}, "c": "bar", "d": "foo!", "e": 1}) def test_deepupdate_with_recursion(self): self.mk_tagger() orig = {"a": {"b": "foo"}, "c": "bar"} new = {"a": {"b": "baz"}} new["a"]["d"] = new TaggingMiddleware._deepupdate(re.match(".*", "foo"), orig, new) self.assertEqual(orig, {"a": {"b": "baz"}, "c": "bar"}) def test_map_msg_to_tag(self): msg = self.mk_msg("123456") self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), None) msg["helper_metadata"]["tag"] = {"tag": ["pool", "mytag"]} self.assertEqual(TaggingMiddleware.map_msg_to_tag(msg), ("pool", "mytag")) def test_add_tag_to_msg(self): msg = self.mk_msg("123456") TaggingMiddleware.add_tag_to_msg(msg, ("pool", "mytag")) self.assertEqual(msg["helper_metadata"]["tag"], {"tag": ["pool", "mytag"]})
def set_conversation_tag(self, msg, conversation): # TOOD: Move into AppWorkerTestCase once it's working tag = (conversation.delivery_tag_pool, conversation.delivery_tag) TaggingMiddleware.add_tag_to_msg(msg, tag) return msg
def get_tag(self, message): tag = TaggingMiddleware.map_msg_to_tag(message) if tag is None: return None else: return ":".join(tag)