Ejemplo n.º 1
0
    def test_store_event(self):
        '''Stores the event under the message ID'''
        store = yield self.create_store()
        event = TransportEvent(user_message_id='message_id',
                               sent_message_id='message_id',
                               event_type='ack')
        yield store.store_event('channel_id', 'message_id', event)

        event_json = yield self.redis.hget(
            'channel_id:outbound_messages:message_id', event['event_id'])
        self.assertEqual(event_json, event.to_json())
Ejemplo n.º 2
0
    def test_store_event(self):
        '''Stores the event under the message ID'''
        store = yield self.create_store()
        event = TransportEvent(
            user_message_id='message_id', sent_message_id='message_id',
            event_type='ack')
        yield store.store_event('channel_id', 'message_id', event)

        event_json = yield self.redis.hget(
            'channel_id:outbound_messages:message_id', event['event_id'])
        self.assertEqual(event_json, event.to_json())
Ejemplo n.º 3
0
    def test_load_all_events_one(self):
        '''Returns a list with one event inside'''
        store = yield self.create_store()
        event = TransportEvent(user_message_id='message_id',
                               sent_message_id='message_id',
                               event_type='delivery_report',
                               delivery_status='pending')
        yield self.redis.hset('channel_id:outbound_messages:message_id',
                              event['event_id'], event.to_json())

        events = yield store.load_all_events('channel_id', 'message_id')
        self.assertEqual(events, [event])
Ejemplo n.º 4
0
    def test_load_event(self):
        store = yield self.create_store()
        event = TransportEvent(user_message_id='message_id',
                               sent_message_id='message_id',
                               event_type='nack',
                               nack_reason='error error')
        yield self.redis.hset('channel_id:outbound_messages:message_id',
                              event['event_id'], event.to_json())

        stored_event = yield store.load_event('channel_id', 'message_id',
                                              event['event_id'])
        self.assertEqual(stored_event, event)
Ejemplo n.º 5
0
    def test_load_all_events_one(self):
        '''Returns a list with one event inside'''
        store = yield self.create_store()
        event = TransportEvent(
            user_message_id='message_id', sent_message_id='message_id',
            event_type='delivery_report', delivery_status='pending')
        yield self.redis.hset(
            'channel_id:outbound_messages:message_id', event['event_id'],
            event.to_json())

        events = yield store.load_all_events('channel_id', 'message_id')
        self.assertEqual(events, [event])
Ejemplo n.º 6
0
    def test_load_event(self):
        store = yield self.create_store()
        event = TransportEvent(
            user_message_id='message_id', sent_message_id='message_id',
            event_type='nack', nack_reason='error error')
        yield self.redis.hset(
            'channel_id:outbound_messages:message_id', event['event_id'],
            event.to_json())

        stored_event = yield store.load_event(
            'channel_id', 'message_id', event['event_id'])
        self.assertEqual(stored_event, event)
Ejemplo n.º 7
0
    def test_load_all_events_with_other_stored_fields(self):
        '''Should return just the stored events'''
        store = yield self.create_store()
        event = TransportEvent(user_message_id='message_id',
                               sent_message_id='message_id',
                               event_type='delivery_report',
                               delivery_status='pending')
        yield self.redis.hset('channel_id:outbound_messages:message_id',
                              event['event_id'], event.to_json())
        yield self.redis.hset('channel_id:outbound_messages:message_id',
                              'event_url', 'test_url')

        stored_events = yield store.load_all_events('channel_id', 'message_id')
        self.assertEqual(stored_events, [event])
Ejemplo n.º 8
0
 def test_transport_event_ack(self):
     msg = TransportEvent(
         event_id='def',
         event_type='ack',
         user_message_id='abc',
         sent_message_id='ghi',
     )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('ack', msg['event_type'])
     self.assertEqual('ack', msg.status())
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     self.assertEqual('ghi', msg['sent_message_id'])
     self.assertEqual({}, msg['helper_metadata'])
Ejemplo n.º 9
0
    def test_load_all_events_with_other_stored_fields(self):
        '''Should return just the stored events'''
        store = yield self.create_store()
        event = TransportEvent(
            user_message_id='message_id', sent_message_id='message_id',
            event_type='delivery_report', delivery_status='pending')
        yield self.redis.hset(
            'channel_id:outbound_messages:message_id', event['event_id'],
            event.to_json())
        yield self.redis.hset(
            'channel_id:outbound_messages:message_id', 'event_url',
            'test_url')

        stored_events = yield store.load_all_events('channel_id', 'message_id')
        self.assertEqual(stored_events, [event])
Ejemplo n.º 10
0
 def test_transport_event_ack(self):
     msg = TransportEvent(
         event_id='def',
         event_type='ack',
         user_message_id='abc',
         sent_message_id='ghi',
         )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('ack', msg['event_type'])
     self.assertEqual('ack', msg.status())
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     self.assertEqual('ghi', msg['sent_message_id'])
     self.assertEqual({}, msg['helper_metadata'])
Ejemplo n.º 11
0
 def test_transport_event_nack(self):
     msg = TransportEvent(
         event_id='def',
         event_type='nack',
         user_message_id='abc',
         nack_reason='unknown',
     )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('nack', msg['event_type'])
     self.assertEqual('nack', msg.status())
     self.assertEqual('unknown', msg['nack_reason'])
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     self.assertEqual({}, msg['helper_metadata'])
Ejemplo n.º 12
0
 def test_transport_event_nack(self):
     msg = TransportEvent(
         event_id='def',
         event_type='nack',
         user_message_id='abc',
         nack_reason='unknown',
         )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('nack', msg['event_type'])
     self.assertEqual('nack', msg.status())
     self.assertEqual('unknown', msg['nack_reason'])
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     self.assertEqual({}, msg['helper_metadata'])
Ejemplo n.º 13
0
 def test_subclassing_api(self):
     worker = get_stubbed_worker(ApplicationWorker,
                                 {'transport_name': 'test'})
     ack = TransportEvent(event_type='ack',
                          sent_message_id='remote-id',
                          user_message_id='ack-uuid')
     dr = TransportEvent(event_type='delivery_report',
                         delivery_status='pending',
                         user_message_id='dr-uuid')
     worker.consume_ack(ack)
     worker.consume_delivery_report(dr)
     worker.consume_unknown_event(FakeUserMessage())
     worker.consume_user_message(FakeUserMessage())
     worker.new_session(FakeUserMessage())
     worker.close_session(FakeUserMessage())
Ejemplo n.º 14
0
 def test_event_dispatch(self):
     events = [
         ('ack',
          TransportEvent(event_type='ack',
                         sent_message_id='remote-id',
                         user_message_id='ack-uuid')),
         ('delivery_report',
          TransportEvent(event_type='delivery_report',
                         delivery_status='pending',
                         user_message_id='dr-uuid')),
     ]
     for name, event in events:
         yield self.send_event(event)
         self.assertEqual(self.worker.record, [(name, event)])
         del self.worker.record[:]
Ejemplo n.º 15
0
 def load_event(self, channel_id, message_id, event_id):
     '''Loads the event with id event_id'''
     key = self.get_key(channel_id, message_id)
     event_json = yield self.load_property(key, event_id)
     if event_json is None:
         returnValue(None)
     returnValue(TransportEvent.from_json(event_json))
Ejemplo n.º 16
0
 def load_all_events(self, channel_id, message_id):
     '''Returns a list of all the stored events'''
     key = self.get_key(channel_id, message_id)
     events_json = yield self.load_all(key)
     self._remove_property_keys(events_json)
     returnValue(
         [TransportEvent.from_json(e) for e in events_json.values()])
Ejemplo n.º 17
0
 def load_all_events(self, channel_id, message_id):
     '''Returns a list of all the stored events'''
     key = self.get_key(channel_id, message_id)
     events_json = yield self.load_all(key)
     self._remove_property_keys(events_json)
     returnValue([
         TransportEvent.from_json(e) for e in events_json.values()])
Ejemplo n.º 18
0
    def test_load_all_events_multiple(self):
        '''Returns a list of all the stored events'''
        store = yield self.create_store()
        events = []
        for i in range(5):
            event = TransportEvent(user_message_id='message_id',
                                   sent_message_id='message_id',
                                   event_type='delivery_report',
                                   delivery_status='pending')
            events.append(event)
            yield self.redis.hset('channel_id:outbound_messages:message_id',
                                  event['event_id'], event.to_json())

        stored_events = yield store.load_all_events('channel_id', 'message_id')
        self.assertEqual(sorted(events, key=lambda e: e['event_id']),
                         sorted(stored_events, key=lambda e: e['event_id']))
Ejemplo n.º 19
0
 def handle_raw_inbound_event(self, request):
     try:
         data = json.loads(request.content.read())
         msg = TransportEvent(_process_fields=True, **to_kwargs(data))
         yield self.handle_inbound_event(msg)
         request.finish()
         if msg.payload["event_type"] == "ack":
             self.update_status(status='ok',
                                component='sent-by-vumi-go',
                                type='vumi_go_sent',
                                message='Sent by Vumi Go')
         elif msg.payload["event_type"] == "nack":
             self.update_status(status='down',
                                component='sent-by-vumi-go',
                                type='vumi_go_failed',
                                message='Vumi Go failed to send')
         self.update_status(status='ok',
                            component='vumi-go-event',
                            type='good_request',
                            message='Good event received from Vumi Go')
     except Exception as e:
         log.err(e)
         request.setResponseCode(400)
         request.finish()
         self.update_status(status='down',
                            component='vumi-go-event',
                            type='bad_request',
                            message='Bad event received from Vumi Go')
Ejemplo n.º 20
0
 def load_event(self, channel_id, message_id, event_id):
     '''Loads the event with id event_id'''
     key = self.get_key(channel_id, message_id)
     event_json = yield self.load_property(key, event_id)
     if event_json is None:
         returnValue(None)
     returnValue(TransportEvent.from_json(event_json))
Ejemplo n.º 21
0
    def test_load_all_events_multiple(self):
        '''Returns a list of all the stored events'''
        store = yield self.create_store()
        events = []
        for i in range(5):
            event = TransportEvent(
                user_message_id='message_id', sent_message_id='message_id',
                event_type='delivery_report', delivery_status='pending')
            events.append(event)
            yield self.redis.hset(
                'channel_id:outbound_messages:message_id', event['event_id'],
                event.to_json())

        stored_events = yield store.load_all_events('channel_id', 'message_id')
        self.assertEqual(
            sorted(events, key=lambda e: e['event_id']),
            sorted(stored_events, key=lambda e: e['event_id']))
Ejemplo n.º 22
0
 def _ignore_message(self, failure, msg):
     failure.trap(IgnoreMessage)
     log.debug("Ignoring msg (with NACK) due to %r: %r" %
               (failure.value, msg))
     return self.publish_event(
         TransportEvent(user_message_id=msg['message_id'],
                        nack_reason=str(failure.value),
                        event_type='nack'))
Ejemplo n.º 23
0
 def test_unknown_event_dispatch(self):
     # temporarily pretend the worker doesn't know about acks
     del self.worker._event_handlers['ack']
     bad_event = TransportEvent(event_type='ack',
                                sent_message_id='remote-id',
                                user_message_id='bad-uuid')
     yield self.send_event(bad_event)
     self.assertEqual(self.worker.record, [('unknown_event', bad_event)])
Ejemplo n.º 24
0
 def make_message(self, **extra_fields):
     fields = dict(
         event_id='def',
         event_type='ack',
         user_message_id='abc',
         sent_message_id='ghi',
     )
     fields.update(extra_fields)
     return TransportEvent(**fields)
Ejemplo n.º 25
0
 def test_transport_event_delivery_report(self):
     msg = TransportEvent(
         event_id='def',
         event_type='delivery_report',
         user_message_id='abc',
         to_addr='+27831234567',
         from_addr='12345',
         # transport_name='sphex',
         delivery_status='delivered',
     )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('delivery_report', msg['event_type'])
     self.assertEqual('delivery_report.delivered', msg.status())
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     # self.assertEqual('sphex', msg['transport_name'])
     self.assertEqual('delivered', msg['delivery_status'])
     self.assertEqual({}, msg['helper_metadata'])
Ejemplo n.º 26
0
 def test_transport_event_delivery_report(self):
     msg = TransportEvent(
         event_id='def',
         event_type='delivery_report',
         user_message_id='abc',
         to_addr='+27831234567',
         from_addr='12345',
         # transport_name='sphex',
         delivery_status='delivered',
         )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('delivery_report', msg['event_type'])
     self.assertEqual('delivery_report.delivered', msg.status())
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     # self.assertEqual('sphex', msg['transport_name'])
     self.assertEqual('delivered', msg['delivery_status'])
     self.assertEqual({}, msg['helper_metadata'])
Ejemplo n.º 27
0
 def mkmsg_ack(self, transport_name, **kw):
     event_kw = dict(
         event_type='ack',
         user_message_id='1',
         sent_message_id='abc',
         transport_name=transport_name,
         transport_metadata={},
     )
     event_kw.update(kw)
     return TransportEvent(**event_kw)
Ejemplo n.º 28
0
 def handle_raw_inbound_event(self, request):
     try:
         data = json.loads(request.content.read())
         msg = TransportEvent(_process_fields=True, **to_kwargs(data))
         yield self.handle_inbound_event(msg)
         request.finish()
     except Exception as e:
         log.err(e)
         request.setResponseCode(400)
         request.finish()
Ejemplo n.º 29
0
    def publish_event(self, **kw):
        """
        Publish a :class:`TransportEvent` message.

        Some default parameters are handled, so subclasses don't have
        to provide a lot of boilerplate.
        """
        kw.setdefault('transport_name', self.transport_name)
        kw.setdefault('transport_metadata', {})
        event = TransportEvent(**kw)
        return self.connectors[self.transport_name].publish_event(event)
Ejemplo n.º 30
0
    def test_forward_dr_no_message(self):
        self.patch_logger()

        event = TransportEvent(event_type='delivery_report',
                               user_message_id='msg-21',
                               delivery_status='pending',
                               timestamp='2015-09-22 15:39:44.827794')

        yield self.worker.consume_delivery_report(event)

        self.assertEqual(self.logging_api.requests, [])
        yield self.assert_event_stored(event)
Ejemplo n.º 31
0
    def test_forward_nack_no_message(self):
        self.patch_logger()

        event = TransportEvent(event_type='nack',
                               user_message_id='msg-21',
                               nack_reason='too many foos',
                               timestamp='2015-09-22 15:39:44.827794')

        yield self.worker.consume_nack(event)

        self.assertEqual(self.logging_api.requests, [])
        yield self.assert_event_stored(event)
Ejemplo n.º 32
0
    def test_add_ack_event(self):
        batch_id = self.store.batch_start([("pool", "tag")])
        msg = self.mkmsg_out(content="outfoo")
        msg_id = msg['message_id']
        ack = TransportEvent(user_message_id=msg_id,
                             event_type='ack',
                             sent_message_id='xyz')
        ack_id = ack['event_id']
        self.store.add_outbound_message(msg, batch_id=batch_id)
        self.store.add_event(ack)

        self.assertEqual(self.store.get_event(ack_id), ack)
        self.assertEqual(self.store.message_events(msg_id), [ack_id])
Ejemplo n.º 33
0
 def mkmsg_ack(self,
               user_message_id='1',
               sent_message_id='abc',
               transport_metadata=None):
     if transport_metadata is None:
         transport_metadata = {}
     return TransportEvent(
         event_type='ack',
         user_message_id=user_message_id,
         sent_message_id=sent_message_id,
         transport_name=self.transport_name,
         transport_metadata=transport_metadata,
     )
Ejemplo n.º 34
0
 def mkmsg_delivery(self,
                    status='delivered',
                    user_message_id='abc',
                    transport_metadata=None):
     if transport_metadata is None:
         transport_metadata = {}
     return TransportEvent(
         event_type='delivery_report',
         transport_name=self.transport_name,
         user_message_id=user_message_id,
         delivery_status=status,
         to_addr='+41791234567',
         transport_metadata=transport_metadata,
     )
Ejemplo n.º 35
0
    def publish_event(self, **kw):
        """
        Publish a :class:`TransportEvent` message.

        Some default parameters are handled, so subclasses don't have
        to provide a lot of boilerplate.
        """
        kw.setdefault('transport_name', self.transport_name)
        kw.setdefault('transport_metadata', {})
        event = TransportEvent(**kw)
        d = self._middlewares.apply_publish("event", event,
                                            self.transport_name)
        d.addCallback(self.event_publisher.publish_message)
        return d
Ejemplo n.º 36
0
 def test_post_delivery_report(self):
     yield self.start_app_worker()
     msg1 = yield self.make_outbound(
         self.conversation, 'out 1', message_id='1')
     event_d = self.app_helper.make_dispatch_delivery_report(
         msg1, conv=self.conversation)
     req = yield self.push_calls.get()
     posted_json_data = req.content.read()
     self.assertEqual(
         req.requestHeaders.getRawHeaders('content-type'),
         ['application/json; charset=utf-8'])
     req.finish()
     dr1 = yield event_d
     self.assertEqual(TransportEvent.from_json(posted_json_data), dr1)
Ejemplo n.º 37
0
    def test_post_inbound_event(self):
        msg1 = yield self.app_helper.make_stored_outbound(
            self.conversation, 'out 1', message_id='1')
        event_d = self.app_helper.make_dispatch_ack(
            msg1, conv=self.conversation)

        req = yield self.push_calls.get()
        posted_json_data = req.content.read()
        self.assertEqual(
            req.requestHeaders.getRawHeaders('content-type'),
            ['application/json; charset=utf-8'])
        req.finish()
        ack1 = yield event_d

        self.assertEqual(TransportEvent.from_json(posted_json_data), ack1)
Ejemplo n.º 38
0
 def test_transport_event_ack(self):
     msg = TransportEvent(
         event_id='def',
         event_type='ack',
         user_message_id='abc',
         # transport_name='sphex',
         sent_message_id='ghi',
     )
     self.assertEqual('event', msg['message_type'])
     self.assertEqual('ack', msg['event_type'])
     self.assertEqual('def', msg['event_id'])
     self.assertEqual('abc', msg['user_message_id'])
     self.assertEqual('20110921', msg['message_version'])
     # self.assertEqual('sphex', msg['transport_name'])
     self.assertEqual('ghi', msg['sent_message_id'])
Ejemplo n.º 39
0
 def mkmsg_ack(self,
               user_message_id='1',
               sent_message_id='abc',
               transport_metadata=None):
     if transport_metadata is None:
         transport_metadata = {}
         return TransportEvent(
             event_id=RegexMatcher(r'^[0-9a-fA-F]{32}$'),
             event_type='ack',
             user_message_id=user_message_id,
             sent_message_id=sent_message_id,
             timestamp=datetime.now(),
             transport_name=self.transport_name,
             transport_metadata=transport_metadata,
         )
Ejemplo n.º 40
0
    def test_post_inbound_event(self):
        msg1 = yield self.app_helper.make_stored_outbound(self.conversation,
                                                          'out 1',
                                                          message_id='1')
        event_d = self.app_helper.make_dispatch_ack(msg1,
                                                    conv=self.conversation)

        req = yield self.push_calls.get()
        posted_json_data = req.content.read()
        self.assertEqual(req.requestHeaders.getRawHeaders('content-type'),
                         ['application/json; charset=utf-8'])
        req.finish()
        ack1 = yield event_d

        self.assertEqual(TransportEvent.from_json(posted_json_data), ack1)
Ejemplo n.º 41
0
 def test_post_unknown_event(self):
     yield self.start_app_worker()
     # temporarily pretend the worker doesn't know about acks
     del self.app._event_handlers['ack']
     msg1 = yield self.make_outbound(
         self.conversation, 'out 1', message_id='1')
     event_d = self.app_helper.make_dispatch_ack(
         msg1, conv=self.conversation)
     req = yield self.push_calls.get()
     posted_json_data = req.content.read()
     self.assertEqual(
         req.requestHeaders.getRawHeaders('content-type'),
         ['application/json; charset=utf-8'])
     req.finish()
     ack1 = yield event_d
     self.assertEqual(TransportEvent.from_json(posted_json_data), ack1)
Ejemplo n.º 42
0
 def test_cache_add_event(self):
     msg_id, msg, batch_id = yield self._create_outbound()
     ack = TransportEvent(user_message_id=msg_id,
                          event_type='ack',
                          sent_message_id='xyz')
     yield self.store.add_event(ack)
     self.assertEqual(
         (yield self.store.cache.get_event_status(batch_id)), {
             'delivery_report': 0,
             'delivery_report.delivered': 0,
             'delivery_report.failed': 0,
             'delivery_report.pending': 0,
             'ack': 1,
             'nack': 0,
             'sent': 1,
         })
Ejemplo n.º 43
0
    def test_forward_dr_amqp(self):
        '''A sent delivery report event should be forwarded to the correct
        AMQP queue if the config option is set.'''
        worker = yield self.get_worker(config={'message_queue': 'testqueue'})
        event = TransportEvent(event_type='delivery_report',
                               user_message_id='msg-21',
                               delivery_status='pending',
                               timestamp='2015-09-22 15:39:44.827794')

        yield worker.consume_nack(event)

        [dispatched_msg
         ] = self.app_helper.get_dispatched('testqueue', 'event',
                                            TransportEvent)

        self.assertEqual(dispatched_msg['event_id'], event['event_id'])
Ejemplo n.º 44
0
    def test_post_inbound_event(self):
        # Set the URL so stuff is HTTP Posted instead of streamed.
        self.conversation.config['http_api'].update({
            'push_event_url': self.mock_push_server.url,
        })
        yield self.conversation.save()

        msg = yield self.app_helper.make_stored_outbound(
            self.conversation, 'out 1', message_id='1')
        event_d = self.app_helper.make_dispatch_ack(
            msg, conv=self.conversation)

        req = yield self.push_calls.get()
        posted_json_data = req.content.read()
        req.finish()
        ack = yield event_d

        self.assertEqual(TransportEvent.from_json(posted_json_data), ack)
Ejemplo n.º 45
0
 def cb(request):
     events.append(TransportEvent.from_json(request.content.getvalue()))
     return ''