def test_tags_count_pagination(mq_create_four_specific_hosts, api_get, subtests): """ simple test to check pagination works for /tags """ created_hosts = mq_create_four_specific_hosts expected_responses_1_per_page = [{ 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") # 1 per page test api_tags_count_pagination_test(api_get, subtests, url, len(created_hosts), 1, expected_responses_1_per_page) expected_responses_2_per_page = [ { created_hosts[0].id: len(created_hosts[0].tags), created_hosts[1].id: len(created_hosts[1].tags) }, { created_hosts[2].id: len(created_hosts[2].tags), created_hosts[3].id: len(created_hosts[3].tags) }, ] # 2 per page test api_tags_count_pagination_test(api_get, subtests, url, len(created_hosts), 2, expected_responses_2_per_page)
def test_tags_count_doesnt_use_staleness_parameter(mq_create_hosts_in_all_states, api_get): created_hosts = mq_create_hosts_in_all_states url = build_tags_count_url(host_list_or_id=created_hosts) response_status, response_data = api_get(url, query_parameters={"staleness": "fresh"}) assert response_status == 400
def test_tags_count_default_ignores_culled(mq_create_hosts_in_all_states, api_get): created_hosts = mq_create_hosts_in_all_states url = build_tags_count_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 tuple(response_data["results"].keys())
def test_get_tags_count_of_hosts_that_doesnt_exist(mq_create_four_specific_hosts, api_get): """ send a request for some hosts that don't exist """ host_id = "fa28ec9b-5555-4b96-9b72-96129e0c3336" url = build_tags_count_url(host_list_or_id=host_id) response_status, response_data = api_get(url) assert response_status == 200 assert {} == response_data["results"]
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_get_tags_count_from_host_with_tag_with_no_value(mq_create_four_specific_hosts, api_get): """ host 0 has 4 tags, one of which has no value """ created_hosts = mq_create_four_specific_hosts host_with_valueless_tag = created_hosts[0] url = build_tags_count_url(host_list_or_id=host_with_valueless_tag.id) response_status, response_data = api_get(url) assert response_status == 200 assert {host_with_valueless_tag.id: 4} == response_data["results"]
def test_get_tags_count_from_host_with_no_tags(mq_create_four_specific_hosts, api_get): """ send a request for a host with no tags """ created_hosts = mq_create_four_specific_hosts host_with_no_tags = created_hosts[3] url = build_tags_count_url(host_list_or_id=host_with_no_tags.id) response_status, response_data = api_get(url) assert response_status == 200 assert {host_with_no_tags.id: 0} == response_data["results"]
def test_get_tags_count_from_host_with_null_tags(tags, mq_create_four_specific_hosts, api_get): # FIXME: Remove this test after migration to NOT NULL. created_hosts = mq_create_four_specific_hosts host_id = created_hosts[0].id update_host_in_db(host_id, tags=tags) url = build_tags_count_url(host_list_or_id=host_id) response_status, response_data = api_get(url) assert response_status == 200 assert {host_id: 0} == response_data["results"]
def test_get_host_tag_count_RBAC_denied(mq_create_four_specific_hosts, mocker, api_get, subtests, enable_rbac): get_rbac_permissions_mock = mocker.patch("lib.middleware.get_rbac_permissions") find_non_culled_hosts_mock = mocker.patch("lib.host_repository.find_non_culled_hosts", wraps=find_non_culled_hosts) created_hosts = mq_create_four_specific_hosts for response_file in READ_PROHIBITED_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 == 403 find_non_culled_hosts_mock.assert_not_called()
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))