test_rights.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Radicale tests with simple requests and rights.
  18. """
  19. import os
  20. from radicale.tests import BaseTest
  21. from radicale.tests.helpers import get_file_content
  22. class TestBaseRightsRequests(BaseTest):
  23. """Tests basic requests with rights."""
  24. def _test_rights(self, rights_type: str, user: str, path: str, mode: str,
  25. expected_status: int, with_auth: bool = True) -> None:
  26. assert mode in ("r", "w")
  27. assert user in ("", "tmp", "user@domain.test")
  28. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  29. with open(htpasswd_file_path, "w") as f:
  30. f.write("tmp:bepo\nother:bepo\nuser@domain.test:bepo")
  31. self.configure({
  32. "rights": {"type": rights_type},
  33. "auth": {"type": "htpasswd" if with_auth else "none",
  34. "htpasswd_filename": htpasswd_file_path,
  35. "htpasswd_encryption": "plain"}})
  36. for u in ("tmp", "other"):
  37. # Indirect creation of principal collection
  38. self.propfind("/%s/" % u, login="%s:bepo" % u)
  39. os.makedirs(os.path.join(self.colpath, "collection-root", "domain.test"), exist_ok=True)
  40. (self.propfind if mode == "r" else self.proppatch)(
  41. path, check=expected_status, login="%s:bepo" % user if user else None)
  42. def test_owner_only(self) -> None:
  43. self._test_rights("owner_only", "", "/", "r", 401)
  44. self._test_rights("owner_only", "", "/", "w", 401)
  45. self._test_rights("owner_only", "", "/tmp/", "r", 401)
  46. self._test_rights("owner_only", "", "/tmp/", "w", 401)
  47. self._test_rights("owner_only", "tmp", "/", "r", 207)
  48. self._test_rights("owner_only", "tmp", "/", "w", 403)
  49. self._test_rights("owner_only", "tmp", "/tmp/", "r", 207)
  50. self._test_rights("owner_only", "tmp", "/tmp/", "w", 207)
  51. self._test_rights("owner_only", "tmp", "/other/", "r", 403)
  52. self._test_rights("owner_only", "tmp", "/other/", "w", 403)
  53. def test_owner_only_without_auth(self) -> None:
  54. self._test_rights("owner_only", "", "/", "r", 207, False)
  55. self._test_rights("owner_only", "", "/", "w", 401, False)
  56. self._test_rights("owner_only", "", "/tmp/", "r", 207, False)
  57. self._test_rights("owner_only", "", "/tmp/", "w", 207, False)
  58. def test_owner_write(self) -> None:
  59. self._test_rights("owner_write", "", "/", "r", 401)
  60. self._test_rights("owner_write", "", "/", "w", 401)
  61. self._test_rights("owner_write", "", "/tmp/", "r", 401)
  62. self._test_rights("owner_write", "", "/tmp/", "w", 401)
  63. self._test_rights("owner_write", "tmp", "/", "r", 207)
  64. self._test_rights("owner_write", "tmp", "/", "w", 403)
  65. self._test_rights("owner_write", "tmp", "/tmp/", "r", 207)
  66. self._test_rights("owner_write", "tmp", "/tmp/", "w", 207)
  67. self._test_rights("owner_write", "tmp", "/other/", "r", 207)
  68. self._test_rights("owner_write", "tmp", "/other/", "w", 403)
  69. def test_owner_write_without_auth(self) -> None:
  70. self._test_rights("owner_write", "", "/", "r", 207, False)
  71. self._test_rights("owner_write", "", "/", "w", 401, False)
  72. self._test_rights("owner_write", "", "/tmp/", "r", 207, False)
  73. self._test_rights("owner_write", "", "/tmp/", "w", 207, False)
  74. def test_authenticated(self) -> None:
  75. self._test_rights("authenticated", "", "/", "r", 401)
  76. self._test_rights("authenticated", "", "/", "w", 401)
  77. self._test_rights("authenticated", "", "/tmp/", "r", 401)
  78. self._test_rights("authenticated", "", "/tmp/", "w", 401)
  79. self._test_rights("authenticated", "tmp", "/", "r", 207)
  80. self._test_rights("authenticated", "tmp", "/", "w", 207)
  81. self._test_rights("authenticated", "tmp", "/tmp/", "r", 207)
  82. self._test_rights("authenticated", "tmp", "/tmp/", "w", 207)
  83. self._test_rights("authenticated", "tmp", "/other/", "r", 207)
  84. self._test_rights("authenticated", "tmp", "/other/", "w", 207)
  85. def test_authenticated_without_auth(self) -> None:
  86. self._test_rights("authenticated", "", "/", "r", 207, False)
  87. self._test_rights("authenticated", "", "/", "w", 207, False)
  88. self._test_rights("authenticated", "", "/tmp/", "r", 207, False)
  89. self._test_rights("authenticated", "", "/tmp/", "w", 207, False)
  90. def test_from_file(self) -> None:
  91. rights_file_path = os.path.join(self.colpath, "rights")
  92. with open(rights_file_path, "w") as f:
  93. f.write("""\
  94. [owner]
  95. user: .+
  96. collection: {user}(/.*)?
  97. permissions: RrWw
  98. [custom]
  99. user: .*
  100. collection: custom(/.*)?
  101. permissions: Rr
  102. [read-domain-principal]
  103. user: .+@([^@]+)
  104. collection: {0}
  105. permissions: R""")
  106. self.configure({"rights": {"file": rights_file_path}})
  107. self._test_rights("from_file", "", "/other/", "r", 401)
  108. self._test_rights("from_file", "tmp", "/tmp/", "r", 207)
  109. self._test_rights("from_file", "tmp", "/other/", "r", 403)
  110. self._test_rights("from_file", "", "/custom/sub", "r", 404)
  111. self._test_rights("from_file", "tmp", "/custom/sub", "r", 404)
  112. self._test_rights("from_file", "", "/custom/sub", "w", 401)
  113. self._test_rights("from_file", "tmp", "/custom/sub", "w", 403)
  114. self._test_rights("from_file", "tmp", "/custom/sub", "w", 403)
  115. self._test_rights("from_file", "user@domain.test", "/domain.test/", "r", 207)
  116. self._test_rights("from_file", "user@domain.test", "/tmp/", "r", 403)
  117. self._test_rights("from_file", "user@domain.test", "/other/", "r", 403)
  118. def test_from_file_limited_get(self):
  119. rights_file_path = os.path.join(self.colpath, "rights")
  120. with open(rights_file_path, "w") as f:
  121. f.write("""\
  122. [write-all]
  123. user: tmp
  124. collection: .*
  125. permissions: RrWw
  126. [limited-public]
  127. user: .*
  128. collection: public/[^/]*
  129. permissions: i""")
  130. self.configure({"rights": {"type": "from_file",
  131. "file": rights_file_path}})
  132. self.mkcalendar("/tmp/calendar", login="tmp:bepo")
  133. self.mkcol("/public", login="tmp:bepo")
  134. self.mkcalendar("/public/calendar", login="tmp:bepo")
  135. self.get("/tmp/calendar", check=401)
  136. self.get("/public/", check=401)
  137. self.get("/public/calendar")
  138. self.get("/public/calendar/1.ics", check=401)
  139. def test_custom(self) -> None:
  140. """Custom rights management."""
  141. self._test_rights("radicale.tests.custom.rights", "", "/", "r", 401)
  142. self._test_rights(
  143. "radicale.tests.custom.rights", "", "/tmp/", "r", 207)
  144. def test_collections_and_items(self) -> None:
  145. """Test rights for creation of collections, calendars and items.
  146. Collections are allowed at "/" and "/.../".
  147. Calendars/Address books are allowed at "/.../.../".
  148. Items are allowed at "/.../.../...".
  149. """
  150. self.mkcalendar("/", check=401)
  151. self.mkcalendar("/user/", check=401)
  152. self.mkcol("/user/")
  153. self.mkcol("/user/calendar/", check=401)
  154. self.mkcalendar("/user/calendar/")
  155. self.mkcol("/user/calendar/item", check=401)
  156. self.mkcalendar("/user/calendar/item", check=401)
  157. def test_put_collections_and_items(self) -> None:
  158. """Test rights for creation of calendars and items with PUT."""
  159. self.put("/user/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR", check=401)
  160. self.mkcol("/user/")
  161. self.put("/user/calendar/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  162. event1 = get_file_content("event1.ics")
  163. self.put("/user/calendar/event1.ics", event1)