コード例 #1
0
ファイル: util.py プロジェクト: carlbordum/os2mo
    def create_app(self, overrides=None):
        os.makedirs(BUILD_DIR, exist_ok=True)

        return app.create_app({
            'DEBUG': False,
            'TESTING': True,
            'LIVESERVER_PORT': 0,
            'PRESERVE_CONTEXT_ON_EXCEPTION': False,
            'SECRET_KEY': 'secret',
            **(overrides or {})
        })
コード例 #2
0
ファイル: util.py プロジェクト: jgj-magenta-aps/os2mo
    def create_app(self, overrides=None):
        session_dir = tempfile.mkdtemp(prefix='session', dir=BUILD_DIR)

        self.addCleanup(shutil.rmtree, session_dir)

        return app.create_app({
            'DEBUG': False,
            'TESTING': True,
            'LIVESERVER_PORT': 0,
            'PRESERVE_CONTEXT_ON_EXCEPTION': False,
            'SESSION_TYPE': 'filesystem',
            'SESSION_FILE_DIR': session_dir,
            **(overrides or {})
        })
コード例 #3
0
ファイル: util.py プロジェクト: syre/os2mo
    def create_app(self, overrides=None):
        os.makedirs(BUILD_DIR, exist_ok=True)

        # make sure the configured organisation is always reset
        # every before test
        service.org.ConfiguredOrganisation.valid = False

        return app.create_app({
            'ENV': 'testing',
            'DUMMY_MODE': True,
            'DEBUG': False,
            'TESTING': True,
            'LIVESERVER_PORT': 0,
            'PRESERVE_CONTEXT_ON_EXCEPTION': False,
            'SECRET_KEY': 'secret',
            **(overrides or {})
        })
コード例 #4
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()
コード例 #5
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