Example #1
0
class GenericRpcProxy(DependencyProvider):
    rpc_reply_listener = ReplyListener()

    def __init__(self):
        self.pool = None

    def get_dependency(self, worker_ctx):

        self.pool = pool = GenericServiceProxyPool(worker_ctx,
                                                   self.rpc_reply_listener)
        return pool
Example #2
0
def test_reply_listener(get_rpc_exchange, queue_consumer, mock_container):

    container = mock_container
    container.shared_extensions = {}
    container.config = {}
    container.service_name = "exampleservice"

    exchange = Mock()
    get_rpc_exchange.return_value = exchange

    reply_listener = ReplyListener().bind(container)

    forced_uuid = uuid.uuid4().hex

    with patch('nameko.rpc.uuid', autospec=True) as patched_uuid:
        patched_uuid.uuid4.return_value = forced_uuid

        reply_listener.setup()
        queue_consumer.setup()

        queue = reply_listener.queue
        assert queue.name == "rpc.reply-exampleservice-{}".format(forced_uuid)
        assert queue.exchange == exchange
        assert queue.routing_key == forced_uuid

    queue_consumer.register_provider.assert_called_once_with(reply_listener)

    correlation_id = 1
    reply_event = reply_listener.get_reply_event(correlation_id)

    assert reply_listener._reply_events == {1: reply_event}

    message = Mock()
    message.properties.get.return_value = correlation_id
    reply_listener.handle_message("msg", message)

    queue_consumer.ack_message.assert_called_once_with(message)
    assert reply_event.ready()
    assert reply_event.wait() == "msg"

    assert reply_listener._reply_events == {}

    with patch('nameko.rpc._log', autospec=True) as log:
        reply_listener.handle_message("msg", message)
        assert log.debug.call_args == call('Unknown correlation id: %s',
                                           correlation_id)
Example #3
0
def test_reply_listener(get_rpc_exchange, queue_consumer, mock_container):

    container = mock_container
    container.shared_extensions = {}
    container.config = {}
    container.service_name = "exampleservice"

    exchange = Mock()
    get_rpc_exchange.return_value = exchange

    reply_listener = ReplyListener().bind(container)

    forced_uuid = uuid.uuid4().hex

    with patch('nameko.rpc.uuid', autospec=True) as patched_uuid:
        patched_uuid.uuid4.return_value = forced_uuid

        reply_listener.setup()
        queue_consumer.setup()

        queue = reply_listener.queue
        assert queue.name == "rpc.reply-exampleservice-{}".format(forced_uuid)
        assert queue.exchange == exchange
        assert queue.routing_key == forced_uuid

    queue_consumer.register_provider.assert_called_once_with(reply_listener)

    correlation_id = 1
    reply_event = reply_listener.get_reply_event(correlation_id)

    assert reply_listener._reply_events == {1: reply_event}

    message = Mock()
    message.properties.get.return_value = correlation_id
    reply_listener.handle_message("msg", message)

    queue_consumer.ack_message.assert_called_once_with(message)
    assert reply_event.ready()
    assert reply_event.wait() == "msg"

    assert reply_listener._reply_events == {}

    with patch('nameko.rpc._log', autospec=True) as log:
        reply_listener.handle_message("msg", message)
        assert log.debug.call_args == call(
            'Unknown correlation id: %s', correlation_id)
Example #4
0
class AuthManager(ServiceDependencyProvider):
    rpc_reply_listener = ReplyListener()
    collector = AuthCollector()

    authorization_required = (lambda self, *args, **kwargs: partial(
        Authorization.decorator,
        collector=self.collector
    )(*args, **kwargs))

    login_required = (lambda self, *args, **kwargs: partial(
        Authentication.decorator,
        collector=self.collector
    )(*args, **kwargs))

    login_validator = Authentication.login_validator_handler
    session_store = Authentication.session_store_handler

    def __init__(self, auth_service=None, oauth_validator=None):
        self.auth_service = auth_service
        self.oauth_validator = oauth_validator
        self.oauth2 = oauth2

    def setup(self):
        oauth2.oauth_validator = self.oauth_validator

    def get_dependency(self, worker_ctx):
        if self.auth_service:
            self.collector.proxy = ServiceProxy(worker_ctx, self.auth_service, self.rpc_reply_listener)

        oauth2.service = worker_ctx.service
        services = dict(
            authorization_required=self.authorization_required,
            login_required=self.login_required,
            oauth2=self.oauth2
        )
        return self.make_dependency(**services)