Ejemplo n.º 1
0
def test_upload_resume(server):
    close_server = threading.Event()
    with SwapAttr(server.client, 'host', 'http://localhost:8081/nuxeo/'):
        try:
            serv = Server.upload_response_server(
                wait_to_close_event=close_server,
                port=8081,
                requests_to_handle=20,
                fail_args={
                    'fail_at': 4,
                    'fail_number': 3
                })
            file_in = 'test_in'

            with serv:
                batch = server.uploads.batch()
                with open(file_in, 'wb') as f:
                    f.write(b'\x00' + os.urandom(1024 * 1024) + b'\x00')
                blob = FileBlob(file_in, mimetype='application/octet-stream')
                with pytest.raises(UploadError) as e:
                    batch.upload(blob, chunked=True, chunk_size=256 * 1024)
                assert text(e.value)
                batch.upload(blob, chunked=True, chunk_size=256 * 1024)
                close_server.set()  # release server block

        finally:
            try:
                os.remove(file_in)
            except OSError:
                pass
Ejemplo n.º 2
0
def test_upload_resume(server):
    close_server = threading.Event()
    with SwapAttr(server.client, 'host', 'http://localhost:8081/nuxeo/'):
        try:
            serv = Server.upload_response_server(
                wait_to_close_event=close_server,
                port=8081,
                requests_to_handle=20,
                fail_args={'fail_at': 4, 'fail_number': 3}
            )
            file_in = 'test_in'

            with serv:
                batch = server.uploads.batch()
                with open(file_in, 'wb') as f:
                    f.write(b'\x00' + os.urandom(1024 * 1024) + b'\x00')
                blob = FileBlob(file_in, mimetype='application/octet-stream')
                with pytest.raises(UploadError) as e:
                    batch.upload(blob, chunked=True, chunk_size=256 * 1024)
                assert text(e.value)
                batch.upload(blob, chunked=True, chunk_size=256 * 1024)
                close_server.set()  # release server block

        finally:
            try:
                os.remove(file_in)
            except OSError:
                pass
Ejemplo n.º 3
0
def test_document_get_child_unknown(server):
    operation = server.operations.new('Document.GetChild')
    operation.params = {'name': 'Workspaces'}
    operation.input_obj = '/default-domain'
    with pytest.raises(HTTPError) as e:
        operation.execute()
    assert text(e.value)
    assert e.value.status == 404
Ejemplo n.º 4
0
def test_cancel(server):
    batch = get_batch(server)
    batch.cancel()
    assert batch.uid is None
    batch.cancel()
    with pytest.raises(InvalidBatch) as e:
        batch.get(0)
    assert text(e.value)
    batch.delete(0)
Ejemplo n.º 5
0
def test_cancel(server):
    batch = get_batch(server)
    batch.cancel()
    assert batch.uid is None
    batch.cancel()
    with pytest.raises(InvalidBatch) as e:
        batch.get(0)
    assert text(e.value)
    batch.delete(0)
Ejemplo n.º 6
0
def test_update_group(server):
    group = get_group(server)
    try:
        grouplabel = text(int(round(time.time() * 1000)))
        group.grouplabel = grouplabel
        group.save()
        group = server.groups.get('plops')
        assert group.grouplabel == grouplabel
    finally:
        group.delete()
Ejemplo n.º 7
0
def test_convert_unavailable(server, monkeypatch):
    def raise_convert(api, uid, options):
        raise UnavailableConvertor(options)
    monkeypatch.setattr('nuxeo.documents.API.convert', raise_convert)

    with Doc(server, with_blob=True) as doc:
        with pytest.raises(UnavailableConvertor) as e:
            doc.convert({'converter': 'office2html'})
        assert text(e.value)
        msg = e.value.message
        assert msg.startswith('UnavailableConvertor: conversion with options')
        assert msg.endswith('is not available')
Ejemplo n.º 8
0
def handle_nuxeo_request(request_content):
    method, path, headers = parse_nuxeo_request(request_content)
    try:
        if len(path) == 1 and path[0] == 'upload':
            if method == 'POST':
                # Create batch
                batch_id = text(uuid.uuid4())
                uploads[batch_id] = {}
                return generate_response('201 Created', {'batchId': batch_id})
        if len(path) == 3 and path[0] == 'upload':
            batch_id, file_idx = path[1:]
            if method == 'GET':
                # Get blob completeness
                blob = uploads[batch_id][file_idx]

                if len(blob['uploadedChunkIds']) < int(blob['chunkCount']):
                    return generate_response('308 Resume Incomplete', blob)
                else:
                    return generate_response('200 OK', blob)
            if method == 'POST':
                # Upload blob
                upload_type = headers.get('X-Upload-Type', 'normal')
                if upload_type == 'normal':
                    blob = {
                        'name': headers['X-File-Name'],
                        'size': headers['Content-Length'],
                        'uploadType': upload_type
                    }
                    uploads[batch_id][file_idx] = blob
                    return generate_response('200 OK', blob)

                blob = uploads.get(batch_id, {}).get(file_idx, None)
                if not blob:
                    blob = {
                        'batchId': batch_id,
                        'fileIdx': file_idx,
                        'uploadType': upload_type,
                        'uploadedChunkIds': [],
                        'uploadedSize': headers['Content-Length'],
                        'chunkCount': headers['X-Upload-Chunk-Count']
                    }
                chunk_idx = headers['X-Upload-Chunk-Index']
                if chunk_idx not in blob['uploadedChunkIds']:
                    blob['uploadedChunkIds'].append(chunk_idx)
                uploads[batch_id][file_idx] = blob

                if len(blob['uploadedChunkIds']) < int(blob['chunkCount']):
                    return generate_response('308 Resume Incomplete', blob)
                else:
                    return generate_response('201 Created', blob)
    except Exception:
        return generate_response('404 Not Found')
Ejemplo n.º 9
0
def test_digester(hash, is_valid, server):
    file_out = 'test_out'
    doc = server.documents.create(new_doc, parent_path=pytest.ws_root_path)
    try:
        batch = get_batch(server)
        operation = server.operations.new('Blob.AttachOnDocument')
        operation.params = {'document': pytest.ws_root_path + '/Document'}
        operation.input_obj = batch.get(0)
        operation.execute(void_op=True)

        operation = server.operations.new('Blob.Get')
        operation.input_obj = pytest.ws_root_path + '/Document'
        if is_valid:
            operation.execute(file_out=file_out, digest=hash)
        else:
            with pytest.raises(CorruptedFile) as e:
                operation.execute(file_out=file_out, digest=hash)
            assert text(e.value)
    finally:
        doc.delete()
        os.remove(file_out)
Ejemplo n.º 10
0
def test_digester(hash, is_valid, server):
    file_out = 'test_out'
    doc = server.documents.create(new_doc, parent_path=pytest.ws_root_path)
    try:
        batch = get_batch(server)
        operation = server.operations.new('Blob.AttachOnDocument')
        operation.params = {'document': pytest.ws_root_path + '/Document'}
        operation.input_obj = batch.get(0)
        operation.execute(void_op=True)

        operation = server.operations.new('Blob.Get')
        operation.input_obj = pytest.ws_root_path + '/Document'
        if is_valid:
            operation.execute(file_out=file_out, digest=hash)
        else:
            with pytest.raises(CorruptedFile) as e:
                operation.execute(file_out=file_out, digest=hash)
            assert text(e.value)
    finally:
        doc.delete()
        os.remove(file_out)
Ejemplo n.º 11
0
def test_unauthorized(server):
    username = '******'
    password = '******'
    user = server.users.create(
        User(properties={
            'username': username,
            'password': password
        }))

    auth = server.client.auth
    server.client.auth = (get_bytes(username), password)
    try:
        with pytest.raises(Unauthorized) as e:
            server.users.create(
                User(properties={
                    'username': '******',
                    'password': '******'
                }))
        assert text(e.value)
    finally:
        server.client.auth = auth
        user.delete()
Ejemplo n.º 12
0
def test_task_transfer(tasks):
    task = Task()
    with pytest.raises(BadQuery) as e:
        tasks.transfer(task, 'bogus_transfer', {})
    msg = text(e.value)
    assert msg == 'Task transfer must be either delegate or reassign.'
Ejemplo n.º 13
0
def test_task_transfer(tasks):
    task = Task()
    with pytest.raises(BadQuery) as e:
        tasks.transfer(task, 'bogus_transfer', {})
    msg = text(e.value)
    assert msg == 'Task transfer must be either delegate or reassign.'