def test_query_existent_hosts(mq_create_three_specific_hosts, api_get,
                              subtests):
    created_hosts = mq_create_three_specific_hosts
    host_lists = [created_hosts[0:1], created_hosts[1:3], created_hosts]

    for host_list in host_lists:
        with subtests.test(host_list=host_list):
            url = build_hosts_url(host_list_or_id=host_list)
            api_query_test(api_get, subtests, url, host_list)
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)
Esempio n. 3
0
def test_get_multiple_states(mq_create_hosts_in_all_states, api_get):
    created_hosts = mq_create_hosts_in_all_states

    url = build_hosts_url(query="?staleness=fresh,stale")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert_host_ids_in_response(
        response_data, [created_hosts["fresh"], created_hosts["stale"]])
def test_query_variables_ordering_by_invalid(query_source_xjoin,
                                             graphql_query_empty_response,
                                             api_get):
    url = build_hosts_url(query="?order_by=fqdn")
    response_status, response_data = api_get(url)

    assert response_status == 400

    graphql_query_empty_response.assert_not_called()
Esempio n. 5
0
def test_get_only_stale_warning(mq_create_hosts_in_all_states, api_get):
    created_hosts = mq_create_hosts_in_all_states

    url = build_hosts_url(query="?staleness=stale_warning")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert_host_ids_in_response(response_data,
                                [created_hosts["stale_warning"]])
Esempio n. 6
0
def test_get_host_by_id_doesnt_use_staleness_parameter(
        mq_create_hosts_in_all_states, api_get):
    created_hosts = mq_create_hosts_in_all_states

    url = build_hosts_url(host_list_or_id=created_hosts)
    response_status, response_data = api_get(
        url, query_parameters={"staleness": "fresh"})

    assert response_status == 400
Esempio n. 7
0
def test_patch_on_non_existent_host(api_patch):
    non_existent_id = generate_uuid()

    patch_doc = {"ansible_host": "NEW_ansible_host"}

    url = build_hosts_url(host_list_or_id=non_existent_id)
    response_status, response_data = api_patch(url, patch_doc)

    assert_response_status(response_status, expected_status=404)
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)
Esempio n. 9
0
def test_patch_with_branch_id_parameter(event_producer_mock,
                                        db_create_multiple_hosts, api_patch):
    patch_doc = {"display_name": "branch_id_test"}

    hosts = db_create_multiple_hosts(how_many=5)

    url = build_hosts_url(host_list_or_id=hosts, query="?branch_id=123")
    response_status, response_data = api_patch(url, patch_doc)

    assert_response_status(response_status, expected_status=200)
Esempio n. 10
0
def test_query_using_fqdn_not_subset_match(mocker, api_get):
    mock = mocker.patch("api.host_query_db.canonical_fact_host_query",
                        wraps=canonical_fact_host_query)

    fqdn = "some fqdn"

    url = build_hosts_url(query=f"?fqdn={fqdn}")
    api_get(url)

    mock.assert_called_once_with(ACCOUNT, "fqdn", fqdn)
def test_patch_host_with_RBAC_bypassed_as_system(api_patch, db_create_host,
                                                 event_producer_mock,
                                                 enable_rbac):
    host = db_create_host()

    url = build_hosts_url(host_list_or_id=host.id)
    response_status, response_data = api_patch(
        url, {"display_name": "fred_flintstone"}, identity_type="System")

    assert_response_status(response_status, 200)
def test_patch_on_multiple_hosts_with_some_non_existent(event_producer_mock, db_create_host, api_patch):
    non_existent_id = generate_uuid()
    host = db_create_host()

    patch_doc = {"ansible_host": "NEW_ansible_host"}

    url = build_hosts_url(host_list_or_id=f"{non_existent_id},{host.id}")
    response_status, response_data = api_patch(url, patch_doc)

    assert_response_status(response_status, expected_status=200)
def test_query_using_fqdn_one_result(mq_create_three_specific_hosts, api_get):
    created_hosts = mq_create_three_specific_hosts
    expected_host_list = build_expected_host_list([created_hosts[2]])

    url = build_hosts_url(query=f"?fqdn={created_hosts[2].fqdn}")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(response_data["results"]) == 1
    assert expected_host_list == response_data["results"]
def test_get_no_host_with_different_tags_same_namespace(mq_create_three_specific_hosts, api_get, subtests):
    """
    Don’t get a host with two tags in the same namespace, from which only one match. This is a
    regression test.
    """
    url = build_hosts_url(query="?tags=NS1/key1=val2,NS1/key2=val1")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert len(response_data["results"]) == 0
def test_query_using_display_name(mq_create_three_specific_hosts, api_get):
    created_hosts = mq_create_three_specific_hosts
    expected_host_list = build_expected_host_list([created_hosts[0]])

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

    assert response_status == 200
    assert len(response_data["results"]) == 1
    assert expected_host_list == response_data["results"]
Esempio n. 16
0
def test_get_host_with_invalid_tag_no_key(mq_create_three_specific_hosts,
                                          api_get):
    """
    Attempt to find host with an incomplete tag (no key).
    Expects 400 response.
    """
    url = build_hosts_url(query="?tags=namespace/=Value")
    response_status, response_data = api_get(url)

    assert response_status == 400
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)
def test_query_host_id_with_incorrect_formats(api_get, subtests):
    host_id = "6a2f41a3-c54c-fce8-32d2-0324e1c32e22"

    bad_host_ids = (f" {host_id}", f"{{{host_id}", f"{host_id}-")

    for bad_host_id in bad_host_ids:
        with subtests.test():
            url = build_hosts_url(host_list_or_id=bad_host_id)
            response_status, response_data = api_get(url)
            assert response_status == 400
Esempio n. 19
0
def test_query_using_insights_id_not_subset_match(mocker, api_get):
    mock = mocker.patch("api.host_query_db.canonical_fact_host_query",
                        wraps=canonical_fact_host_query)

    insights_id = "ff13a346-19cb-42ae-9631-44c42927fb92"

    url = build_hosts_url(query=f"?insights_id={insights_id}")
    api_get(url)

    mock.assert_called_once_with(ACCOUNT, "insights_id", insights_id)
Esempio n. 20
0
def test_query_with_matching_insights_id_and_branch_id(
        mq_create_three_specific_hosts, api_get, subtests):
    created_hosts = mq_create_three_specific_hosts
    valid_insights_id = created_hosts[0].insights_id

    url = build_hosts_url(
        query=f"?insights_id={valid_insights_id}&branch_id=123")
    response_status, response_data = api_get(url)

    assert response_status == 200
Esempio n. 21
0
def test_no_event_on_noop(event_producer, db_create_host, db_get_host,
                          api_patch, api_delete_host, mocker):
    mocker.patch.object(event_producer, "write_event")

    host = db_create_host()

    url = build_hosts_url(host_list_or_id=host.id)
    api_patch(url, {})

    assert event_producer.write_event.call_count == 0
def test_query_variables_staleness(staleness, expected, mocker,
                                   culling_datetime_mock, query_source_xjoin,
                                   graphql_query_empty_response, api_get):
    url = build_hosts_url(query=f"?staleness={staleness}")
    response_status, response_data = api_get(url)

    assert response_status == 200

    assert_graph_query_single_call_with_staleness(
        mocker, graphql_query_empty_response, (expected, ))
def test_get_hosts_by_id_default_ignores_culled(mq_create_hosts_in_all_states,
                                                api_get):
    created_hosts = mq_create_hosts_in_all_states

    url = build_hosts_url(host_list_or_id=created_hosts)
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert created_hosts["culled"].id not in [
        host["id"] for host in response_data["results"]
    ]
Esempio n. 24
0
def test_query_multiple_hosts_with_some_non_existent(
        mq_create_three_specific_hosts, api_get, subtests):
    created_hosts = mq_create_three_specific_hosts
    host_list = created_hosts[0:1]

    existent_host_id_list = build_host_id_list_for_url(host_list)
    non_existent_host_id = generate_uuid()

    url = build_hosts_url(
        host_list_or_id=f"{non_existent_host_id},{existent_host_id_list}")
    api_query_test(api_get, subtests, url, host_list)
def test_get_hosts_sap_system_bad_parameter_values(patch_xjoin_post, api_get,
                                                   subtests,
                                                   query_source_xjoin):
    patch_xjoin_post(response={})

    values = ("True", "False", "Garfield")

    for value in values:
        with subtests.test(value=value):
            implicit_url = build_hosts_url(
                query=f"?filter[system_profile][sap_system]={value}")
            eq_url = build_hosts_url(
                query=f"?filter[system_profile][sap_system][eq]={value}")

            implicit_response_status, implicit_response_data = api_get(
                implicit_url)
            eq_response_status, eq_response_data = api_get(eq_url)

            assert_response_status(implicit_response_status, 400)
            assert_response_status(eq_response_status, 400)
def test_get_hosts_with_RBAC_bypassed_as_system(db_create_host, api_get,
                                                enable_rbac):
    host = db_create_host(
        extra_data={"system_profile_facts": {
            "owner_id": generate_uuid()
        }})

    url = build_hosts_url(host_list_or_id=host.id)
    response_status, response_data = api_get(url, identity_type="System")

    assert_response_status(response_status, 200)
def test_invalid_host_id(db_create_host, api_patch, subtests):
    host = db_create_host()

    patch_doc = {"display_name": "branch_id_test"}
    host_id_lists = ["notauuid", f"{host.id},notauuid"]

    for host_id_list in host_id_lists:
        with subtests.test(host_id_list=host_id_list):
            url = build_hosts_url(host_list_or_id=host_id_list)
            response_status, response_data = api_patch(url, patch_doc)
            assert_response_status(response_status, expected_status=400)
def test_update_fields(patch_doc, event_producer_mock, db_create_host, db_get_host, api_patch):
    host = db_create_host()

    url = build_hosts_url(host_list_or_id=host.id)
    response_status, response_data = api_patch(url, patch_doc)

    assert_response_status(response_status, expected_status=200)

    record = db_get_host(host.id)

    for key in patch_doc:
        assert getattr(record, key) == patch_doc[key]
def test_get_host_tag_part_too_long(tag_query, part_name, mq_create_three_specific_hosts, api_get):
    """
    send a request to find hosts with a string tag where the length
    of the namespace excedes the 255 character limit
    """

    url = build_hosts_url(query=f"?tags={tag_query}")
    response_status, response_data = api_get(url)

    assert_error_response(
        response_data, expected_status=400, expected_detail=f"{part_name} is longer than 255 characters"
    )
def test_culled_timestamp(culling_culled_offset_days, inventory_config, mq_create_or_update_host, api_get):
    inventory_config.culling_culled_offset_days = culling_culled_offset_days

    stale_timestamp = now() + timedelta(hours=1)
    host = minimal_host(stale_timestamp=stale_timestamp.isoformat())
    created_host = mq_create_or_update_host(host)

    url = build_hosts_url(created_host.id)
    response_status, response_data = api_get(url)
    assert response_status == 200

    culled_timestamp = stale_timestamp + timedelta(days=culling_culled_offset_days)
    assert culled_timestamp.isoformat() == response_data["results"][0]["culled_timestamp"]