tests/_fixtures.py
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_import_barred_list_user IN ROLE dirbs_core_import_barred_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_barred_tac_list_user IN ROLE dirbs_core_import_barred_tac_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_core_import_subscribers_registration_list_user IN ROLE'
                       ' dirbs_core_import_subscribers_registration_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_device_association_list_user IN ROLE '
                       'dirbs_core_import_device_association_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()
Implementation of fixture to initialise a temporary PostgreSQL instance with a clean DB schema.
Implementation of fixture to initialise a temporary PostgreSQL instance with a clean DB schema.
Implementation of fixture to initialise a temporary PostgreSQL instance with a clean DB schema .
def _postgres_impl(mocked_config):
    postgresql = testing.postgresql.Postgresql(initdb_args='-U postgres -A trust --lc-collate=C.UTF-8 '
                                                           '--lc-ctype=C.UTF-8')
    dsn = postgresql.dsn()
    for setting in ['database', 'host', 'port', 'user', 'password']:
        setattr(mocked_config.db_config, setting, dsn.get(setting, None))
    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'))))
    result = runner.invoke(dirbs_db_cli, ['install'], catch_exceptions=False, 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 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_import_barred_list_user IN ROLE dirbs_core_import_barred_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_barred_tac_list_user IN ROLE dirbs_core_import_barred_tac_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_core_import_subscribers_registration_list_user IN ROLE'
                       ' dirbs_core_import_subscribers_registration_list LOGIN')
        cursor.execute('CREATE ROLE dirbs_import_device_association_list_user IN ROLE '
                       'dirbs_core_import_device_association_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()
def _postgres_impl ( mocked_config ) : postgresql = testing . postgresql . Postgresql ( initdb_args = '-U postgres -A trust --lc-collate=C.UTF-8 ' '--lc-ctype=C.UTF-8' ) dsn = postgresql . dsn ( ) for setting in [ 'database' , 'host' , 'port' , 'user' , 'password' ] : setattr ( mocked_config . db_config , setting , dsn . get ( setting , None ) ) 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' ) ) ) ) result = runner . invoke ( dirbs_db_cli , [ 'install' ] , catch_exceptions = False , 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 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_import_barred_list_user IN ROLE dirbs_core_import_barred_list LOGIN' ) cursor . execute ( 'CREATE ROLE dirbs_import_barred_tac_list_user IN ROLE dirbs_core_import_barred_tac_list LOGIN' ) cursor . execute ( 'CREATE ROLE dirbs_core_import_subscribers_registration_list_user IN ROLE' ' dirbs_core_import_subscribers_registration_list LOGIN' ) cursor . execute ( 'CREATE ROLE dirbs_import_device_association_list_user IN ROLE ' 'dirbs_core_import_device_association_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 ( )
Implementation of fixture to initialise a temporary PostgreSQL instance with a clean DB schema.
Implementation of fixture to initialise a temporary PostgreSQL instance with a clean DB schema .
"""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. # Monkey-patch Postgres config to use temp postgres instance # Run dirbs-db install_roles using db args from the temp postgres instance # Run dirbs-db install using db args from the temp postgres instance # Create the necessary roles for security tests
mocked_config