test_rights.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2017-2018 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 base64
  20. import os
  21. import shutil
  22. import tempfile
  23. from radicale import Application, config
  24. from .test_base import BaseTest
  25. class TestBaseAuthRequests(BaseTest):
  26. """Tests basic requests with rights."""
  27. def setup(self):
  28. self.configuration = config.load()
  29. self.colpath = tempfile.mkdtemp()
  30. self.configuration["storage"]["filesystem_folder"] = self.colpath
  31. # Disable syncing to disk for better performance
  32. self.configuration["internal"]["filesystem_fsync"] = "False"
  33. def teardown(self):
  34. shutil.rmtree(self.colpath)
  35. def _test_rights(self, rights_type, user, path, mode, expected_status):
  36. assert mode in ("r", "w")
  37. assert user in ("", "tmp")
  38. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  39. with open(htpasswd_file_path, "w") as f:
  40. f.write("tmp:bepo\nother:bepo")
  41. self.configuration["rights"]["type"] = rights_type
  42. self.configuration["auth"]["type"] = "htpasswd"
  43. self.configuration["auth"]["htpasswd_filename"] = htpasswd_file_path
  44. self.configuration["auth"]["htpasswd_encryption"] = "plain"
  45. self.application = Application(self.configuration)
  46. for u in ("tmp", "other"):
  47. status, _, _ = self.request(
  48. "PROPFIND", "/%s" % u, HTTP_AUTHORIZATION="Basic %s" %
  49. base64.b64encode(("%s:bepo" % u).encode()).decode())
  50. assert status == 207
  51. status, _, _ = self.request(
  52. "PROPFIND" if mode == "r" else "PROPPATCH", path,
  53. HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
  54. ("tmp:bepo").encode()).decode() if user else "")
  55. assert status == expected_status
  56. def test_owner_only(self):
  57. self._test_rights("owner_only", "", "/", "r", 401)
  58. self._test_rights("owner_only", "", "/", "w", 401)
  59. self._test_rights("owner_only", "", "/tmp", "r", 401)
  60. self._test_rights("owner_only", "", "/tmp", "w", 401)
  61. self._test_rights("owner_only", "tmp", "/", "r", 207)
  62. self._test_rights("owner_only", "tmp", "/", "w", 403)
  63. self._test_rights("owner_only", "tmp", "/tmp", "r", 207)
  64. self._test_rights("owner_only", "tmp", "/tmp", "w", 207)
  65. self._test_rights("owner_only", "tmp", "/other", "r", 403)
  66. self._test_rights("owner_only", "tmp", "/other", "w", 403)
  67. def test_owner_write(self):
  68. self._test_rights("owner_write", "", "/", "r", 401)
  69. self._test_rights("owner_write", "", "/", "w", 401)
  70. self._test_rights("owner_write", "", "/tmp", "r", 401)
  71. self._test_rights("owner_write", "", "/tmp", "w", 401)
  72. self._test_rights("owner_write", "tmp", "/", "r", 207)
  73. self._test_rights("owner_write", "tmp", "/", "w", 403)
  74. self._test_rights("owner_write", "tmp", "/tmp", "r", 207)
  75. self._test_rights("owner_write", "tmp", "/tmp", "w", 207)
  76. self._test_rights("owner_write", "tmp", "/other", "r", 207)
  77. self._test_rights("owner_write", "tmp", "/other", "w", 403)
  78. def test_authenticated(self):
  79. self._test_rights("authenticated", "", "/", "r", 401)
  80. self._test_rights("authenticated", "", "/", "w", 401)
  81. self._test_rights("authenticated", "", "/tmp", "r", 401)
  82. self._test_rights("authenticated", "", "/tmp", "w", 401)
  83. self._test_rights("authenticated", "tmp", "/", "r", 207)
  84. self._test_rights("authenticated", "tmp", "/", "w", 207)
  85. self._test_rights("authenticated", "tmp", "/tmp", "r", 207)
  86. self._test_rights("authenticated", "tmp", "/tmp", "w", 207)
  87. self._test_rights("authenticated", "tmp", "/other", "r", 207)
  88. self._test_rights("authenticated", "tmp", "/other", "w", 207)
  89. def test_from_file(self):
  90. rights_file_path = os.path.join(self.colpath, "rights")
  91. with open(rights_file_path, "w") as f:
  92. f.write("""\
  93. [owner]
  94. user: .+
  95. collection: %(login)s(/.*)?
  96. permissions: RrWw
  97. [custom]
  98. user: .*
  99. collection: custom(/.*)?
  100. permissions: Rr""")
  101. self.configuration["rights"]["file"] = rights_file_path
  102. self._test_rights("from_file", "", "/other", "r", 401)
  103. self._test_rights("from_file", "tmp", "/other", "r", 403)
  104. self._test_rights("from_file", "", "/custom/sub", "r", 404)
  105. self._test_rights("from_file", "tmp", "/custom/sub", "r", 404)
  106. self._test_rights("from_file", "", "/custom/sub", "w", 401)
  107. self._test_rights("from_file", "tmp", "/custom/sub", "w", 403)
  108. def test_custom(self):
  109. """Custom rights management."""
  110. self._test_rights("tests.custom.rights", "", "/", "r", 401)
  111. self._test_rights("tests.custom.rights", "", "/tmp", "r", 207)