test_auth.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2016 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale tests with simple requests and authentication.
  20. """
  21. import base64
  22. import os
  23. import shutil
  24. import tempfile
  25. import pytest
  26. from radicale import Application, config
  27. from .test_base 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 setup(self):
  33. self.configuration = config.load()
  34. self.colpath = tempfile.mkdtemp()
  35. self.configuration.update({
  36. "storage": {"filesystem_folder": self.colpath},
  37. # Disable syncing to disk for better performance
  38. "internal": {"filesystem_fsync": "False"},
  39. # Set incorrect authentication delay to a very low value
  40. "auth": {"delay": "0.002"}}, "test", internal=True)
  41. def teardown(self):
  42. shutil.rmtree(self.colpath)
  43. def _test_htpasswd(self, htpasswd_encryption, htpasswd_content,
  44. test_matrix=None):
  45. """Test htpasswd authentication with user "tmp" and password "bepo"."""
  46. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  47. with open(htpasswd_file_path, "w") as f:
  48. f.write(htpasswd_content)
  49. self.configuration.update({
  50. "auth": {"type": "htpasswd",
  51. "htpasswd_filename": htpasswd_file_path,
  52. "htpasswd_encryption": htpasswd_encryption}}, "test")
  53. self.application = Application(self.configuration)
  54. if test_matrix is None:
  55. test_matrix = (
  56. ("tmp", "bepo", 207), ("tmp", "tmp", 401), ("tmp", "", 401),
  57. ("unk", "unk", 401), ("unk", "", 401), ("", "", 401))
  58. for user, password, expected_status in test_matrix:
  59. status, _, answer = self.request(
  60. "PROPFIND", "/",
  61. HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
  62. ("%s:%s" % (user, password)).encode()).decode())
  63. assert status == expected_status
  64. def test_htpasswd_plain(self):
  65. self._test_htpasswd("plain", "tmp:bepo")
  66. def test_htpasswd_plain_password_split(self):
  67. self._test_htpasswd("plain", "tmp:be:po", (
  68. ("tmp", "be:po", 207), ("tmp", "bepo", 401)))
  69. def test_htpasswd_sha1(self):
  70. self._test_htpasswd("sha1", "tmp:{SHA}UWRS3uSJJq2itZQEUyIH8rRajCM=")
  71. def test_htpasswd_ssha(self):
  72. self._test_htpasswd("ssha", "tmp:{SSHA}qbD1diw9RJKi0DnW4qO8WX9SE18W")
  73. def test_htpasswd_md5(self):
  74. try:
  75. import passlib # noqa: F401
  76. except ImportError:
  77. pytest.skip("passlib is not installed")
  78. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  79. def test_htpasswd_crypt(self):
  80. try:
  81. import crypt # noqa: F401
  82. except ImportError:
  83. pytest.skip("crypt is not installed")
  84. self._test_htpasswd("crypt", "tmp:dxUqxoThMs04k")
  85. def test_htpasswd_bcrypt(self):
  86. try:
  87. from passlib.hash import bcrypt
  88. from passlib.exc import MissingBackendError
  89. except ImportError:
  90. pytest.skip("passlib is not installed")
  91. try:
  92. bcrypt.hash("test-bcrypt-backend")
  93. except MissingBackendError:
  94. pytest.skip("bcrypt backend for passlib is not installed")
  95. self._test_htpasswd(
  96. "bcrypt",
  97. "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
  98. def test_htpasswd_multi(self):
  99. self._test_htpasswd("plain", "ign:ign\ntmp:bepo")
  100. @pytest.mark.skipif(os.name == "nt", reason="leading and trailing "
  101. "whitespaces not allowed in file names")
  102. def test_htpasswd_whitespace_preserved(self):
  103. self._test_htpasswd("plain", " tmp : bepo ",
  104. ((" tmp ", " bepo ", 207),))
  105. def test_htpasswd_whitespace_not_trimmed(self):
  106. self._test_htpasswd("plain", " tmp : bepo ", (("tmp", "bepo", 401),))
  107. def test_htpasswd_comment(self):
  108. self._test_htpasswd("plain", "#comment\n #comment\n \ntmp:bepo\n\n")
  109. def test_remote_user(self):
  110. self.configuration.update({"auth": {"type": "remote_user"}}, "test")
  111. self.application = Application(self.configuration)
  112. status, _, answer = self.request(
  113. "PROPFIND", "/",
  114. """<?xml version="1.0" encoding="utf-8"?>
  115. <propfind xmlns="DAV:">
  116. <prop>
  117. <current-user-principal />
  118. </prop>
  119. </propfind>""", REMOTE_USER="test")
  120. assert status == 207
  121. assert ">/test/<" in answer
  122. def test_http_x_remote_user(self):
  123. self.configuration.update(
  124. {"auth": {"type": "http_x_remote_user"}}, "test")
  125. self.application = Application(self.configuration)
  126. status, _, answer = self.request(
  127. "PROPFIND", "/",
  128. """<?xml version="1.0" encoding="utf-8"?>
  129. <propfind xmlns="DAV:">
  130. <prop>
  131. <current-user-principal />
  132. </prop>
  133. </propfind>""", HTTP_X_REMOTE_USER="test")
  134. assert status == 207
  135. assert ">/test/<" in answer
  136. def test_custom(self):
  137. """Custom authentication."""
  138. self.configuration.update(
  139. {"auth": {"type": "tests.custom.auth"}}, "test")
  140. self.application = Application(self.configuration)
  141. status, _, answer = self.request(
  142. "PROPFIND", "/tmp", HTTP_AUTHORIZATION="Basic %s" %
  143. base64.b64encode(("tmp:").encode()).decode())
  144. assert status == 207