Example #1
0
def test_get_host_with_escaped_special_characters(namespace, key, value,
                                                  mq_create_or_update_host,
                                                  api_get):
    tags = [
        {
            "namespace": ";,/?:@&=+$",
            "key": "-_.!~*'()",
            "value": "#"
        },
        {
            "namespace": " \t\n\r\f\v",
            "key": " \t\n\r\f\v",
            "value": " \t\n\r\f\v"
        },
    ]

    host = minimal_host(tags=tags)
    created_host = mq_create_or_update_host(host)

    tags_query = quote(
        f"{quote_everything(namespace)}/{quote_everything(key)}={quote_everything(value)}"
    )
    url = build_hosts_url(query=f"?tags={tags_query}")
    response_status, response_data = api_get(url)

    assert response_status == 200
    assert response_data["count"]
    assert response_data["results"][0]["id"] == created_host.id
def test_query_variables_tags_with_special_characters_unescaped(
        mocker, query_source_xjoin, graphql_tag_query_empty_response, api_get):
    tags_query = quote(";?:@&+$/-_.!~*'()=#")
    url = build_tags_url(query=f"?tags={tags_query}")
    response_status, response_data = api_get(url)

    assert response_status == 200

    graphql_tag_query_empty_response.assert_called_once_with(
        TAGS_QUERY,
        {
            "order_by": "tag",
            "order_how": "ASC",
            "limit": 50,
            "offset": 0,
            "hostFilter": {
                "AND": ({
                    "tag": {
                        "namespace": {
                            "eq": ";?:@&+$"
                        },
                        "key": {
                            "eq": "-_.!~*'()"
                        },
                        "value": {
                            "eq": "#"
                        }
                    }
                }, ),
                "OR":
                mocker.ANY,
            },
        },
    )
def test_query_variables_tags_with_search(field, mocker, query_source_xjoin,
                                          graphql_query_empty_response,
                                          api_get):
    value = quote(generate_uuid())

    url = build_hosts_url(query=f"?{field}={value}&tags=a/b=c")
    response_status, response_data = api_get(url)

    assert response_status == 200

    search_any = mocker.ANY
    tag_filter = {
        "tag": {
            "namespace": {
                "eq": "a"
            },
            "key": {
                "eq": "b"
            },
            "value": {
                "eq": "c"
            }
        }
    }

    graphql_query_empty_response.assert_called_once_with(
        HOST_QUERY,
        {
            "order_by": mocker.ANY,
            "order_how": mocker.ANY,
            "limit": mocker.ANY,
            "offset": mocker.ANY,
            "filter": (search_any, tag_filter, mocker.ANY),
        },
    )
            ),
            "a/b=c,d/e=f",
        ),
        (
            ({
                "namespace": {
                    "eq": "a/a=a"
                },
                "key": {
                    "eq": "b/b=b"
                },
                "value": {
                    "eq": "c/c=c"
                }
            }, ),
            quote("a/a=a") + "/" + quote("b/b=b") + "=" + quote("c/c=c"),
        ),
        (({
            "namespace": {
                "eq": "ɑ"
            },
            "key": {
                "eq": "β"
            },
            "value": {
                "eq": "ɣ"
            }
        }, ), "ɑ/β=ɣ"),
    ),
)
def test_query_variables_tags(tags, query_param, mocker, query_source_xjoin,