Exemplo n.º 1
0
def test_modifying_db_config_by_admin_with_invalid_password_must_fail(
        flask_app_client, admin_user, db):
    # pylint: disable=invalid-name
    try:
        utils.get_and_check_houston_configs(
        )  # Ensure an empty database of existing configs

        new_env = 'testing-with-db'

        with flask_app_client.login(admin_user,
                                    auth_scopes=('config.houston:write', )):
            data = [
                test_utils.patch_test_op('invalid_password'),
                test_utils.patch_add_op(new_env, path='ENV'),
            ]
            response = _patch_request(flask_app_client, data)

            assert response.status_code == 403
            assert response.content_type == 'application/json'
            assert isinstance(response.json, dict)
            assert set(response.json.keys()) >= {'status', 'message'}

            utils.get_and_check_houston_configs(
            )  # Ensure an empty database of existing configs
    except Exception as ex:
        raise ex
    finally:
        utils.delete_all_houston_configs(db)
Exemplo n.º 2
0
def test_modifying_db_config_by_admin(flask_app_client, admin_user, db):
    # pylint: disable=invalid-name
    try:
        utils.get_and_check_houston_configs(
        )  # Ensure an empty database of existing configs

        new_env = 'testing-with-db'

        with flask_app_client.login(admin_user,
                                    auth_scopes=('config.houston:write', )):
            data = [
                test_utils.patch_test_op(admin_user.password_secret),
                test_utils.patch_add_op(new_env, path='ENV'),
            ]
            response = _patch_request(flask_app_client, data)

            assert response.json
            utils.get_and_check_houston_configs(key='ENV', value=new_env)

            data = [
                test_utils.patch_test_op(admin_user.password_secret),
                test_utils.patch_remove_op(path='ENV'),
            ]
            response = _patch_request(flask_app_client, data)

            assert response.json
            utils.get_and_check_houston_configs()
    except Exception as ex:
        raise ex
    finally:
        utils.delete_all_houston_configs(db)
Exemplo n.º 3
0
def test_forget_db_config(flask_app, db):
    # Ensure this config is empty and store the current ENV value
    current_env = flask_app.config['ENV']

    try:
        utils.get_and_check_houston_configs(
        )  # Ensure an empty database of existing configs

        new_env = 'testing-with-db'

        # Update with database
        with flask_app.config.db(flask_app):
            utils.check_inside_db_context(flask_app, db_app=flask_app)
            flask_app.config['ENV'] = new_env

        # Check that the value has updated in the Flask config
        utils.check_outside_db_context(flask_app)
        assert flask_app.config['ENV'] == new_env

        # Check that the value has updated in the database
        utils.get_and_check_houston_configs(key='ENV', value=new_env)

        # Delete (by forgetting) the database value override for this configuration
        flask_app.config.forget('ENV')
    finally:
        # Ensure that the value has been deleted from the database
        flask_app.config['ENV'] = current_env
        utils.delete_all_houston_configs(db)
Exemplo n.º 4
0
def test_modifying_db_config_by_regular_user(flask_app_client, regular_user,
                                             db):
    try:
        utils.get_and_check_houston_configs(
        )  # Ensure an empty database of existing configs

        with flask_app_client.login(regular_user,
                                    auth_scopes=('config.houston:write', )):
            data = [
                test_utils.patch_test_op(regular_user.password_secret),
                test_utils.patch_add_op('testing-with-db', path='ENV'),
            ]
            response = _patch_request(flask_app_client, data)

            assert response.status_code == 403
            assert response.content_type == 'application/json'
            assert isinstance(response.json, dict)
            assert set(response.json.keys()) >= {'status', 'message'}

            utils.get_and_check_houston_configs(
            )  # Ensure an empty database of existing configs
    except Exception as ex:
        raise ex
    finally:
        utils.delete_all_houston_configs(db)
Exemplo n.º 5
0
def test_set_db_config(flask_app, db):
    from app.extensions.config import HoustonFlaskConfig

    current_env = flask_app.config['ENV']

    try:
        utils.get_and_check_houston_configs(
        )  # Ensure an empty database of existing configs

        # Check that the context is setup correctly on launch
        assert isinstance(flask_app.config, HoustonFlaskConfig)
        utils.check_outside_db_context(flask_app)

        # First, check (current_app) environment is setup and torn down correctly by context
        with flask_app.config.db():
            utils.check_inside_db_context(flask_app)

        # Check that the context properly exited
        utils.check_outside_db_context(flask_app)

        # Second, check if a specific app environment works correclt
        with flask_app.config.db(flask_app):
            utils.check_inside_db_context(flask_app, db_app=flask_app)

        # Check that the context properly exited
        utils.check_outside_db_context(flask_app)

        new_env = 'testing-no-db'

        # Update config without a database context
        flask_app.config['ENV'] = new_env
        assert flask_app.config['ENV'] == new_env

        # Ensure that there are still no existing configs in the database
        utils.get_and_check_houston_configs()

        new_env = 'testing-with-db'

        # Update with database
        with flask_app.config.db(flask_app):
            utils.check_inside_db_context(flask_app, db_app=flask_app)
            flask_app.config['ENV'] = new_env

        # Check that the value has updated in the Flask config
        utils.check_outside_db_context(flask_app)
        assert flask_app.config['ENV'] == new_env

        # Check that the value has updated in the database
        utils.get_and_check_houston_configs(key='ENV', value=new_env)
    finally:
        flask_app.config['ENV'] = current_env
        utils.delete_all_houston_configs(db)