def app(mocked_config, tmpdir_factory):
    """Method to create an app for testing."""
    # need to import this late as it might have side effects
    from app import app as app_

    # need to save old configurations of the app
    # to restore them later upon tear down
    old_url_map = copy.copy(app_.url_map)
    old_view_functions = copy.copy(app_.view_functions)
    app_.testing = True
    app_.debug = False
    old_config = copy.copy(app_.config)

    # initialize temp database and yield app
    postgresql = Postgresql()
    dsn = postgresql.dsn()
    # monkey patch database configs
    for setting in ['user', 'password', 'port', 'host', 'database']:
        mocked_config['database'][setting] = dsn.get(setting, '')

    # monkey patch temp dirs
    temp_lists = tmpdir_factory.mktemp('lists')
    mocked_config['lists']['path'] = str(temp_lists)
    temp_uploads = tmpdir_factory.mktemp('uploads')
    mocked_config['global']['upload_directory'] = str(temp_uploads)
    app_.config['drs_config'] = mocked_config
    app_.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url()
    app_.config['DRS_UPLOADS'] = str(temp_uploads)
    app_.config['DRS_LISTS'] = str(temp_lists)
    app_.config['CORE_BASE_URL'] = mocked_config['global']['core_api_v2']
    app_.config['DVS_BASE_URL'] = mocked_config['global']['dvs_api_v1']

    yield app_

    # restore old configs after successful session
    app_.url_map = old_url_map
    app_.view_functions = old_view_functions
    app_.config = old_config
    shutil.rmtree(str(temp_lists))
    shutil.rmtree(str(temp_uploads))
    postgresql.stop()