test_expand.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Radicale tests with expand requests.
  19. """
  20. import os
  21. from typing import ClassVar, List
  22. from radicale.tests import BaseTest
  23. from radicale.tests.helpers import get_file_content
  24. ONLY_DATES = True
  25. CONTAINS_TIMES = False
  26. class TestExpandRequests(BaseTest):
  27. """Tests with expand requests."""
  28. # Allow skipping sync-token tests, when not fully supported by the backend
  29. full_sync_token_support: ClassVar[bool] = True
  30. def setup_method(self) -> None:
  31. BaseTest.setup_method(self)
  32. rights_file_path = os.path.join(self.colpath, "rights")
  33. with open(rights_file_path, "w") as f:
  34. f.write("""\
  35. [permit delete collection]
  36. user: .*
  37. collection: test-permit-delete
  38. permissions: RrWwD
  39. [forbid delete collection]
  40. user: .*
  41. collection: test-forbid-delete
  42. permissions: RrWwd
  43. [permit overwrite collection]
  44. user: .*
  45. collection: test-permit-overwrite
  46. permissions: RrWwO
  47. [forbid overwrite collection]
  48. user: .*
  49. collection: test-forbid-overwrite
  50. permissions: RrWwo
  51. [allow all]
  52. user: .*
  53. collection: .*
  54. permissions: RrWw""")
  55. self.configure({"rights": {"file": rights_file_path,
  56. "type": "from_file"}})
  57. def _test_expand(self,
  58. expected_uid: str,
  59. start: str,
  60. end: str,
  61. expected_recurrence_ids: List[str],
  62. expected_start_times: List[str],
  63. expected_end_times: List[str],
  64. only_dates: bool,
  65. nr_uids: int) -> None:
  66. self.put("/calendar.ics/", get_file_content(f"{expected_uid}.ics"))
  67. req_body_without_expand = \
  68. f"""<?xml version="1.0" encoding="utf-8" ?>
  69. <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  70. <D:prop>
  71. <C:calendar-data>
  72. </C:calendar-data>
  73. </D:prop>
  74. <C:filter>
  75. <C:comp-filter name="VCALENDAR">
  76. <C:comp-filter name="VEVENT">
  77. <C:time-range start="{start}" end="{end}"/>
  78. </C:comp-filter>
  79. </C:comp-filter>
  80. </C:filter>
  81. </C:calendar-query>
  82. """
  83. _, responses = self.report("/calendar.ics/", req_body_without_expand)
  84. assert len(responses) == 1
  85. response_without_expand = responses[f'/calendar.ics/{expected_uid}.ics']
  86. assert not isinstance(response_without_expand, int)
  87. status, element = response_without_expand["C:calendar-data"]
  88. assert status == 200 and element.text
  89. assert "RRULE" in element.text
  90. if not only_dates:
  91. assert "BEGIN:VTIMEZONE" in element.text
  92. if nr_uids == 1:
  93. assert "RECURRENCE-ID" not in element.text
  94. uids: List[str] = []
  95. for line in element.text.split("\n"):
  96. if line.startswith("UID:"):
  97. uid = line[len("UID:"):]
  98. assert uid == expected_uid
  99. uids.append(uid)
  100. assert len(uids) == nr_uids
  101. req_body_with_expand = \
  102. f"""<?xml version="1.0" encoding="utf-8" ?>
  103. <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  104. <D:prop>
  105. <C:calendar-data>
  106. <C:expand start="{start}" end="{end}"/>
  107. </C:calendar-data>
  108. </D:prop>
  109. <C:filter>
  110. <C:comp-filter name="VCALENDAR">
  111. <C:comp-filter name="VEVENT">
  112. <C:time-range start="{start}" end="{end}"/>
  113. </C:comp-filter>
  114. </C:comp-filter>
  115. </C:filter>
  116. </C:calendar-query>
  117. """
  118. _, responses = self.report("/calendar.ics/", req_body_with_expand)
  119. assert len(responses) == 1
  120. response_with_expand = responses[f'/calendar.ics/{expected_uid}.ics']
  121. assert not isinstance(response_with_expand, int)
  122. status, element = response_with_expand["C:calendar-data"]
  123. assert status == 200 and element.text
  124. assert "RRULE" not in element.text
  125. assert "BEGIN:VTIMEZONE" not in element.text
  126. uids = []
  127. recurrence_ids = []
  128. for line in element.text.split("\n"):
  129. if line.startswith("UID:"):
  130. assert line == f"UID:{expected_uid}"
  131. uids.append(line)
  132. if line.startswith("RECURRENCE-ID:"):
  133. assert line in expected_recurrence_ids
  134. recurrence_ids.append(line)
  135. if line.startswith("DTSTART:"):
  136. assert line in expected_start_times
  137. if line.startswith("DTEND:"):
  138. assert line in expected_end_times
  139. assert len(uids) == len(expected_recurrence_ids)
  140. assert len(set(recurrence_ids)) == len(expected_recurrence_ids)
  141. def test_report_with_expand_property(self) -> None:
  142. """Test report with expand property"""
  143. self._test_expand(
  144. "event_daily_rrule",
  145. "20060103T000000Z",
  146. "20060105T000000Z",
  147. ["RECURRENCE-ID:20060103T170000Z", "RECURRENCE-ID:20060104T170000Z"],
  148. ["DTSTART:20060103T170000Z", "DTSTART:20060104T170000Z"],
  149. [],
  150. CONTAINS_TIMES,
  151. 1
  152. )
  153. def test_report_with_expand_property_all_day_event(self) -> None:
  154. """Test report with expand property for all day events"""
  155. self._test_expand(
  156. "event_full_day_rrule",
  157. "20060103T000000Z",
  158. "20060105T000000Z",
  159. ["RECURRENCE-ID:20060103", "RECURRENCE-ID:20060104", "RECURRENCE-ID:20060105"],
  160. ["DTSTART:20060103", "DTSTART:20060104", "DTSTART:20060105"],
  161. ["DTEND:20060104", "DTEND:20060105", "DTEND:20060106"],
  162. ONLY_DATES,
  163. 1
  164. )
  165. def test_report_with_expand_property_overridden(self) -> None:
  166. """Test report with expand property with overridden events"""
  167. self._test_expand(
  168. "event_daily_rrule_overridden",
  169. "20060103T000000Z",
  170. "20060105T000000Z",
  171. ["RECURRENCE-ID:20060103T170000Z", "RECURRENCE-ID:20060104T170000Z"],
  172. ["DTSTART:20060103T170000Z", "DTSTART:20060104T190000Z"],
  173. [],
  174. CONTAINS_TIMES,
  175. 2
  176. )
  177. def test_report_with_expand_property_timezone(self):
  178. self._test_expand(
  179. "event_weekly_rrule",
  180. "20060320T000000Z",
  181. "20060414T000000Z",
  182. [
  183. "RECURRENCE-ID:20060321T200000Z",
  184. "RECURRENCE-ID:20060328T200000Z",
  185. "RECURRENCE-ID:20060404T190000Z",
  186. "RECURRENCE-ID:20060411T190000Z",
  187. ],
  188. [
  189. "DTSTART:20060321T200000Z",
  190. "DTSTART:20060328T200000Z",
  191. "DTSTART:20060404T190000Z",
  192. "DTSTART:20060411T190000Z",
  193. ],
  194. [],
  195. CONTAINS_TIMES,
  196. 1
  197. )