def test_query_with_no_matching_insights_id(mq_create_three_specific_hosts, api_get, subtests):
    url = build_hosts_url(query=f"?insights_id={generate_uuid()}")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(response_data["results"]) == 0

    api_pagination_test(api_get, subtests, url, expected_total=0)
def test_query_using_non_existent_id(mq_create_three_specific_hosts, api_get, subtests):
    url = build_hosts_url(query=f"?hostname_or_id={generate_uuid()}")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(response_data["results"]) == 0

    api_pagination_test(api_get, subtests, url, expected_total=0)
def test_query_using_id(mq_create_three_specific_hosts, api_get, subtests):
    created_hosts = mq_create_three_specific_hosts

    url = build_hosts_url(query=f"?hostname_or_id={created_hosts[0].id}")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(response_data["results"]) == 1

    api_pagination_test(api_get, subtests, url, expected_total=1)
示例#4
0
def test_get_tag_count_of_multiple_hosts(mq_create_four_specific_hosts, api_get, subtests):
    created_hosts = mq_create_four_specific_hosts
    expected_response = {host.id: len(host.tags) for host in created_hosts}

    url = build_tags_count_url(host_list_or_id=created_hosts, query="?order_by=updated&order_how=ASC")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response) == len(response_data["results"])

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response))
def test_query_using_display_name_substring(mq_create_three_specific_hosts, api_get, subtests):
    created_hosts = mq_create_three_specific_hosts
    expected_host_list = build_expected_host_list(created_hosts)

    host_name_substr = created_hosts[0].display_name[:4]

    url = build_hosts_url(query=f"?display_name={host_name_substr}")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert expected_host_list == response_data["results"]

    api_pagination_test(api_get, subtests, url, expected_total=len(created_hosts))
def test_get_multiple_hosts_by_tag(mq_create_three_specific_hosts, api_get, subtests):
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[0], created_hosts[1]]

    url = build_hosts_url(query="?tags=NS1/key1=val1&order_by=updated&order_how=ASC")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
def test_get_host_by_tag(mq_create_three_specific_hosts, api_get, subtests):
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[0]]

    url = build_hosts_url(query="?tags=SPECIAL/tag=ToFind")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
示例#8
0
def test_get_tags_of_multiple_hosts(mq_create_four_specific_hosts, api_get, subtests):
    """
    Send a request for the tag count of 1 host and check
    that it is the correct number
    """
    created_hosts = mq_create_four_specific_hosts
    expected_response = {host.id: host.tags for host in created_hosts}

    url = build_host_tags_url(host_list_or_id=created_hosts, query="?order_by=updated&order_how=ASC")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response) == len(response_data["results"])

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response))
def test_get_host_with_tag_only_key(mq_create_three_specific_hosts, api_get, subtests):
    """
    Attempt to find host with a tag with no namespace.
    """
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[2]]

    url = build_hosts_url(query="?tags=key5")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
def test_get_host_with_tag_no_value_in_query(mq_create_three_specific_hosts, api_get, subtests):
    """
    Attempt to find host with a tag with a stored value by a value-less query
    """
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[0]]

    url = build_hosts_url(query="?tags=NS1/key2")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
def test_get_host_with_same_tags_different_namespaces(mq_create_three_specific_hosts, api_get, subtests):
    """
    get a host with two tags in the same namespace with different key and same value
    """
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[2]]

    url = build_hosts_url(query="?tags=NS3/key3=val3,NS1/key3=val3")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
def test_get_host_by_subset_of_tags(mq_create_three_specific_hosts, api_get, subtests):
    """
    Get a host using a subset of it's tags
    """
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[1]]

    url = build_hosts_url(query="?tags=NS1/key1=val1,NS3/key3=val3")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
def test_get_host_tag_count_RBAC_allowed(mq_create_four_specific_hosts, mocker, api_get, subtests, enable_rbac):
    get_rbac_permissions_mock = mocker.patch("lib.middleware.get_rbac_permissions")

    created_hosts = mq_create_four_specific_hosts
    expected_response = {host.id: len(host.tags) for host in created_hosts}

    for response_file in READ_ALLOWED_RBAC_RESPONSE_FILES:
        mock_rbac_response = create_mock_rbac_response(response_file)
        with subtests.test():
            get_rbac_permissions_mock.return_value = mock_rbac_response

            url = build_tags_count_url(host_list_or_id=created_hosts, query="?order_by=updated&order_how=ASC")
            response_status, response_data = api_get(url)

            assert response_status == 200
            assert len(expected_response) == len(response_data["results"])

            api_pagination_test(api_get, subtests, url, expected_total=len(expected_response))
def test_get_host_by_display_name_and_tag_backwards(mq_create_three_specific_hosts, api_get, subtests):
    """
    Attempt to get only the host with the specified key and
    the specified display name, but the parameters are backwards
    """
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[0]]

    url = build_hosts_url(query="?display_name=host1&tags=NS1/key1=val1")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))
def test_get_host_by_multiple_tags(mq_create_three_specific_hosts, api_get, subtests):
    """
    Get only the host with all three tags on it and not the other host
    which both have some, but not all of the tags we query for.
    """
    created_hosts = mq_create_three_specific_hosts
    expected_response_list = [created_hosts[1]]

    url = build_hosts_url(query="?tags=NS1/key1=val1,NS2/key2=val2,NS3/key3=val3")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(expected_response_list) == len(response_data["results"])

    for host, result in zip(expected_response_list, response_data["results"]):
        assert host.id == result["id"]

    api_pagination_test(api_get, subtests, url, expected_total=len(expected_response_list))