Пример #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 = make_session_maker(str(tmpdir.mkdir('original')))()
    original_schema = get_schema(session)

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

    session = make_session_maker(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)
Пример #2
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'])

    session_maker = make_session_maker(models_homedir)
    session = session_maker()
    Base.metadata.create_all(bind=session.get_bind(), checkfirst=False)
    assert Base.metadata.naming_convention == convention
    models_schema = get_schema(session)
    Base.metadata.drop_all(bind=session.get_bind())
    session.close()

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

    # 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)
Пример #3
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)

    load_font('Montserrat')
    load_font('Source_Sans_Pro')

    prevent_second_instance(app, args.sdc_home)

    session_maker = make_session_maker(args.sdc_home)

    gui = Window()

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

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

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

    sys.exit(app.exec_())
Пример #4
0
def functional_test_logged_out_context(homedir, reply_status_codes, session,
                                       config):
    """
    Returns a tuple containing a Window instance and a Controller instance that
    have been correctly set up and isolated from any other instances of the
    application to be run in the test suite.
    """

    gui = Window()
    # Configure test keys.
    create_gpg_test_context(homedir)

    # Configure and create the database.
    session_maker = make_session_maker(homedir)

    # Create the controller.
    controller = Controller(HOSTNAME, gui, session_maker, homedir, False,
                            False)
    # Link the gui and controller together.
    gui.controller = controller
    # Et Voila...
    return (gui, controller, homedir)
Пример #5
0
def get_test_context(sdc_home):
    """
    Returns a tuple containing a Window instance and a Controller instance that
    have been correctly set up and isolated from any other instances of the
    application to be run in the test suite.
    """
    # The application's window.
    gui = Window()
    # Create all app assets in a new temp directory and sub-directories.
    safe_mkdir(os.path.join(sdc_home.name, "gpg"))
    safe_mkdir(os.path.join(sdc_home.name, "data"))
    # Configure test keys.
    create_gpg_test_context(sdc_home)
    # Configure and create the database.
    session_maker = make_session_maker(sdc_home.name)
    create_dev_data(sdc_home.name)
    # Create the controller.
    controller = Controller(HOSTNAME, gui, session_maker, sdc_home.name, False,
                            False)
    # Link the gui and controller together.
    gui.controller = controller
    # Et Voila...
    return (gui, controller)
Пример #6
0
#!/usr/bin/env python3
import json
import os
from sqlalchemy.orm.exc import NoResultFound
import sys

from securedrop_client.config import Config
from securedrop_client.db import Base, make_session_maker, ReplySendStatus, ReplySendStatusCodes

sdc_home = sys.argv[1]
session = make_session_maker(sdc_home)()
Base.metadata.create_all(bind=session.get_bind())

with open(os.path.join(sdc_home, Config.CONFIG_NAME), 'w') as f:
    f.write(
        json.dumps({
            'journalist_key_fingerprint':
            '65A1B5FF195B56353CC63DFFCC40EF1228271441',
        }))

for reply_send_status in ReplySendStatusCodes:
    try:
        reply_status = session.query(ReplySendStatus).filter_by(
            name=reply_send_status.value).one()
    except NoResultFound:
        reply_status = ReplySendStatus(reply_send_status.value)
        session.add(reply_status)
        session.commit()
Пример #7
0
 def run(self):
     session = db.make_session_maker(homedir)()
     session.begin(subtransactions=True)
     delete_local_source_by_uuid(session, self.source_uuid, homedir)
     session.commit()
     self.exit()
Пример #8
0
#!/usr/bin/env python3
import json
import os
from sqlalchemy.orm.exc import NoResultFound
import sys

from securedrop_client.config import Config
from securedrop_client import db

sdc_home = sys.argv[1]
session = db.make_session_maker(sdc_home)()
db.Base.metadata.create_all(bind=session.get_bind())

with open(os.path.join(sdc_home, Config.CONFIG_NAME), "w") as f:
    f.write(
        json.dumps({
            "journalist_key_fingerprint":
            "65A1B5FF195B56353CC63DFFCC40EF1228271441"
        }))

for reply_send_status in db.ReplySendStatusCodes:
    try:
        reply_status = (session.query(
            db.ReplySendStatus).filter_by(name=reply_send_status.value).one())
    except NoResultFound:
        reply_status = db.ReplySendStatus(reply_send_status.value)
        session.add(reply_status)
        session.commit()

for download_error in db.DownloadErrorCodes:
    try:
Пример #9
0
def session_maker(homedir):
    return make_session_maker(homedir)