mirror of
https://github.com/bringout/oca-storage.git
synced 2026-04-18 16:12:09 +02:00
Initial commit: OCA Storage packages (17 packages)
This commit is contained in:
commit
7a380f05d3
659 changed files with 41828 additions and 0 deletions
|
|
@ -0,0 +1,2 @@
|
|||
from . import common
|
||||
from . import test_fs_storage
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
# Copyright 2023 ACSONE SA/NV (http://acsone.eu).
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
|
||||
import base64
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
from odoo.tests.common import TransactionCase
|
||||
|
||||
from ..models.fs_storage import FSStorage
|
||||
|
||||
|
||||
class TestFSStorageCase(TransactionCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
|
||||
cls.backend: FSStorage = cls.env.ref("fs_storage.default_fs_storage")
|
||||
cls.backend.json_options = {"target_options": {"auto_mkdir": "True"}}
|
||||
cls.filedata = base64.b64encode(b"This is a simple file")
|
||||
cls.filename = "test_file.txt"
|
||||
cls.case_with_subdirectory = "subdirectory/here"
|
||||
cls.demo_user = cls.env.ref("base.user_demo")
|
||||
cls.temp_dir = tempfile.mkdtemp()
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
mocked_backend = mock.patch.object(
|
||||
self.backend.__class__, "_get_filesystem_storage_path"
|
||||
)
|
||||
mocked_get_filesystem_storage_path = mocked_backend.start()
|
||||
mocked_get_filesystem_storage_path.return_value = self.temp_dir
|
||||
self.backend.write({"directory_path": self.temp_dir})
|
||||
|
||||
# pylint: disable=unused-variable
|
||||
@self.addCleanup
|
||||
def stop_mock():
|
||||
mocked_backend.stop()
|
||||
# recursively delete the tempdir
|
||||
if os.path.exists(self.temp_dir):
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
def _create_file(self, backend: FSStorage, filename: str, filedata: str):
|
||||
with backend.fs.open(filename, "wb") as f:
|
||||
f.write(filedata)
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
# Copyright 2023 ACSONE SA/NV (http://acsone.eu).
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
|
||||
import warnings
|
||||
from unittest import mock
|
||||
|
||||
from odoo.tests import Form
|
||||
from odoo.tools import mute_logger
|
||||
|
||||
from .common import TestFSStorageCase
|
||||
|
||||
|
||||
class TestFSStorage(TestFSStorageCase):
|
||||
@mute_logger("py.warnings")
|
||||
def _test_deprecated_setting_and_getting_data(self):
|
||||
# Check that the directory is empty
|
||||
warnings.filterwarnings("ignore")
|
||||
files = self.backend.list_files()
|
||||
self.assertNotIn(self.filename, files)
|
||||
|
||||
# Add a new file
|
||||
self.backend.add(
|
||||
self.filename, self.filedata, mimetype="text/plain", binary=False
|
||||
)
|
||||
|
||||
# Check that the file exist
|
||||
files = self.backend.list_files()
|
||||
self.assertIn(self.filename, files)
|
||||
|
||||
# Retrieve the file added
|
||||
data = self.backend.get(self.filename, binary=False)
|
||||
self.assertEqual(data, self.filedata)
|
||||
|
||||
# Delete the file
|
||||
self.backend.delete(self.filename)
|
||||
files = self.backend.list_files()
|
||||
self.assertNotIn(self.filename, files)
|
||||
|
||||
@mute_logger("py.warnings")
|
||||
def _test_deprecated_find_files(self):
|
||||
warnings.filterwarnings("ignore")
|
||||
self.backend.add(
|
||||
self.filename, self.filedata, mimetype="text/plain", binary=False
|
||||
)
|
||||
try:
|
||||
res = self.backend.find_files(r".*\.txt")
|
||||
self.assertListEqual([self.filename], res)
|
||||
res = self.backend.find_files(r".*\.text")
|
||||
self.assertListEqual([], res)
|
||||
finally:
|
||||
self.backend.delete(self.filename)
|
||||
|
||||
def test_deprecated_setting_and_getting_data_from_root(self):
|
||||
self._test_deprecated_setting_and_getting_data()
|
||||
|
||||
def test_deprecated_setting_and_getting_data_from_dir(self):
|
||||
self.backend.directory_path = self.case_with_subdirectory
|
||||
self._test_deprecated_setting_and_getting_data()
|
||||
|
||||
def test_deprecated_find_files_from_root(self):
|
||||
self._test_deprecated_find_files()
|
||||
|
||||
def test_deprecated_find_files_from_dir(self):
|
||||
self.backend.directory_path = self.case_with_subdirectory
|
||||
self._test_deprecated_find_files()
|
||||
|
||||
def test_ensure_one_fs_by_record(self):
|
||||
# in this test we ensure that we've one fs by record
|
||||
backend_ids = []
|
||||
for i in range(4):
|
||||
backend_ids.append(
|
||||
self.backend.create(
|
||||
{"name": f"name{i}", "directory_path": f"{i}", "code": f"code{i}"}
|
||||
).id
|
||||
)
|
||||
records = self.backend.browse(backend_ids)
|
||||
fs = None
|
||||
for rec in records:
|
||||
self.assertNotEqual(fs, rec.fs)
|
||||
|
||||
def test_relative_access(self):
|
||||
self.backend.directory_path = self.case_with_subdirectory
|
||||
self._create_file(self.backend, self.filename, self.filedata)
|
||||
other_subdirectory = "other_subdirectory"
|
||||
backend2 = self.backend.copy({"directory_path": other_subdirectory})
|
||||
self._create_file(backend2, self.filename, self.filedata)
|
||||
with self.assertRaises(PermissionError), self.env.cr.savepoint():
|
||||
# check that we can't access outside the subdirectory
|
||||
backend2.fs.ls("../")
|
||||
with self.assertRaises(PermissionError), self.env.cr.savepoint():
|
||||
# check that we can't access the file into another subdirectory
|
||||
backend2.fs.ls(f"../{self.case_with_subdirectory}")
|
||||
self.backend.fs.rm_file(self.filename)
|
||||
backend2.fs.rm_file(self.filename)
|
||||
|
||||
def test_recursive_add_odoo_storage_path_to_options(self):
|
||||
options = {
|
||||
"directory_path": "/tmp/my_backend",
|
||||
"target_protocol": "odoofs",
|
||||
}
|
||||
self.backend._recursive_add_odoo_storage_path(options)
|
||||
self.assertEqual(
|
||||
self.backend._odoo_storage_path,
|
||||
options.get("target_options").get("odoo_storage_path"),
|
||||
)
|
||||
options = {
|
||||
"directory_path": "/tmp/my_backend",
|
||||
"target_protocol": "dir",
|
||||
"target_options": {
|
||||
"path": "/my_backend",
|
||||
"target_protocol": "odoofs",
|
||||
},
|
||||
}
|
||||
self.backend._recursive_add_odoo_storage_path(options)
|
||||
self.assertEqual(
|
||||
self.backend._odoo_storage_path,
|
||||
options.get("target_options")
|
||||
.get("target_options")
|
||||
.get("odoo_storage_path"),
|
||||
)
|
||||
|
||||
def test_interface_values(self):
|
||||
protocol = "file" # should be available by default in the list of protocols
|
||||
with Form(self.env["fs.storage"]) as new_storage:
|
||||
new_storage.name = "Test storage"
|
||||
new_storage.code = "code"
|
||||
new_storage.protocol = protocol
|
||||
self.assertEqual(new_storage.protocol, protocol)
|
||||
# the options should follow the protocol
|
||||
self.assertEqual(new_storage.options_protocol, protocol)
|
||||
description = new_storage.protocol_descr
|
||||
self.assertTrue("Interface to files on local storage" in description)
|
||||
# this is still true after saving
|
||||
self.assertEqual(new_storage.options_protocol, protocol)
|
||||
|
||||
def test_options_env(self):
|
||||
self.backend.json_options = {"key": {"sub_key": "$KEY_VAR"}}
|
||||
eval_json_options = {"key": {"sub_key": "TEST"}}
|
||||
options = self.backend._get_fs_options()
|
||||
self.assertDictEqual(options, self.backend.json_options)
|
||||
self.backend.eval_options_from_env = True
|
||||
with mock.patch.dict("os.environ", {"KEY_VAR": "TEST"}):
|
||||
options = self.backend._get_fs_options()
|
||||
self.assertDictEqual(options, eval_json_options)
|
||||
with self.assertLogs(level="WARNING") as log:
|
||||
options = self.backend._get_fs_options()
|
||||
self.assertIn(
|
||||
(
|
||||
f"Environment variable KEY_VAR is not set for "
|
||||
f"fs_storage {self.backend.display_name}."
|
||||
),
|
||||
log.output[0],
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue