def test_configure_filedepot(self, no_filedepots): from depot.manager import DepotManager from kotti.filedepot import configure_filedepot from kotti import tests tests.TFS1 = Mock(return_value=Mock(marker="TFS1")) tests.TFS2 = Mock(return_value=Mock(marker="TFS2")) settings = { "kotti.depot.0.backend": "kotti.tests.TFS1", "kotti.depot.0.name": "localfs", "kotti.depot.0.location": "/tmp", "kotti.depot.1.backend": "kotti.tests.TFS2", "kotti.depot.1.uri": "mongo://", "kotti.depot.1.name": "mongo", } configure_filedepot(settings) assert DepotManager.get().marker == "TFS1" assert DepotManager.get("localfs").marker == "TFS1" assert DepotManager.get("mongo").marker == "TFS2" tests.TFS1.assert_called_with(location="/tmp") tests.TFS2.assert_called_with(uri="mongo://") del tests.TFS1 del tests.TFS2
def init_depots(app: Flask): """Setup all configured depots""" depot_name = 'avatar' depot_config = default_config(app) DepotManager.configure(depot_name, depot_config)
def setup_app(app_name=__name__, db_uri=None): """ Set up Flask application and database. Args: app_name: Name of the Flask application. db_uri: Database URI for SQLAlchemy to connected to. """ global app, db # Flask application app = Flask(app_name) # Application configuration app.config.update({ "SQLALCHEMY_DATABASE_URI": db_uri, "SQLALCHEMY_TRACK_MODIFICATIONS": False }) # Database object db = SQLAlchemy(app) # Depot DepotManager.configure("default", {"depot.storage_path": DATA_ROOT}) app.wsgi_app = DepotManager.make_middleware(app.wsgi_app, replace_wsgi_filewrapper=True) # Import all related modules import_module("app.models") import_module("app.views")
def test_invalid_mountpoint(self): try: DepotManager.make_middleware(self.wsgi_app, mountpoint='hello') except ValueError as err: assert 'mountpoint must be an absolute path' in str(err) else: assert False, 'Should have raised ValueError'
def configure_filedepot(settings): config = extract_depot_settings('kotti.depot.', settings) for conf in config: name = conf.pop('name') if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix='')
def upgrade(): from depot.manager import DepotManager from depot.fields.upload import UploadedFile from depot.fields.sqlalchemy import UploadedFileField from kotti import DBSession, metadata from kotti.resources import File t = sa.Table('files', metadata) t.c.data.type = sa.LargeBinary() dn = DepotManager.get_default() update = t.update() conn = DBSession.connection() for obj in DBSession.query(File): uploaded_file = UploadedFile({'depot_name': dn, 'files': []}) uploaded_file._thaw() uploaded_file.process_content(obj.data, filename=obj.filename, content_type=obj.mimetype) stored_file = DepotManager.get().get(uploaded_file['file_id']) stmt = update.where(t.c.id == obj.id).values( data=uploaded_file.encode()) res = conn.execute(stmt) assert res.rowcount == 1 stored_file.last_modified = obj.modification_date log.info("Migrated {} bytes for File with pk {} to {}/{}".format( len(obj.data), obj.id, dn, uploaded_file['file_id'])) DBSession.flush() if DBSession.get_bind().name != 'sqlite': # not supported by sqlite op.alter_column('files', 'data', type_=UploadedFileField())
def configure_filedepot(settings: Dict[str, str]) -> None: config = extract_depot_settings("kotti.depot.", settings) for conf in config: name = conf.pop("name") if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix="")
def configure_filedepot(settings: Dict[str, str]) -> None: config = extract_depot_settings("kotti.depot.", settings) for conf in config: name = conf.pop("name") if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix="")
def upgrade(): from depot.manager import DepotManager from depot.fields.upload import UploadedFile from depot.fields.sqlalchemy import UploadedFileField from kotti import DBSession, metadata from kotti.resources import File t = sa.Table('files', metadata) t.c.data.type = sa.LargeBinary() dn = DepotManager.get_default() update = t.update() conn = DBSession.connection() for obj in DBSession.query(File): uploaded_file = UploadedFile({'depot_name': dn, 'files': []}) uploaded_file._thaw() uploaded_file.process_content( obj.data, filename=obj.filename, content_type=obj.mimetype) stored_file = DepotManager.get().get(uploaded_file['file_id']) stmt = update.where( t.c.id == obj.id).values(data=uploaded_file.encode()) res = conn.execute(stmt) assert res.rowcount == 1 stored_file.last_modified = obj.modification_date log.info("Migrated {} bytes for File with pk {} to {}/{}".format( len(obj.data), obj.id, dn, uploaded_file['file_id'])) DBSession.flush() if DBSession.get_bind().name != 'sqlite': # not supported by sqlite op.alter_column('files', 'data', type_=UploadedFileField())
def test_application_separation(app): app.set_application_id('apps/one') ensure_correct_depot(app) transaction.begin() files = FileCollection(app.session()) first_id = files.add('readme.txt', b'README').id transaction.commit() app.set_application_id('apps/two') ensure_correct_depot(app) transaction.begin() files = FileCollection(app.session()) second_id = files.add('readme.txt', b'README').id transaction.commit() assert len(DepotManager.get('apps-one').list()) == 1 assert len(DepotManager.get('apps-two').list()) == 1 client = Client(app) app.set_application_id('apps/one') assert client.get('/storage/{}'.format(first_id))\ .status_code == 200 assert client.get('/storage/{}'.format(second_id), expect_errors=True)\ .status_code == 404 app.set_application_id('apps/two') assert client.get('/storage/{}'.format(first_id), expect_errors=True)\ .status_code == 404 assert client.get('/storage/{}'.format(second_id))\ .status_code == 200
def test_func__user_update_command__ok__nominal_case( self, hapic, session, user_api_factory) -> None: """ Test user password update """ api = user_api_factory.get() user = api.get_one_by_email("*****@*****.**") assert user.email == "*****@*****.**" assert user.validate_password("*****@*****.**") assert not user.validate_password("new_password") session.close() # NOTE GM 2019-07-21: Unset Depot configuration. Done here and not in fixture because # TracimCLI need reseted context when ran. DepotManager._clear() app = TracimCLI() result = app.run([ "user", "update", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH), "-l", "*****@*****.**", "-p", "new_password", "--debug", ]) assert result == 0 api = user_api_factory.get() new_user = api.get_one_by_email("*****@*****.**") assert new_user.email == "*****@*****.**" assert new_user.validate_password("new_password") assert not new_user.validate_password("*****@*****.**")
def test__delete__db__err_no_force_param(self, hapic, session, user_api_factory): """ Test database deletion """ api = user_api_factory.get() user = api.get_one_by_email("*****@*****.**") assert user.email == "*****@*****.**" assert user.validate_password("*****@*****.**") assert not user.validate_password("new_password") session.close() # NOTE GM 2019-07-21: Unset Depot configuration. Done here and not in fixture because # TracimCLI need reseted context when ran. DepotManager._clear() app = TracimCLI() with pytest.raises(ForceArgumentNeeded): app.run([ "db", "delete", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH), "--debug" ]) result = app.run([ "db", "delete", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH) ]) assert result == 1
def test__init__db__ok_nominal_case(self, hapic, session, user_api_factory): """ Test database initialisation """ api = user_api_factory.get() user = api.get_one_by_email("*****@*****.**") assert user.email == "*****@*****.**" assert user.validate_password("*****@*****.**") assert not user.validate_password("new_password") session.close() # NOTE GM 2019-07-21: Unset Depot configuration. Done here and not in fixture because # TracimCLI need reseted context when ran. DepotManager._clear() app = TracimCLI() # delete config to be sure command will work app.run([ "db", "delete", "--force", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH), "--debug", ]) result = app.run([ "db", "init", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH), "--debug" ]) assert result == 0
def test_configure_filedepot(self, no_filedepots): from depot.manager import DepotManager from kotti.filedepot import configure_filedepot from kotti import tests tests.TFS1 = Mock(return_value=Mock(marker="TFS1")) tests.TFS2 = Mock(return_value=Mock(marker="TFS2")) settings = { 'kotti.depot.0.backend': 'kotti.tests.TFS1', 'kotti.depot.0.name': 'localfs', 'kotti.depot.0.location': '/tmp', 'kotti.depot.1.backend': 'kotti.tests.TFS2', 'kotti.depot.1.uri': 'mongo://', 'kotti.depot.1.name': 'mongo', } configure_filedepot(settings) assert DepotManager.get().marker == 'TFS1' assert DepotManager.get('localfs').marker == 'TFS1' assert DepotManager.get('mongo').marker == 'TFS2' tests.TFS1.assert_called_with(location='/tmp') tests.TFS2.assert_called_with(uri='mongo://') del tests.TFS1 del tests.TFS2
def test_func__user_create_command__ok__nominal_case( self, session, user_api_factory) -> None: """ Test User creation """ api = user_api_factory.get() with pytest.raises(UserDoesNotExist): api.get_one_by_email("command_test@user") session.close() # NOTE GM 2019-07-21: Unset Depot configuration. Done here and not in fixture because # TracimCLI needs the context to be reset when ran. DepotManager._clear() app = TracimCLI() result = app.run([ "user", "create", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH), "-e", "command_test@user", "-p", "new_password", "--debug", ]) assert result == 0 api = user_api_factory.get() new_user = api.get_one_by_email("command_test@user") assert new_user.email == "command_test@user" assert new_user.validate_password("new_password") assert new_user.profile.slug == "users"
def upgrade(): sa.orm.events.MapperEvents._clear() # avoids filedepot magic from depot.manager import DepotManager from depot.fields.upload import UploadedFile from depot.fields.sqlalchemy import UploadedFileField from kotti import DBSession, metadata from kotti.resources import File t = sa.Table("files", metadata) t.c.data.type = sa.LargeBinary() dn = DepotManager.get_default() for obj in DBSession.query(File): uploaded_file = UploadedFile({"depot_name": dn, "files": []}) uploaded_file._thaw() uploaded_file.process_content(obj.data, filename=obj.filename, content_type=obj.mimetype) stored_file = DepotManager.get().get(uploaded_file["file_id"]) obj.data = uploaded_file.encode() stored_file.last_modified = obj.modification_date log.info( "Migrated {} bytes for File with pk {} to {}/{}".format(len(obj.data), obj.id, dn, uploaded_file["file_id"]) ) DBSession.flush() if DBSession.get_bind().name != "sqlite": # not supported by sqlite op.alter_column("files", "data", type_=UploadedFileField())
def webdav_testapp(config_uri, config_section) -> TestApp: DepotManager._clear() settings = plaster.get_settings(config_uri, config_section) settings["here"] = os.path.dirname(os.path.abspath(TEST_CONFIG_FILE_PATH)) app_factory = WebdavAppFactory(**settings) app = app_factory.get_wsgi_app() return TestApp(app)
def test_public_url_gets_redirect(self): try: global S3Storage from depot.io.awss3 import S3Storage except ImportError: raise SkipTest('Boto not installed') env = os.environ access_key_id = env.get('AWS_ACCESS_KEY_ID') secret_access_key = env.get('AWS_SECRET_ACCESS_KEY') if access_key_id is None or secret_access_key is None: raise SkipTest('Amazon S3 credentials not available') PID = os.getpid() NODE = str(uuid.uuid1()).rsplit('-', 1)[-1] # Travis runs multiple tests concurrently default_bucket_name = 'filedepot-%s' % (access_key_id.lower(), ) cred = (access_key_id, secret_access_key) bucket_name = 'filedepot-testfs-%s-%s-%s' % (access_key_id.lower(), NODE, PID) DepotManager.configure('awss3', {'depot.backend': 'depot.io.awss3.S3Storage', 'depot.access_key_id': access_key_id, 'depot.secret_access_key': secret_access_key, 'depot.bucket': bucket_name}) DepotManager.set_default('awss3') app = self.make_app() new_file = app.post('/create_file').json file_path = DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file) uploaded_file = app.get(file_path) assert uploaded_file.body == FILE_CONTENT, uploaded_file
def register_depot(app: Flask) -> Flask: from flask import request from werkzeug import Response from depot.manager import DepotManager from flask_login import login_required try: os.makedirs(app.config['DEPOT_STORAGE_PATH']) except FileExistsError: pass DepotManager.configure( 'default', {'depot.storage_path': app.config['DEPOT_STORAGE_PATH']}) app.depot_middleware = DepotManager.make_middleware(None, cache_max_age=0) @app.route('/depot/<path:path>', methods=['GET', 'HEAD']) @login_required def depot_proxy(path): kwargs = {} def fake_start_response(status, headers): kwargs['status'] = status kwargs['headers'] = headers kwargs['response'] = \ app.depot_middleware(request.environ, fake_start_response) return Response(**kwargs) return app
def test_func__user_update_command__err_password_modification_failed__external_auth( self, hapic, session, user_api_factory) -> None: """ Test user password update """ api = user_api_factory.get() user = api.get_one_by_email("*****@*****.**") assert user.email == "*****@*****.**" assert user.validate_password("*****@*****.**") assert not user.validate_password("new_password") user.auth_type = AuthType.LDAP assert user.auth_type == AuthType.LDAP session.add(user) session.flush() transaction.commit() session.close() # NOTE GM 2019-07-21: Unset Depot configuration. Done here and not in fixture because # TracimCLI need reseted context when ran. DepotManager._clear() app = TracimCLI() with pytest.raises(ExternalAuthUserPasswordModificationDisallowed): app.run([ "user", "update", "-c", "{}#command_test".format(TEST_CONFIG_FILE_PATH), "-l", "*****@*****.**", "-p", "new_ldap_password", "--debug", ])
def setUp(self): logger.debug(self, 'Setup Test...') self.config = testing.setUp( settings={ 'sqlalchemy.url': 'sqlite:///:memory:', 'user.auth_token.validity': '604800', 'depot_storage_dir': '/tmp/test/depot', 'depot_storage_name': 'test', 'preview_cache_dir': '/tmp/test/preview_cache', }) self.config.include('tracim.models') DepotManager._clear() DepotManager.configure( 'test', {'depot.backend': 'depot.io.memory.MemoryFileStorage'}) settings = self.config.get_settings() self.app_config = CFG(settings) from tracim.models import ( get_engine, get_session_factory, get_tm_session, ) self.engine = get_engine(settings) session_factory = get_session_factory(self.engine) self.session = get_tm_session(session_factory, transaction.manager) self.init_database()
def test_invalid_mountpoint(self): try: DepotManager.make_middleware(self.wsgi_app, mountpoint='hello') except ValueError as err: assert 'mountpoint must be an absolute path' in str(err) else: assert False, 'Should have raised ValueError'
def test_public_url_gets_redirect(self): try: global S3Storage from depot.io.awss3 import S3Storage except ImportError: raise SkipTest('Boto not installed') env = os.environ access_key_id = env.get('AWS_ACCESS_KEY_ID') secret_access_key = env.get('AWS_SECRET_ACCESS_KEY') if access_key_id is None or secret_access_key is None: raise SkipTest('Amazon S3 credentials not available') PID = os.getpid() NODE = str(uuid.uuid1()).rsplit('-', 1)[-1] # Travis runs multiple tests concurrently default_bucket_name = 'filedepot-%s' % (access_key_id.lower(), ) cred = (access_key_id, secret_access_key) bucket_name = 'filedepot-testfs-%s-%s-%s' % (access_key_id.lower(), NODE, PID) DepotManager.configure('awss3', {'depot.backend': 'depot.io.awss3.S3Storage', 'depot.access_key_id': access_key_id, 'depot.secret_access_key': secret_access_key, 'depot.bucket': bucket_name}) DepotManager.set_default('awss3') app = self.make_app() new_file = app.post('/create_file').json file_path = DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file) uploaded_file = app.get(file_path) assert uploaded_file.body == FILE_CONTENT, uploaded_file
def test_configure_filedepot(self, no_filedepots): from depot.manager import DepotManager from kotti.filedepot import configure_filedepot from kotti import tests tests.TFS1 = Mock(return_value=Mock(marker="TFS1")) tests.TFS2 = Mock(return_value=Mock(marker="TFS2")) settings = { "kotti.depot.0.backend": "kotti.tests.TFS1", "kotti.depot.0.name": "localfs", "kotti.depot.0.location": "/tmp", "kotti.depot.1.backend": "kotti.tests.TFS2", "kotti.depot.1.uri": "mongo://", "kotti.depot.1.name": "mongo", } configure_filedepot(settings) assert DepotManager.get().marker == "TFS1" assert DepotManager.get("localfs").marker == "TFS1" assert DepotManager.get("mongo").marker == "TFS2" tests.TFS1.assert_called_with(location="/tmp") tests.TFS2.assert_called_with(uri="mongo://") del tests.TFS1 del tests.TFS2
def configure_filedepot(settings): config = extract_depot_settings('kotti.depot.', settings) for conf in config: name = conf.pop('name') if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix='')
def test_configure_filedepot(self, no_filedepots): from depot.manager import DepotManager from kotti.filedepot import configure_filedepot from kotti import tests tests.TFS1 = Mock(return_value=Mock(marker="TFS1")) tests.TFS2 = Mock(return_value=Mock(marker="TFS2")) settings = { 'kotti.depot.0.backend': 'kotti.tests.TFS1', 'kotti.depot.0.name': 'localfs', 'kotti.depot.0.location': '/tmp', 'kotti.depot.1.backend': 'kotti.tests.TFS2', 'kotti.depot.1.uri': 'mongo://', 'kotti.depot.1.name': 'mongo', } configure_filedepot(settings) assert DepotManager.get().marker == 'TFS1' assert DepotManager.get('localfs').marker == 'TFS1' assert DepotManager.get('mongo').marker == 'TFS2' tests.TFS1.assert_called_with(location='/tmp') tests.TFS2.assert_called_with(uri='mongo://') del tests.TFS1 del tests.TFS2
def nanopub_depot(self): if self._nanopub_depot is None and 'nanopub_archive' in self.config: if DepotManager.get('nanopublications') is None: DepotManager.configure('nanopublications', self.config['nanopub_archive']) self._nanopub_depot = DepotManager.get('nanopublications') return self._nanopub_depot
def enable_depot(): # DEPOT setup from depot.manager import DepotManager DepotManager.configure('contact_images', config, 'depot.contact_images.') DepotManager.alias('contact_image', 'contact_images')
def tearDown(self): logger.debug(self, 'TearDown Test...') from tracim.models.meta import DeclarativeBase testing.tearDown() transaction.abort() DeclarativeBase.metadata.drop_all(self.engine) DepotManager._clear()
def setup(): setup_database() DepotManager._clear() DepotManager.configure('default', {'depot.storage_path': './lfs'}) DepotManager.configure('another', {'depot.storage_path': './lfs'}) DepotManager.alias('another_alias', 'another') DepotManager.make_middleware(None)
def disconnect_database(self, remove_tables: bool = False) -> None: self.session.rollback() transaction.abort() self.session.close_all() self.engine.dispose() if remove_tables: DeclarativeBase.metadata.drop_all(self.engine) DepotManager._clear()
def setup(): setup_database() DepotManager._clear() DepotManager.configure("default", {"depot.storage_path": "./lfs"}) DepotManager.configure("another", {"depot.storage_path": "./lfs"}) DepotManager.alias("another_alias", "another") DepotManager.make_middleware(None)
def configure_filedepot(self): depot_storage_name = self.DEPOT_STORAGE_NAME depot_storage_path = self.DEPOT_STORAGE_DIR depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def setup(): setup_database() DepotManager._clear() DepotManager.configure('default', {'depot.storage_path': './lfs'}) DepotManager.configure('another', {'depot.storage_path': './lfs'}) DepotManager.alias('another_alias', 'another') DepotManager.make_middleware(None)
def configure_depot(): """Configure Depot.""" depot_storage_name = context.config.get_main_option('depot_storage_name') depot_storage_path = context.config.get_main_option('depot_storage_dir') depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def depot(temporary_directory): DepotManager.configure('default', { 'depot.backend': 'depot.io.local.LocalFileStorage', 'depot.storage_path': temporary_directory }) yield DepotManager.get() DepotManager._clear()
def configure_depot(): """Configure Depot.""" depot_storage_name = CFG.get_instance().DEPOT_STORAGE_NAME depot_storage_path = CFG.get_instance().DEPOT_STORAGE_DIR depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def configure_filedepot(settings): from kotti.util import extract_depot_settings from depot.manager import DepotManager config = extract_depot_settings("kotti.depot.", settings) for conf in config: name = conf.pop("name") if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix="")
def configure_depot(): """Configure Depot.""" depot_storage_name = context.config.get_main_option('depot_storage_name') depot_storage_path = context.config.get_main_option('depot_storage_dir') depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def test_prevent_configuring_two_storages_with_same_name(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) try: DepotManager.configure('first', {'depot.storage_path': './lfs2'}) except RuntimeError: pass else: assert False, 'Should have raised RunetimeError here'
def test_prevent_configuring_two_storages_with_same_name(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) try: DepotManager.configure('first', {'depot.storage_path': './lfs2'}) except RuntimeError: pass else: assert False, 'Should have raised RunetimeError here'
def configure_filedepot(self) -> None: # TODO - G.M - 2018-08-08 - [GlobalVar] Refactor Global var # of tracim_backend, Be careful DepotManager is a Singleton ! depot_storage_name = self.DEPOT_STORAGE_NAME depot_storage_path = self.DEPOT_STORAGE_DIR depot_storage_settings = {"depot.storage_path": depot_storage_path} DepotManager.configure(depot_storage_name, depot_storage_settings)
def configure_depot(): """Configure Depot.""" depot_storage_name = CFG.get_instance().DEPOT_STORAGE_NAME depot_storage_path = CFG.get_instance().DEPOT_STORAGE_DIR depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def create_file(self, lang='en'): fname = {'en': 'hello.txt', 'ru': u_('Крупный'), 'it': u_('àèìòù')}.get(lang, 'unknown') self.UPLOADED_FILES += [DepotManager.get().create(FILE_CONTENT, filename=fname)] return dict(files=self.UPLOADED_FILES, uploaded_to=DepotManager.get_default(), last=self.UPLOADED_FILES[-1])
def create_file(self, lang='en'): fname = {'en': 'hello.txt', 'ru': u_('Крупный'), 'it': u_('àèìòù')}.get(lang, 'unknown') self.UPLOADED_FILES += [DepotManager.get().create(FILE_CONTENT, filename=fname)] return dict(files=self.UPLOADED_FILES, uploaded_to=DepotManager.get_default(), last=self.UPLOADED_FILES[-1])
def __init__(self, fileid, depot_name=None): if depot_name is None: depot_name = DepotManager.get_default() depot_name = DepotManager.resolve_alias(depot_name) if not depot_name: raise ValueError('Storage has not been found in DEPOT') self.depot_name = depot_name self.name = fileid
def depot(temporary_directory): DepotManager.configure( 'default', { 'depot.backend': 'depot.io.local.LocalFileStorage', 'depot.storage_path': temporary_directory }) yield DepotManager.get() DepotManager._clear()
def test_default_filedepot(self, db_session): from kotti import main from depot.manager import DepotManager settings = self.required_settings() with patch("kotti.resources.initialize_sql"): with patch("kotti.filedepot.TweenFactory"): main({}, **settings) assert DepotManager.get().__class__.__name__ == "DBFileStorage" DepotManager._clear()
def test_aliases(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) DepotManager.configure('second', {'depot.storage_path': './lfs2'}) DepotManager.alias('used_storage', 'first') storage = DepotManager.get('used_storage') assert storage.storage_path == './lfs', storage DepotManager.alias('used_storage', 'second') storage = DepotManager.get('used_storage') assert storage.storage_path == './lfs2', storage
def no_filedepots(db_session, depot_tween): """ A filedepot fixture to empty and then restore DepotManager configuration """ from depot.manager import DepotManager DepotManager._depots = {} DepotManager._default_depot = None yield DepotManager db_session.rollback() DepotManager._clear()
def tearDown(self) -> None: logger.debug(self, 'TearDown Test...') self.session.rollback() self.session.close_all() transaction.abort() DeclarativeBase.metadata.drop_all(self.engine) sql = text('DROP TABLE IF EXISTS migrate_version;') result = self.engine.execute(sql) self.engine.dispose() DepotManager._clear() testing.tearDown()
def configure_filedepot(self): # TODO - G.M - 2018-08-08 - [GlobalVar] Refactor Global var # of tracim_backend, Be careful DepotManager is a Singleton ! depot_storage_name = self.DEPOT_STORAGE_NAME depot_storage_path = self.DEPOT_STORAGE_DIR depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def test_session_rollback(self, factory, db_session, filedepot): from depot.manager import DepotManager f = factory(data='file content', name=u'content', title=u'content') id = f.data['file_id'] db_session.add(f) db_session.flush() assert id in DepotManager.get()._storage.keys() db_session.rollback() assert id not in DepotManager.get()._storage.keys() assert DepotManager.get().delete.called
def filedepot(db_session, depot_tween): """ Configures a dbsession integrated mock depot store for :class:`depot.manager.DepotManager` """ from depot.manager import DepotManager DepotManager._depots = {"filedepot": MagicMock(wraps=TestStorage())} DepotManager._default_depot = "filedepot" yield DepotManager db_session.rollback() DepotManager._clear()
def index(request): if request.method == 'POST': file = request.FILES['file'] if file: fileid = DepotManager.get().create(file) UPLOADED_FILES.append(fileid) return HttpResponseRedirect('/') files = [DepotManager.get().get(fileid) for fileid in UPLOADED_FILES] template = loader.get_template('index.html') return HttpResponse(template.render({ 'files': files }, request))
def __init__(self, handler: Optional[Callable], registry: Registry) -> None: """ :param handler: Downstream tween or main Pyramid request handler (Kotti) :type handler: function :param registry: Application registry :type registry: :class:`pyramid.registry.Registry` """ self.mountpoint = registry.settings["kotti.depot_mountpoint"] self.handler = handler self.registry = registry DepotManager.set_middleware(self)
def mock_filedepot(depot_tween): """ Configures a mock depot store for :class:`depot.manager.DepotManager` This filedepot is not integrated with dbsession. Can be used in simple, standalone unit tests. """ from depot.manager import DepotManager DepotManager._depots = {"mockdepot": MagicMock(wraps=TestStorage())} DepotManager._default_depot = "mockdepot" yield DepotManager DepotManager._clear()
def test_serving_files_content_disposition(self): app = self.make_app() new_file = app.post('/create_file', params={'lang': 'ru'}).json uploaded_file = app.get(DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file)) content_disposition = uploaded_file.headers['Content-Disposition'] assert content_disposition == "inline;filename=Krupnyi;filename*=utf-8''%D0%9A%D1%80%D1%83%D0%BF%D0%BD%D1%8B%D0%B9", content_disposition new_file = app.post('/create_file', params={'lang': 'it'}).json uploaded_file = app.get(DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file)) content_disposition = uploaded_file.headers['Content-Disposition'] _, asciiname, uniname = content_disposition.split(';') assert asciiname == 'filename=aeiou', asciiname assert u_(unquote(uniname[17:])) == u_('àèìòù'), unquote(uniname[17:])
def tearDown(): from depot.manager import DepotManager from kotti import events from kotti import security from kotti.message import _inject_mailer # These should arguable use the configurator, so they don't need # to be torn down separately: events.clear() security.reset() _inject_mailer[:] = [] transaction.abort() DepotManager._clear() testing.tearDown()
def test_post_is_forwarded_to_app(self): app = self.make_app() new_file = app.post('/create_file').json file_path = DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file) uploaded_file = app.post(file_path, status=404) assert 'Not Found' in uploaded_file.status, uploaded_file