mirror of
https://github.com/bringout/oca-storage.git
synced 2026-04-18 14:32:03 +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_filesystem
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
# Copyright 2017 Akretion (http://www.akretion.com).
|
||||
# @author Sébastien BEAU <sebastien.beau@akretion.com>
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
import base64
|
||||
from unittest import mock
|
||||
|
||||
from odoo.addons.component.tests.common import TransactionComponentCase
|
||||
|
||||
|
||||
class BackendStorageTestMixin(object):
|
||||
def _test_setting_and_getting_data(self):
|
||||
# Check that the directory is empty
|
||||
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)
|
||||
|
||||
def _test_setting_and_getting_data_from_root(self):
|
||||
self._test_setting_and_getting_data()
|
||||
|
||||
def _test_setting_and_getting_data_from_dir(self):
|
||||
self.backend.directory_path = self.case_with_subdirectory
|
||||
self._test_setting_and_getting_data()
|
||||
|
||||
def _test_find_files(
|
||||
self,
|
||||
backend,
|
||||
adapter_dotted_path,
|
||||
mocked_filepaths,
|
||||
pattern,
|
||||
expected_filepaths,
|
||||
):
|
||||
with mock.patch(adapter_dotted_path + ".list") as mocked:
|
||||
mocked.return_value = mocked_filepaths
|
||||
res = backend.find_files(pattern)
|
||||
self.assertEqual(sorted(res), sorted(expected_filepaths))
|
||||
|
||||
def _test_move_files(
|
||||
self,
|
||||
backend,
|
||||
adapter_dotted_path,
|
||||
filename,
|
||||
destination_path,
|
||||
expected_filepaths,
|
||||
):
|
||||
with mock.patch(adapter_dotted_path + ".move_files") as mocked:
|
||||
mocked.return_value = expected_filepaths
|
||||
res = backend.move_files(filename, destination_path)
|
||||
self.assertEqual(sorted(res), sorted(expected_filepaths))
|
||||
|
||||
|
||||
class CommonCase(TransactionComponentCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
|
||||
cls.backend = cls.env.ref("storage_backend.default_storage_backend")
|
||||
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")
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
# Copyright 2017 Akretion (http://www.akretion.com).
|
||||
# @author Sébastien BEAU <sebastien.beau@akretion.com>
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
import os
|
||||
|
||||
from odoo.exceptions import AccessError
|
||||
|
||||
from .common import BackendStorageTestMixin, CommonCase
|
||||
|
||||
ADAPTER_PATH = (
|
||||
"odoo.addons.storage_backend.components.filesystem_adapter.FileSystemStorageBackend"
|
||||
)
|
||||
|
||||
|
||||
class FileSystemCase(CommonCase, BackendStorageTestMixin):
|
||||
def test_setting_and_getting_data_from_root(self):
|
||||
self._test_setting_and_getting_data_from_root()
|
||||
|
||||
def test_setting_and_getting_data_from_dir(self):
|
||||
self._test_setting_and_getting_data_from_dir()
|
||||
|
||||
def test_find_files(self):
|
||||
good_filepaths = ["somepath/file%d.good" % x for x in range(1, 10)]
|
||||
bad_filepaths = ["somepath/file%d.bad" % x for x in range(1, 10)]
|
||||
mocked_filepaths = bad_filepaths + good_filepaths
|
||||
backend = self.backend.sudo()
|
||||
base_dir = backend._get_adapter()._basedir()
|
||||
expected = [base_dir + "/" + path for path in good_filepaths]
|
||||
self._test_find_files(
|
||||
backend, ADAPTER_PATH, mocked_filepaths, r".*\.good$", expected
|
||||
)
|
||||
|
||||
def test_move_files(self):
|
||||
backend = self.backend.sudo()
|
||||
base_dir = backend._get_adapter()._basedir()
|
||||
expected = [base_dir + "/" + self.filename]
|
||||
destination_path = os.path.join(base_dir, "destination")
|
||||
self._test_move_files(
|
||||
backend, ADAPTER_PATH, self.filename, destination_path, expected
|
||||
)
|
||||
|
||||
|
||||
class FileSystemDemoUserAccessCase(CommonCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.backend = cls.backend.with_user(cls.demo_user)
|
||||
|
||||
def test_cannot_add_file(self):
|
||||
with self.assertRaises(AccessError):
|
||||
self.backend.add(
|
||||
self.filename, self.filedata, mimetype="text/plain", binary=False
|
||||
)
|
||||
|
||||
def test_cannot_list_file(self):
|
||||
with self.assertRaises(AccessError):
|
||||
self.backend.list_files()
|
||||
|
||||
def test_cannot_read_file(self):
|
||||
with self.assertRaises(AccessError):
|
||||
self.backend.get(self.filename, binary=False)
|
||||
|
||||
def test_cannot_delete_file(self):
|
||||
with self.assertRaises(AccessError):
|
||||
self.backend.delete(self.filename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue