def test_remove_chunked_upload(self):
        uri = '/chunked_upload'
        chunk = os.urandom(random.randint(12, 64))
        with app.test_request_context(uri, method='PUT', data=chunk):
            fs.register_chunk(self.id_, request.stream)
        with app.test_request_context(uri, method='PUT', data=chunk):
            fs.remove_chunked_upload(self.id_)

        test_path = os.path.join(fs.storage_root, config.chunk_storage)
        self.assertTrue(os.path.exists(test_path))
        test_path = os.path.join(test_path, self.id_[:2])
        self.assertTrue(os.path.exists(test_path))
        test_path = os.path.join(test_path, self.id_[2:])
        self.assertFalse(os.path.exists(test_path))
    def setUp(self):
        fs.storage_root = tempfile.mkdtemp()
        fs.initialize()

        uri = '/files/%s/%s' % (self.root, self.path)
        self.data = os.urandom(64)
        self.ctx = app.test_request_context(uri, method='PUT', data=self.data)
        self.ctx.push()
        self.stream = stream.ChecksumCalcStream(request.stream)
        self.hash = self.stream.hash
    def test_register_chunk(self):
        data = os.urandom(64)
        uri = '/chunked_upload'
        with app.test_request_context(uri, method='PUT', data=data):
            offset = fs.register_chunk(self.id_, request.stream)
            self.assertEqual(offset, len(data))

        test_path = os.path.join(fs.storage_root, config.chunk_storage)
        self.assertTrue(os.path.exists(test_path))
        test_path = os.path.join(test_path, self.id_[:2])
        self.assertTrue(os.path.exists(test_path))
        test_path = os.path.join(test_path, self.id_[2:])
        self.assertTrue(os.path.exists(test_path))

        with open(test_path) as f:
            self.assertEqual(f.read(), data)
 def test_hasher(self):
     data = os.urandom(64)
     with app.test_request_context('', method='PUT', data=data):
         s = stream.ChecksumCalcStream(request.stream)
         f_hash = fs.register_blob('root', 'f', s, s.hash)
         self.assertEqual(f_hash, hashlib.sha1(data).hexdigest())