[10321] | 1 | import os |
---|
| 2 | import shutil |
---|
| 3 | import tempfile |
---|
| 4 | import unittest |
---|
| 5 | from paste.deploy import loadapp |
---|
| 6 | from webob import Request |
---|
| 7 | from waeup.cas.server import CASServer |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | class CASServerTests(unittest.TestCase): |
---|
| 11 | |
---|
| 12 | def setUp(self): |
---|
[10348] | 13 | # Create a new location where tempfiles are created. This way |
---|
| 14 | # also temporary dirs of local CASServers can be removed on |
---|
| 15 | # tear-down. |
---|
| 16 | self._new_tmpdir = tempfile.mkdtemp() |
---|
| 17 | self._old_tmpdir = tempfile.tempdir |
---|
| 18 | tempfile.tempdir = self._new_tmpdir |
---|
| 19 | self.workdir = os.path.join(self._new_tmpdir, 'home') |
---|
| 20 | self.db_path = os.path.join(self.workdir, 'mycas.db') |
---|
[10321] | 21 | self.paste_conf1 = os.path.join( |
---|
| 22 | os.path.dirname(__file__), 'sample1.ini') |
---|
[10352] | 23 | self.paste_conf2 = os.path.join( |
---|
| 24 | os.path.dirname(__file__), 'sample2.ini') |
---|
[10321] | 25 | |
---|
| 26 | def tearDown(self): |
---|
[10348] | 27 | # remove local tempfile and reset old tempdir setting |
---|
| 28 | if os.path.isdir(self._new_tmpdir): |
---|
| 29 | shutil.rmtree(self._new_tmpdir) |
---|
| 30 | tempfile.tempdir = self._old_tmpdir |
---|
[10321] | 31 | |
---|
| 32 | def test_paste_deploy_loader(self): |
---|
| 33 | # we can load the CAS server via paste.deploy plugin |
---|
| 34 | app = loadapp('config:%s' % self.paste_conf1) |
---|
| 35 | assert isinstance(app, CASServer) |
---|
[10352] | 36 | assert hasattr(app, 'db_path') |
---|
| 37 | assert app.db_path is not None |
---|
[10321] | 38 | |
---|
| 39 | def test_paste_deploy_options(self): |
---|
| 40 | # we can set CAS server-related options via paste.deploy config |
---|
[10352] | 41 | app = loadapp('config:%s' % self.paste_conf2) |
---|
[10321] | 42 | assert isinstance(app, CASServer) |
---|
[10352] | 43 | assert app.db_path == '/a/path/to/cas.db' |
---|
[10321] | 44 | |
---|
[10351] | 45 | def test_init(self): |
---|
| 46 | # we get a db dir set automatically |
---|
| 47 | app = CASServer() |
---|
| 48 | assert hasattr(app, 'db_path') |
---|
| 49 | assert app.db_path is not None |
---|
| 50 | assert os.path.exists(os.path.dirname(app.db_path)) |
---|
| 51 | |
---|
| 52 | def test_init_explicit_db_path(self): |
---|
| 53 | # we can set a db_path explicitly |
---|
| 54 | app = CASServer(db_path=self.db_path) |
---|
| 55 | assert hasattr(app, 'db_path') |
---|
| 56 | assert app.db_path == self.db_path |
---|
| 57 | |
---|
[10321] | 58 | def test_call_root(self): |
---|
[10326] | 59 | # the CAS protocol requires no root |
---|
[10321] | 60 | app = CASServer() |
---|
| 61 | req = Request.blank('http://localhost/') |
---|
| 62 | resp = app(req) |
---|
[10322] | 63 | assert resp.status == '404 Not Found' |
---|
[10326] | 64 | |
---|
| 65 | def test_first_time_login(self): |
---|
| 66 | # we can get a login page |
---|
| 67 | app = CASServer() |
---|
| 68 | req = Request.blank('http://localhost/login') |
---|
| 69 | resp = app(req) |
---|
| 70 | assert resp.status == '200 OK' |
---|
| 71 | |
---|
| 72 | def test_validate(self): |
---|
| 73 | # we can access a validation page |
---|
| 74 | app = CASServer() |
---|
| 75 | req = Request.blank('http://localhost/validate') |
---|
| 76 | resp = app(req) |
---|
| 77 | assert resp.status == '501 Not Implemented' |
---|
| 78 | |
---|
| 79 | def test_logout(self): |
---|
| 80 | # we can access a logout page |
---|
| 81 | app = CASServer() |
---|
| 82 | req = Request.blank('http://localhost/logout') |
---|
| 83 | resp = app(req) |
---|
| 84 | assert resp.status == '501 Not Implemented' |
---|
[10335] | 85 | |
---|
| 86 | def test_login_simple(self): |
---|
| 87 | # a simple login with no service will result in login screen |
---|
| 88 | # (2.1.1#service of protocol specs) |
---|
| 89 | app = CASServer() |
---|
| 90 | req = Request.blank('http://localhost/login') |
---|
| 91 | resp = app(req) |
---|
| 92 | assert resp.status == '200 OK' |
---|
| 93 | assert resp.content_type == 'text/html' |
---|
| 94 | assert b'<form ' in resp.body |
---|