Exemplo n.º 1
0
def base_app(tmpdir, request, sqlalchemy_uri):
    """
    App instance without context

    Creates and returns a bare app. If you wish
    an app with an initialised application context,
    use the `app` fixture instead
    """

    db_fd, db_path = None, None

    try:

        # ------------------------------------------------------------------ --

        # if sqlalchemy_uri is the fallback, establish a temp file

        if sqlalchemy_uri == 'sqlite:///{}':
            db_fd, db_path = tempfile.mkstemp()
            sqlalchemy_uri = sqlalchemy_uri.format(db_path)

        # ------------------------------------------------------------------ --

        # Skip test if incompatible with sqlite

        if sqlalchemy_uri.startswith("sqlite:"):

            if request.node.get_closest_marker('exclude_sqlite'):
                pytest.skip("non sqlite database required for test")

        # ------------------------------------------------------------------ --

        # create the app with common test config

        base_app_config = dict(TESTING=True,
                               SQLALCHEMY_DATABASE_URI=sqlalchemy_uri,
                               LOGFILE_DIR=tmpdir)

        app = create_app('testing', base_app_config)

        yield app

    finally:

        # ------------------------------------------------------------------ --

        # in case of sqlite tempfile fallback, we have to wipe the dishes here

        if db_fd:
            os.close(db_fd)

        if db_path:
            os.unlink(db_path)
Exemplo n.º 2
0
def base_app():
    """
    App instance without context

    Creates and returns a bare app. If you wish
    an app with an initialised application context,
    use the `app` fixture instead
    """

    base_app_config = dict(Base_App_Config)

    # in case of sqlite we use create a temporary file to isolate
    # the database for each test

    db_fd = None
    db_path = None

    db_type = base_app_config['SQLALCHEMY_DATABASE_URI']

    if db_type == "sqlite:///{}":

        db_fd, db_path = tempfile.mkstemp()
        base_app_config['SQLALCHEMY_DATABASE_URI'] = db_type.format(db_path)


    # create the app with common test config
    app = create_app('testing', base_app_config)

    yield app

    # in case of sqlite, close and remove the temporary database

    if db_fd:
        os.close(db_fd)

    if db_path:
        os.unlink(db_path)
Exemplo n.º 3
0
#!/usr/bin/python

import os

from linotp.app import create_app

app = create_app(
    os.getenv('LINOTP_CONFIG') or os.getenv('FLASK_ENV') or 'default')
Exemplo n.º 4
0
def base_app(tmp_path, request, sqlalchemy_uri, key_directory):
    """
    App instance without context

    Creates and returns a bare app. If you wish
    an app with an initialised application context,
    use the `app` fixture instead
    """

    db_fd, db_path = None, None

    try:

        # ------------------------------------------------------------------ --

        # if sqlalchemy_uri is the fallback, establish a temp file

        if sqlalchemy_uri == 'sqlite:///{}':
            db_fd, db_path = tempfile.mkstemp()
            sqlalchemy_uri = sqlalchemy_uri.format(db_path)

        # ------------------------------------------------------------------ --

        # Skip test if incompatible with sqlite

        if sqlalchemy_uri.startswith("sqlite:"):

            if request.node.get_closest_marker('exclude_sqlite'):
                pytest.skip("non sqlite database required for test")

        # ------------------------------------------------------------------ --

        # create the app with common test config

        base_app_config = dict(
            ENV='testing',  # doesn't make a huge difference for us
            TESTING=True,
            DATABASE_URI=sqlalchemy_uri,
            SQLALCHEMY_TRACK_MODIFICATIONS=False,
            ROOT_DIR=tmp_path,
            CACHE_DIR=tmp_path / "cache",
            DATA_DIR=tmp_path / "data",
            LOGFILE_DIR=tmp_path / "logs",
            AUDIT_PUBLIC_KEY_FILE=key_directory / "audit-public.pem",
            AUDIT_PRIVATE_KEY_FILE=key_directory / "audit-private.pem",
            SECRET_FILE=key_directory / "encKey",
            LOGGING_LEVEL="DEBUG",
            LOGGING_CONSOLE_LEVEL="DEBUG",
        )

        config = request.node.get_closest_marker("app_config")
        if config is not None:
            base_app_config.update(config.args[0])

        os.environ["LINOTP_CFG"] = ""

        # Pre-generate the important directories
        for key in ('CACHE_DIR', 'DATA_DIR', 'LOGFILE_DIR'):
            os.makedirs(base_app_config[key], mode=0o770, exist_ok=True)

        # Create secrete key / audit key if necessary.
        with mock.patch('linotp.cli.init_cmd.current_app') as mock_app:
            # The cli commands use current_app.echo to display messages, but this
            # fails here because there no context yet. So we temporary route
            # echo to plain print.
            mock_app.echo = Echo()

            # Fake running `linotp init enc-key`
            secret_file = base_app_config['SECRET_FILE']
            if not os.path.exists(secret_file):
                create_secret_key(filename=secret_file)

            # Fake running `linotp init audit-keys`
            audit_private_key_file = str(
                base_app_config['AUDIT_PRIVATE_KEY_FILE'])
            if not os.path.exists(audit_private_key_file):
                create_audit_keys(
                    audit_private_key_file,
                    str(base_app_config['AUDIT_PUBLIC_KEY_FILE']))

        os.environ["LINOTP_CMD"] = "init-database"
        app = create_app('testing', base_app_config)

        # Fake running `linotp init database`
        with app.app_context():
            init_db_tables(app, drop_data=False, add_defaults=True)

        yield app

    finally:

        # ------------------------------------------------------------------ --

        # in case of sqlite tempfile fallback, we have to wipe the dishes here

        if db_fd:
            os.close(db_fd)

        if db_path:
            os.unlink(db_path)
Exemplo n.º 5
0
def base_app(tmp_path, request, sqlalchemy_uri, key_directory):
    """
    App instance without context

    Creates and returns a bare app. If you wish
    an app with an initialised application context,
    use the `app` fixture instead
    """

    db_fd, db_path = None, None

    try:

        # ------------------------------------------------------------------ --

        # if sqlalchemy_uri is the fallback, establish a temp file

        if sqlalchemy_uri == "sqlite:///{}":
            db_fd, db_path = tempfile.mkstemp()
            sqlalchemy_uri = sqlalchemy_uri.format(db_path)

        # ------------------------------------------------------------------ --

        # Skip test if incompatible with sqlite

        if sqlalchemy_uri.startswith("sqlite:"):

            if request.node.get_closest_marker("exclude_sqlite"):
                pytest.skip("non sqlite database required for test")

        # ------------------------------------------------------------------ --

        # create the app with common test config

        base_app_config = dict(
            ENV="testing",  # doesn't make a huge difference for us
            TESTING=True,
            DATABASE_URI=sqlalchemy_uri,
            AUDIT_DATABASE_URI="SHARED",
            SQLALCHEMY_TRACK_MODIFICATIONS=False,
            ROOT_DIR=tmp_path,
            CACHE_DIR=tmp_path / "cache",
            DATA_DIR=tmp_path / "data",
            LOG_FILE_DIR=tmp_path / "logs",
            AUDIT_PUBLIC_KEY_FILE=key_directory / "audit-public.pem",
            AUDIT_PRIVATE_KEY_FILE=key_directory / "audit-private.pem",
            SECRET_FILE=key_directory / "encKey",
            LOGGING_LEVEL="DEBUG",
            LOGGING_CONSOLE_LEVEL="DEBUG",
            DISABLE_CONTROLLERS="",
        )

        config = request.node.get_closest_marker("app_config")
        if config is not None:
            base_app_config.update(config.args[0])

        os.environ["LINOTP_CFG"] = ""

        # Pre-generate the important directories
        for key in ("CACHE_DIR", "DATA_DIR", "LOG_FILE_DIR"):
            os.makedirs(base_app_config[key], mode=0o770, exist_ok=True)

        # -----------------------------------------------------------------------

        # Fake running `linotp init enc-key`
        secret_file = base_app_config["SECRET_FILE"]
        if not os.path.exists(secret_file):
            sec_key = 3 * "0123456789abcdef" * 4
            create_secret_key(filename=secret_file, data=sec_key)

        # Fake running `linotp init audit-keys`
        audit_private_key_file = str(base_app_config["AUDIT_PRIVATE_KEY_FILE"])
        if not os.path.exists(audit_private_key_file):
            create_audit_keys(
                audit_private_key_file,
                str(base_app_config["AUDIT_PUBLIC_KEY_FILE"]),
            )

        # -----------------------------------------------------------------------

        os.environ["LINOTP_CMD"] = "init-database"
        app = create_app("testing", base_app_config)

        # Fake running `linotp init database`
        with app.app_context():
            init_db_tables(app, drop_data=True, add_defaults=True)

        yield app

    finally:

        # ------------------------------------------------------------------ --

        # in case of sqlite tempfile fallback, we have to wipe the dishes here

        if db_fd:
            os.close(db_fd)

        if db_path:
            os.unlink(db_path)
Exemplo n.º 6
0
        if admin_resolvers:
            admin_resolvers_new += "," + admin_resolvers

        set_config(
            key=admin_resolvers_key,
            value=admin_resolvers_new,
            typ="text",
            description="None",
            update=True,
        )

        self.session.commit()


if __name__ == "__main__":
    app = create_app()

    data = {
        "givenname": "Donald",
        "surname": "Duck",
        "phone": "56789",
        "mobile": "1234567",
        "email": "*****@*****.**",
    }

    with app.app_context():
        la = LocalAdminResolver(app)

        la._remove_all_users()
        la.add_user(username="******", password="******", **data)
        print(la.list_users())
Exemplo n.º 7
0
#!/usr/bin/python

import os

from linotp.app import create_app

app = create_app(
    os.getenv("LINOTP_CONFIG") or os.getenv("FLASK_ENV") or "default")