diff --git a/tests/__init__.py b/tests/__init__.py index e6916145..ec5a84da 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -21,6 +21,8 @@ Tests for Radicale. """ +import base64 +import hashlib import os import shutil import sys @@ -32,6 +34,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) import radicale from radicale import config +from radicale.auth import htpasswd from radicale.storage import filesystem, database from .helpers import get_file_content from sqlalchemy.orm import sessionmaker @@ -114,3 +117,22 @@ class GitFileSystem(FileSystem): class GitMultiFileSystem(GitFileSystem, MultiFileSystem): """Base class for multifilesystem tests using Git""" + + +class HtpasswdAuthSystem(BaseTest): + """Base class to test Radicale with Htpasswd authentication""" + def setup(self): + self.colpath = tempfile.mkdtemp() + htpasswd_file_path = os.path.join(self.colpath, ".htpasswd") + with open(htpasswd_file_path, "w") as fd: + fd.write('tmp:{SHA}' + base64.b64encode( + hashlib.sha1("bépo").digest())) + config.set("auth", "type", "htpasswd") + self.userpass = base64.b64encode("tmp:bépo") + self.application = radicale.Application() + htpasswd.FILENAME = htpasswd_file_path + htpasswd.ENCRYPTION = "sha1" + + def teardown(self): + config.set("auth", "type", "None") + radicale.auth.is_authenticated = lambda *_: True diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 00000000..4c5a550f --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Radicale Server - Calendar Server +# Copyright © 2012-2013 Guillaume Ayoub +# Copyright © 2012-2013 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 . + +""" +Radicale tests with simple requests and authentication. + +""" + +from nose import with_setup +from . import HtpasswdAuthSystem + + +class TestBaseAuthRequests(HtpasswdAuthSystem): + """ + Tests basic requests with auth. + + ..note Only htpasswd works at the moment since + it requires to spawn processes running servers for + others auth methods (ldap). + """ + + @with_setup(HtpasswdAuthSystem.setup, HtpasswdAuthSystem.teardown) + def test_root(self): + """Tests a GET request at "/".""" + status, headers, answer = self.request( + "GET", "/", HTTP_AUTHORIZATION=self.userpass) + assert status == 200 + assert "Radicale works!" in answer