示例#1
0
 def test_build_sha256_path_raises_IrmaValueError(self):
     sha = str()
     with self.assertRaises(IrmaValueError) as context:
         module.build_sha256_path(sha)
     self.assertEqual(str(context.exception),
                      "too much prefix for file storage")
     self.assertTrue(module.config.get_samples_storage_path.called)
     self.assertFalse(module.os.path.join.called)
     self.assertFalse(module.os.path.exists.called)
     self.assertFalse(module.os.makedirs.called)
示例#2
0
 def test_build_sha256_path_raises_IrmaFileSystemError(self):
     sha = "1234567890"
     module.os.path.isdir.return_value = False
     with self.assertRaises(IrmaFileSystemError) as context:
         module.build_sha256_path(sha)
     self.assertEqual(str(context.exception),
                      "storage path is not a directory")
     self.assertTrue(module.config.get_samples_storage_path.called)
     self.assertEqual(module.os.path.join.call_count, 3)
     self.assertEqual(module.os.path.exists.call_count, 1)
     self.assertFalse(module.os.makedirs.called)
示例#3
0
 def test_build_sha256_path_ok(self):
     sha = "1234567890"
     result = module.build_sha256_path(sha)
     self.assertTrue(module.config.get_samples_storage_path.called)
     self.assertEqual(module.os.path.join.call_count, 4)
     self.assertEqual(module.os.path.exists.call_count, 1)
     self.assertEqual(module.os.path.join.call_args,
                      ((module.os.path.join(), sha), ))
     self.assertFalse(module.os.makedirs.called)
     self.assertEqual(result, module.os.path.join())
示例#4
0
    def get_or_create(cls, fileobj, session):
        """ Retrieve a File if already present in DB otherwise
        create a new entry.

        :param fileobj: file-like object containing file data
        :param session: database session
        :return: File object
        """
        sha256 = sha256sum(fileobj)
        # split files between subdirs
        path = build_sha256_path(sha256)
        try:
            # The file exists
            log.debug("try opening file with sha256: %s", sha256)
            file = File.load_from_sha256(sha256, session)
            if file.path is None:
                log.debug("file sample missing writing it")
                save_to_file(fileobj, path)
                file.path = path
        except IrmaDatabaseResultNotFound:
            # It doesn't
            file = cls.create(fileobj, sha256, path, session)
        return file