Unrud 5 лет назад
Родитель
Сommit
9bd852ba5e

+ 9 - 3
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):

+ 2 - 5
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:")

+ 4 - 9
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("/", """\
 <?xml version="1.0" encoding="utf-8"?>
 <propfind xmlns="DAV:">
     <prop>
         <current-user-principal />
     </prop>
-</propfind>""", HTTP_AUTHORIZATION=authorization)
+</propfind>""", 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."""

+ 4 - 8
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)