def test_set_user_permissions(name, permissions, expected_data):
    client = RabbitMQRestClient(request_handler=Mock())

    mock_request = Mock()
    client.perform_request = mock_request

    client.set_user_permissions(name, permissions)

    mock_request.assert_called_once_with(
        'PUT', f'api/permissions/{client.vhost}/{name}', json=expected_data)
def test_get_user__http_error_code__raises_api_error(error_code):
    response = Mock()
    response.status_code = error_code

    request_handler = Mock()
    request_handler.get = Mock(return_value=response)

    client = RabbitMQRestClient(request_handler=request_handler)

    with pytest.raises(APIError):
        client.get_user('name')
def test_create_user(name, password, tags, expected_data):
    client = RabbitMQRestClient(request_handler=Mock())

    mock_request = Mock()
    client.perform_request = mock_request

    client.create_user(name, password, tags)

    mock_request.assert_called_once_with('PUT',
                                         f'api/users/{name}',
                                         json=expected_data)
def test_create_policy__http_error_code__raises_api_error(error_code):
    response = Mock()
    response.status_code = error_code

    request_handler = Mock()
    request_handler.put = Mock(return_value=response)

    client = RabbitMQRestClient(request_handler=request_handler)

    with pytest.raises(APIError):
        client.create_policy('name', 'pattern', 1, "queues")
def test_delete_queue():
    client = RabbitMQRestClient(request_handler=Mock())
    mock_request = Mock()
    client.perform_request = mock_request

    queue_name = 'some queue'

    client.delete_queue(queue_name)

    mock_request.assert_called_once_with(
        'DELETE', f'api/queues/{client.vhost}/{queue_name}')
def test_delete_topic():
    client = RabbitMQRestClient(request_handler=Mock())
    mock_request = Mock()
    client.perform_request = mock_request

    topic_name = 'some topic'

    client.delete_topic(topic_name)

    mock_request.assert_called_once_with(
        'DELETE', f'api/exchanges/{client.vhost}/{topic_name}')
def test_add_user__http_error_code__raises_api_error(error_code):
    response = Mock()
    response.status_code = error_code

    request_handler = Mock()
    request_handler.put = Mock(return_value=response)

    client = RabbitMQRestClient(request_handler=request_handler)

    with pytest.raises(APIError):
        client.add_user(
            'name', 'password',
            RabbitMQUserPermissions(configure=".*", write=".*", read=".*"))
def test_get_queue_bindings__filter_is_applied_on_topic_and_key(
        bindings, topic, key, expected_binding):

    response = Mock()
    response.status_code = 200
    response.content = bindings
    response.json = Mock(return_value=bindings)

    request_handler = Mock()
    request_handler.get = Mock(return_value=response)

    client = RabbitMQRestClient(request_handler=request_handler)

    assert [expected_binding] == client.get_queue_bindings('queue', topic, key)
def test_create_queue_without_max_length():
    durable, auto_delete = False, False
    queue_name = 'some topic'

    data = {"durable": durable, "auto_delete": auto_delete, "arguments": {}}

    client = RabbitMQRestClient(request_handler=Mock())
    mock_request = Mock()
    client.perform_request = mock_request

    client.create_queue(queue_name)

    mock_request.assert_called_once_with(
        'PUT', f'api/queues/{client.vhost}/{queue_name}', json=data)
def _get_rabbitmq_rest_client():
    config = app.config['BROKER']
    return RabbitMQRestClient.create(host=config['host'],
                                     https=config['https'],
                                     username=config['username'],
                                     password=config['password'],
                                     verify=config.get('cert_path') or False)
def test_create_policy(name, pattern, priority, apply_to, definitions,
                       expected_data):
    client = RabbitMQRestClient(request_handler=Mock())

    mock_request = Mock()
    client.perform_request = mock_request

    client.create_policy(name,
                         pattern,
                         priority,
                         apply_to,
                         definitions=definitions)

    mock_request.assert_called_once_with('PUT',
                                         f'api/policies/{client.vhost}/{name}',
                                         json=expected_data)
def test_bind_queue_to_topic(topic_name, expected_topic_name):
    queue_name = 'some queue'
    durable = False
    key = 'routing key'

    data = {"routing_key": key, "arguments": {"durable": durable}}

    client = RabbitMQRestClient(request_handler=Mock())
    mock_request = Mock()
    client.perform_request = mock_request

    client.bind_queue_to_topic(queue_name, key, topic_name, durable)

    mock_request.assert_called_once_with(
        'POST',
        f'api/bindings/{client.vhost}/e/{expected_topic_name}/q/{queue_name}',
        json=data)
Пример #13
0
def _get_rabbitmq_rest_client(config):
    return RabbitMQRestClient.create(
        host=config['host'],
        https=config['https'],
        username=config.get('username'),
        password=config.get('password'),
        verify=config.get('cert_path') or False,
        retry=config['retry']
    )
def test_get_user__user_exists_and_is_returned(user_dict, expected_user):

    response = Mock()
    response.status_code = 200
    response.content = user_dict
    response.json = Mock(return_value=user_dict)

    request_handler = Mock()
    request_handler.get = Mock(return_value=response)

    client = RabbitMQRestClient(request_handler=request_handler)

    user = client.get_user('rabbitmq')

    assert expected_user == user

    called_url = request_handler.get.call_args[0][0]
    assert 'api/users/rabbitmq' == called_url
def test_create_topic():
    durable, auto_delete = False, False
    topic_name = 'some topic'

    data = {
        "type": "topic",
        "durable": durable,
        "auto_delete": auto_delete,
        "internal": False,
        "arguments": {}
    }

    client = RabbitMQRestClient(request_handler=Mock())
    mock_request = Mock()
    client.perform_request = mock_request

    client.create_topic(topic_name)

    mock_request.assert_called_once_with(
        'PUT', f'api/exchanges/{client.vhost}/{topic_name}', json=data)
def test_add_user():
    mock_create_user = Mock()
    mock_set_user_permissions = Mock()

    client = RabbitMQRestClient(request_handler=Mock())
    client.create_user = mock_create_user
    client.set_user_permissions = mock_set_user_permissions

    name = 'username'
    password = '******'
    tags = ['administrator']
    permissions = RabbitMQUserPermissions(configure=".*",
                                          write=".*",
                                          read=".*")

    client.add_user(name, password, permissions, tags)

    mock_create_user.assert_called_once_with(name, password, tags)
    mock_set_user_permissions.assert_called_once_with(name, permissions)
def test_delete_binding__url_contains_properties_key_of_binding(
        topic_name, expected_topic_name):
    properties_key = 'props'
    queue_name = 'queue'
    key = 'routing_key'

    client = RabbitMQRestClient(request_handler=Mock())

    client.get_queue_bindings = Mock(
        return_value=[{
            'source': 'topic1',
            'routing_key': 'key1',
            'properties_key': properties_key
        }])

    mock_request = Mock()
    client.perform_request = mock_request

    client.delete_queue_binding(queue_name, topic_name, key)

    mock_request.assert_called_once_with(
        'DELETE',
        f'api/bindings/%2F/e/{expected_topic_name}/q/{queue_name}/{properties_key}'
    )
def test_vhost():
    client = RabbitMQRestClient(request_handler=Mock())
    assert "%2F" == client.vhost