def test_invalid_info_request_genetaror():
    gen = request_generator(None)
    res = gen(None)
    assert isinstance(res, AbsentValue)

    gen = request_generator({})
    res = gen(None)
    assert isinstance(res, AbsentValue)

    gen = request_generator(["A", "B", "C"])
    res = gen(None)
    assert isinstance(res, AbsentValue)
def test_request_generator_must_return_valid_post_request(openapi3_content):
    query = request_generator(openapi3_content)
    gen = query("/account", method="put")
    res = next(gen)

    assert isinstance(res, dict)

    assert "method" in res
    the_method = res["method"]
    assert the_method == "put"

    assert "path" in res
    the_path = res["path"]
    assert re.match(VALID_PATH, the_path)

    assert "headers" in res
    the_headers = res["headers"]
    assert "Content-Type" in the_headers
    assert the_headers["Content-Type"] == "application/json"

    assert "body" in res
    item = res["body"]
    assert "address_1" in item
    assert "address_2" in item
    assert "balance" in item
    assert "city" in item
    assert "company" in item
    assert "credit_card" in item
    assert "email" in item
    assert "first_name" in item
    assert "last_name" in item
    assert "phone" in item
    assert "state" in item
    assert "tax_id" in item
    assert "zip" in item
def test_request_generator_function_valid_endpoint(openapi3_content):
    query = request_generator(openapi3_content)

    res = query(None)
    assert isinstance(res, AbsentValue)
    res = query("")
    assert isinstance(res, AbsentValue)
def test_all_in(openapi3_content):
    raw_endpoints = search(openapi3_content, "paths")
    query = request_generator(openapi3_content)
    resolver = ref_resolver(openapi3_content)
    endpoints = transform_tree(raw_endpoints, resolver)

    for url, endpoint in endpoints.items():
        try:
            if "get" in endpoint:
                gen = query(url)
                res = next(gen)
                assert res is not None
            if "put" in endpoint:
                gen = query(url, method="put")
                res = next(gen)
                assert res is not None
            if "post" in endpoint:
                gen = query(url, method="post")
                res = next(gen)
                assert res is not None
            if "delete" in endpoint:
                gen = query(url, method="delete")
                res = next(gen)
                assert res is not None
        except Exception as ex:
            assert False, f"uncontrolled exception in {url}, {endpoint}, {ex}"
Пример #5
0
def test_custom_policy(openapi3_content):
    url = "/linode/instances/{linodeId}/disks"
    rules = {
        "/linode/instances/{linodeId}/disks": {
            "pathParams": {
                "linodeId": 500
            },
            "body": {
                "stackscript_data": {
                    "type": "dictionary",
                    "values": ["A"]
                }
            }
        }
    }
    query = request_generator(openapi3_content)
    try:
        gen = query(url, method="post")
        res = next(gen)

        # TODO: must pass this test
        # assert "/linode/instances/500/disks" == res["path"]
    except ValueError as ve:
        pass
        # assert False, f"can't raise value error due new rules, {ve}"
    except Exception as ex:
        pass
Пример #6
0
def test_request_generator_none_if_query_not_found(openapi3_content):
    query = request_generator(openapi3_content)

    try:
        query("/cuck_norris")
    except:
        assert True
    else:
        assert False, "exception expected"
Пример #7
0
async def send_to_proxy_from_definition(running_config: RunningConfig):
    openapi3_content: dict = await openapi3_from_db(running_config.api_id)

    session_user_agent = generate_user_agent()

    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(
            verify_ssl=False)) as session:

        raw_endpoints = search(openapi3_content, "paths")
        query = request_generator(openapi3_content)
        resolver = ref_resolver(openapi3_content)
        endpoints = transform_tree(raw_endpoints, resolver)

        http_scheme, netloc, path, *_ = urlparse(running_config.api_url)

        host, port = split_netloc(netloc, http_scheme)

        for url, endpoint in endpoints.items():

            logger.info(f"Generating data for End Point: {url}")

            try:
                for method in ("get", "put", "post", "delete"):
                    if method in endpoint:
                        gen = query(url, method=method)
                        req: dict = next(gen)
                        break
                else:
                    raise APICheckException("Unknown method in url: ", url)

            except ValueError as ve:
                logger.error(f"cannot generate data: {ve} - {url}")

            url = f"{http_scheme}://{host}:{port}{path}{req['path']}"

            custom_headers = req["headers"]
            custom_headers["user-agent"] = session_user_agent

            fn_params = dict(url=url,
                             headers=custom_headers,
                             proxy=f"http://{running_config.proxy_ip}:"
                             f"{running_config.proxy_port}",
                             skip_auto_headers=("content-type", "user-agent"))

            try:
                fn_params["data"] = req["body"]
            except KeyError:
                fn_params["data"] = None

            fn_method = getattr(session, req["method"])

            async with fn_method(**fn_params) as response:
                resp = await response.text()
Пример #8
0
def test_strange_parameters(openapi3_content):
    url = "/linode/instances/{linodeId}/disks"
    current = search(openapi3_content, url)
    query = request_generator(openapi3_content)
    try:
        if "post" in current:
            gen = query(url, method="post")
            res = next(gen)
            assert res is not None
    except ValueError as ve:
        print("cannot generate data", ve)
    except Exception as ex:
        assert False, f"uncontrolled exception in, {ex}"
Пример #9
0
def test_request_generator_function_valid_endpoint(openapi3_content):
    query = request_generator(openapi3_content)

    try:
        query(None)
    except ValueError as ex:
        assert isinstance(ex, ValueError)
    else:
        assert False, "Value Error expected if None is supplied as query"
    try:
        query("")
    except ValueError as ex:
        assert isinstance(ex, ValueError)
    else:
        assert False, "Value Error expected if empty query"
Пример #10
0
def test_no_struct_schema(openapi3_content):
    current = search(openapi3_content, "/linode/instances")
    query = request_generator(openapi3_content)
    try:
        if "get" in current:
            gen = query("/linode/instances")
            res = next(gen)
            assert res is not None
        if "post" in current:
            gen = query("/linode/instances", method="post")
            res = next(gen)
            assert res is not None
    except ValueError as ve:
        print("cannot generate data", ve)
    except Exception as ex:
        assert False, f"uncontrolled exception in, {ex}"
def test_request_generator_must_return_valid_request(openapi3_content):
    query = request_generator(openapi3_content)

    gen = query("/account")

    res = next(gen)

    assert isinstance(res, dict)

    assert "method" in res, "Response must have a methd"
    the_method = res["method"]
    assert the_method == "get", "the method will be get by default"

    assert "path" in res, "Response must have a path to call"
    the_path = res["path"]
    assert re.match(VALID_PATH, the_path), "must be a valid path"

    assert "headers" in res, "Response must have headers, even empty"
    the_headers = res["headers"]
    assert len(the_headers) == 0, "Headers are empty now"
Пример #12
0
def test_invalid_info_request_genetaror():
    try:
        request_generator(None)
    except ValueError as ex:
        assert isinstance(ex, ValueError)
    else:
        assert False, "Value Error expected if None data supplied"
    try:
        request_generator({})
    except ValueError as ex:
        assert isinstance(ex, ValueError)
    else:
        assert False, "Value Error expected if empty dict supplied"
    try:
        request_generator(["A", "B", "C"])
    except ValueError as ex:
        assert isinstance(ex, ValueError)
    else:
        assert False, "Value Error expected if not dict supplied"
def test_request_generator_none_if_query_not_found(openapi3_content):
    query = request_generator(openapi3_content)
    res = query("/cuck_norris")
    assert isinstance(res, AbsentValue)
def test_request_generator_must_return_a_generator(openapi3_content):
    query = request_generator(openapi3_content)

    res = query("/account")

    assert isinstance(res, Generator)
def test_request_generator_must_return_a_function(openapi3_content):
    res = request_generator(openapi3_content)
    assert isinstance(res, Callable)