Remove global state about configuration and logs

Many things have been changed to make this possible, probably leading to
many hidden bugs waiting to be found.

Related to #122.
This commit is contained in:
Guillaume Ayoub
2016-04-22 11:37:02 +09:00
parent 8ac19ae0fc
commit 2f97d7d1e1
15 changed files with 576 additions and 488 deletions

View File

@ -25,11 +25,8 @@ from io import BytesIO
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname(
os.path.dirname(__file__)), "config")
class BaseTest(object):
class BaseTest:
"""Base class for tests."""
def request(self, method, path, data=None, **args):
"""Send a request."""

View File

@ -23,6 +23,9 @@ Just check username for testing
"""
from radicale import auth
def is_authenticated(user, password):
return user == 'tmp'
class Auth(auth.BaseAuth):
def is_authenticated(self, user, password):
return user == 'tmp'

View File

@ -24,5 +24,10 @@ Copy of filesystem storage backend for testing
from radicale import storage
# TODO: make something more in this collection (and test it)
class Collection(storage.Collection):
"""Collection stored in a folder."""
def __init__(self, path, principal=False):
super().__init__(path, principal)
self._filesystem_path = storage.path_to_filesystem(
self.configuration.get("storage", "test_folder"), self.path)

View File

@ -22,11 +22,12 @@ Radicale tests with simple requests and authentication.
import base64
import hashlib
import logging
import os
import radicale
import shutil
import tempfile
from radicale import config, auth
from radicale import Application, config
from . import BaseTest
@ -37,38 +38,40 @@ class TestBaseAuthRequests(BaseTest):
We should setup auth for each type before creating the Application object.
"""
def setup(self):
self.userpass = "dG1wOmJlcG8="
self.colpath = tempfile.mkdtemp()
def teardown(self):
config.set("auth", "type", "None")
radicale.auth.is_authenticated = lambda *_: True
shutil.rmtree(self.colpath)
def test_root(self):
"""Htpasswd authentication."""
self.colpath = tempfile.mkdtemp()
htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
with open(htpasswd_file_path, "wb") as fd:
fd.write(b"tmp:{SHA}" + base64.b64encode(
hashlib.sha1(b"bepo").digest()))
config.set("auth", "type", "htpasswd")
auth.FILENAME = htpasswd_file_path
auth.ENCRYPTION = "sha1"
configuration = config.load()
configuration.set("auth", "type", "htpasswd")
configuration.set("auth", "htpasswd_filename", htpasswd_file_path)
configuration.set("auth", "htpasswd_encryption", "sha1")
self.application = radicale.Application()
self.application = Application(
configuration, logging.getLogger("radicale_test"))
status, headers, answer = self.request(
"GET", "/", HTTP_AUTHORIZATION=self.userpass)
"GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
assert status == 200
assert "Radicale works!" in answer
def test_custom(self):
"""Custom authentication."""
config.set("auth", "type", "tests.custom.auth")
self.application = radicale.Application()
configuration = config.load()
configuration.set("auth", "type", "tests.custom.auth")
self.application = Application(
configuration, logging.getLogger("radicale_test"))
status, headers, answer = self.request(
"GET", "/", HTTP_AUTHORIZATION=self.userpass)
"GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
assert status == 200
assert "Radicale works!" in answer

View File

@ -19,20 +19,24 @@ Radicale tests with simple requests.
"""
import radicale
import logging
import shutil
import tempfile
from radicale import Application, config
from . import BaseTest
from .helpers import get_file_content
class BaseRequests(object):
class BaseRequests:
"""Tests with simple requests."""
storage_type = None
def setup(self):
radicale.config.set("storage", "type", self.storage_type)
self.configuration = config.load()
self.configuration.set("storage", "type", self.storage_type)
self.logger = logging.getLogger("radicale_test")
def test_root(self):
"""GET request at "/"."""
@ -95,30 +99,25 @@ class TestMultiFileSystem(BaseRequests, BaseTest):
storage_type = "multifilesystem"
def setup(self):
"""Setup function for each test."""
super().setup()
self.colpath = tempfile.mkdtemp()
from radicale import storage
storage.FOLDER = self.colpath
self.application = radicale.Application()
self.configuration.set("storage", "filesystem_folder", self.colpath)
self.application = Application(self.configuration, self.logger)
def teardown(self):
"""Teardown function for each test."""
shutil.rmtree(self.colpath)
class TestCustomStorageSystem(BaseRequests, BaseTest):
"""Base class for custom backend tests."""
storage_type = "custom"
storage_type = "tests.custom.storage"
def setup(self):
"""Setup function for each test."""
super().setup()
self.colpath = tempfile.mkdtemp()
radicale.config.set("storage", "type", "tests.custom.storage")
from tests.custom import storage
storage.FOLDER = self.colpath
self.application = radicale.Application()
self.configuration.set("storage", "filesystem_folder", self.colpath)
self.configuration.set("storage", "test_folder", self.colpath)
self.application = Application(self.configuration, self.logger)
def teardown(self):
"""Teardown function for each test."""
shutil.rmtree(self.colpath)