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