test_auth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2013 Guillaume Ayoub
  3. # Copyright © 2012-2016 Jean-Marc Martins
  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 logging
  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.logger = logging.getLogger("radicale_test")
  35. self.colpath = tempfile.mkdtemp()
  36. self.configuration.set("storage", "filesystem_folder", self.colpath)
  37. # Disable syncing to disk for better performance
  38. self.configuration.set("storage", "filesystem_fsync", "False")
  39. # Required on Windows, doesn't matter on Unix
  40. self.configuration.set("storage", "close_lock_file", "True")
  41. # Set incorrect authentication delay to a very low value
  42. self.configuration.set("auth", "delay", "0.002")
  43. def teardown(self):
  44. shutil.rmtree(self.colpath)
  45. def _test_htpasswd(self, htpasswd_encryption, htpasswd_content):
  46. """Test htpasswd authentication with user "tmp" and password "bepo"."""
  47. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  48. with open(htpasswd_file_path, "w") as f:
  49. f.write(htpasswd_content)
  50. self.configuration.set("auth", "type", "htpasswd")
  51. self.configuration.set("auth", "htpasswd_filename", htpasswd_file_path)
  52. self.configuration.set("auth", "htpasswd_encryption",
  53. htpasswd_encryption)
  54. self.application = Application(self.configuration, self.logger)
  55. for user, password, expeced_status in (
  56. ("tmp", "bepo", 207), ("tmp", "tmp", 401), ("tmp", "", 401),
  57. ("unk", "unk", 401), ("unk", "", 401), ("", "", 401)):
  58. status, headers, answer = self.request(
  59. "PROPFIND", "/",
  60. HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
  61. ("%s:%s" % (user, password)).encode()).decode())
  62. assert status == expeced_status
  63. def test_htpasswd_plain(self):
  64. self._test_htpasswd("plain", "tmp:bepo")
  65. def test_htpasswd_sha1(self):
  66. self._test_htpasswd("sha1", "tmp:{SHA}UWRS3uSJJq2itZQEUyIH8rRajCM=")
  67. def test_htpasswd_ssha(self):
  68. self._test_htpasswd("ssha", "tmp:{SSHA}qbD1diw9RJKi0DnW4qO8WX9SE18W")
  69. def test_htpasswd_md5(self):
  70. try:
  71. import passlib # noqa: F401
  72. except ImportError:
  73. pytest.skip("passlib is not installed")
  74. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  75. def test_htpasswd_crypt(self):
  76. try:
  77. import crypt # noqa: F401
  78. except ImportError:
  79. pytest.skip("crypt is not installed")
  80. self._test_htpasswd("crypt", "tmp:dxUqxoThMs04k")
  81. def test_htpasswd_bcrypt(self):
  82. try:
  83. from passlib.hash import bcrypt
  84. from passlib.exc import MissingBackendError
  85. except ImportError:
  86. pytest.skip("passlib is not installed")
  87. try:
  88. bcrypt.encrypt("test-bcrypt-backend")
  89. except MissingBackendError:
  90. pytest.skip("bcrypt backend for passlib is not installed")
  91. self._test_htpasswd(
  92. "bcrypt",
  93. "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
  94. def test_custom(self):
  95. """Custom authentication."""
  96. self.configuration.set("auth", "type", "tests.custom.auth")
  97. self.application = Application(self.configuration, self.logger)
  98. status, headers, answer = self.request(
  99. "GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
  100. assert status == 200
  101. assert "Radicale works!" in answer