Beispiel #1
0
def get_app_config(container):
    # type: (AnySettingsContainer, bool) -> Configurator
    """
    Generates application configuration with all required utilities and settings configured.
    """
    import cowbird.constants  # pylint: disable=C0415  # to override specific constants/variables

    # override INI config path if provided with --paste to gunicorn, otherwise use environment variable
    config_settings = get_settings(container)
    config_env = get_constant("COWBIRD_INI_FILE_PATH",
                              config_settings,
                              raise_missing=True)
    config_ini = (container or {}).get("__file__", config_env)
    LOGGER.info("Using initialisation file : [%s]", config_ini)
    if config_ini != config_env:
        cowbird.constants.COWBIRD_INI_FILE_PATH = config_ini
        config_settings["cowbird.ini_file_path"] = config_ini
        LOGGER.info("Environment variable COWBIRD_INI_FILE_PATH [%s] ignored",
                    config_env)
    settings = get_settings_from_config_ini(config_ini)
    settings.update(config_settings)

    print_log("Setting up loggers...", LOGGER)
    log_lvl = get_constant("COWBIRD_LOG_LEVEL",
                           settings,
                           "cowbird.log_level",
                           default_value="INFO",
                           raise_missing=False,
                           raise_not_set=False,
                           print_missing=True)
    # apply proper value in case it was in ini AND env since up until then, only env was check
    # we want to prioritize the ini definition
    cowbird.constants.COWBIRD_LOG_LEVEL = log_lvl
    LOGGER.setLevel(log_lvl)

    print_log("Validate settings that require explicit definitions...", LOGGER)
    validate_required(settings)

    # avoid cornice conflicting with pyramid exception views
    settings["handle_exceptions"] = False

    # create configurator or use one defined as input to preserve previous setup/include/etc.
    config = Configurator() if not isinstance(container,
                                              Configurator) else container
    config.setup_registry(settings=settings)

    # Celery is not required in cli mode
    if not settings.get(CLI_MODE_CFG, False):
        # Must be done before include scan, see configure_celery for more details
        configure_celery(config, config_ini)

    # don't use scan otherwise modules like 'cowbird.adapter' are
    # automatically found and cause import errors on missing packages
    print_log("Including Cowbird modules...", LOGGER)
    config.include("pyramid_mako")
    config.include("cowbird")
    # NOTE: don't call 'config.scan("cowbird")' to avoid parsing issues with colander/cornice,
    #       add them explicitly with 'config.include(<module>)', and then they can do 'config.scan()'

    return config
Beispiel #2
0
def includeme(config):
    # import needs to be here, otherwise ImportError happens during setup.py install (modules not yet installed)
    # pylint: disable=C0415
    from pyramid.events import NewRequest
    from pyramid.tweens import EXCVIEW

    from cowbird.api import generic as ag
    from cowbird.constants import get_constant
    from cowbird.utils import fully_qualified_name, get_logger, log_exception_tween, log_request

    mod_dir = get_constant("COWBIRD_MODULE_DIR", config)
    logger = get_logger(__name__)
    logger.info("Adding COWBIRD_MODULE_DIR='%s' to path.", mod_dir)
    sys.path.insert(0, mod_dir)

    config.add_exception_view(ag.internal_server_error)
    config.add_notfound_view(RemoveSlashNotFoundViewFactory(
        ag.not_found_or_method_not_allowed),
                             append_slash=True)

    tween_position = fully_qualified_name(ag.apply_response_format_tween)
    config.add_tween(tween_position, over=EXCVIEW)
    if get_constant("COWBIRD_LOG_REQUEST", config):
        config.add_subscriber(log_request, NewRequest)
    if get_constant("COWBIRD_LOG_EXCEPTION", config):
        tween_name = fully_qualified_name(log_exception_tween)
        config.add_tween(tween_name, under=tween_position)
        tween_position = tween_name
    config.add_tween(fully_qualified_name(ag.validate_accept_header_tween),
                     under=tween_position)

    config.include("cornice")
    config.include("cornice_swagger")
    config.include("cowbird.api")
    config.include("cowbird.database")
Beispiel #3
0
def test_constant_prioritize_setting_before_env_when_specified():
    settings = {"cowbird.some_existing_var": "FROM_SETTING"}
    override = {"COWBIRD_SOME_EXISTING_VAR": "FROM_ENV"}
    with mock.patch.dict("os.environ", override):
        var = c.get_constant("COWBIRD_SOME_EXISTING_VAR", settings)
        assert var == settings["cowbird.some_existing_var"]
        var = c.get_constant("COWBIRD_SOME_EXISTING_VAR")
        assert var == override["COWBIRD_SOME_EXISTING_VAR"]
Beispiel #4
0
def test_get_constant_raise_missing_when_requested():
    with pytest.raises(LookupError):
        c.get_constant("COWBIRD_DOESNT_EXIST", raise_missing=True)

    try:
        value = c.get_constant("COWBIRD_DOESNT_EXIST", raise_missing=False)
        assert value is None
    except LookupError:
        pytest.fail(msg="Should not have raised although constant is missing.")
Beispiel #5
0
def test_get_constant_with_settings():
    settings = {
        "cowbird.test_some_value": "some-value",
        "COWBIRD_TEST_ANOTHER": "another-value",
    }
    assert c.get_constant("COWBIRD_TEST_ANOTHER",
                          settings) == settings["COWBIRD_TEST_ANOTHER"]
    assert c.get_constant("cowbird.test_some_value",
                          settings) == settings["cowbird.test_some_value"]
Beispiel #6
0
def test_get_constant_with_same_name():
    # TODO: Not so sure of the intention of this test, was using COWBIRD_URL, but obviously COWBIRD_URL is override
    #  from settings and test failed.  COWBIRD_ROOT is in COWBIRD_CONSTANTS and not overridable so now it works.
    test_value = "test-constant"
    c.COWBIRD_ROOT = test_value
    value = c.get_constant("COWBIRD_ROOT")
    assert value == test_value
Beispiel #7
0
def get_config_path():
    settings = get_settings(None, app=True)
    return get_constant("COWBIRD_CONFIG_PATH",
                        settings,
                        default_value=None,
                        raise_missing=False,
                        raise_not_set=False,
                        print_missing=True)
Beispiel #8
0
def get_ssl_verify(container):
    # type: (Optional[AnySettingsContainer]) -> bool
    return asbool(
        get_constant("COWBIRD_SSL_VERIFY",
                     container,
                     default_value=True,
                     raise_missing=False,
                     raise_not_set=False,
                     print_missing=True))
Beispiel #9
0
def get_hostname(test_item):
    # type: (AnyTestItemType) -> str
    """
    Obtains stored hostname in the class implementation.
    """
    app_or_url = get_app_or_url(test_item)
    if isinstance(app_or_url, TestApp):
        app_or_url = get_constant("COWBIRD_URL", app_or_url.app.registry)
    return urlparse(app_or_url).hostname
Beispiel #10
0
def get_config_parser():
    # type: () -> argparse.ArgumentParser
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("-c",
                        "--config",
                        help="INI configuration file to employ.",
                        default=get_constant("COWBIRD_INI_FILE_PATH",
                                             raise_missing=False,
                                             raise_not_set=False))
    return parser
Beispiel #11
0
def api_schema(request):
    # type: (Request) -> JSON
    """
    Return JSON Swagger specifications of Cowbird REST API.
    """
    swagger_base_spec = {
        "host": get_constant("COWBIRD_URL", request.registry),
        "schemes": [request.scheme]
    }
    return s.generate_api_schema(swagger_base_spec)
Beispiel #12
0
def get_homepage(request):  # noqa: W0212
    """
    Cowbird API homepage.
    """
    body = deepcopy(s.InfoAPI)
    body.update({
        "title": s.TitleAPI,
        "name": __meta__.__package__,
        "documentation": get_constant("COWBIRD_URL", request) + s.SwaggerAPI.path
    })
    return ax.valid_http(http_success=HTTPOk, content=body, content_type=CONTENT_TYPE_JSON,
                         detail=s.Homepage_GET_OkResponseSchema.description)
Beispiel #13
0
def test_get_constant_raise_not_set_when_requested():
    settings = {"cowbird.not_set_but_exists": None}
    with pytest.raises(ValueError):
        c.get_constant("COWBIRD_NOT_SET_BUT_EXISTS",
                       settings,
                       raise_not_set=True)
    with pytest.raises(ValueError):
        c.get_constant("cowbird.not_set_but_exists",
                       settings,
                       raise_not_set=True)

    try:
        value = c.get_constant("COWBIRD_NOT_SET_BUT_EXISTS",
                               settings,
                               raise_not_set=False)
        assert value is None
    except LookupError:
        pytest.fail(msg="Should not have raised although constant is not set.")
    try:
        value = c.get_constant("cowbird.not_set_but_exists",
                               settings,
                               raise_not_set=False)
        assert value is None
    except LookupError:
        pytest.fail(msg="Should not have raised although constant is not set.")
Beispiel #14
0
def api_swagger(request):  # noqa: F811
    """
    Swagger UI route to display the Cowbird REST API schemas.
    """
    swagger_versions_dir = os.path.abspath(
        os.path.join(get_constant("COWBIRD_MODULE_DIR"),
                     "ui/swagger/versions"))
    swagger_ui_path = s.SwaggerGenerator.path.lstrip("/")
    return_data = {
        "api_title": s.TitleAPI,
        "api_schema_path": swagger_ui_path,
        "api_schema_versions_dir": swagger_versions_dir
    }
    return return_data
Beispiel #15
0
def get_security(service, method):
    definitions = service.definitions
    args = {}
    for definition in definitions:
        met, _, args = definition
        if met == method:
            break
    # automatically retrieve permission if specified within the view definition
    permission = args.get("permission")
    if permission == NO_PERMISSION_REQUIRED:
        return SecurityEveryoneAPI
    if permission == get_constant("COWBIRD_ADMIN_PERMISSION"):
        return SecurityAdministratorAPI
    # return default admin permission otherwise unless specified form cornice decorator
    return SecurityAdministratorAPI if "security" not in args else args[
        "security"]
Beispiel #16
0
def test_constant_protected_no_override():
    for const_name in c.COWBIRD_CONSTANTS:
        with mock.patch.dict("os.environ", {const_name: "override-value"}):
            const = c.get_constant(const_name)
            assert const != "override-value"
Beispiel #17
0
def test_get_constant_alternative_name():
    settings = {"cowbird.test_some_value": "some-value"}
    assert c.get_constant("COWBIRD_TEST_SOME_VALUE",
                          settings) == settings["cowbird.test_some_value"]