test_storage.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.configure({"auth": {"type": "none"}})
  67. self.propfind("/", login="user:")
  68. self.propfind("/created_by_hook/")
  69. def test_hook_fail(self) -> None:
  70. """Verify that a request succeeded if the hook still fails (anyhow no rollback implemented)."""
  71. self.configure({"storage": {"hook": "exit 1"}})
  72. self.mkcalendar("/calendar.ics/", check=201)
  73. def test_item_cache_rebuild(self) -> None:
  74. """Delete the item cache and verify that it is rebuild."""
  75. self.mkcalendar("/calendar.ics/")
  76. event = get_file_content("event1.ics")
  77. path = "/calendar.ics/event1.ics"
  78. self.put(path, event)
  79. _, answer1 = self.get(path)
  80. cache_folder = os.path.join(self.colpath, "collection-root",
  81. "calendar.ics", ".Radicale.cache", "item")
  82. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  83. shutil.rmtree(cache_folder)
  84. _, answer2 = self.get(path)
  85. assert answer1 == answer2
  86. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  87. def test_item_cache_rebuild_subfolder(self) -> None:
  88. """Delete the item cache and verify that it is rebuild."""
  89. self.configure({"storage": {"use_cache_subfolder_for_item": "True"}})
  90. self.mkcalendar("/calendar.ics/")
  91. event = get_file_content("event1.ics")
  92. path = "/calendar.ics/event1.ics"
  93. self.put(path, event)
  94. _, answer1 = self.get(path)
  95. cache_folder = os.path.join(self.colpath, "collection-cache",
  96. "calendar.ics", ".Radicale.cache", "item")
  97. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  98. shutil.rmtree(cache_folder)
  99. _, answer2 = self.get(path)
  100. assert answer1 == answer2
  101. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  102. def test_item_cache_rebuild_mtime_and_size(self) -> None:
  103. """Delete the item cache and verify that it is rebuild."""
  104. self.configure({"storage": {"use_mtime_and_size_for_item_cache": "True"}})
  105. self.mkcalendar("/calendar.ics/")
  106. event = get_file_content("event1.ics")
  107. path = "/calendar.ics/event1.ics"
  108. self.put(path, event)
  109. _, answer1 = self.get(path)
  110. cache_folder = os.path.join(self.colpath, "collection-root",
  111. "calendar.ics", ".Radicale.cache", "item")
  112. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  113. shutil.rmtree(cache_folder)
  114. _, answer2 = self.get(path)
  115. assert answer1 == answer2
  116. assert os.path.exists(os.path.join(cache_folder, "event1.ics"))
  117. def test_put_whole_calendar_uids_used_as_file_names(self) -> None:
  118. """Test if UIDs are used as file names."""
  119. _TestBaseRequests.test_put_whole_calendar(
  120. cast(_TestBaseRequests, self))
  121. for uid in ("todo", "event"):
  122. _, answer = self.get("/calendar.ics/%s.ics" % uid)
  123. assert "\r\nUID:%s\r\n" % uid in answer
  124. def test_put_whole_calendar_random_uids_used_as_file_names(self) -> None:
  125. """Test if UIDs are used as file names."""
  126. _TestBaseRequests.test_put_whole_calendar_without_uids(
  127. cast(_TestBaseRequests, self))
  128. _, answer = self.get("/calendar.ics")
  129. assert answer is not None
  130. uids = []
  131. for line in answer.split("\r\n"):
  132. if line.startswith("UID:"):
  133. uids.append(line[len("UID:"):])
  134. for uid in uids:
  135. _, answer = self.get("/calendar.ics/%s.ics" % uid)
  136. assert answer is not None
  137. assert "\r\nUID:%s\r\n" % uid in answer
  138. def test_put_whole_addressbook_uids_used_as_file_names(self) -> None:
  139. """Test if UIDs are used as file names."""
  140. _TestBaseRequests.test_put_whole_addressbook(
  141. cast(_TestBaseRequests, self))
  142. for uid in ("contact1", "contact2"):
  143. _, answer = self.get("/contacts.vcf/%s.vcf" % uid)
  144. assert "\r\nUID:%s\r\n" % uid in answer
  145. def test_put_whole_addressbook_random_uids_used_as_file_names(
  146. self) -> None:
  147. """Test if UIDs are used as file names."""
  148. _TestBaseRequests.test_put_whole_addressbook_without_uids(
  149. cast(_TestBaseRequests, self))
  150. _, answer = self.get("/contacts.vcf")
  151. assert answer is not None
  152. uids = []
  153. for line in answer.split("\r\n"):
  154. if line.startswith("UID:"):
  155. uids.append(line[len("UID:"):])
  156. for uid in uids:
  157. _, answer = self.get("/contacts.vcf/%s.vcf" % uid)
  158. assert answer is not None
  159. assert "\r\nUID:%s\r\n" % uid in answer
  160. class TestMultiFileSystemNoLock(BaseTest):
  161. """Tests for multifilesystem_nolock."""
  162. def setup_method(self) -> None:
  163. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  164. self.configure({"storage": {"type": "multifilesystem_nolock"}})
  165. test_add_event = _TestBaseRequests.test_add_event
  166. test_item_cache_rebuild = TestMultiFileSystem.test_item_cache_rebuild
  167. class TestCustomStorageSystem(BaseTest):
  168. """Test custom backend loading."""
  169. def setup_method(self) -> None:
  170. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  171. self.configure({"storage": {
  172. "type": "radicale.tests.custom.storage_simple_sync"}})
  173. full_sync_token_support: ClassVar[bool] = False
  174. test_add_event = _TestBaseRequests.test_add_event
  175. _report_sync_token = _TestBaseRequests._report_sync_token
  176. # include tests related to sync token
  177. s: str = ""
  178. for s in dir(_TestBaseRequests):
  179. if s.startswith("test_") and "sync" in s.split("_"):
  180. locals()[s] = getattr(_TestBaseRequests, s)
  181. del s
  182. class TestCustomStorageSystemCallable(BaseTest):
  183. """Test custom backend loading with ``callable``."""
  184. def setup_method(self) -> None:
  185. _TestBaseRequests.setup_method(cast(_TestBaseRequests, self))
  186. self.configure({"storage": {
  187. "type": radicale.tests.custom.storage_simple_sync.Storage}})
  188. test_add_event = _TestBaseRequests.test_add_event