def test_create_files_request(self):
        expected_file_descriptor = FileDescriptor('/fish', 'file-id',
                                                  FileType.directory,
                                                  FileMimeType.directory, 0)
        expected_response_body = RestResult(
            0, 'OK', expected_file_descriptor.serialize())

        httpretty.register_uri(httpretty.POST,
                               'https://fish.barrel/_api/files',
                               body=json.dumps(
                                   expected_response_body.serialize()))

        response = self.file_service.create_files_request().add_file(
            self.file_service.create_file_request().set_path(
                '/fish')).execute()

        assert_that(response.file_descriptors, instance_of(list))
        assert_that(response.file_descriptors, has_length(1))
        assert_that(response.file_descriptors[0], instance_of(FileDescriptor))
        assert_that(response.file_descriptors[0].serialize(),
                    is_(expected_file_descriptor.serialize()))

        assert_that(
            json.loads(httpretty.last_request().body),
            is_({
                'mimeType': FileMimeType.directory,
                'path': '/fish',
                'size': 0,
                'type': FileType.directory,
                'acl': ACL.public,
                'id': None,
                'bucket': None
            }))
    def validate(self):
        FileDescriptor.acl_validator(self.acl)

        if self.path is not None:
            FileDescriptor.path_validator(self.path)

        if not self.path and not self.file_id:
            raise ValueError('must provide path or id')
    def test_create_archive_manifest_request(self):
        payload = FileDescriptor('/m.zip', 'file-id', FileType.file,
                                 'application/vnd.wix-media.zip',
                                 123).serialize()
        response_body = RestResult(0, 'OK', payload)
        httpretty.register_uri(
            httpretty.POST,
            'https://fish.appspot.com/_api/archive/create/manifest',
            body=json.dumps(response_body.serialize()))

        file_descriptor = self.archive_service.create_archive_manifest_request(
        ).add_sources(Source('/video.mp4')).set_destination(
            Destination('/m.zip')).set_algorithm(
                ZipAlgorithm.store).set_name('archive.zip').execute()

        assert_that(file_descriptor, instance_of(FileDescriptor))
        assert_that(file_descriptor.path, is_('/m.zip'))
        assert_that(
            json.loads(httpretty.last_request().body),
            is_({
                'sources': [{
                    'path': '/video.mp4',
                    'fileId': None
                }],
                'destination': {
                    'directory': None,
                    'path': '/m.zip',
                    'lifecycle': None,
                    'acl': 'public',
                    'bucket': None
                },
                'name': 'archive.zip',
                'algorithm': 'store'
            }))
    def test_file_list_request(self):
        response_body = RestResult(
            0, 'OK', {
                'nextPageToken':
                'next page',
                'files': [
                    FileDescriptor('/fish.txt', 'file-id', FileType.file,
                                   'text/plain', 123).serialize()
                ]
            })
        httpretty.register_uri(httpretty.GET,
                               'https://fish.barrel/_api/files/ls_dir',
                               body=json.dumps(response_body.serialize()))

        file_list = self.file_service.file_list_request().set_path('/files').set_next_page_token('next') \
            .set_order_by(OrderBy.name).set_page_size(12).set_recursive(True).set_type(FileType.file).execute()

        assert_that(file_list.next_page_token, is_('next page'))
        assert_that(
            httpretty.last_request().querystring,
            is_({
                'nextPageToken': ['next'],
                'orderBy': ['name'],
                'pageSize': ['12'],
                'r': ['True'],
                'path': ['/files'],
                'type': ['-'],
                'orderDirection': ['des']
            }))
    def test_copy_file_request(self):
        response_body = RestResult(
            0, 'OK',
            FileDescriptor('/file.copy.txt', 'file-new-id', FileType.file,
                           'text/plain', 123).serialize())
        httpretty.register_uri(httpretty.POST,
                               'https://fish.barrel/_api/copy/file',
                               body=json.dumps(response_body.serialize()))

        file_descriptor = self.file_service.copy_file_request().set_source(
            Source('/file.txt')).set_destination(
                Destination('/file.copy.txt')).execute()

        assert_that(file_descriptor, instance_of(FileDescriptor))
        assert_that(file_descriptor.path, is_('/file.copy.txt'))
        assert_that(
            json.loads(httpretty.last_request().body),
            is_({
                'source': {
                    'path': '/file.txt',
                    'fileId': None
                },
                'destination': {
                    'directory': None,
                    'path': '/file.copy.txt',
                    'lifecycle': None,
                    'acl': 'public',
                    'bucket': None
                }
            }))
    def test_image_operation_request(self):
        payload = FileDescriptor('/pony.png', 'file-id', FileType.file,
                                 'image/png', 123).serialize()
        response = RestResult(0, 'OK', payload)
        httpretty.register_uri(httpretty.POST,
                               'https://fish.barrel/_api/images/operations',
                               body=json.dumps(response.serialize()))

        features = self.image_service.image_operation_request().set_source(
            Source('/omg.png')).set_specification(
                ImageOperationSpecification(
                    '/v1/fit/w_200,h_100',
                    Destination('/pony.png'))).execute()

        assert_that(features, instance_of(FileDescriptor))
        assert_that(
            json.loads(httpretty.last_request().body),
            is_({
                'source': {
                    'path': '/omg.png',
                    'fileId': None
                },
                'specification': {
                    'destination': {
                        'directory': None,
                        'path': '/pony.png',
                        'lifecycle': None,
                        'acl': 'public',
                        'bucket': None
                    },
                    'command': '/v1/fit/w_200,h_100'
                }
            }))
    def test_upload_file_request_with_callback(self):
        url_response_body = RestResult(
            0, 'OK', {
                'uploadToken': 'token',
                'uploadUrl': 'https://fish.barrel/cryptic-path'
            })
        httpretty.register_uri(httpretty.GET,
                               'https://fish.barrel/_api/upload/url',
                               body=json.dumps(url_response_body.serialize()))

        upload_response_body = RestResult(0, 'OK', [
            FileDescriptor('/fish.txt', 'file-id', FileType.file,
                           FileMimeType.defualt, 123).serialize()
        ])
        httpretty.register_uri(httpretty.POST,
                               'https://fish.barrel/cryptic-path',
                               body=json.dumps(
                                   upload_response_body.serialize()))

        callback = Callback('https://valid.url/', {'attach': 'fish'},
                            {'head': 'dog'})
        self.file_service.upload_file_request().set_path(
            '/fish.txt').set_content('some content').set_callback(
                callback).execute()

        request_body = httpretty.last_request().body.decode('utf-8')
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="acl"\r\n\r\n%s' %
                ACL.public))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="path"\r\n\r\n%s' %
                '/fish.txt'))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="uploadToken"\r\n\r\n%s'
                % 'token'))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="mimeType"\r\n\r\n%s' %
                FileMimeType.defualt))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="callback"\r\n\r\n%s' %
                json.dumps(callback.serialize())))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="file"; filename="file-name"\r\n'
                'Content-Type: %s\r\n\r\n%s' %
                (FileMimeType.defualt, 'some content')))
    def deserialize(cls, data):
        # type: (dict) -> FontFileMetadata
        if data['mediaType'] != MediaType.font:
            raise ValueError('not font metadata')

        file_descriptor = FileDescriptor.deserialize(data['fileDescriptor'])
        basic = FontBasic.deserialize(
            data.get('basic')) if data.get('basic') else None

        return FontFileMetadata(file_descriptor, basic)
    def test_deserialize(self):
        file_descriptor = FileDescriptor('/fish.jpg', 'file-id', FileType.file,
                                         'image/jpg', 1200).serialize()

        data = {'file': file_descriptor, 'attachment': {'dog': 'bull'}}

        payload = FileUploadCallbackPayload.deserialize(data)

        assert_that(payload.attachment, is_({'dog': 'bull'}))
        assert_that(payload.file_descriptor.id, is_('file-id'))
    def test_playlist_request(self):
        url = self.transcode_service.playlist_request().add_files(
            FileDescriptor('/movie.720.mp4', 'file-id', FileType.file,
                           'video/mp4',
                           123)).add_paths('/movie.1080p.mp4').execute()

        assert_that(
            url,
            is_('//packager-fish.wixmp.com/movie.,720,1080p,.mp4.urlset/master.m3u8'
                ))
Beispiel #11
0
    def deserialize(cls, data):
        # type: (dict) -> VideoFileMetadata
        if data['mediaType'] != MediaType.video:
            raise ValueError('not video metadata')

        file_descriptor = FileDescriptor.deserialize(data['fileDescriptor'])
        basic = VideoBasic.deserialize(
            data.get('basic')) if data.get('basic') else None

        return VideoFileMetadata(file_descriptor, basic)
    def deserialize(cls, data):
        # type: (dict) -> AudioFileMetadata
        if data['mediaType'] != MediaType.audio:
            raise ValueError('not audio metadata')

        file_descriptor = FileDescriptor.deserialize(data['fileDescriptor'])
        basic = AudioBasic.deserialize(
            data.get('basic')) if data.get('basic') else None
        extra = AudioExtra.deserialize(
            data.get('extra')) if data.get('extra') else None

        return AudioFileMetadata(file_descriptor, basic, extra)
Beispiel #13
0
    def deserialize(cls, data):
        # type: (dict) -> ImageFileMetadata
        if data['mediaType'] != MediaType.image:
            raise ValueError('not image metadata')

        file_descriptor = FileDescriptor.deserialize(data['fileDescriptor'])
        basic = ImageBasic.deserialize(
            data.get('basic')) if data.get('basic') else None
        features = ImageFeatures.deserialize(
            data.get('features')) if data.get('features') else None

        return ImageFileMetadata(file_descriptor, basic, features)
    def test_deserialize(self):
        file_data = {
            'id': 'moshe',
            'path': '/client/images/animals/cat.jpg',
            'type': FileType.file,
            'mimeType': 'image/jpg',
            'size': 15431,
            'acl': ACL.private,
            'hash': 'bulldog'
        }

        file_descriptor = FileDescriptor.deserialize(file_data)

        assert_that(file_descriptor.id, is_(file_data['id']))
        assert_that(file_descriptor.path, is_(file_data['path']))
    def test_upload_file_v2_request(self):
        url_response_body = RestResult(
            0, 'OK', {
                'uploadToken': 'token',
                'uploadUrl': 'https://fish.barrel/v2/cryptic-path'
            })
        httpretty.register_uri(
            httpretty.POST,
            'https://fish.barrel/_api/v2/upload/configuration',
            body=json.dumps(url_response_body.serialize()))

        upload_mime_type = 'text/plain'
        upload_response_body = RestResult(
            0, 'OK',
            FileDescriptor('/fish.txt', 'file-id', FileType.file,
                           upload_mime_type, 123).serialize())
        httpretty.register_uri(httpretty.POST,
                               'https://fish.barrel/v2/cryptic-path',
                               body=json.dumps(
                                   upload_response_body.serialize()))

        upload_content = 'some content'
        upload_lifecycle = Lifecycle(age=30, action=Action.delete)
        file_descriptor = self.file_service.upload_file_v2_request() \
            .set_acl(ACL.private).set_path('/fish.txt').set_content(upload_content).set_mime_type(upload_mime_type) \
            .set_lifecycle(upload_lifecycle).set_filename('fishenzon').execute()

        assert_that(file_descriptor, instance_of(FileDescriptor))
        assert_that(file_descriptor.path, is_('/fish.txt'))

        request_body = httpretty.last_request().body.decode('utf-8')

        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="uploadToken"\r\n\r\n%s'
                % 'token'))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="lifecycle"\r\n\r\n%s' %
                json.dumps(upload_lifecycle.serialize())))
        assert_that(
            request_body,
            contains_string(
                'Content-Disposition: form-data; name="file"; filename="fishenzon"\r\n'
                'Content-Type: %s\r\n\r\n%s' %
                (upload_mime_type, upload_content)))
    def deserialize(cls, data):
        # type: (dict) -> Operation
        o = Component.deserialize(data)
        o.__class__ = cls

        o.status = data['status']
        o.results = [
            FileDescriptor.deserialize(r) for r in data.get('results', [])
        ]
        o.jobs = data.get('jobs', [])
        o.extra_results = data.get('extraResults', {})
        o.error_message = data.get('errorMessage')
        o.error_code = data.get('errorCode')
        o.state_id = data.get('stateId')
        o.component_key = data.get('componentKey')
        return o
    def test_file_request(self):
        payload = FileDescriptor('/fish.txt', 'file-id', FileType.file,
                                 'text/plain', 123).serialize()
        response_body = RestResult(0, 'OK', payload)
        httpretty.register_uri(
            httpretty.GET,
            'https://fish.barrel/_api/files?path=%2Ffist.txt',
            body=json.dumps(response_body.serialize()))

        file_descriptor = self.file_service.file_request().set_path(
            '/fish.txt').execute()

        assert_that(file_descriptor.serialize(), is_(payload))
        assert_that(file_descriptor, instance_of(FileDescriptor))
        assert_that(httpretty.last_request().querystring,
                    is_({'path': ['/fish.txt']}))
    def _validate_values(path, directory, acl):
        # type: (str, str, ACL) -> None

        if not (path or directory):
            raise ValueError('path or directory must be specified')

        FileDescriptor.acl_validator(acl)

        if path:
            FileDescriptor.path_validator(path)

        if directory:
            FileDescriptor.path_validator(directory)
    def test_update_file_request(self):
        payload = FileDescriptor('/fish.txt', 'file-id', FileType.file,
                                 'text/plain', 18).serialize()
        response_body = RestResult(0, 'OK', payload)
        httpretty.register_uri(httpretty.PUT,
                               'https://fish.barrel/_api/files',
                               body=json.dumps(response_body.serialize()))

        file_descriptor = self.file_service.update_file_request().set_path(
            '/fish.txt').set_acl(ACL.public).execute()

        assert_that(file_descriptor.serialize(), is_(payload))
        assert_that(file_descriptor, instance_of(FileDescriptor))
        assert_that(json.loads(httpretty.last_request().body),
                    is_({
                        'path': '/fish.txt',
                        'id': None,
                        'acl': 'public'
                    }))
    def test_serialize(self):
        file_data = {
            'path': '/client/images/animals/cat.jpg',
            'type': FileType.file,
            'mimeType': 'image/jpg',
            'size': 15431,
            'acl': ACL.private,
            'hash': '324234234',
            'id': '123456789',
            'lifecycle': {
                'age': 500,
                'action': 'delete'
            },
            'dateUpdated': '2017-01-01T00:00:00Z',
            'dateCreated': '2017-01-01T00:00:00Z'
        }

        file_descriptor = FileDescriptor.deserialize(file_data)

        assert_that(file_descriptor.serialize(), is_(file_data))
    def test_create_file_request(self):
        payload = FileDescriptor('/fish', 'file-id', FileType.directory,
                                 FileMimeType.directory, 0).serialize()
        response_body = RestResult(0, 'OK', payload)
        httpretty.register_uri(httpretty.POST,
                               'https://fish.barrel/_api/files',
                               body=json.dumps(response_body.serialize()))

        file_descriptor = self.file_service.create_file_request().set_path(
            '/fish').execute()

        assert_that(file_descriptor.serialize(), is_(payload))
        assert_that(file_descriptor, instance_of(FileDescriptor))
        assert_that(
            json.loads(httpretty.last_request().body),
            is_({
                'mimeType': FileMimeType.directory,
                'path': '/fish',
                'size': 0,
                'type': FileType.directory,
                'acl': ACL.public,
                'id': None,
                'bucket': None
            }))
Beispiel #22
0
    def deserialize(cls, data):
        # type: (dict) -> FileMetadata
        file_descriptor = FileDescriptor.deserialize(data['fileDescriptor'])

        return FileMetadata(MediaType.undefined, file_descriptor)
Beispiel #23
0
 def validate(self):
     FileDescriptor.path_validator(self.path)
     FileDescriptor.acl_validator(self.acl)
Beispiel #24
0
 def deserialize(cls, data):
     return _UploadFileResponse(
         [FileDescriptor.deserialize(item) for item in data])
Beispiel #25
0
 def deserialize(cls, data):
     # type: (dict) -> _CreateFilesResponse
     return cls([FileDescriptor.deserialize(f) for f in data['files']])
Beispiel #26
0
    def deserialize(cls, data):
        # type: (dict) -> FileList

        files = [FileDescriptor.deserialize(f) for f in data['files']]

        return FileList(data['nextPageToken'], files)