test_auth.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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["storage"]["filesystem_fsync"] = "False"
  37. # Required on Windows, doesn't matter on Unix
  38. self.configuration["storage"]["filesystem_close_lock_file"] = "True"
  39. # Set incorrect authentication delay to a very low value
  40. self.configuration["auth"]["delay"] = "0.002"
  41. def teardown(self):
  42. shutil.rmtree(self.colpath)
  43. def _test_htpasswd(self, htpasswd_encryption, htpasswd_content):
  44. """Test htpasswd authentication with user "tmp" and password "bepo"."""
  45. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  46. with open(htpasswd_file_path, "w") as f:
  47. f.write(htpasswd_content)
  48. self.configuration["auth"]["type"] = "htpasswd"
  49. self.configuration["auth"]["htpasswd_filename"] = htpasswd_file_path
  50. self.configuration["auth"]["htpasswd_encryption"] = htpasswd_encryption
  51. self.application = Application(self.configuration, self.logger)
  52. for user, password, expected_status in (
  53. ("tmp", "bepo", 207), ("tmp", "tmp", 401), ("tmp", "", 401),
  54. ("unk", "unk", 401), ("unk", "", 401), ("", "", 401)):
  55. status, _, answer = self.request(
  56. "PROPFIND", "/",
  57. HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
  58. ("%s:%s" % (user, password)).encode()).decode())
  59. assert status == expected_status
  60. def test_htpasswd_plain(self):
  61. self._test_htpasswd("plain", "tmp:bepo")
  62. def test_htpasswd_sha1(self):
  63. self._test_htpasswd("sha1", "tmp:{SHA}UWRS3uSJJq2itZQEUyIH8rRajCM=")
  64. def test_htpasswd_ssha(self):
  65. self._test_htpasswd("ssha", "tmp:{SSHA}qbD1diw9RJKi0DnW4qO8WX9SE18W")
  66. def test_htpasswd_md5(self):
  67. try:
  68. import passlib # noqa: F401
  69. except ImportError:
  70. pytest.skip("passlib is not installed")
  71. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  72. def test_htpasswd_crypt(self):
  73. try:
  74. import crypt # noqa: F401
  75. except ImportError:
  76. pytest.skip("crypt is not installed")
  77. self._test_htpasswd("crypt", "tmp:dxUqxoThMs04k")
  78. def test_htpasswd_bcrypt(self):
  79. try:
  80. from passlib.hash import bcrypt
  81. from passlib.exc import MissingBackendError
  82. except ImportError:
  83. pytest.skip("passlib is not installed")
  84. try:
  85. bcrypt.encrypt("test-bcrypt-backend")
  86. except MissingBackendError:
  87. pytest.skip("bcrypt backend for passlib is not installed")
  88. self._test_htpasswd(
  89. "bcrypt",
  90. "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
  91. def test_remote_user(self):
  92. self.configuration["auth"]["type"] = "remote_user"
  93. self.application = Application(self.configuration, self.logger)
  94. status, _, answer = self.request(
  95. "PROPFIND", "/",
  96. """<?xml version="1.0" encoding="utf-8"?>
  97. <propfind xmlns="DAV:">
  98. <prop>
  99. <current-user-principal />
  100. </prop>
  101. </propfind>""", REMOTE_USER="test")
  102. assert status == 207
  103. assert ">/test/<" in answer
  104. def test_http_x_remote_user(self):
  105. self.configuration["auth"]["type"] = "http_x_remote_user"
  106. self.application = Application(self.configuration, self.logger)
  107. status, _, answer = self.request(
  108. "PROPFIND", "/",
  109. """<?xml version="1.0" encoding="utf-8"?>
  110. <propfind xmlns="DAV:">
  111. <prop>
  112. <current-user-principal />
  113. </prop>
  114. </propfind>""", HTTP_X_REMOTE_USER="test")
  115. assert status == 207
  116. assert ">/test/<" in answer
  117. def test_custom(self):
  118. """Custom authentication."""
  119. self.configuration["auth"]["type"] = "tests.custom.auth"
  120. self.application = Application(self.configuration, self.logger)
  121. status, _, answer = self.request(
  122. "PROPFIND", "/tmp", HTTP_AUTHORIZATION="Basic %s" %
  123. base64.b64encode(("tmp:").encode()).decode())
  124. assert status == 207