test_auth.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2016 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2022 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Radicale tests with simple requests and authentication.
  21. """
  22. import os
  23. import sys
  24. from typing import Iterable, Tuple, Union
  25. import pytest
  26. from radicale import xmlutils
  27. from radicale.tests import BaseTest
  28. class TestBaseAuthRequests(BaseTest):
  29. """Tests basic requests with auth.
  30. We should setup auth for each type before creating the Application object.
  31. """
  32. def _test_htpasswd(self, htpasswd_encryption: str, htpasswd_content: str,
  33. test_matrix: Union[str, Iterable[Tuple[str, str, bool]]]
  34. = "ascii") -> None:
  35. """Test htpasswd authentication with user "tmp" and password "bepo" for
  36. ``test_matrix`` "ascii" or user "😀" and password "🔑" for
  37. ``test_matrix`` "unicode"."""
  38. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  39. encoding: str = self.configuration.get("encoding", "stock")
  40. with open(htpasswd_file_path, "w", encoding=encoding) as f:
  41. f.write(htpasswd_content)
  42. self.configure({"auth": {"type": "htpasswd",
  43. "htpasswd_filename": htpasswd_file_path,
  44. "htpasswd_encryption": htpasswd_encryption}})
  45. if test_matrix == "ascii":
  46. test_matrix = (("tmp", "bepo", True), ("tmp", "tmp", False),
  47. ("tmp", "", False), ("unk", "unk", False),
  48. ("unk", "", False), ("", "", False))
  49. elif test_matrix == "unicode":
  50. test_matrix = (("😀", "🔑", True), ("😀", "🌹", False),
  51. ("😁", "🔑", False), ("😀", "", False),
  52. ("", "🔑", False), ("", "", False))
  53. elif isinstance(test_matrix, str):
  54. raise ValueError("Unknown test matrix %r" % test_matrix)
  55. for user, password, valid in test_matrix:
  56. self.propfind("/", check=207 if valid else 401,
  57. login="%s:%s" % (user, password))
  58. def test_htpasswd_plain(self) -> None:
  59. self._test_htpasswd("plain", "tmp:bepo")
  60. def test_htpasswd_plain_password_split(self) -> None:
  61. self._test_htpasswd("plain", "tmp:be:po", (
  62. ("tmp", "be:po", True), ("tmp", "bepo", False)))
  63. def test_htpasswd_plain_unicode(self) -> None:
  64. self._test_htpasswd("plain", "😀:🔑", "unicode")
  65. def test_htpasswd_md5(self) -> None:
  66. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  67. def test_htpasswd_md5_unicode(self):
  68. self._test_htpasswd(
  69. "md5", "😀:$apr1$w4ev89r1$29xO8EvJmS2HEAadQ5qy11", "unicode")
  70. def test_htpasswd_sha256(self) -> None:
  71. self._test_htpasswd("sha256", "tmp:$5$i4Ni4TQq6L5FKss5$ilpTjkmnxkwZeV35GB9cYSsDXTALBn6KtWRJAzNlCL/")
  72. def test_htpasswd_sha512(self) -> None:
  73. self._test_htpasswd("sha512", "tmp:$6$3Qhl8r6FLagYdHYa$UCH9yXCed4A.J9FQsFPYAOXImzZUMfvLa0lwcWOxWYLOF5sE/lF99auQ4jKvHY2vijxmefl7G6kMqZ8JPdhIJ/")
  74. def test_htpasswd_bcrypt(self) -> None:
  75. self._test_htpasswd("bcrypt", "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3V"
  76. "NTRI3w5KDnj8NTUKJNWfVpvRq")
  77. def test_htpasswd_bcrypt_unicode(self) -> None:
  78. self._test_htpasswd("bcrypt", "😀:$2y$10$Oyz5aHV4MD9eQJbk6GPemOs4T6edK"
  79. "6U9Sqlzr.W1mMVCS8wJUftnW", "unicode")
  80. def test_htpasswd_multi(self) -> None:
  81. self._test_htpasswd("plain", "ign:ign\ntmp:bepo")
  82. @pytest.mark.skipif(sys.platform == "win32", reason="leading and trailing "
  83. "whitespaces not allowed in file names")
  84. def test_htpasswd_whitespace_user(self) -> None:
  85. for user in (" tmp", "tmp ", " tmp "):
  86. self._test_htpasswd("plain", "%s:bepo" % user, (
  87. (user, "bepo", True), ("tmp", "bepo", False)))
  88. def test_htpasswd_whitespace_password(self) -> None:
  89. for password in (" bepo", "bepo ", " bepo "):
  90. self._test_htpasswd("plain", "tmp:%s" % password, (
  91. ("tmp", password, True), ("tmp", "bepo", False)))
  92. def test_htpasswd_comment(self) -> None:
  93. self._test_htpasswd("plain", "#comment\n #comment\n \ntmp:bepo\n\n")
  94. def test_htpasswd_lc_username(self) -> None:
  95. self.configure({"auth": {"lc_username": "True"}})
  96. self._test_htpasswd("plain", "tmp:bepo", (
  97. ("tmp", "bepo", True), ("TMP", "bepo", True), ("tmp1", "bepo", False)))
  98. def test_htpasswd_strip_domain(self) -> None:
  99. self.configure({"auth": {"strip_domain": "True"}})
  100. self._test_htpasswd("plain", "tmp:bepo", (
  101. ("tmp", "bepo", True), ("tmp@domain.example", "bepo", True), ("tmp1", "bepo", False)))
  102. def test_remote_user(self) -> None:
  103. self.configure({"auth": {"type": "remote_user"}})
  104. _, responses = self.propfind("/", """\
  105. <?xml version="1.0" encoding="utf-8"?>
  106. <propfind xmlns="DAV:">
  107. <prop>
  108. <current-user-principal />
  109. </prop>
  110. </propfind>""", REMOTE_USER="test")
  111. assert responses is not None
  112. response = responses["/"]
  113. assert not isinstance(response, int)
  114. status, prop = response["D:current-user-principal"]
  115. assert status == 200
  116. href_element = prop.find(xmlutils.make_clark("D:href"))
  117. assert href_element is not None and href_element.text == "/test/"
  118. def test_http_x_remote_user(self) -> None:
  119. self.configure({"auth": {"type": "http_x_remote_user"}})
  120. _, responses = self.propfind("/", """\
  121. <?xml version="1.0" encoding="utf-8"?>
  122. <propfind xmlns="DAV:">
  123. <prop>
  124. <current-user-principal />
  125. </prop>
  126. </propfind>""", HTTP_X_REMOTE_USER="test")
  127. assert responses is not None
  128. response = responses["/"]
  129. assert not isinstance(response, int)
  130. status, prop = response["D:current-user-principal"]
  131. assert status == 200
  132. href_element = prop.find(xmlutils.make_clark("D:href"))
  133. assert href_element is not None and href_element.text == "/test/"
  134. def test_custom(self) -> None:
  135. """Custom authentication."""
  136. self.configure({"auth": {"type": "radicale.tests.custom.auth"}})
  137. self.propfind("/tmp/", login="tmp:")
  138. def test_none(self) -> None:
  139. self.configure({"auth": {"type": "none"}})
  140. self.propfind("/tmp/", login="tmp:")
  141. def test_denyall(self) -> None:
  142. self.configure({"auth": {"type": "denyall"}})
  143. self.propfind("/tmp/", login="tmp:", check=401)