示例#1
0
def test_schema_unchanged_after_up_then_downgrade(alembic_config, tmpdir,
                                                  migration):
    migrations = list_migrations(alembic_config, migration)

    if len(migrations) > 1:
        target = migrations[-2]
        upgrade(alembic_config, target)
    else:
        # The first migration is the degenerate case where we don't need to
        # get the database to some base state.
        pass

    session = sessionmaker(make_engine(str(tmpdir.mkdir('original'))))()
    original_schema = get_schema(session)

    upgrade(alembic_config, '+1')
    downgrade(alembic_config, '-1')

    session = sessionmaker(make_engine(str(tmpdir.mkdir('reverted'))))()
    reverted_schema = get_schema(session)

    # The initial migration is a degenerate case because it creates the table
    # 'alembic_version', but rolling back the migration doesn't clear it.
    if len(migrations) == 1:
        reverted_schema = {
            k: v
            for k, v in reverted_schema.items() if k[2] != 'alembic_version'
        }

    assert_schemas_equal(reverted_schema, original_schema)
def session(homedir):
    engine = make_engine(homedir)
    Base.metadata.create_all(bind=engine, checkfirst=False)
    Session = sessionmaker(bind=engine)
    session = Session()
    yield session
    session.close()
    def __init__(self, api, home, is_qubes):
        super().__init__()

        engine = make_engine(home)
        Session = sessionmaker(bind=engine)
        self.session = Session()  # Reference to the SqlAlchemy session.
        self.api = api
        self.home = home
        self.is_qubes = is_qubes
        self.gpg = GpgHelper(home, is_qubes)
示例#4
0
    def __init__(self, api: API, home: str, is_qubes: bool) -> None:
        super().__init__()

        engine = make_engine(home)
        current_session = sessionmaker(bind=engine)
        self.session = current_session()  # type: Session
        self.api = api
        self.home = home
        self.is_qubes = is_qubes
        self.gpg = GpgHelper(home, is_qubes)
示例#5
0
def test_alembic_head_matches_db_models(tmpdir):
    '''
    This test is to make sure that our database models in `db.py` are always in sync with the schema
    generated by `alembic upgrade head`.
    '''
    models_homedir = str(tmpdir.mkdir('models'))
    subprocess.check_call(
        ['sqlite3',
         os.path.join(models_homedir, 'svs.sqlite'), '.databases'])
    engine = make_engine(models_homedir)
    Base.metadata.create_all(bind=engine, checkfirst=False)
    assert Base.metadata.naming_convention == convention
    session = sessionmaker(engine)()
    models_schema = get_schema(session)
    Base.metadata.drop_all(bind=engine)
    session.close()
    engine.dispose()

    alembic_homedir = str(tmpdir.mkdir('alembic'))
    subprocess.check_call(
        ['sqlite3',
         os.path.join(alembic_homedir, 'svs.sqlite'), '.databases'])
    engine = make_engine(alembic_homedir)
    session = sessionmaker(engine)()
    alembic_config = conftest._alembic_config(alembic_homedir)
    upgrade(alembic_config, 'head')
    alembic_schema = get_schema(session)
    Base.metadata.drop_all(bind=engine)
    session.close()
    engine.dispose()

    # The initial migration creates the table 'alembic_version', but this is
    # not present in the schema created by `Base.metadata.create_all()`.
    alembic_schema = {
        k: v
        for k, v in alembic_schema.items() if k[2] != 'alembic_version'
    }

    assert_schemas_equal(alembic_schema, models_schema)
示例#6
0
    def __init__(self, sdc_home: str, is_qubes: bool) -> None:
        '''
        :param sdc_home: Home directory for the SecureDrop client
        :param is_qubes: Whether the client is running in Qubes or not
        '''
        safe_mkdir(os.path.join(sdc_home), "gpg")
        self.sdc_home = sdc_home
        self.is_qubes = is_qubes
        engine = make_engine(sdc_home)
        Session = sessionmaker(bind=engine)
        self.session = Session()

        config = Config.from_home_dir(self.sdc_home)
        self.journalist_key_fingerprint = config.journalist_key_fingerprint
示例#7
0
def start_app(args, qt_args) -> None:
    """
    Create all the top-level assets for the application, set things up and
    run the application. Specific tasks include:

    - set up locale and language.
    - set up logging.
    - create an application object.
    - create a window for the app.
    - create an API connection to the SecureDrop proxy.
    - create a SqlAlchemy session to local storage.
    - configure the client (logic) object.
    - ensure the application is setup in the default safe starting state.
    """
    configure_locale_and_language()
    init(args.sdc_home)
    configure_logging(args.sdc_home)
    logging.info('Starting SecureDrop Client {}'.format(__version__))

    app = QApplication(qt_args)
    app.setApplicationName('SecureDrop Client')
    app.setDesktopFileName('org.freedomofthepress.securedrop.client')
    app.setApplicationVersion(__version__)
    app.setAttribute(Qt.AA_UseHighDpiPixmaps)

    prevent_second_instance(app, args.sdc_home)

    gui = Window()
    app.setWindowIcon(load_icon(gui.icon))
    app.setStyleSheet(load_css('sdclient.css'))

    engine = make_engine(args.sdc_home)
    Session = sessionmaker(bind=engine)
    session = Session()

    controller = Controller("http://localhost:8081/", gui, session,
                            args.sdc_home, not args.no_proxy)
    controller.setup()

    configure_signal_handlers(app)
    timer = QTimer()
    timer.start(500)
    timer.timeout.connect(lambda: None)

    sys.exit(app.exec_())
示例#8
0
#!/usr/bin/env python3
import json
import os
import sys
from securedrop_client.config import Config
from securedrop_client.db import Base, make_engine

sdc_home = sys.argv[1]
Base.metadata.create_all(make_engine(sdc_home))

with open(os.path.join(sdc_home, Config.CONFIG_NAME), 'w') as f:
    f.write(
        json.dumps({
            'journalist_key_fingerprint':
            '65A1B5FF195B56353CC63DFFCC40EF1228271441',
        }))
示例#9
0
def session(homedir):
    engine = make_engine(homedir)
    Base.metadata.create_all(bind=engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    return session