test_storage.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2022 Unrud <unrud@outlook.com>
  4. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Tests for storage backends.
  20. """
  21. import os
  22. import shutil
  23. from typing import ClassVar, cast
  24. import pytest
  25. import radicale.tests.custom.storage_simple_sync
  26. from radicale.tests import BaseTest
  27. from radicale.tests.helpers import get_file_content
  28. from radicale.tests.test_base import TestBaseRequests as _TestBaseRequests
  29. class TestMultiFileSystem(BaseTest):
  30. """Tests for multifilesystem."""
  31. def setup_method(self) -> None:
  32. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  33. self.configure({"storage": {"type": "multifilesystem"}})
  34. def test_folder_creation(self) -> None:
  35. """Verify that the folder is created."""
  36. folder = os.path.join(self.colpath, "subfolder")
  37. self.configure({"storage": {"filesystem_folder": folder}})
  38. assert os.path.isdir(folder)
  39. def test_fsync(self) -> None:
  40. """Create a directory and file with syncing enabled."""
  41. self.configure({"storage": {"_filesystem_fsync": "True"}})
  42. self.mkcalendar("/calendar.ics/")
  43. def test_hook(self) -> None:
  44. """Run hook."""
  45. self.configure({"storage": {"hook": "mkdir %s" % os.path.join(
  46. "collection-root", "created_by_hook")}})
  47. self.mkcalendar("/calendar.ics/")
  48. self.propfind("/created_by_hook/")
  49. def test_hook_read_access(self) -> None:
  50. """Verify that hook is not run for read accesses."""
  51. self.configure({"storage": {"hook": "mkdir %s" % os.path.join(
  52. "collection-root", "created_by_hook")}})
  53. self.propfind("/")
  54. self.propfind("/created_by_hook/", check=404)
  55. @pytest.mark.skipif(not shutil.which("flock"),
  56. reason="flock command not found")
  57. def test_hook_storage_locked(self) -> None:
  58. """Verify that the storage is locked when the hook runs."""
  59. self.configure({"storage": {"hook": (
  60. "flock -n .Radicale.lock || exit 0; exit 1")}})
  61. self.mkcalendar("/calendar.ics/")
  62. def test_hook_principal_collection_creation(self) -> None:
  63. """Verify that the hooks runs when a new user is created."""
  64. self.configure({"storage": {"hook": "mkdir %s" % os.path.join(
  65. "collection-root", "created_by_hook")}})
  66. self.propfind("/", login="user:")
  67. self.propfind("/created_by_hook/")
  68. def test_hook_fail(self) -> None:
  69. """Verify that a request succeeded if the hook still fails (anyhow no rollback implemented)."""
  70. self.configure({"storage": {"hook": "exit 1"}})
  71. self.mkcalendar("/calendar.ics/", check=201)
  72. def test_item_cache_rebuild(self) -> None:
  73. """Delete the item cache and verify that it is rebuild."""
  74. self.mkcalendar("/calendar.ics/")
  75. event = get_file_content("event1.ics")
  76. path = "/calendar.ics/event1.ics"
  77. self.put(path, event)
  78. _, answer1 = self.get(path)
  79. cache_folder = os.path.join(self.colpath, "collection-root",
  80. "calendar.ics", ".Radicale.cache", "item")
  81. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  82. shutil.rmtree(cache_folder)
  83. _, answer2 = self.get(path)
  84. assert answer1 == answer2
  85. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  86. def test_item_cache_rebuild_subfolder(self) -> None:
  87. """Delete the item cache and verify that it is rebuild."""
  88. self.configure({"storage": {"use_cache_subfolder_for_item": "True"}})
  89. self.mkcalendar("/calendar.ics/")
  90. event = get_file_content("event1.ics")
  91. path = "/calendar.ics/event1.ics"
  92. self.put(path, event)
  93. _, answer1 = self.get(path)
  94. cache_folder = os.path.join(self.colpath, "collection-cache",
  95. "calendar.ics", ".Radicale.cache", "item")
  96. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  97. shutil.rmtree(cache_folder)
  98. _, answer2 = self.get(path)
  99. assert answer1 == answer2
  100. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  101. def test_item_cache_rebuild_mtime_and_size(self) -> None:
  102. """Delete the item cache and verify that it is rebuild."""
  103. self.configure({"storage": {"use_mtime_and_size_for_item_cache": "True"}})
  104. self.mkcalendar("/calendar.ics/")
  105. event = get_file_content("event1.ics")
  106. path = "/calendar.ics/event1.ics"
  107. self.put(path, event)
  108. _, answer1 = self.get(path)
  109. cache_folder = os.path.join(self.colpath, "collection-root",
  110. "calendar.ics", ".Radicale.cache", "item")
  111. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  112. shutil.rmtree(cache_folder)
  113. _, answer2 = self.get(path)
  114. assert answer1 == answer2
  115. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  116. def test_put_whole_calendar_uids_used_as_file_names(self) -> None:
  117. """Test if UIDs are used as file names."""
  118. _TestBaseRequests.test_put_whole_calendar(
  119. cast(_TestBaseRequests, self))
  120. for uid in ("todo", "event"):
  121. _, answer = self.get("/calendar.ics/%s.ics" % uid)
  122. assert "\r\nUID:%s\r\n" % uid in answer
  123. def test_put_whole_calendar_random_uids_used_as_file_names(self) -> None:
  124. """Test if UIDs are used as file names."""
  125. _TestBaseRequests.test_put_whole_calendar_without_uids(
  126. cast(_TestBaseRequests, self))
  127. _, answer = self.get("/calendar.ics")
  128. assert answer is not None
  129. uids = []
  130. for line in answer.split("\r\n"):
  131. if line.startswith("UID:"):
  132. uids.append(line[len("UID:"):])
  133. for uid in uids:
  134. _, answer = self.get("/calendar.ics/%s.ics" % uid)
  135. assert answer is not None
  136. assert "\r\nUID:%s\r\n" % uid in answer
  137. def test_put_whole_addressbook_uids_used_as_file_names(self) -> None:
  138. """Test if UIDs are used as file names."""
  139. _TestBaseRequests.test_put_whole_addressbook(
  140. cast(_TestBaseRequests, self))
  141. for uid in ("contact1", "contact2"):
  142. _, answer = self.get("/contacts.vcf/%s.vcf" % uid)
  143. assert "\r\nUID:%s\r\n" % uid in answer
  144. def test_put_whole_addressbook_random_uids_used_as_file_names(
  145. self) -> None:
  146. """Test if UIDs are used as file names."""
  147. _TestBaseRequests.test_put_whole_addressbook_without_uids(
  148. cast(_TestBaseRequests, self))
  149. _, answer = self.get("/contacts.vcf")
  150. assert answer is not None
  151. uids = []
  152. for line in answer.split("\r\n"):
  153. if line.startswith("UID:"):
  154. uids.append(line[len("UID:"):])
  155. for uid in uids:
  156. _, answer = self.get("/contacts.vcf/%s.vcf" % uid)
  157. assert answer is not None
  158. assert "\r\nUID:%s\r\n" % uid in answer
  159. class TestMultiFileSystemNoLock(BaseTest):
  160. """Tests for multifilesystem_nolock."""
  161. def setup_method(self) -> None:
  162. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  163. self.configure({"storage": {"type": "multifilesystem_nolock"}})
  164. test_add_event = _TestBaseRequests.test_add_event
  165. test_item_cache_rebuild = TestMultiFileSystem.test_item_cache_rebuild
  166. class TestCustomStorageSystem(BaseTest):
  167. """Test custom backend loading."""
  168. def setup_method(self) -> None:
  169. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  170. self.configure({"storage": {
  171. "type": "radicale.tests.custom.storage_simple_sync"}})
  172. full_sync_token_support: ClassVar[bool] = False
  173. test_add_event = _TestBaseRequests.test_add_event
  174. _report_sync_token = _TestBaseRequests._report_sync_token
  175. # include tests related to sync token
  176. s: str = ""
  177. for s in dir(_TestBaseRequests):
  178. if s.startswith("test_") and "sync" in s.split("_"):
  179. locals()[s] = getattr(_TestBaseRequests, s)
  180. del s
  181. class TestCustomStorageSystemCallable(BaseTest):
  182. """Test custom backend loading with ``callable``."""
  183. def setup_method(self) -> None:
  184. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  185. self.configure({"storage": {
  186. "type": radicale.tests.custom.storage_simple_sync.Storage}})
  187. test_add_event = _TestBaseRequests.test_add_event