def test_wrong_dif(self): content1 = 'foo'.encode('utf-8') fileobj1 = ContentFile(content1) content2 = 'bar'.encode('utf-8') fileobj2 = ContentFile(content2) content3 = 'baz'.encode('utf-8') fileobj3 = ContentFile(content3) total_checksum = sha1(content2 + content1 + content3).hexdigest() # The order here is on purpose because we check for the order of checksums blob1 = FileBlob.from_file(fileobj1) blob3 = FileBlob.from_file(fileobj3) blob2 = FileBlob.from_file(fileobj2) chunks = [blob2.checksum, blob1.checksum, blob3.checksum] assemble_dif( project_id=self.project.id, name='foo.sym', checksum=total_checksum, chunks=chunks, ) assert get_assemble_status(self.project, total_checksum)[0] == ChunkFileState.ERROR
def test_assemble(self, mock_assemble_dif): content1 = 'foo'.encode('utf-8') fileobj1 = ContentFile(content1) checksum1 = sha1(content1).hexdigest() content2 = 'bar'.encode('utf-8') fileobj2 = ContentFile(content2) checksum2 = sha1(content2).hexdigest() content3 = 'baz'.encode('utf-8') fileobj3 = ContentFile(content3) checksum3 = sha1(content3).hexdigest() total_checksum = sha1(content2 + content1 + content3).hexdigest() # The order here is on purpose because we check for the order of checksums blob1 = FileBlob.from_file(fileobj1) FileBlobOwner.objects.get_or_create( organization=self.organization, blob=blob1 ) blob3 = FileBlob.from_file(fileobj3) FileBlobOwner.objects.get_or_create( organization=self.organization, blob=blob3 ) blob2 = FileBlob.from_file(fileobj2) # we make a request now but we are missing ownership for chunk 2 response = self.client.post( self.url, data={ total_checksum: { 'name': 'test', 'chunks': [ checksum2, checksum1, checksum3 ] } }, HTTP_AUTHORIZATION=u'Bearer {}'.format(self.token.token) ) assert response.status_code == 200, response.content assert response.data[total_checksum]['state'] == ChunkFileState.NOT_FOUND assert response.data[total_checksum]['missingChunks'] == [checksum2] # we add ownership to chunk 2 FileBlobOwner.objects.get_or_create( organization=self.organization, blob=blob2 ) # new request, ownership for all chunks is there but file does not exist yet response = self.client.post( self.url, data={ total_checksum: { 'name': 'test', 'chunks': [ checksum2, checksum1, checksum3 ], } }, HTTP_AUTHORIZATION=u'Bearer {}'.format(self.token.token) ) assert response.status_code == 200, response.content assert response.data[total_checksum]['state'] == ChunkFileState.CREATED assert response.data[total_checksum]['missingChunks'] == [] chunks = [checksum2, checksum1, checksum3] mock_assemble_dif.apply_async.assert_called_once_with( kwargs={ 'project_id': self.project.id, 'name': 'test', 'chunks': chunks, 'checksum': total_checksum, } ) file = assemble_file(self.project, 'test', total_checksum, chunks, 'project.dif')[0] assert get_assemble_status(self.project, total_checksum)[0] != ChunkFileState.ERROR assert file.checksum == total_checksum file_blob_index = FileBlobIndex.objects.all() assert len(file_blob_index) == 3
def test_assemble(self, mock_assemble_dif): content1 = 'foo'.encode('utf-8') fileobj1 = ContentFile(content1) checksum1 = sha1(content1).hexdigest() content2 = 'bar'.encode('utf-8') fileobj2 = ContentFile(content2) checksum2 = sha1(content2).hexdigest() content3 = 'baz'.encode('utf-8') fileobj3 = ContentFile(content3) checksum3 = sha1(content3).hexdigest() total_checksum = sha1(content2 + content1 + content3).hexdigest() # The order here is on purpose because we check for the order of checksums blob1 = FileBlob.from_file(fileobj1) FileBlobOwner.objects.get_or_create(organization=self.organization, blob=blob1) blob3 = FileBlob.from_file(fileobj3) FileBlobOwner.objects.get_or_create(organization=self.organization, blob=blob3) blob2 = FileBlob.from_file(fileobj2) # we make a request now but we are missing ownership for chunk 2 response = self.client.post( self.url, data={ total_checksum: { 'name': 'test', 'chunks': [checksum2, checksum1, checksum3] } }, HTTP_AUTHORIZATION=u'Bearer {}'.format(self.token.token)) assert response.status_code == 200, response.content assert response.data[total_checksum][ 'state'] == ChunkFileState.NOT_FOUND assert response.data[total_checksum]['missingChunks'] == [checksum2] # we add ownership to chunk 2 FileBlobOwner.objects.get_or_create(organization=self.organization, blob=blob2) # new request, ownership for all chunks is there but file does not exist yet response = self.client.post( self.url, data={ total_checksum: { 'name': 'test', 'chunks': [checksum2, checksum1, checksum3], } }, HTTP_AUTHORIZATION=u'Bearer {}'.format(self.token.token)) assert response.status_code == 200, response.content assert response.data[total_checksum]['state'] == ChunkFileState.CREATED assert response.data[total_checksum]['missingChunks'] == [] chunks = [checksum2, checksum1, checksum3] mock_assemble_dif.apply_async.assert_called_once_with( kwargs={ 'project_id': self.project.id, 'name': 'test', 'chunks': chunks, 'checksum': total_checksum, }) file = assemble_file(self.project, 'test', total_checksum, chunks, 'project.dif')[0] assert get_assemble_status(self.project, total_checksum)[0] != ChunkFileState.ERROR assert file.checksum == total_checksum file_blob_index = FileBlobIndex.objects.all() assert len(file_blob_index) == 3