import os import shutil import tempfile import unittest from paste.deploy import loadapp from webob import Request from waeup.cas.server import CASServer class CASServerTests(unittest.TestCase): def setUp(self): # Create a new location where tempfiles are created. This way # also temporary dirs of local CASServers can be removed on # tear-down. self._new_tmpdir = tempfile.mkdtemp() self._old_tmpdir = tempfile.tempdir tempfile.tempdir = self._new_tmpdir self.workdir = os.path.join(self._new_tmpdir, 'home') self.db_path = os.path.join(self.workdir, 'mycas.db') self.paste_conf1 = os.path.join( os.path.dirname(__file__), 'sample1.ini') self.paste_conf2 = os.path.join( os.path.dirname(__file__), 'sample2.ini') def tearDown(self): # remove local tempfile and reset old tempdir setting if os.path.isdir(self._new_tmpdir): shutil.rmtree(self._new_tmpdir) tempfile.tempdir = self._old_tmpdir def test_paste_deploy_loader(self): # we can load the CAS server via paste.deploy plugin app = loadapp('config:%s' % self.paste_conf1) assert isinstance(app, CASServer) assert hasattr(app, 'db_path') assert app.db_path is not None def test_paste_deploy_options(self): # we can set CAS server-related options via paste.deploy config app = loadapp('config:%s' % self.paste_conf2) assert isinstance(app, CASServer) assert app.db_path == '/a/path/to/cas.db' def test_init(self): # we get a db dir set automatically app = CASServer() assert hasattr(app, 'db_path') assert app.db_path is not None assert os.path.exists(os.path.dirname(app.db_path)) def test_init_explicit_db_path(self): # we can set a db_path explicitly app = CASServer(db_path=self.db_path) assert hasattr(app, 'db_path') assert app.db_path == self.db_path def test_call_root(self): # the CAS protocol requires no root app = CASServer() req = Request.blank('http://localhost/') resp = app(req) assert resp.status == '404 Not Found' def test_first_time_login(self): # we can get a login page app = CASServer() req = Request.blank('http://localhost/login') resp = app(req) assert resp.status == '200 OK' def test_validate(self): # we can access a validation page app = CASServer() req = Request.blank('http://localhost/validate') resp = app(req) assert resp.status == '501 Not Implemented' def test_logout(self): # we can access a logout page app = CASServer() req = Request.blank('http://localhost/logout') resp = app(req) assert resp.status == '501 Not Implemented' def test_login_simple(self): # a simple login with no service will result in login screen # (2.1.1#service of protocol specs) app = CASServer() req = Request.blank('http://localhost/login') resp = app(req) assert resp.status == '200 OK' assert resp.content_type == 'text/html' assert b'