diff --git a/radicale/tests/__init__.py b/radicale/tests/__init__.py index 06ee0e9..423279f 100644 --- a/radicale/tests/__init__.py +++ b/radicale/tests/__init__.py @@ -20,6 +20,7 @@ Tests for Radicale. """ +import base64 import logging import sys from io import BytesIO @@ -36,10 +37,13 @@ radicale.log.logger.setLevel(logging.DEBUG) class BaseTest: """Base class for tests.""" - def request(self, method, path, data=None, **args): + def request(self, method, path, data=None, login=None, **args): """Send a request.""" for key in args: args[key.upper()] = args[key] + if login: + args["HTTP_AUTHORIZATION"] = "Basic " + base64.b64encode( + login.encode()).decode() args["REQUEST_METHOD"] = method.upper() args["PATH_INFO"] = path if data: @@ -89,8 +93,10 @@ class BaseTest: @staticmethod def _check_status(status, good_status, check=True): - if check is not False: - assert status in (good_status, check) + if check is True: + assert status == good_status + elif check is not False: + assert status == check return status == good_status def get(self, path, check=True, **args): diff --git a/radicale/tests/test_auth.py b/radicale/tests/test_auth.py index b83bee1..83d7ee0 100644 --- a/radicale/tests/test_auth.py +++ b/radicale/tests/test_auth.py @@ -21,7 +21,6 @@ Radicale tests with simple requests and authentication. """ -import base64 import os import shutil import tempfile @@ -85,8 +84,7 @@ class TestBaseAuthRequests(BaseTest): ("", "🔑", False), ("", "", False)) for user, password, valid in test_matrix: self.propfind("/", check=207 if valid else 401, - HTTP_AUTHORIZATION=("Basic %s" % base64.b64encode( - ("%s:%s" % (user, password)).encode()).decode())) + login="%s:%s" % (user, password)) def test_htpasswd_plain(self): self._test_htpasswd("plain", "tmp:bepo") @@ -165,5 +163,4 @@ class TestBaseAuthRequests(BaseTest): self.configuration.update( {"auth": {"type": "radicale.tests.custom.auth"}}, "test") self.application = Application(self.configuration) - self.propfind("/tmp/", HTTP_AUTHORIZATION="Basic %s" % - base64.b64encode(("tmp:").encode()).decode()) + self.propfind("/tmp/", login="tmp:") diff --git a/radicale/tests/test_base.py b/radicale/tests/test_base.py index f64f1ae..1e716f6 100644 --- a/radicale/tests/test_base.py +++ b/radicale/tests/test_base.py @@ -20,7 +20,6 @@ Radicale tests with simple requests. """ -import base64 import os import posixpath import shutil @@ -1273,14 +1272,13 @@ class BaseRequestsMixIn: assert status == 200 and prop.text == "text/vcard;charset=utf-8" def test_authorization(self): - authorization = "Basic " + base64.b64encode(b"user:").decode() _, responses = self.propfind("/", """\ -""", HTTP_AUTHORIZATION=authorization) +""", login="user:") assert len(responses["/"]) == 1 status, prop = responses["/"]["D:current-user-principal"] assert status == 200 and len(prop) == 1 @@ -1300,8 +1298,7 @@ class BaseRequestsMixIn: def test_principal_collection_creation(self): """Verify existence of the principal collection.""" - self.propfind("/user/", HTTP_AUTHORIZATION=( - "Basic " + base64.b64encode(b"user:").decode())) + self.propfind("/user/", login="user:") def test_existence_of_root_collections(self): """Verify that the root collection always exists.""" @@ -1412,16 +1409,14 @@ class TestMultiFileSystem(BaseFileSystemTest, BaseRequestsMixIn): "hook": ("mkdir %s" % os.path.join( "collection-root", "created_by_hook"))}}, "test") self.application = Application(self.configuration) - self.propfind("/", HTTP_AUTHORIZATION=( - "Basic " + base64.b64encode(b"user:").decode())) + self.propfind("/", login="user:") self.propfind("/created_by_hook/") def test_hook_fail(self): """Verify that a request fails if the hook fails.""" self.configuration.update({"storage": {"hook": "exit 1"}}, "test") self.application = Application(self.configuration) - status = self.mkcalendar("/calendar.ics/", check=False) - assert status != 201 + self.mkcalendar("/calendar.ics/", check=500) def test_item_cache_rebuild(self): """Delete the item cache and verify that it is rebuild.""" diff --git a/radicale/tests/test_rights.py b/radicale/tests/test_rights.py index 28d959d..d44c4e7 100644 --- a/radicale/tests/test_rights.py +++ b/radicale/tests/test_rights.py @@ -18,7 +18,6 @@ Radicale tests with simple requests and rights. """ -import base64 import os import shutil import tempfile @@ -57,13 +56,10 @@ class TestBaseRightsRequests(BaseTest): "htpasswd_encryption": "plain"}}, "test") self.application = Application(self.configuration) for u in ("tmp", "other"): - status, _ = self.propfind( - "/%s/" % u, HTTP_AUTHORIZATION="Basic %s" % - base64.b64encode(("%s:bepo" % u).encode()).decode()) - status, _ = (self.propfind if mode == "r" else self.proppatch)( - path, check=False, HTTP_AUTHORIZATION="Basic %s" % - base64.b64encode(("tmp:bepo").encode()).decode() if user else "") - assert status == expected_status + # Indirect creation of principal collection + self.propfind("/%s/" % u, login="%s:bepo" % u) + (self.propfind if mode == "r" else self.proppatch)( + path, check=expected_status, login="tmp:bepo" if user else None) def test_owner_only(self): self._test_rights("owner_only", "", "/", "r", 401)