| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- # This file is part of Radicale Server - Calendar Server
- # Copyright © 2012-2013 Guillaume Ayoub
- # Copyright © 2012-2016 Jean-Marc Martins
- #
- # This library is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
- """
- Radicale tests with simple requests and authentication.
- """
- import base64
- import logging
- import os
- import shutil
- import tempfile
- import pytest
- from radicale import Application, config
- from .test_base import BaseTest
- class TestBaseAuthRequests(BaseTest):
- """Tests basic requests with auth.
- We should setup auth for each type before creating the Application object.
- """
- def setup(self):
- self.configuration = config.load()
- self.logger = logging.getLogger("radicale_test")
- self.colpath = tempfile.mkdtemp()
- self.configuration.set("storage", "filesystem_folder", self.colpath)
- # Disable syncing to disk for better performance
- self.configuration.set("storage", "filesystem_fsync", "False")
- # Required on Windows, doesn't matter on Unix
- self.configuration.set("storage", "close_lock_file", "True")
- # Set incorrect authentication delay to a very low value
- self.configuration.set("auth", "delay", "0.002")
- def teardown(self):
- shutil.rmtree(self.colpath)
- def _test_htpasswd(self, htpasswd_encryption, htpasswd_content):
- """Test htpasswd authentication with user "tmp" and password "bepo"."""
- htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
- with open(htpasswd_file_path, "w") as f:
- f.write(htpasswd_content)
- self.configuration.set("auth", "type", "htpasswd")
- self.configuration.set("auth", "htpasswd_filename", htpasswd_file_path)
- self.configuration.set("auth", "htpasswd_encryption",
- htpasswd_encryption)
- self.application = Application(self.configuration, self.logger)
- for user, password, expeced_status in (
- ("tmp", "bepo", 207), ("tmp", "tmp", 401), ("tmp", "", 401),
- ("unk", "unk", 401), ("unk", "", 401), ("", "", 401)):
- status, headers, answer = self.request(
- "PROPFIND", "/",
- HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
- ("%s:%s" % (user, password)).encode()).decode())
- assert status == expeced_status
- def test_htpasswd_plain(self):
- self._test_htpasswd("plain", "tmp:bepo")
- def test_htpasswd_sha1(self):
- self._test_htpasswd("sha1", "tmp:{SHA}UWRS3uSJJq2itZQEUyIH8rRajCM=")
- def test_htpasswd_ssha(self):
- self._test_htpasswd("ssha", "tmp:{SSHA}qbD1diw9RJKi0DnW4qO8WX9SE18W")
- def test_htpasswd_md5(self):
- try:
- import passlib # noqa: F401
- except ImportError:
- pytest.skip("passlib is not installed")
- self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
- def test_htpasswd_crypt(self):
- try:
- import crypt # noqa: F401
- except ImportError:
- pytest.skip("crypt is not installed")
- self._test_htpasswd("crypt", "tmp:dxUqxoThMs04k")
- def test_htpasswd_bcrypt(self):
- try:
- from passlib.hash import bcrypt
- from passlib.exc import MissingBackendError
- except ImportError:
- pytest.skip("passlib is not installed")
- try:
- bcrypt.encrypt("test-bcrypt-backend")
- except MissingBackendError:
- pytest.skip("bcrypt backend for passlib is not installed")
- self._test_htpasswd(
- "bcrypt",
- "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
- def test_custom(self):
- """Custom authentication."""
- self.configuration.set("auth", "type", "tests.custom.auth")
- self.application = Application(self.configuration, self.logger)
- status, headers, answer = self.request(
- "GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
- assert status == 200
- assert "Radicale works!" in answer
|