Ejemplo n.º 1
0
def exporter_db_conn():
    """Fixture for db connection."""
    def _handler(postgresql):
        """Init DB with data."""
        conn = psycopg2.connect(**postgresql.dsn())
        cursor = conn.cursor()
        with open("../database/vmaas_user_create_postgresql.sql",
                  "r") as psql_user:
            cursor.execute(psql_user.read())
        with open("../database/vmaas_db_postgresql.sql", "r") as vmaas_db:
            cursor.execute(vmaas_db.read())
        with open("test_data/exporter/exporter_test_data.sql",
                  "r") as test_data:
            cursor.execute(test_data.read())
        cursor.close()
        conn.commit()
        conn.close()

    # Create temporary posgresql server
    # pylint: disable=invalid-name
    Postgresql = testing.postgresql.PostgresqlFactory(
        cache_initialized_db=True, on_initialized=_handler)
    postgresql = Postgresql()

    os.environ["POSTGRESQL_PORT"] = str(postgresql.dsn()["port"])
    DatabaseHandler.close_connection()
    init_db()
    conn = psycopg2.connect(**postgresql.dsn())
    yield conn

    # teardown - close connection, stop postgresql
    conn.close()
    postgresql.stop()
Ejemplo n.º 2
0
def postgresql(postgresql_factory):
    postgresql = postgresql_factory()
    dsn = postgresql.dsn()
    os.environ["PGDATABASE"] = dsn["database"]
    os.environ["PGHOST"] = dsn["host"]
    os.environ["PGPORT"] = str(dsn["port"])
    os.environ["PGUSER"] = dsn["user"]
    yield postgresql
    postgresql.stop()
Ejemplo n.º 3
0
def db_conn():
    """Fixture for db connection."""
    postgresql = create_pg()
    init_db()
    conn = psycopg2.connect(**postgresql.dsn())
    yield conn

    # teardown - close connection, stop postgresql
    conn.close()
    postgresql.stop()
Ejemplo n.º 4
0
def _postgres_impl(mocked_config):
    """Implementation of fixture to initialise a temporary PostgreSQL instance with a clean DB schema."""
    # The system needs to be set to the C locale other than en_US.UTF8 to assume that,
    # in collation order uppercase will come before lowercase.
    postgresql = testing.postgresql.Postgresql(initdb_args='-U postgres -A trust --lc-collate=C.UTF-8 '
                                                           '--lc-ctype=C.UTF-8')
    dsn = postgresql.dsn()

    # Monkey-patch Postgres config to use temp postgres instance
    for setting in ['database', 'host', 'port', 'user', 'password']:
        setattr(mocked_config.db_config, setting, dsn.get(setting, None))

    # Run dirbs-db install_roles using db args from the temp postgres instance
    runner = CliRunner()
    result = runner.invoke(dirbs_db_cli, ['install_roles'], obj={'APP_CONFIG': mocked_config})
    assert result.exit_code == 0

    with create_db_connection(mocked_config.db_config) as conn, conn.cursor() as cursor:
        cursor.execute('CREATE SCHEMA hll;')
        cursor.execute('GRANT USAGE ON SCHEMA hll TO dirbs_core_base;')
        cursor.execute('CREATE EXTENSION hll SCHEMA hll;')
        cursor.execute(sql.SQL('ALTER DATABASE {0} OWNER TO dirbs_core_power_user')
                       .format(sql.Identifier(dsn.get('database'))))

    # Run dirbs-db install using db args from the temp postgres instance
    result = runner.invoke(dirbs_db_cli, ['install'], catch_exceptions=False, obj={'APP_CONFIG': mocked_config})
    assert result.exit_code == 0

    # Create the necessary roles for security tests
    with create_db_connection(mocked_config.db_config) as conn, conn.cursor() as cursor:
        cursor.execute('CREATE ROLE dirbs_import_operator_user IN ROLE dirbs_core_import_operator LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_gsma_user IN ROLE dirbs_core_import_gsma LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_pairing_list_user IN ROLE dirbs_core_import_pairing_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_stolen_list_user IN ROLE dirbs_core_import_stolen_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_registration_list_user '
                       'IN ROLE dirbs_core_import_registration_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_golden_list_user IN ROLE dirbs_core_import_golden_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_classify_user IN ROLE dirbs_core_classify LOGIN')
        cursor.execute('CREATE ROLE dirbs_listgen_user IN ROLE dirbs_core_listgen LOGIN')
        cursor.execute('CREATE ROLE dirbs_report_user IN ROLE dirbs_core_report LOGIN')
        cursor.execute('CREATE ROLE dirbs_api_user IN ROLE dirbs_core_api LOGIN')
        cursor.execute('CREATE ROLE dirbs_catalog_user IN ROLE dirbs_core_catalog LOGIN')
        cursor.execute('CREATE ROLE dirbs_poweruser_login IN ROLE dirbs_core_power_user LOGIN')
        cursor.execute('CREATE ROLE unknown_user LOGIN')

    yield postgresql
    postgresql.stop()
Ejemplo n.º 5
0
    def test_verifies_directories_exist(self):
        postgresql = testing.postgresql.Postgresql()
        db_url = postgresql.url()
        with tempfile.TemporaryDirectory() as module_dir:
            with tempfile.TemporaryDirectory() as stream_dir:
                with tempfile.TemporaryDirectory() as socket_dir:

                    parser = configparser.ConfigParser()
                    parser.read_string(
                        """
                                [Main]
                                ModuleDirectory=%s
                                StreamDirectory=%s
                                SocketDirectory=%s
                                Database=%s
                            """ %
                        (module_dir, stream_dir, socket_dir, db_url[13:]))
                    my_config = load_config.run(custom_values=parser)
                    self.assertEqual(my_config.stream_directory, stream_dir)
                    self.assertEqual(my_config.module_directory, module_dir)
        postgresql.stop()
Ejemplo n.º 6
0
def pg(request):
    postgresql = testing.postgresql.Postgresql()
    yield postgresql
    postgresql.stop()
Ejemplo n.º 7
0
 def fin():
     postgresql.stop()
Ejemplo n.º 8
0
 def fin():
     postgresql.stop()
Ejemplo n.º 9
0
def test_project():
    """Setup a test project, test app, and test app package.
    Load settings and create tables.
    Cleans up when test session ends.
    """
    # Setup test package:
    pip_main(['install', 'tests/example_package/'])
    # Create a test project
    test_dir = 'temp_test'
    test_project = 'test_project'
    if os.path.exists(test_dir):
        shutil.rmtree(test_dir)
    os.mkdir(test_dir)
    from jawaf.management.commands import start_project, start_app
    start_project.Command().handle(name=test_project,
                                   directory=os.path.abspath(test_dir))
    os.mkdir(os.path.join(os.path.abspath(test_dir), 'static'))
    start_app.Command().handle(name='test_app',
                               directory=os.path.abspath(
                                   os.path.join(test_dir, test_project)))
    # Create the code for the test project
    templates.write_template(
        'project_routes',
        os.path.abspath(
            os.path.join(test_dir, test_project, test_project, 'routes.py')))
    templates.write_template(
        'app_routes',
        os.path.abspath(
            os.path.join(test_dir, test_project, 'test_app', 'routes.py')))
    templates.write_template(
        'app_views',
        os.path.abspath(
            os.path.join(test_dir, test_project, 'test_app', 'views.py')))
    templates.write_template(
        'app_tables',
        os.path.abspath(
            os.path.join(test_dir, test_project, 'test_app', 'tables.py')))
    templates.edit_settings(
        os.path.abspath(
            os.path.join(test_dir, test_project, test_project, 'settings.py')),
        targets=[
            [
                "'jawaf.auth',",
                "'jawaf.auth',\n    'test_app',\n    'jawaf_example_app',"
            ],
            ['HOST', "SMTP = {'host':'localhost', 'port':8024}\n\nHOST"],
            ["# STATIC", 'STATIC'],
        ])
    # Setup test postgresql
    postgresql = testing.postgresql.Postgresql()
    create_engine(postgresql.url())
    # Hot Patch
    import smtplibaio
    from jawaf.utils.testing import MockSMTP
    smtplibaio.SMTP = MockSMTP
    smtplibaio.SMTP_SSL = MockSMTP
    # Setup Settings and reload modules to ensure project settings are loaded.
    os.environ.setdefault(
        'JAWAF_SETTINGS_PATH',
        f'{test_dir}/{test_project}/{test_project}/settings.py')
    sys.path.insert(0, os.path.abspath(test_dir))
    from imp import reload
    from jawaf import conf, db, management, security, server, utils
    import jawaf.auth
    import jawaf.auth.users
    import jawaf.auth.utils
    import jawaf.admin
    import jawaf.admin.utils
    reload(conf)
    reload(db)
    reload(management)
    reload(security)
    reload(server)
    reload(utils)
    reload(jawaf.auth.users)
    reload(jawaf.auth.utils)
    reload(jawaf.admin)
    reload(jawaf.admin.utils)
    from jawaf.conf import settings
    p_dsn = postgresql.dsn()
    settings['DATABASES']['default'] = {'engine': 'postgresql', 'password': ''}
    for key in ('database', 'host', 'port', 'user'):
        settings['DATABASES']['default'][key] = p_dsn[key]
    settings['SESSION'] = {'interface': 'memory'}
    # Create tables for auth and the example app
    from jawaf.db import create_tables
    create_tables(['jawaf.auth'], warn=False)
    create_tables(['jawaf.admin'], warn=False)
    create_tables(['jawaf_example_app'], warn=False)
    yield True
    # Clean up
    postgresql.stop()
    shutil.rmtree(test_dir)
    pip_main(['uninstall', 'jawaf_example_app', '-y'])
Ejemplo n.º 10
0
 def cleanup():
     print("cleanup")
     postgresql.stop()
Ejemplo n.º 11
0
def test_project():
    """Setup a test project, test app, and test app package.
    Load settings and create tables.
    Cleans up when test session ends.
    """
    # Setup test package:
    pip.main(['install', 'tests/example_package/'])
    # Create a test project
    test_dir = 'temp_test'
    test_project = 'test_project'
    if os.path.exists(test_dir):
        shutil.rmtree(test_dir)
    os.mkdir(test_dir)
    from jawaf.management.commands import start_project, start_app
    start_project.Command().handle(name=test_project,
                                   directory=os.path.abspath(test_dir))
    start_app.Command().handle(name='test_app',
                               directory=os.path.abspath(
                                   os.path.join(test_dir, test_project)))
    # Setup test code
    templates.write_template(
        'project_routes',
        os.path.abspath(
            os.path.join(test_dir, test_project, test_project, 'routes.py')))
    templates.write_template(
        'app_routes',
        os.path.abspath(
            os.path.join(test_dir, test_project, 'test_app', 'routes.py')))
    templates.write_template(
        'app_views',
        os.path.abspath(
            os.path.join(test_dir, test_project, 'test_app', 'views.py')))
    templates.write_template(
        'app_tables',
        os.path.abspath(
            os.path.join(test_dir, test_project, 'test_app', 'tables.py')))
    templates.edit_settings(
        os.path.abspath(
            os.path.join(test_dir, test_project, test_project,
                         'settings.py')), "'jawaf.auth',",
        "'jawaf.auth',\n    'test_app',\n    'jawaf_example_app',")
    # Setup test postgresql
    postgresql = testing.postgresql.Postgresql()
    engine = create_engine(postgresql.url())
    # Setup Settings and Reload modules to ensure the project settings are loaded.
    os.environ.setdefault(
        'JAWAF_SETTINGS_MODULE',
        '%s.%s.%s.settings' % (test_dir, test_project, test_project))
    sys.path.insert(0, os.path.abspath(test_dir))
    from imp import reload
    from jawaf import conf, db, management, server
    from jawaf.auth import users
    reload(conf)
    reload(db)
    reload(management)
    reload(server)
    reload(users)
    from jawaf.conf import settings
    p_dsn = postgresql.dsn()
    settings['DATABASES']['default']['database'] = p_dsn['database']
    settings['DATABASES']['default']['host'] = p_dsn['host']
    settings['DATABASES']['default']['port'] = p_dsn['port']
    settings['DATABASES']['default']['user'] = p_dsn['user']
    # Create auth tables
    from jawaf.db import create_tables
    create_tables(['jawaf.auth'], warn=False)
    create_tables(['jawaf_example_app'], warn=False)
    yield True
    # Clean up
    postgresql.stop()
    shutil.rmtree(test_dir)
    pip.main(['uninstall', 'jawaf_example_app', '-y'])
Ejemplo n.º 12
0
 def teardown_module():
     """
     Delete the temporary database.
     """
     postgresql.stop()