Exemplo n.º 1
0
def test_file_output(call_generate_swagger, db):
    prefix = os.path.join(tempfile.gettempdir(), tempfile.gettempprefix())
    name = "".join(
        random.choice(string.ascii_lowercase + string.digits)
        for _ in range(8))
    yaml_file = prefix + name + ".yaml"
    json_file = prefix + name + ".json"
    other_file = prefix + name + ".txt"

    try:
        # when called with output file nothing should be written to stdout
        assert call_generate_swagger(output_file=yaml_file) == ""
        assert call_generate_swagger(output_file=json_file) == ""
        assert call_generate_swagger(output_file=other_file) == ""

        with pytest.raises(OSError):
            # a second call should fail because file exists
            call_generate_swagger(output_file=yaml_file)

        # a second call with overwrite should still succeed
        assert call_generate_swagger(output_file=json_file,
                                     overwrite=True) == ""

        with open(yaml_file) as f:
            content = f.read()
            # YAML is a superset of JSON - that means we have to check that
            # the file is really YAML and not just JSON parsed by the YAML parser
            with pytest.raises(ValueError):
                json.loads(content)
            output_yaml = yaml_sane_load(content)
        with open(json_file) as f:
            output_json = json.load(f, object_pairs_hook=OrderedDict)
        with open(other_file) as f:
            output_other = json.load(f, object_pairs_hook=OrderedDict)

        assert output_yaml == output_json == output_other
    finally:
        silentremove(yaml_file)
        silentremove(json_file)
        silentremove(other_file)
Exemplo n.º 2
0
def test_non_public(client):
    response = client.get("/private/swagger.yaml")
    swagger = yaml_sane_load(response.content.decode("utf-8"))
    assert len(swagger["paths"]) == 0
Exemplo n.º 3
0
def test_yaml_and_json_match(codec_yaml, codec_json, swagger):
    yaml_schema = yaml_sane_load(codec_yaml.encode(swagger).decode("utf-8"))
    json_schema = json.loads(codec_json.encode(swagger).decode("utf-8"),
                             object_pairs_hook=OrderedDict)
    assert yaml_schema == json_schema
Exemplo n.º 4
0
def test_yaml_codec_roundtrip(codec_yaml, swagger, validate_schema):
    yaml_bytes = codec_yaml.encode(swagger)
    assert b"omap" not in yaml_bytes  # ensure no ugly !!omap is outputted
    assert (b"&id" not in yaml_bytes and b"*id" not in yaml_bytes
            )  # ensure no YAML references are generated
    validate_schema(yaml_sane_load(yaml_bytes.decode("utf-8")))
Exemplo n.º 5
0
def reference_schema():
    with open(os.path.join(os.path.dirname(__file__),
                           "reference.yaml")) as reference:
        return yaml_sane_load(reference)
Exemplo n.º 6
0
def test_non_public(call_generate_swagger, db):
    output = call_generate_swagger(format="yaml",
                                   api_url="http://test.local:8002/",
                                   private=True)
    output_schema = yaml_sane_load(output)
    assert len(output_schema["paths"]) == 0
Exemplo n.º 7
0
def test_reference_schema(call_generate_swagger, db, reference_schema):
    output = call_generate_swagger(format="yaml",
                                   api_url="http://test.local:8002/",
                                   user="******")
    output_schema = yaml_sane_load(output)
    assert output_schema == reference_schema