Remove duplicated code

This commit is contained in:
Unrud 2020-04-22 19:20:36 +02:00
parent d73a308294
commit 9bd852ba5e
4 changed files with 19 additions and 25 deletions

View File

@ -20,6 +20,7 @@ Tests for Radicale.
""" """
import base64
import logging import logging
import sys import sys
from io import BytesIO from io import BytesIO
@ -36,10 +37,13 @@ radicale.log.logger.setLevel(logging.DEBUG)
class BaseTest: class BaseTest:
"""Base class for tests.""" """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.""" """Send a request."""
for key in args: for key in args:
args[key.upper()] = args[key] args[key.upper()] = args[key]
if login:
args["HTTP_AUTHORIZATION"] = "Basic " + base64.b64encode(
login.encode()).decode()
args["REQUEST_METHOD"] = method.upper() args["REQUEST_METHOD"] = method.upper()
args["PATH_INFO"] = path args["PATH_INFO"] = path
if data: if data:
@ -89,8 +93,10 @@ class BaseTest:
@staticmethod @staticmethod
def _check_status(status, good_status, check=True): def _check_status(status, good_status, check=True):
if check is not False: if check is True:
assert status in (good_status, check) assert status == good_status
elif check is not False:
assert status == check
return status == good_status return status == good_status
def get(self, path, check=True, **args): def get(self, path, check=True, **args):

View File

@ -21,7 +21,6 @@ Radicale tests with simple requests and authentication.
""" """
import base64
import os import os
import shutil import shutil
import tempfile import tempfile
@ -85,8 +84,7 @@ class TestBaseAuthRequests(BaseTest):
("", "🔑", False), ("", "", False)) ("", "🔑", False), ("", "", False))
for user, password, valid in test_matrix: for user, password, valid in test_matrix:
self.propfind("/", check=207 if valid else 401, self.propfind("/", check=207 if valid else 401,
HTTP_AUTHORIZATION=("Basic %s" % base64.b64encode( login="%s:%s" % (user, password))
("%s:%s" % (user, password)).encode()).decode()))
def test_htpasswd_plain(self): def test_htpasswd_plain(self):
self._test_htpasswd("plain", "tmp:bepo") self._test_htpasswd("plain", "tmp:bepo")
@ -165,5 +163,4 @@ class TestBaseAuthRequests(BaseTest):
self.configuration.update( self.configuration.update(
{"auth": {"type": "radicale.tests.custom.auth"}}, "test") {"auth": {"type": "radicale.tests.custom.auth"}}, "test")
self.application = Application(self.configuration) self.application = Application(self.configuration)
self.propfind("/tmp/", HTTP_AUTHORIZATION="Basic %s" % self.propfind("/tmp/", login="tmp:")
base64.b64encode(("tmp:").encode()).decode())

View File

@ -20,7 +20,6 @@ Radicale tests with simple requests.
""" """
import base64
import os import os
import posixpath import posixpath
import shutil import shutil
@ -1273,14 +1272,13 @@ class BaseRequestsMixIn:
assert status == 200 and prop.text == "text/vcard;charset=utf-8" assert status == 200 and prop.text == "text/vcard;charset=utf-8"
def test_authorization(self): def test_authorization(self):
authorization = "Basic " + base64.b64encode(b"user:").decode()
_, responses = self.propfind("/", """\ _, responses = self.propfind("/", """\
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<propfind xmlns="DAV:"> <propfind xmlns="DAV:">
<prop> <prop>
<current-user-principal /> <current-user-principal />
</prop> </prop>
</propfind>""", HTTP_AUTHORIZATION=authorization) </propfind>""", login="user:")
assert len(responses["/"]) == 1 assert len(responses["/"]) == 1
status, prop = responses["/"]["D:current-user-principal"] status, prop = responses["/"]["D:current-user-principal"]
assert status == 200 and len(prop) == 1 assert status == 200 and len(prop) == 1
@ -1300,8 +1298,7 @@ class BaseRequestsMixIn:
def test_principal_collection_creation(self): def test_principal_collection_creation(self):
"""Verify existence of the principal collection.""" """Verify existence of the principal collection."""
self.propfind("/user/", HTTP_AUTHORIZATION=( self.propfind("/user/", login="user:")
"Basic " + base64.b64encode(b"user:").decode()))
def test_existence_of_root_collections(self): def test_existence_of_root_collections(self):
"""Verify that the root collection always exists.""" """Verify that the root collection always exists."""
@ -1412,16 +1409,14 @@ class TestMultiFileSystem(BaseFileSystemTest, BaseRequestsMixIn):
"hook": ("mkdir %s" % os.path.join( "hook": ("mkdir %s" % os.path.join(
"collection-root", "created_by_hook"))}}, "test") "collection-root", "created_by_hook"))}}, "test")
self.application = Application(self.configuration) self.application = Application(self.configuration)
self.propfind("/", HTTP_AUTHORIZATION=( self.propfind("/", login="user:")
"Basic " + base64.b64encode(b"user:").decode()))
self.propfind("/created_by_hook/") self.propfind("/created_by_hook/")
def test_hook_fail(self): def test_hook_fail(self):
"""Verify that a request fails if the hook fails.""" """Verify that a request fails if the hook fails."""
self.configuration.update({"storage": {"hook": "exit 1"}}, "test") self.configuration.update({"storage": {"hook": "exit 1"}}, "test")
self.application = Application(self.configuration) self.application = Application(self.configuration)
status = self.mkcalendar("/calendar.ics/", check=False) self.mkcalendar("/calendar.ics/", check=500)
assert status != 201
def test_item_cache_rebuild(self): def test_item_cache_rebuild(self):
"""Delete the item cache and verify that it is rebuild.""" """Delete the item cache and verify that it is rebuild."""

View File

@ -18,7 +18,6 @@
Radicale tests with simple requests and rights. Radicale tests with simple requests and rights.
""" """
import base64
import os import os
import shutil import shutil
import tempfile import tempfile
@ -57,13 +56,10 @@ class TestBaseRightsRequests(BaseTest):
"htpasswd_encryption": "plain"}}, "test") "htpasswd_encryption": "plain"}}, "test")
self.application = Application(self.configuration) self.application = Application(self.configuration)
for u in ("tmp", "other"): for u in ("tmp", "other"):
status, _ = self.propfind( # Indirect creation of principal collection
"/%s/" % u, HTTP_AUTHORIZATION="Basic %s" % self.propfind("/%s/" % u, login="%s:bepo" % u)
base64.b64encode(("%s:bepo" % u).encode()).decode()) (self.propfind if mode == "r" else self.proppatch)(
status, _ = (self.propfind if mode == "r" else self.proppatch)( path, check=expected_status, login="tmp:bepo" if user else None)
path, check=False, HTTP_AUTHORIZATION="Basic %s" %
base64.b64encode(("tmp:bepo").encode()).decode() if user else "")
assert status == expected_status
def test_owner_only(self): def test_owner_only(self):
self._test_rights("owner_only", "", "/", "r", 401) self._test_rights("owner_only", "", "/", "r", 401)