Beispiel #1
0
 def setUp(self):
     self.app = eve.Eve(__name__, {"DOMAIN": {}})
     self.app.config["MEDIA_PREFIX"] = "http://localhost/upload-raw"
     self.app.config["DOMAIN"] = {"upload": {}}
     self.app.config["MONGO_DBNAME"] = "sptests"
     self.app.data = SuperdeskDataLayer(self.app)
     self.media = SuperdeskGridFSMediaStorage(self.app)
     self.app.register_blueprint(bp)
     self.app.upload_url = upload_url
Beispiel #2
0
 def setUp(self):
     self.app = eve.Eve(__name__, {'DOMAIN': {}})
     self.app.config['SERVER_NAME'] = 'localhost'
     self.app.config['DOMAIN'] = {'upload': {}}
     self.app.config['MONGO_DBNAME'] = 'sptests'
     self.app.data = SuperdeskDataLayer(self.app)
     self.media = SuperdeskGridFSMediaStorage(self.app)
     self.app.register_blueprint(bp)
     self.app.upload_url = upload_url
 def setUp(self):
     self.app = eve.Eve(__name__, {'DOMAIN': {}})
     self.app.config['MEDIA_PREFIX'] = 'http://localhost/upload-raw'
     self.app.config['DOMAIN'] = {'upload': {}}
     self.app.config['MONGO_DBNAME'] = 'sptests'
     self.app.data = SuperdeskDataLayer(self.app)
     self.media = SuperdeskGridFSMediaStorage(self.app)
     self.app.register_blueprint(bp)
     self.app.upload_url = upload_url
Beispiel #4
0
    def __init__(self,
                 import_name=__package__,
                 config=None,
                 testing=False,
                 **kwargs):
        """Override __init__ to do Newsroom specific config and still be able
        to create an instance using ``app = Newsroom()``
        """
        self.sidenavs = []
        self.settings_apps = []
        self.download_formatters = {}
        self.extensions = {}
        self.theme_folder = 'theme'
        self.sections = []
        self.dashboards = []
        self._testing = testing
        self._general_settings = {}

        app_config = os.path.join(NEWSROOM_DIR, 'default_settings.py')

        # get content api default conf

        if config:
            app_config = flask.Config(app_config)
            app_config.from_object('content_api.app.settings')
            app_config.update(config)

        super(Newsroom, self).__init__(
            import_name,
            data=SuperdeskDataLayer,
            auth=SessionAuth,
            settings=app_config,
            template_folder=os.path.join(NEWSROOM_DIR, 'templates'),
            static_folder=os.path.join(NEWSROOM_DIR, 'static'),
            json_encoder=MongoJSONEncoder,
            **kwargs)
        self.json_encoder = MongoJSONEncoder
        if self.config.get('AMAZON_CONTAINER_NAME'):
            self.media = AmazonMediaStorage(self)
        else:
            self.media = SuperdeskGridFSMediaStorage(self)
        self._setup_jinja()
        self._setup_limiter()
        self._setup_babel()
        init_celery(self)
        newsroom.app = self
        self._setup_blueprints(self.config['BLUEPRINTS'])
        self._setup_apps(self.config['CORE_APPS'])
        self._setup_apps(self.config.get('INSTALLED_APPS', []))
        self._setup_webpack()
        self._setup_email()
        self._setup_cache()
        self._setup_error_handlers()
        self._setup_theme()
Beispiel #5
0
class GridFSMediaStorageTestCase(unittest.TestCase):
    def setUp(self):
        self.app = eve.Eve(__name__, {'DOMAIN': {}})
        self.app.config['SERVER_NAME'] = 'localhost'
        self.app.config['DOMAIN'] = {'upload': {}}
        self.app.config['MONGO_DBNAME'] = 'sptests'
        self.app.data = SuperdeskDataLayer(self.app)
        self.media = SuperdeskGridFSMediaStorage(self.app)
        self.app.register_blueprint(bp)
        self.app.upload_url = upload_url

    def test_url_for_media(self):
        _id = bson.ObjectId(sha('test')[:24])
        with self.app.app_context():
            url = self.media.url_for_media(_id)
        self.assertEqual('http://localhost/upload-raw/%s' % _id, url)

    def test_url_for_media_content_type(self):
        _id_str = '1' * 24
        _id = bson.ObjectId(_id_str)
        with self.app.app_context():
            url = self.media.url_for_media(_id, "image/jpeg")
        self.assertEqual('http://localhost/upload-raw/{}.jpg'.format(_id_str),
                         url)

    def test_put_media_with_id(self):
        data = io.StringIO("test data")
        filename = 'x'

        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()

        with self.app.app_context():
            self.media.put(data, filename, 'text/plain', _id=str(_id))

        kwargs = {
            'content_type': 'text/plain',
            'filename': filename,
            'metadata': None,
            '_id': _id,
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_put_into_folder(self):
        data = b'test data'
        filename = 'x'
        folder = 'gridtest'

        gridfs = self._mock_gridfs()

        with self.app.app_context():
            self.media.put(data,
                           filename=filename,
                           content_type='text/plain',
                           folder=folder)

        kwargs = {
            'content_type': 'text/plain',
            'filename': '{}/{}'.format(folder, filename),
            'metadata': None
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_find_files(self):
        gridfs = self._mock_gridfs()
        upload_date = {'$lte': utcnow(), '$gte': utcnow() - timedelta(hours=1)}
        folder = 'gridtest'
        query_filename = {'filename': {'$regex': '^{}/'.format(folder)}}
        query_upload_date = {'uploadDate': upload_date}

        with self.app.app_context():
            self.media.find(folder=folder, upload_date=upload_date)
            gridfs.find.assert_called_once_with(
                {'$and': [query_filename, query_upload_date]})

            self.media.find(folder=folder)
            gridfs.find.assert_called_with(query_filename)

            self.media.find(upload_date=upload_date)
            gridfs.find.assert_called_with(query_upload_date)

            self.media.find()
            gridfs.find.assert_called_with({})

    def _mock_gridfs(self):
        gridfs = Mock()
        gridfs.put = Mock(return_value='y')
        gridfs.find = Mock(return_value=[])
        self.media._fs['MONGO'] = gridfs
        return gridfs
Beispiel #6
0
 def setup_media_storage(self):
     if self.config.get('AMAZON_CONTAINER_NAME'):
         self.media = AmazonMediaStorage(self)
     else:
         self.media = SuperdeskGridFSMediaStorage(self)
Beispiel #7
0
class GridFSMediaStorageTestCase(unittest.TestCase):
    def setUp(self):
        self.app = eve.Eve(__name__, {"DOMAIN": {}})
        self.app.config["MEDIA_PREFIX"] = "http://localhost/upload-raw"
        self.app.config["DOMAIN"] = {"upload": {}}
        self.app.config["MONGO_DBNAME"] = "sptests"
        self.app.data = SuperdeskDataLayer(self.app)
        self.media = SuperdeskGridFSMediaStorage(self.app)
        self.app.register_blueprint(bp)
        self.app.upload_url = upload_url

    def test_url_for_media(self):
        _id = bson.ObjectId(sha("test")[:24])
        with self.app.app_context():
            url = self.media.url_for_media(_id)
        self.assertEqual("http://localhost/upload-raw/%s" % _id, url)

    def test_url_for_media_content_type(self):
        _id_str = "1" * 24
        _id = bson.ObjectId(_id_str)
        with self.app.app_context():
            url = self.media.url_for_media(_id, "image/jpeg")
        self.assertEqual("http://localhost/upload-raw/{}.jpg".format(_id_str), url)

    def test_put_media_with_id(self):
        data = io.StringIO("test data")
        filename = "x"

        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()

        with self.app.app_context():
            self.media.put(data, filename=filename, content_type="text/plain", _id=str(_id))

        kwargs = {
            "content_type": "text/plain",
            "filename": filename,
            "metadata": None,
            "_id": _id,
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_put_into_folder(self):
        data = b"test data"
        filename = "x"
        folder = "gridtest"

        gridfs = self._mock_gridfs()

        with self.app.app_context():
            self.media.put(data, filename=filename, content_type="text/plain", folder=folder)

        kwargs = {"content_type": "text/plain", "filename": "{}/{}".format(folder, filename), "metadata": None}

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_find_files(self):
        gridfs = self._mock_gridfs()
        upload_date = {"$lte": utcnow(), "$gte": utcnow() - timedelta(hours=1)}
        folder = "gridtest"
        query_filename = {"filename": {"$regex": "^{}/".format(folder)}}
        query_upload_date = {"uploadDate": upload_date}

        with self.app.app_context():
            self.media.find(folder=folder, upload_date=upload_date)
            gridfs.find.assert_called_once_with({"$and": [query_filename, query_upload_date]})

            self.media.find(folder=folder)
            gridfs.find.assert_called_with(query_filename)

            self.media.find(upload_date=upload_date)
            gridfs.find.assert_called_with(query_upload_date)

            self.media.find()
            gridfs.find.assert_called_with({})

    def test_custom_id(self):
        data = b"foo"
        with self.app.app_context():
            self.media.put(data, _id="foo")
            _file = self.media.get("foo")
            assert data == _file.read()

    def _mock_gridfs(self):
        gridfs = Mock()
        gridfs.put = Mock(return_value="y")
        gridfs.find = Mock(return_value=[])
        self.media._fs["MONGO"] = gridfs
        return gridfs

    def test_mimetype_detect(self):
        # keep default mimetype
        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()
        content = b"bytes are here"
        filename = "extensionless"
        content_type = "text/css"
        with self.app.app_context():
            self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
        kwargs = {
            "content_type": content_type,
            "filename": filename,
            "metadata": None,
            "_id": _id,
        }
        gridfs.put.assert_called_once_with(content, **kwargs)

        # get mimetype from the filename
        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()
        content = b"bytes are here"
        filename = "styles.css"
        content_type = "application/pdf"
        with self.app.app_context():
            self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
        kwargs = {
            "content_type": "text/css",
            "filename": filename,
            "metadata": None,
            "_id": _id,
        }
        gridfs.put.assert_called_once_with(content, **kwargs)

        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()
        content = b"bytes are here"
        filename = "styles.JpG"
        content_type = "application/pdf"
        with self.app.app_context():
            self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
        kwargs = {
            "content_type": "image/jpeg",
            "filename": filename,
            "metadata": None,
            "_id": _id,
        }
        gridfs.put.assert_called_once_with(content, **kwargs)

        # get mimetype from the file
        fixtures_path = os.path.join(os.path.dirname(__file__), "fixtures")
        with open(os.path.join(fixtures_path, "file_example-jpg.jpg"), "rb") as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = "extensionless"
            content_type = "dummy/text"
            with self.app.app_context():
                self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
            kwargs = {
                "content_type": "image/jpeg",
                "filename": filename,
                "metadata": None,
                "_id": _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)

        with open(os.path.join(fixtures_path, "file_example-xls.xls"), "rb") as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = "extensionless"
            content_type = "dummy/text"
            with self.app.app_context():
                self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
            kwargs = {
                "content_type": "application/vnd.ms-excel",
                "filename": filename,
                "metadata": None,
                "_id": _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)

        with open(os.path.join(fixtures_path, "file_example-xlsx.xlsx"), "rb") as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = "extensionless"
            content_type = "dummy/text"
            with self.app.app_context():
                self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
            kwargs = {
                "content_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "filename": filename,
                "metadata": None,
                "_id": _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)

        with open(os.path.join(fixtures_path, "file_example-docx.docx"), "rb") as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = "extensionless"
            content_type = "dummy/text"
            with self.app.app_context():
                self.media.put(content, filename=filename, content_type=content_type, _id=str(_id))
            kwargs = {
                "content_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "filename": filename,
                "metadata": None,
                "_id": _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)
class GridFSMediaStorageTestCase(unittest.TestCase):
    def setUp(self):
        self.app = eve.Eve(__name__, {'DOMAIN': {}})
        self.app.config['MEDIA_PREFIX'] = 'http://localhost/upload-raw'
        self.app.config['DOMAIN'] = {'upload': {}}
        self.app.config['MONGO_DBNAME'] = 'sptests'
        self.app.data = SuperdeskDataLayer(self.app)
        self.media = SuperdeskGridFSMediaStorage(self.app)
        self.app.register_blueprint(bp)
        self.app.upload_url = upload_url

    def test_url_for_media(self):
        _id = bson.ObjectId(sha('test')[:24])
        with self.app.app_context():
            url = self.media.url_for_media(_id)
        self.assertEqual('http://localhost/upload-raw/%s' % _id, url)

    def test_url_for_media_content_type(self):
        _id_str = '1' * 24
        _id = bson.ObjectId(_id_str)
        with self.app.app_context():
            url = self.media.url_for_media(_id, "image/jpeg")
        self.assertEqual('http://localhost/upload-raw/{}.jpg'.format(_id_str),
                         url)

    def test_put_media_with_id(self):
        data = io.StringIO("test data")
        filename = 'x'

        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()

        with self.app.app_context():
            self.media.put(data, filename, 'text/plain', _id=str(_id))

        kwargs = {
            'content_type': 'text/plain',
            'filename': filename,
            'metadata': None,
            '_id': _id,
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_put_into_folder(self):
        data = b'test data'
        filename = 'x'
        folder = 'gridtest'

        gridfs = self._mock_gridfs()

        with self.app.app_context():
            self.media.put(data,
                           filename=filename,
                           content_type='text/plain',
                           folder=folder)

        kwargs = {
            'content_type': 'text/plain',
            'filename': '{}/{}'.format(folder, filename),
            'metadata': None
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_find_files(self):
        gridfs = self._mock_gridfs()
        upload_date = {'$lte': utcnow(), '$gte': utcnow() - timedelta(hours=1)}
        folder = 'gridtest'
        query_filename = {'filename': {'$regex': '^{}/'.format(folder)}}
        query_upload_date = {'uploadDate': upload_date}

        with self.app.app_context():
            self.media.find(folder=folder, upload_date=upload_date)
            gridfs.find.assert_called_once_with(
                {'$and': [query_filename, query_upload_date]})

            self.media.find(folder=folder)
            gridfs.find.assert_called_with(query_filename)

            self.media.find(upload_date=upload_date)
            gridfs.find.assert_called_with(query_upload_date)

            self.media.find()
            gridfs.find.assert_called_with({})

    def test_custom_id(self):
        data = b'foo'
        with self.app.app_context():
            self.media.put(data, _id='foo')
            _file = self.media.get('foo')
            assert data == _file.read()

    def _mock_gridfs(self):
        gridfs = Mock()
        gridfs.put = Mock(return_value='y')
        gridfs.find = Mock(return_value=[])
        self.media._fs['MONGO'] = gridfs
        return gridfs

    def test_mimetype_detect(self):
        # keep default mimetype
        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()
        content = b'bytes are here'
        filename = 'extensionless'
        content_type = 'text/css'
        with self.app.app_context():
            self.media.put(content, filename, content_type, _id=str(_id))
        kwargs = {
            'content_type': content_type,
            'filename': filename,
            'metadata': None,
            '_id': _id,
        }
        gridfs.put.assert_called_once_with(content, **kwargs)

        # get mimetype from the filename
        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()
        content = b'bytes are here'
        filename = 'styles.css'
        content_type = 'application/pdf'
        with self.app.app_context():
            self.media.put(content, filename, content_type, _id=str(_id))
        kwargs = {
            'content_type': 'text/css',
            'filename': filename,
            'metadata': None,
            '_id': _id,
        }
        gridfs.put.assert_called_once_with(content, **kwargs)

        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()
        content = b'bytes are here'
        filename = 'styles.JpG'
        content_type = 'application/pdf'
        with self.app.app_context():
            self.media.put(content, filename, content_type, _id=str(_id))
        kwargs = {
            'content_type': 'image/jpeg',
            'filename': filename,
            'metadata': None,
            '_id': _id,
        }
        gridfs.put.assert_called_once_with(content, **kwargs)

        # get mimetype from the file
        fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
        with open(os.path.join(fixtures_path, "file_example-jpg.jpg"),
                  'rb') as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = 'extensionless'
            content_type = 'dummy/text'
            with self.app.app_context():
                self.media.put(content, filename, content_type, _id=str(_id))
            kwargs = {
                'content_type': 'image/jpeg',
                'filename': filename,
                'metadata': None,
                '_id': _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)

        with open(os.path.join(fixtures_path, "file_example-xls.xls"),
                  'rb') as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = 'extensionless'
            content_type = 'dummy/text'
            with self.app.app_context():
                self.media.put(content, filename, content_type, _id=str(_id))
            kwargs = {
                'content_type': 'application/vnd.ms-excel',
                'filename': filename,
                'metadata': None,
                '_id': _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)

        with open(os.path.join(fixtures_path, "file_example-xlsx.xlsx"),
                  'rb') as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = 'extensionless'
            content_type = 'dummy/text'
            with self.app.app_context():
                self.media.put(content, filename, content_type, _id=str(_id))
            kwargs = {
                'content_type':
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'filename': filename,
                'metadata': None,
                '_id': _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)

        with open(os.path.join(fixtures_path, "file_example-docx.docx"),
                  'rb') as content:
            gridfs = self._mock_gridfs()
            _id = bson.ObjectId()
            filename = 'extensionless'
            content_type = 'dummy/text'
            with self.app.app_context():
                self.media.put(content, filename, content_type, _id=str(_id))
            kwargs = {
                'content_type':
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'filename': filename,
                'metadata': None,
                '_id': _id,
            }
            gridfs.put.assert_called_once_with(content, **kwargs)
class GridFSMediaStorageTestCase(unittest.TestCase):

    def setUp(self):
        self.app = eve.Eve(__name__, {'DOMAIN': {}})
        self.app.config['MEDIA_PREFIX'] = 'http://localhost/upload-raw'
        self.app.config['DOMAIN'] = {'upload': {}}
        self.app.config['MONGO_DBNAME'] = 'sptests'
        self.app.data = SuperdeskDataLayer(self.app)
        self.media = SuperdeskGridFSMediaStorage(self.app)
        self.app.register_blueprint(bp)
        self.app.upload_url = upload_url

    def test_url_for_media(self):
        _id = bson.ObjectId(sha('test')[:24])
        with self.app.app_context():
            url = self.media.url_for_media(_id)
        self.assertEqual('http://localhost/upload-raw/%s' % _id, url)

    def test_url_for_media_content_type(self):
        _id_str = '1' * 24
        _id = bson.ObjectId(_id_str)
        with self.app.app_context():
            url = self.media.url_for_media(_id, "image/jpeg")
        self.assertEqual('http://localhost/upload-raw/{}.jpg'.format(_id_str), url)

    def test_put_media_with_id(self):
        data = io.StringIO("test data")
        filename = 'x'

        gridfs = self._mock_gridfs()
        _id = bson.ObjectId()

        with self.app.app_context():
            self.media.put(data, filename, 'text/plain', _id=str(_id))

        kwargs = {
            'content_type': 'text/plain',
            'filename': filename,
            'metadata': None,
            '_id': _id,
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_put_into_folder(self):
        data = b'test data'
        filename = 'x'
        folder = 'gridtest'

        gridfs = self._mock_gridfs()

        with self.app.app_context():
            self.media.put(data, filename=filename, content_type='text/plain', folder=folder)

        kwargs = {
            'content_type': 'text/plain',
            'filename': '{}/{}'.format(folder, filename),
            'metadata': None
        }

        gridfs.put.assert_called_once_with(data, **kwargs)

    def test_find_files(self):
        gridfs = self._mock_gridfs()
        upload_date = {'$lte': utcnow(), '$gte': utcnow() - timedelta(hours=1)}
        folder = 'gridtest'
        query_filename = {'filename': {'$regex': '^{}/'.format(folder)}}
        query_upload_date = {'uploadDate': upload_date}

        with self.app.app_context():
            self.media.find(folder=folder, upload_date=upload_date)
            gridfs.find.assert_called_once_with({'$and': [query_filename, query_upload_date]})

            self.media.find(folder=folder)
            gridfs.find.assert_called_with(query_filename)

            self.media.find(upload_date=upload_date)
            gridfs.find.assert_called_with(query_upload_date)

            self.media.find()
            gridfs.find.assert_called_with({})

    def test_custom_id(self):
        data = b'foo'
        with self.app.app_context():
            self.media.put(data, _id='foo')
            _file = self.media.get('foo')
            assert data == _file.read()

    def _mock_gridfs(self):
        gridfs = Mock()
        gridfs.put = Mock(return_value='y')
        gridfs.find = Mock(return_value=[])
        self.media._fs['MONGO'] = gridfs
        return gridfs