test_storage.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Tests for storage backends.
  19. """
  20. import os
  21. import shutil
  22. import sys
  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(self) -> None:
  32. _TestBaseRequests.setup(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 fails if the hook fails."""
  70. self.configure({"storage": {"hook": "exit 1"}})
  71. self.mkcalendar("/calendar.ics/", check=500)
  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. @pytest.mark.skipif(os.name != "posix" and sys.platform != "win32",
  87. reason="Only supported on 'posix' and 'win32'")
  88. def test_put_whole_calendar_uids_used_as_file_names(self) -> None:
  89. """Test if UIDs are used as file names."""
  90. _TestBaseRequests.test_put_whole_calendar(
  91. cast(_TestBaseRequests, self))
  92. for uid in ("todo", "event"):
  93. _, answer = self.get("/calendar.ics/%s.ics" % uid)
  94. assert "\r\nUID:%s\r\n" % uid in answer
  95. @pytest.mark.skipif(os.name != "posix" and sys.platform != "win32",
  96. reason="Only supported on 'posix' and 'win32'")
  97. def test_put_whole_calendar_random_uids_used_as_file_names(self) -> None:
  98. """Test if UIDs are used as file names."""
  99. _TestBaseRequests.test_put_whole_calendar_without_uids(
  100. cast(_TestBaseRequests, self))
  101. _, answer = self.get("/calendar.ics")
  102. assert answer is not None
  103. uids = []
  104. for line in answer.split("\r\n"):
  105. if line.startswith("UID:"):
  106. uids.append(line[len("UID:"):])
  107. for uid in uids:
  108. _, answer = self.get("/calendar.ics/%s.ics" % uid)
  109. assert answer is not None
  110. assert "\r\nUID:%s\r\n" % uid in answer
  111. @pytest.mark.skipif(os.name != "posix" and sys.platform != "win32",
  112. reason="Only supported on 'posix' and 'win32'")
  113. def test_put_whole_addressbook_uids_used_as_file_names(self) -> None:
  114. """Test if UIDs are used as file names."""
  115. _TestBaseRequests.test_put_whole_addressbook(
  116. cast(_TestBaseRequests, self))
  117. for uid in ("contact1", "contact2"):
  118. _, answer = self.get("/contacts.vcf/%s.vcf" % uid)
  119. assert "\r\nUID:%s\r\n" % uid in answer
  120. @pytest.mark.skipif(os.name != "posix" and sys.platform != "win32",
  121. reason="Only supported on 'posix' and 'win32'")
  122. def test_put_whole_addressbook_random_uids_used_as_file_names(
  123. self) -> None:
  124. """Test if UIDs are used as file names."""
  125. _TestBaseRequests.test_put_whole_addressbook_without_uids(
  126. cast(_TestBaseRequests, self))
  127. _, answer = self.get("/contacts.vcf")
  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("/contacts.vcf/%s.vcf" % uid)
  135. assert answer is not None
  136. assert "\r\nUID:%s\r\n" % uid in answer
  137. class TestMultiFileSystemNoLock(BaseTest):
  138. """Tests for multifilesystem_nolock."""
  139. def setup(self) -> None:
  140. _TestBaseRequests.setup(cast(_TestBaseRequests, self))
  141. self.configure({"storage": {"type": "multifilesystem_nolock"}})
  142. test_add_event = _TestBaseRequests.test_add_event
  143. test_item_cache_rebuild = TestMultiFileSystem.test_item_cache_rebuild
  144. class TestCustomStorageSystem(BaseTest):
  145. """Test custom backend loading."""
  146. def setup(self) -> None:
  147. _TestBaseRequests.setup(cast(_TestBaseRequests, self))
  148. self.configure({"storage": {
  149. "type": "radicale.tests.custom.storage_simple_sync"}})
  150. full_sync_token_support: ClassVar[bool] = False
  151. test_add_event = _TestBaseRequests.test_add_event
  152. _report_sync_token = _TestBaseRequests._report_sync_token
  153. # include tests related to sync token
  154. s: str = ""
  155. for s in dir(_TestBaseRequests):
  156. if s.startswith("test_") and "sync" in s.split("_"):
  157. locals()[s] = getattr(_TestBaseRequests, s)
  158. del s
  159. class TestCustomStorageSystemCallable(BaseTest):
  160. """Test custom backend loading with ``callable``."""
  161. def setup(self) -> None:
  162. _TestBaseRequests.setup(cast(_TestBaseRequests, self))
  163. self.configure({"storage": {
  164. "type": radicale.tests.custom.storage_simple_sync.Storage}})
  165. test_add_event = _TestBaseRequests.test_add_event