test_auth.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2016 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  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 simple requests and authentication.
  19. """
  20. import base64
  21. import os
  22. import shutil
  23. import tempfile
  24. import pytest
  25. from radicale import Application, config
  26. from .test_base import BaseTest
  27. class TestBaseAuthRequests(BaseTest):
  28. """Tests basic requests with auth.
  29. We should setup auth for each type before creating the Application object.
  30. """
  31. def setup(self):
  32. self.configuration = config.load()
  33. self.colpath = tempfile.mkdtemp()
  34. self.configuration["storage"]["filesystem_folder"] = self.colpath
  35. # Disable syncing to disk for better performance
  36. self.configuration["internal"]["filesystem_fsync"] = "False"
  37. # Set incorrect authentication delay to a very low value
  38. self.configuration["auth"]["delay"] = "0.002"
  39. def teardown(self):
  40. shutil.rmtree(self.colpath)
  41. def _test_htpasswd(self, htpasswd_encryption, htpasswd_content,
  42. test_matrix=None):
  43. """Test htpasswd authentication with user "tmp" and password "bepo"."""
  44. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  45. with open(htpasswd_file_path, "w") as f:
  46. f.write(htpasswd_content)
  47. self.configuration["auth"]["type"] = "htpasswd"
  48. self.configuration["auth"]["htpasswd_filename"] = htpasswd_file_path
  49. self.configuration["auth"]["htpasswd_encryption"] = htpasswd_encryption
  50. self.application = Application(self.configuration)
  51. if test_matrix is None:
  52. test_matrix = (
  53. ("tmp", "bepo", 207), ("tmp", "tmp", 401), ("tmp", "", 401),
  54. ("unk", "unk", 401), ("unk", "", 401), ("", "", 401))
  55. for user, password, expected_status in test_matrix:
  56. status, _, answer = self.request(
  57. "PROPFIND", "/",
  58. HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
  59. ("%s:%s" % (user, password)).encode()).decode())
  60. assert status == expected_status
  61. def test_htpasswd_plain(self):
  62. self._test_htpasswd("plain", "tmp:bepo")
  63. def test_htpasswd_plain_password_split(self):
  64. self._test_htpasswd("plain", "tmp:be:po", (
  65. ("tmp", "be:po", 207), ("tmp", "bepo", 401)))
  66. def test_htpasswd_sha1(self):
  67. self._test_htpasswd("sha1", "tmp:{SHA}UWRS3uSJJq2itZQEUyIH8rRajCM=")
  68. def test_htpasswd_ssha(self):
  69. self._test_htpasswd("ssha", "tmp:{SSHA}qbD1diw9RJKi0DnW4qO8WX9SE18W")
  70. def test_htpasswd_md5(self):
  71. try:
  72. import passlib # noqa: F401
  73. except ImportError:
  74. pytest.skip("passlib is not installed")
  75. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  76. def test_htpasswd_crypt(self):
  77. try:
  78. import crypt # noqa: F401
  79. except ImportError:
  80. pytest.skip("crypt is not installed")
  81. self._test_htpasswd("crypt", "tmp:dxUqxoThMs04k")
  82. def test_htpasswd_bcrypt(self):
  83. try:
  84. from passlib.hash import bcrypt
  85. from passlib.exc import MissingBackendError
  86. except ImportError:
  87. pytest.skip("passlib is not installed")
  88. try:
  89. bcrypt.encrypt("test-bcrypt-backend")
  90. except MissingBackendError:
  91. pytest.skip("bcrypt backend for passlib is not installed")
  92. self._test_htpasswd(
  93. "bcrypt",
  94. "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
  95. def test_htpasswd_multi(self):
  96. self._test_htpasswd("plain", "ign:ign\ntmp:bepo")
  97. @pytest.mark.skipif(os.name == "nt", reason="leading and trailing "
  98. "whitespaces not allowed in file names")
  99. def test_htpasswd_whitespace_preserved(self):
  100. self._test_htpasswd("plain", " tmp : bepo ",
  101. ((" tmp ", " bepo ", 207),))
  102. def test_htpasswd_whitespace_not_trimmed(self):
  103. self._test_htpasswd("plain", " tmp : bepo ", (("tmp", "bepo", 401),))
  104. def test_htpasswd_comment(self):
  105. self._test_htpasswd("plain", "#comment\n #comment\n \ntmp:bepo\n\n")
  106. def test_remote_user(self):
  107. self.configuration["auth"]["type"] = "remote_user"
  108. self.application = Application(self.configuration)
  109. status, _, answer = self.request(
  110. "PROPFIND", "/",
  111. """<?xml version="1.0" encoding="utf-8"?>
  112. <propfind xmlns="DAV:">
  113. <prop>
  114. <current-user-principal />
  115. </prop>
  116. </propfind>""", REMOTE_USER="test")
  117. assert status == 207
  118. assert ">/test/<" in answer
  119. def test_http_x_remote_user(self):
  120. self.configuration["auth"]["type"] = "http_x_remote_user"
  121. self.application = Application(self.configuration)
  122. status, _, answer = self.request(
  123. "PROPFIND", "/",
  124. """<?xml version="1.0" encoding="utf-8"?>
  125. <propfind xmlns="DAV:">
  126. <prop>
  127. <current-user-principal />
  128. </prop>
  129. </propfind>""", HTTP_X_REMOTE_USER="test")
  130. assert status == 207
  131. assert ">/test/<" in answer
  132. def test_custom(self):
  133. """Custom authentication."""
  134. self.configuration["auth"]["type"] = "tests.custom.auth"
  135. self.application = Application(self.configuration)
  136. status, _, answer = self.request(
  137. "PROPFIND", "/tmp", HTTP_AUTHORIZATION="Basic %s" %
  138. base64.b64encode(("tmp:").encode()).decode())
  139. assert status == 207