radicale/tests/test_auth.py

78 lines
2.5 KiB
Python
Raw Normal View History

# This file is part of Radicale Server - Calendar Server
# Copyright © 2012-2013 Guillaume Ayoub
2016-03-31 19:57:40 +02:00
# Copyright © 2012-2016 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 <http://www.gnu.org/licenses/>.
"""
Radicale tests with simple requests and authentication.
"""
2013-12-28 10:31:32 +01:00
import base64
import hashlib
import logging
2013-12-28 10:31:32 +01:00
import os
import shutil
2013-12-28 10:31:32 +01:00
import tempfile
2016-03-31 19:57:40 +02:00
from radicale import Application, config
2016-03-31 19:57:40 +02:00
from . import BaseTest
2013-12-28 11:40:29 +01:00
class TestBaseAuthRequests(BaseTest):
2015-07-24 16:01:03 +02:00
"""Tests basic requests with auth.
We should setup auth for each type before creating the Application object.
"""
2013-12-28 11:40:29 +01:00
def setup(self):
self.colpath = tempfile.mkdtemp()
2013-12-28 11:40:29 +01:00
def teardown(self):
shutil.rmtree(self.colpath)
2013-12-28 11:40:29 +01:00
def test_root(self):
2015-07-24 16:01:03 +02:00
"""Htpasswd authentication."""
2013-12-28 10:31:32 +01:00
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()))
configuration = config.load()
configuration.set("auth", "type", "htpasswd")
configuration.set("auth", "htpasswd_filename", htpasswd_file_path)
configuration.set("auth", "htpasswd_encryption", "sha1")
2013-12-28 10:31:32 +01:00
self.application = Application(
configuration, logging.getLogger("radicale_test"))
2013-12-28 10:31:32 +01:00
status, headers, answer = self.request(
"GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
2013-12-28 10:31:32 +01:00
assert status == 200
assert "Radicale works!" in answer
def test_custom(self):
2015-07-24 16:01:03 +02:00
"""Custom authentication."""
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="dG1wOmJlcG8=")
assert status == 200
assert "Radicale works!" in answer