def test_no_strategies():
    field = {"type": "string", "example": "fieldname"}
    gen = generator(field, None)

    res = next(gen)

    assert isinstance(res, AbsentValue)
Пример #2
0
def _post_gen(query, item, strategy, params=None):
    def recover_content_type_schema(cur_item):
        body_spec = current["requestBody"]["content"]
        for x, y in body_spec.items():
            return x, y["schema"]

    path = query
    if params:
        path = _param_generator(strategy, path, params)
    else:

        def _query():
            while True:
                yield query

        path = _query()
    while True:
        res = {"method": "post", "headers": {}}
        res["path"] = next(path)
        current = item["post"]
        if "requestBody" in current:
            content_type, schema = recover_content_type_schema(current)
            gen = generator(schema, strategy)
            res["headers"]["Content-Type"] = content_type
            body = next(gen)
            res["body"] = body
        yield res
def test_no_strategy_found():
    field = {"type": "strong", "example": "waka"}
    gen = generator(field, open_api_strategies)

    res = next(gen)

    assert isinstance(res, AbsentValue)
Пример #4
0
def _gen_properties(properties):
    res = {}
    for k, v in properties.items():
        if isinstance(v, dict):
            gen = generator(properties[k], rules_strategy)
            res[k] = next(gen)
        else:
            res[k] = v
    return res
Пример #5
0
def _generate_new_query(properties):
    out = {}
    for k, v in properties.items():
        if isinstance(v, dict):
            gen = generator(v, rules_strategy)
            out[k] = str(next(gen))
        else:
            out[k] = str(v)
    return out
def test_integer_multiple_of_1_10_7():
    field = {
        "type": "integer",
        "minimum": 1,
        "maximum": 10,
        "multipleOf": 7,
    }

    gen = generator(field, open_api_strategies)
    res = next(gen)
    assert res == 7
def test_integer_multiple_of_19_20_6():
    field = {
        "type": "integer",
        "minimum": 19,
        "maximum": 20,
        "multipleOf": 6,
    }

    gen = generator(field, open_api_strategies)
    res = next(gen)
    assert isinstance(res, AbsentValue)
def test_integer_multiple_of_15_20_6():
    field = {
        "type": "integer",
        "minimum": 15,
        "maximum": 20,
        "multipleOf": 6,
    }

    gen = generator(field, open_api_strategies)
    res = next(gen)
    assert res == 18
def test_integer_multiple_of_15_25_6():
    field = {
        "type": "integer",
        "minimum": 15,
        "maximum": 25,
        "multipleOf": 6,
    }

    gen = generator(field, open_api_strategies)
    for _ in range(1000):
        res = next(gen)
        assert res in [18, 24]
def test_integer_multiple_of():
    field = {
        "type": "integer",
        "description": "This authorization's ID, used for revoking access.\n",
        "multipleOf": 10,
        "example": 123
    }

    gen = generator(field, open_api_strategies)
    for _ in range(1000):
        res = next(gen)
        assert res % 10 == 0
def test_integer_incorrect_boundaries():
    field = {
        "type": "integer",
        "description": "This authorization's ID, used for revoking access.\n",
        "minimum": 10,
        "maximum": 0,
        "example": 123
    }

    gen = generator(field, open_api_strategies)
    for _ in range(1000):
        res = next(gen)
        assert isinstance(res, AbsentValue)
def test_string_incorrect_boundaries():
    field = {
        "type": "string",
        "minLength": 10,
        "maxLength": 5,
        "example": "fieldname"
    }

    gen = generator(field, open_api_strategies)

    for _ in range(1000):
        res = next(gen)
        assert isinstance(res, AbsentValue)
def test_boolean_field():
    field = {"type": "boolean"}

    gen = generator(field, open_api_strategies)

    res = next(gen)

    assert isinstance(res, bool)

    res2 = [next(gen) for _ in range(1000)]
    assert any(res2)
    not_res2 = [not x for x in res2]
    assert any(not_res2)
def test_string_boundaries():
    field = {
        "type": "string",
        "minLength": 2,
        "maxLength": 10,
        "example": "fieldname"
    }

    gen = generator(field, open_api_strategies)

    for _ in range(1000):
        res = next(gen)
        assert len(res) >= field["minLength"]
        assert len(res) <= field["maxLength"]
Пример #15
0
 def _gen():
     url = path
     for p in parameters:
         if "schema" not in p:
             raise ValueError("cannot generate values without schema!")
         gen = generator(p["schema"], strategy)
         res = next(gen)
         if "in" in p:
             if p["in"] == "path":
                 url = re.sub(r"\{" + p["name"] + r"\}", str(res), url)
             else:
                 raise NotImplementedError("query params")
         else:
             raise NotImplementedError("Nope")
     return url
def test_integer_exclusive_boundaries():
    field = {
        "type": "integer",
        "description": "This authorization's ID, used for revoking access.\n",
        "minimum": 0,
        "maximum": 10,
        "exclusiveMinimum": True,
        "exclusiveMaximum": True,
        "example": 123
    }

    gen = generator(field, open_api_strategies)
    for _ in range(1000):
        res = next(gen)
        assert res > 0
        assert res < 10
def test_array_field():
    field = {"type": "array", "items": {"type": "integer", "example": 123}}

    gen = generator(field, open_api_strategies)

    res = next(gen)

    assert isinstance(res, List)
    assert len(res) <= 10

    res2 = next(gen)

    assert isinstance(res2, List)
    assert len(res2) <= 10

    assert res != res2
Пример #18
0
def test_wordlist_generator():
    field = {"type": "string"}

    words = ["A", "B", "C"]

    def allways_true(v):
        return True

    gen = generator(field, [(allways_true, dict_generator(words))])

    res = list(gen)

    assert isinstance(res, List)
    assert res[0] == "A"
    assert res[1] == "B"
    assert res[2] == "C"
Пример #19
0
def merge_paths(current_path, rule_path, properties):
    parsed = urlparse(current_path)
    parsed_parts = parsed.path.split("/")
    rule_parts = rule_path.split("/")
    for prop, info in properties.items():
        i = _find_index_in_part(rule_parts, "{" + prop + "}")
        if not i:
            continue
        if isinstance(i, dict):
            gen = generator(info, rules_strategy)
            parsed_parts[i] = str(next(gen))
        else:
            parsed_parts[i] = str(info)
    new_path = "/".join(parsed_parts)

    return urlunparse(parsed._replace(path=new_path))
def test_string_field():
    field = {"type": "string", "example": "fieldname"}

    gen = generator(field, open_api_strategies)

    res = next(gen)

    assert isinstance(res, str)
    assert res != ""

    res2 = next(gen)

    assert isinstance(res2, str)
    assert res2 != ""

    assert res != res2
def test_array_boundaries():
    field = {
        "type": "array",
        "maxItems": 9,
        "minItems": 2,
        "items": {
            "type": "integer",
            "example": 123
        }
    }

    gen = generator(field, open_api_strategies)

    for _ in range(1000):
        res = next(gen)
        assert isinstance(res, List)
        assert len(res) <= 9
        assert len(res) >= 2
def test_integer_field():
    field = {
        "type": "integer",
        "description": "This authorization's ID, used for revoking access.\n",
        "example": 123
    }

    gen = generator(field, open_api_strategies)

    res = next(gen)

    assert isinstance(res, int)

    res2 = next(gen)

    assert isinstance(res, int)

    assert res != res2
Пример #23
0
def merge_queries(current_path, properties):
    parsed = urlparse(current_path)
    parsed_query = parsed.query.split("&")
    out = {}
    for part in parsed_query:
        [key, value] = part.split("=")
        if key in properties:
            spec = properties[key]
            if isinstance(spec, dict):
                gen = generator(spec, rules_strategy)
                out[key] = str(next(gen))
            else:
                out[key] = str(spec)
        else:
            out[key] = value
    parts_joined = [f"{k}={v}" for k, v in out.items()]
    new_query = "&".join(parts_joined)

    return urlunparse(parsed._replace(query=new_query))
def test_object_field():
    field = {
        "type":
        "object",
        "description":
        "An object for describing a "
        "single error that occurred "
        "during the processing of a "
        "request.\n",
        "properties": {
            "reason": {
                "type":
                "string",
                "description":
                "What happened to "
                "cause this error. In "
                "most cases, this can "
                "be fixed immediately "
                "by changing the data "
                "you sent in the "
                "request, but in some "
                "cases you will be "
                "instructed to [open "
                "a Support Ticket]("
                "#operation/createTicket) or perform some other action before you can complete the request successfully.\n",
                "example":
                "fieldname must be a "
                "valid value"
            }
        }
    }

    gen = generator(field, open_api_strategies)

    res = next(gen)

    assert isinstance(res, dict)
    assert len(res.keys()) > 0
    assert "reason" in res

    value = res["reason"]

    assert isinstance(value, str)
Пример #25
0
def _put_gen(query, item, strategy, params=None):
    path = query
    if params:
        path = _param_generator(strategy, path, params)
    else:
        def _query():
            while True:
                yield query
        path = _query()
    current = item["put"]
    body = current["requestBody"]["content"]
    content_type, schema = [(x, y["schema"]) for x, y in body.items()][0]
    gen = generator(schema, strategy)
    while True:
        yield {
            "method": "put",
            "path": next(path),
            "headers": {
                "Content-Type": content_type
            },
            "body": next(gen)
        }
def test_array_unique():
    field = {
        "type": "array",
        "uniqueItems": True,
        "minItems": 10,
        "items": {
            "type": "integer",
            "minimum": 1,
            "maximum": 40,
            "example": 123
        }
    }

    gen = generator(field, open_api_strategies)

    for _ in range(1000):
        try:
            res = next(gen)
            assert isinstance(res, List)
            len(res) == len(set(res))
            break
        except:
            pass
def test_no_field():
    gen = generator(None, open_api_strategies)

    res = next(gen)

    assert isinstance(res, AbsentValue)
def test_compund_item():
    compound_item = {
        "type": "object",
        "properties": {
            "errors": {
                "type": "array",
                "items": {
                    "type":
                    "object",
                    "description":
                    "An object for describing a "
                    "single error that occurred "
                    "during the processing of a "
                    "request.\n",
                    "properties": {
                        "reason": {
                            "type":
                            "string",
                            "description":
                            "What happened to "
                            "cause this error. In "
                            "most cases, this can "
                            "be fixed immediately "
                            "by changing the data "
                            "you sent in the "
                            "request, but in some "
                            "cases you will be "
                            "instructed to [open "
                            "a Support Ticket]("
                            "#operation/createTicket) or perform some other action before you can complete the request successfully.\n",
                            "example":
                            "fieldname must be a "
                            "valid value"
                        },
                        "field": {
                            "type":
                            "string",
                            "description":
                            "The field in the "
                            "request that caused "
                            "this error. This may "
                            "be a path, separated "
                            "by periods in the "
                            "case of nested "
                            "fields. In some "
                            "cases this may come "
                            "back as \"null\" if "
                            "the error is not "
                            "specific to any "
                            "single element of "
                            "the request.\n",
                            "example":
                            "fieldname"
                        }
                    }
                }
            }
        }
    }

    gen = generator(compound_item, open_api_strategies)

    res = next(gen)

    assert isinstance(res, dict)
    assert "errors" in res

    error_list = res["errors"]
    assert isinstance(error_list, List)
    assert len(error_list) <= 10

    for item in error_list:
        assert "reason" in item
        assert isinstance(item["reason"], str)
        assert "field" in item
        assert isinstance(item["field"], str)