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_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_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_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 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_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 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'
                ))
    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 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 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_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
            }))