예제 #1
0
def test_set_flask_metadata():
    """Test metadata creation with legal parameters.
    """
    app = Flask("bob")
    apikit.set_flask_metadata(app, "2.0", "http://example.repo", "BobApp")
    apikit.add_metadata_route(app, "")
    apikit.add_metadata_route(app, "bob")
    apikit.add_metadata_route(app, ["bob"])
    apikit.add_metadata_route(app, ["bob", "chuck"])
def test_add_metadata_route_type_errors():
    """Test set_flask_metadata function for parameters causing TypeErrors.
    """
    app = Flask("bob")
    apikit.set_flask_metadata(app, "2.0", "http://example.repo", "BobApp")
    # Add a not-string
    with pytest.raises(TypeError):
        apikit.add_metadata_route(app, 2.0)
    # Add a list with a not-string
    with pytest.raises(TypeError):
        apikit.add_metadata_route(app, [2.0])
def test_set_flask_metadata():
    """Test set_flask_metadata function for parameters causing TypeErrors.
    """
    # No arguments at all.
    # Obviously the linter is correct here...
    with pytest.raises(TypeError):
        # pylint: disable=no-value-for-parameter
        apikit.set_flask_metadata()
        # First argument is not flask.Flask
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(42, "2.0", "http://example.repo", "BobApp")
    app = Flask("bob")
    # Version is not a string
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(app, 2.0, "http://example.repo", "BobApp")
    # Repository is not a string
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(app, 2.0, ["repo", "man"], "BobApp")
    # Description is not a string
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(app, 2.0, "", "http://example.repo",
                                  {"totally": "bogus"})
    # Auth is not None, the empty string or "none", or a dict
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(app,
                                  "2.0",
                                  "http://example.repo",
                                  "BobApp",
                                  auth=5)
    # Auth is not None, the empty string or "none", or a dict
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(app,
                                  "2.0",
                                  "http://example.repo",
                                  "BobApp",
                                  auth="bob")
    # Api_version is not a string
    with pytest.raises(TypeError):
        apikit.set_flask_metadata(app,
                                  "2.0",
                                  "http://example.repo",
                                  "BobApp",
                                  api_version=5,
                                  auth="")
def test_set_flask_metadata_value_errors():
    """Test set_flask_metadata for input parameters causing ValueErrors.
    """
    app = Flask("bob")
    # Empty version
    with pytest.raises(ValueError):
        apikit.set_flask_metadata(app, "", "http://example.repo", "BobApp")
        # Empty repo
    with pytest.raises(ValueError):
        apikit.set_flask_metadata(app, "2.0", "", "BobApp")
    # Empty description
    with pytest.raises(ValueError):
        apikit.set_flask_metadata(app, "2.0", "http://example.repo", "")
    # No field 'type' in 'auth'
    with pytest.raises(ValueError):
        apikit.set_flask_metadata(app,
                                  "2.0",
                                  "http://example.repo",
                                  "BobApp",
                                  auth={"notype": "notbob"})
    # Field 'auth' has 'type' with bad value
    with pytest.raises(ValueError):
        apikit.set_flask_metadata(app,
                                  "2.0",
                                  "http://example.repo",
                                  "BobApp",
                                  auth={
                                      "type": "frank",
                                      "data": {}
                                  })
    # Field 'auth' has correct 'type' but no 'data'
    with pytest.raises(ValueError):
        apikit.set_flask_metadata(app,
                                  "2.0",
                                  "http://example.repo",
                                  "BobApp",
                                  auth={"type": "basic"})
def test_set_flask_metadata():
    """Test metadata creation with legal parameters.
    """
    # We need a fresh app each time.
    # Minimal invocation
    app = Flask("bob")
    apikit.set_flask_metadata(app, "2.0", "http://example.repo", "BobApp")
    assert app.name == "bob"
    assert app.config["NAME"] == "bob"
    assert app.config["VERSION"] == "2.0"
    assert app.config["REPOSITORY"] == "http://example.repo"
    assert app.config["API_VERSION"] == "1.0"
    assert app.config["DESCRIPTION"] == "BobApp"
    assert isinstance(app.config["AUTH"], dict)
    assert app.config["AUTH"]["type"] == "none"
    # Invocation with empty string for auth
    app = Flask("bob")
    apikit.set_flask_metadata(app,
                              "2.0",
                              "http://example.repo",
                              "BobApp",
                              auth="")
    # Reset name by passing explicit name parameter
    app = Flask("bob")
    apikit.set_flask_metadata(app,
                              "2.0",
                              "http://example.repo",
                              "BobApp",
                              name="tommy")
    assert app.name == "tommy"
    assert app.config["NAME"] == "tommy"
    # Set api_version
    app = Flask("bob")
    apikit.set_flask_metadata(app,
                              "2.0",
                              "http://example.repo",
                              "BobApp",
                              api_version="5",
                              auth="")
    assert app.config["API_VERSION"] == "5"
    # Auth type string 'none'
    app = Flask("bob")
    apikit.set_flask_metadata(app,
                              "2.0",
                              "http://example.repo",
                              "BobApp",
                              auth="none")
    # Auth type 'basic'
    app = Flask("bob")
    apikit.set_flask_metadata(app,
                              "2.0",
                              "http://example.repo",
                              "BobApp",
                              auth={
                                  "type": "basic",
                                  "data": {}
                              })
    assert app.config["AUTH"]["type"] == "basic"
    # Auth type 'bitly-proxy'
    app = Flask("bob")
    apikit.set_flask_metadata(app,
                              "2.0",
                              "http://example.repo",
                              "BobApp",
                              auth={
                                  "type": "bitly-proxy",
                                  "data": {}
                              })
    assert app.config["AUTH"]["type"] == "bitly-proxy"