예제 #1
0
    def _create_conf_data(self, inconsistent=False):

        defaults = {
            'show_roles': 'True',
            'show_user_key': 'False',
            'show_location': 'True'
        }

        p_url = test_support.psql().url()
        p_port = p_url[p_url.rfind(':') + 1:p_url.rfind('/')]

        with psycopg2.connect(p_url) as conn:
            conn.autocommit = True
            with conn.cursor() as curs:
                try:
                    curs.execute(
                        "CREATE USER {} WITH ENCRYPTED PASSWORD '{}'".format(
                            settings.CONF_DB_USER, settings.CONF_DB_PASSWORD))
                except psycopg2.ProgrammingError:
                    curs.execute("DROP DATABASE {};".format(
                        settings.CONF_DB_NAME, ))

                curs.execute("CREATE DATABASE {} OWNER {};".format(
                    settings.CONF_DB_NAME, settings.CONF_DB_USER))
                curs.execute(
                    "GRANT ALL PRIVILEGES ON DATABASE {} TO {};".format(
                        settings.CONF_DB_NAME, settings.CONF_DB_USER))

        with psycopg2.connect(user=settings.CONF_DB_USER,
                              dbname=settings.CONF_DB_NAME,
                              host=settings.CONF_DB_HOST,
                              password=settings.CONF_DB_PASSWORD,
                              port=p_port) as conn:
            conn.autocommit = True
            with conn.cursor() as curs:

                curs.execute("""
                CREATE TABLE orgunit_settings(id serial PRIMARY KEY,
                object UUID, setting varchar(255) NOT NULL,
                value varchar(255) NOT NULL);
                """)

                query = """
                INSERT INTO orgunit_settings (object, setting, value)
                VALUES (NULL, %s, %s);
                """

                for setting, value in defaults.items():
                    curs.execute(query, (setting, value))

                if inconsistent:
                    # Insert once more, making an invalid configuration set
                    for setting, value in defaults.items():
                        curs.execute(query, (setting, value))
        return p_port
예제 #2
0
파일: base.py 프로젝트: carlbordum/os2mo
def full_run(**kwargs):
    '''Runs a development server with a one-off LoRA.

    '''

    from unittest import mock

    import psycopg2

    from oio_rest import app as lora_app
    from oio_rest.utils import test_support
    from oio_rest import db
    import settings as lora_settings

    from mora import app

    def make_server(app, startport=5000):
        '''create a server at the first available port after startport'''
        for port in range(startport, 65536):
            try:
                return (
                    werkzeug.serving.make_server(
                        'localhost',
                        port,
                        app,
                        threaded=True,
                    ),
                    port,
                )
            except OSError as exc:
                pass

        raise exc

    lora_server, lora_port = make_server(lora_app.app, 6000)
    mora_server, mora_port = make_server(app.create_app(), 5000)

    with \
            test_support.psql() as psql, \
            mock.patch('settings.LOG_AMQP_SERVER', None), \
            mock.patch('settings.DB_HOST', psql.dsn()['host'], create=True), \
            mock.patch('settings.DB_PORT', psql.dsn()['port'], create=True), \
            mock.patch('oio_rest.db.pool',
                       psycopg2.pool.PersistentConnectionPool(
                           0, 100,
                           **psql.dsn(database=lora_settings.DATABASE),
                       )), \
            mock.patch('mora.settings.LORA_URL',
                       'http://localhost:{}/'.format(lora_port)):
        test_support._initdb()

        threading.Thread(
            target=lora_server.serve_forever,
            args=(),
            daemon=True,
        ).start()

        print(' * LoRA running at {}'.format(settings.LORA_URL))

        conn = db.get_connection()

        try:
            with \
                    conn.cursor() as curs, \
                    open(os.path.join(backenddir, 'tests', 'fixtures',
                                      'dummy.sql')) as fp:
                curs.execute(fp.read())

        finally:
            db.pool.putconn(conn)

        print(' * Backend running at http://localhost:{}/'.format(mora_port))

        threading.Thread(
            target=mora_server.serve_forever,
            args=(),
            daemon=True,
        ).start()

        with subprocess.Popen(
                get_yarn_cmd('dev'),
                cwd=frontenddir,
                env={
                    **os.environ,
                    'BASE_URL': 'http://localhost:{}'.format(mora_port),
                },
        ) as frontend:
            pass

        db.pool.closeall()
        mora_server.shutdown()
        lora_server.shutdown()
예제 #3
0
def run_with_db(**kwargs):
    from unittest import mock

    import psycopg2

    from oio_rest import app as lora_app
    from oio_rest.utils import test_support
    import settings as lora_settings

    from mora import app
    from mora.importing import spreadsheets

    with \
            test_support.psql() as psql, \
            mock.patch('settings.LOG_AMQP_SERVER', None), \
            mock.patch('settings.DB_HOST', psql.dsn()['host'], create=True), \
            mock.patch('settings.DB_PORT', psql.dsn()['port'], create=True):
        test_support._initdb()

        lora_server = werkzeug.serving.make_server(
            'localhost',
            0,
            lora_app.app,
            threaded=kwargs['threaded'],
        )

        lora_port = lora_server.socket.getsockname()[1]

        lora_thread = threading.Thread(
            target=lora_server.serve_forever,
            args=(),
            daemon=True,
        )

        lora_thread.start()

        with \
                mock.patch('oio_rest.db.pool',
                           psycopg2.pool.PersistentConnectionPool(
                               1, 100,
                               database=lora_settings.DATABASE,
                               user=psql.dsn()['user'],
                               password=psql.dsn().get('password'),
                               host=psql.dsn()['host'],
                               port=psql.dsn()['port'],
                           )), \
                mock.patch('mora.settings.LORA_URL',
                           'http://localhost:{}/'.format(lora_port)):
            print(' * LoRA running at {}'.format(settings.LORA_URL))

            spreadsheets.run(
                target=settings.LORA_URL.rstrip('/'),
                sheets=[
                    os.path.join(
                        backenddir,
                        'tests/fixtures/importing/BALLERUP.csv',
                    ),
                ],
                dry_run=False,
                verbose=False,
                jobs=1,
                failfast=False,
                include=None,
                check=False,
                exact=False,
            )

            werkzeug.serving.run_simple(application=app.app, **kwargs)
예제 #4
0
def make_dummy_instance(idp_url=None):
    from unittest import mock

    import psycopg2

    from oio_rest import views as lora_app
    from oio_rest.utils import test_support
    from oio_rest import settings as lora_settings

    from mora import app

    # ensure that we can import the tests module, regardless of $PWD
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))

    sys.path.pop(0)

    def make_server(app, startport=5000):
        '''create a server at the first available port after startport'''
        last_exc = None

        for port in range(startport, 65536):
            try:
                return (
                    werkzeug.serving.make_server(
                        'localhost',
                        port,
                        app,
                        threaded=True,
                    ),
                    port,
                )
            except OSError as exc:
                last_exc = exc

        if last_exc is not None:
            raise last_exc

    exts = json.loads(
        pkgutil.get_data('mora', 'db_extensions.json').decode(), )

    with test_support.psql() as psql, contextlib.ExitStack() as stack:

        def doublepatch(k, v, *, create=False):
            stack.enter_context(
                mock.patch("oio_rest.settings." + k, v, create=create))

            stack.enter_context(
                mock.patch("mora.settings." + k, v, create=create))

        for k, v in {
                "oio_rest.settings.DB_HOST":
                psql.dsn()["host"],
                "oio_rest.settings.DB_PORT":
                psql.dsn()["port"],
                "oio_rest.settings.LOG_AMQP_SERVER":
                None,
                "oio_rest.db.pool":
                psycopg2.pool.ThreadedConnectionPool(
                    0, 100, **psql.dsn(database=lora_settings.DATABASE)),
        }.items():
            stack.enter_context(mock.patch(k, v, create=True))

        if idp_url:
            doublepatch("SAML_AUTH_ENABLE", True, create=True)
            doublepatch("SQLALCHEMY_DATABASE_URI", psql.url(), create=True)
            doublepatch(
                "SAML_IDP_METADATA_URL",
                "{}/simplesaml/saml2/idp/metadata.php".format(idp_url),
            )

        stack.enter_context(test_support.extend_db_struct(exts))

        mora_server, mora_port = make_server(app.create_app(), 5000)
        lora_server, lora_port = make_server(lora_app.app, 6000)

        stack.enter_context(
            mock.patch(
                "mora.settings.LORA_URL",
                "http://localhost:{}/".format(lora_port),
            ))

        with stack:
            test_support._initdb()

            yield psql, lora_server, mora_server