示例#1
0
 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
示例#2
0
 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"})
示例#3
0
 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
示例#4
0
 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"})
示例#5
0
 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})
示例#6
0
 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)
示例#7
0
 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])
示例#9
0
 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'],
                 },
             },
         })
示例#10
0
 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])
示例#11
0
 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)
示例#12
0
 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)
示例#13
0
    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
示例#14
0
文件: utils.py 项目: TouK/vumi-go
    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)
示例#15
0
    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)
示例#16
0
    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
示例#17
0
 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)
示例#18
0
 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()
示例#19
0
 def set_tag(self, tag):
     TaggingMiddleware.add_tag_to_msg(self.message, tag)
     self.tag = TaggingMiddleware.map_msg_to_tag(self.message)
示例#20
0
 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'],
         })
示例#21
0
 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"]}}})
示例#22
0
 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"))
示例#23
0
 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"))
示例#24
0
 def handle_outbound(self, message, endpoint):
     tag = TaggingMiddleware.map_msg_to_tag(message)
     self.store.add_outbound_message(message, tag=tag)
     return message
示例#25
0
 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)
示例#26
0
 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()
示例#27
0
 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"]})
示例#28
0
 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)
示例#29
0
 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)
示例#30
0
 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})
示例#31
0
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'],
                    },
                },
            })
示例#32
0
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"]})
示例#33
0
文件: utils.py 项目: TouK/vumi-go
 def set_tag(self, tag):
     TaggingMiddleware.add_tag_to_msg(self.message, tag)
     self.tag = TaggingMiddleware.map_msg_to_tag(self.message)
示例#34
0
 def handle_outbound(self, message, endpoint):
     tag = TaggingMiddleware.map_msg_to_tag(message)
     self.store.add_outbound_message(message, tag=tag)
     return message
示例#35
0
 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
示例#36
0
 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
示例#37
0
 def get_tag(self, message):
     tag = TaggingMiddleware.map_msg_to_tag(message)
     if tag is None:
         return None
     else:
         return ":".join(tag)
示例#38
0
 def get_tag(self, message):
     tag = TaggingMiddleware.map_msg_to_tag(message)
     if tag is None:
         return None
     else:
         return ":".join(tag)