test_expand.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. expected_recurrence_ids: List[str],
  60. expected_start_times: List[str],
  61. expected_end_times: List[str],
  62. only_dates: bool,
  63. nr_uids: int) -> None:
  64. self.put("/calendar.ics/", get_file_content(f"{expected_uid}.ics"))
  65. req_body_without_expand = \
  66. """<?xml version="1.0" encoding="utf-8" ?>
  67. <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  68. <D:prop>
  69. <C:calendar-data>
  70. </C:calendar-data>
  71. </D:prop>
  72. <C:filter>
  73. <C:comp-filter name="VCALENDAR">
  74. <C:comp-filter name="VEVENT">
  75. <C:time-range start="20060103T000000Z" end="20060105T000000Z"/>
  76. </C:comp-filter>
  77. </C:comp-filter>
  78. </C:filter>
  79. </C:calendar-query>
  80. """
  81. _, responses = self.report("/calendar.ics/", req_body_without_expand)
  82. assert len(responses) == 1
  83. response_without_expand = responses[f'/calendar.ics/{expected_uid}.ics']
  84. assert not isinstance(response_without_expand, int)
  85. status, element = response_without_expand["C:calendar-data"]
  86. assert status == 200 and element.text
  87. assert "RRULE" in element.text
  88. if not only_dates:
  89. assert "BEGIN:VTIMEZONE" in element.text
  90. if nr_uids == 1:
  91. assert "RECURRENCE-ID" not in element.text
  92. uids: List[str] = []
  93. for line in element.text.split("\n"):
  94. if line.startswith("UID:"):
  95. uid = line[len("UID:"):]
  96. assert uid == expected_uid
  97. uids.append(uid)
  98. assert len(uids) == nr_uids
  99. req_body_with_expand = \
  100. """<?xml version="1.0" encoding="utf-8" ?>
  101. <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  102. <D:prop>
  103. <C:calendar-data>
  104. <C:expand start="20060103T000000Z" end="20060105T000000Z"/>
  105. </C:calendar-data>
  106. </D:prop>
  107. <C:filter>
  108. <C:comp-filter name="VCALENDAR">
  109. <C:comp-filter name="VEVENT">
  110. <C:time-range start="20060103T000000Z" end="20060105T000000Z"/>
  111. </C:comp-filter>
  112. </C:comp-filter>
  113. </C:filter>
  114. </C:calendar-query>
  115. """
  116. _, responses = self.report("/calendar.ics/", req_body_with_expand)
  117. assert len(responses) == 1
  118. response_with_expand = responses[f'/calendar.ics/{expected_uid}.ics']
  119. assert not isinstance(response_with_expand, int)
  120. status, element = response_with_expand["C:calendar-data"]
  121. assert status == 200 and element.text
  122. assert "RRULE" not in element.text
  123. assert "BEGIN:VTIMEZONE" not in element.text
  124. uids = []
  125. recurrence_ids = []
  126. for line in element.text.split("\n"):
  127. if line.startswith("UID:"):
  128. assert line == f"UID:{expected_uid}"
  129. uids.append(line)
  130. if line.startswith("RECURRENCE-ID:"):
  131. assert line in expected_recurrence_ids
  132. recurrence_ids.append(line)
  133. if line.startswith("DTSTART:"):
  134. assert line in expected_start_times
  135. if line.startswith("DTEND:"):
  136. assert line in expected_end_times
  137. assert len(uids) == len(expected_recurrence_ids)
  138. assert len(set(recurrence_ids)) == len(expected_recurrence_ids)
  139. def test_report_with_expand_property(self) -> None:
  140. """Test report with expand property"""
  141. self._test_expand(
  142. "event_daily_rrule",
  143. ["RECURRENCE-ID:20060103T170000Z", "RECURRENCE-ID:20060104T170000Z"],
  144. ["DTSTART:20060103T170000Z", "DTSTART:20060104T170000Z"],
  145. [],
  146. CONTAINS_TIMES,
  147. 1
  148. )
  149. def test_report_with_expand_property_all_day_event(self) -> None:
  150. """Test report with expand property for all day events"""
  151. self._test_expand(
  152. "event_full_day_rrule",
  153. ["RECURRENCE-ID:20060103", "RECURRENCE-ID:20060104", "RECURRENCE-ID:20060105"],
  154. ["DTSTART:20060103", "DTSTART:20060104", "DTSTART:20060105"],
  155. ["DTEND:20060104", "DTEND:20060105", "DTEND:20060106"],
  156. ONLY_DATES,
  157. 1
  158. )
  159. def test_report_with_expand_property_overridden(self) -> None:
  160. """Test report with expand property with overridden events"""
  161. self._test_expand(
  162. "event_daily_rrule_overridden",
  163. ["RECURRENCE-ID:20060103T170000Z", "RECURRENCE-ID:20060104T170000Z"],
  164. ["DTSTART:20060103T170000Z", "DTSTART:20060104T190000Z"],
  165. [],
  166. CONTAINS_TIMES,
  167. 2
  168. )