def test_handle_message_missing_key(self, mock_log): """Assert a message missing a key is logged.""" delivery_service = service.DeliveryService() delivery_service.handle_message(None) mock_log.exception.assert_called_with('Received a malformed message, "%r", from the' ' backend queue, dropping message!', None)
def test_handle_message_error(self, mock_log): """Assert a general exception while handling a message is logged.""" delivery_service = service.DeliveryService() message = { 'context': 'sse', 'recipient': {}, 'fedmsg': {}, 'formatted_message': '' } delivery_service = service.DeliveryService() mock_backend = Mock() mock_backend.deliver.side_effect = Exception delivery_service.backends = {'sse': mock_backend} delivery_service.handle_message(message) mock_log.exception.assert_called_with( 'The "%s" backend raised an unexpected exception while trying to ' 'deliver a notification to recipient "%r"', 'sse', {})
def test_start_service(self, mock_reactor): """Assert startService starts the consumer.""" test_config = { 'fmn.backends.debug': True, } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): delivery_service.startService() mock_reactor.callInThread.assert_called_with(delivery_service.consumer.run)
def test_invalid_backends(self, mock_reactor): """Assert an invalid backend in the configuration raises a ValueError.""" test_config = { 'fmn.backends.debug': True, 'fmn.confirmation_frequency': 15, 'fmn.backends': ['carrier bird'], } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): self.assertRaises(ValueError, delivery_service.startService)
def test_handle_batch(self, mock_log): delivery_service = service.DeliveryService() message = { 'context': 'sse', 'recipient': {}, 'fedmsg': [{'msg_id': 1}, {'msg_id': 2}], 'formatted_message': '' } test_config = { 'fmn.backends.debug': True, 'fmn.backends': ['sse'], } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): delivery_service.startService() delivery_service.handle_message(message) mock_log.info.assert_called_with( 'Successfully delivered a batch of %r messages to %r via %r', 2, 'UNKOWN_USER', 'sse')
def test_handle_message(self, mock_log): """Assert well-formed messages are handled.""" delivery_service = service.DeliveryService() message = { 'context': 'sse', 'recipient': {}, 'fedmsg': {}, 'formatted_message': '' } test_config = { 'fmn.backends.debug': True, 'fmn.backends': ['sse'], } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): delivery_service.startService() delivery_service.handle_message(message) mock_log.info.assert_called_with( 'Successfully delivered message %s to %s via %s', 'UNKNOWN_ID', 'UNKOWN_USER', 'sse')
def test_stop_service(self): """Assert stopService forwards to the consumer.""" test_config = { 'fmn.backends.debug': True, 'fmn.backends': ['sse'], } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): delivery_service.startService() self.assertFalse(delivery_service.consumer.should_stop) delivery_service.stopService() self.assertTrue(delivery_service.consumer.should_stop)
def test_handle_message_missing_backend(self, mock_log): """Assert when the backend doesn't exist for the message, an error is logged.""" delivery_service = service.DeliveryService() message = { 'context': 'carrier bird', 'recipient': {}, 'fedmsg': {}, 'formatted_message': '' } test_config = { 'fmn.backends.debug': True, 'fmn.confirmation_frequency': 15, 'fmn.backends': ['sse'], } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): delivery_service.startService() delivery_service.handle_message(message) mock_log.error.assert_called_with( 'Delivery request to the "%s" backend failed because there is no ' 'backend loaded with that name', 'carrier bird')
def test_prune_backends(self, mock_reactor): """Assert backends are pruned to match the configured backends.""" test_config = { 'fmn.backends.debug': True, 'fmn.confirmation_frequency': 15, 'fmn.backends': ['sse'], } delivery_service = service.DeliveryService() with patch.dict(service.config.app_conf, test_config): delivery_service.startService() self.assertEqual(1, len(delivery_service.backends)) self.assertTrue('sse' in delivery_service.backends)