source: main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py @ 10476

Last change on this file since 10476 was 10476, checked in by uli, 11 years ago

pep8.

File size: 3.9 KB
Line 
1import os
2import unittest
3from paste.deploy import loadapp
4from waeup.cas.authenticators import (
5    get_all_authenticators, get_authenticator, filter_auth_opts,
6    Authenticator, DummyAuthenticator, KofaAuthenticator
7    )
8from waeup.cas.server import CASServer
9
10
11class TestHelpers(unittest.TestCase):
12
13    def test_get_all_authenticators(self):
14        # we can get authenticators via entry points
15        auths = get_all_authenticators()
16        assert 'dummy' in auths
17        assert auths['dummy'] is DummyAuthenticator
18
19    def test_filter_auth_opts(self):
20        # we can filter auth opts
21        result = filter_auth_opts(
22            dict(auth='foo', auth_bar='baz', auth_baz='blah')
23            )
24        assert result == (
25            {'auth': 'foo'},
26            {'auth_bar': 'baz', 'auth_baz': 'blah'}
27            )
28
29    def test_get_authenticator(self):
30        # we can parse a paste.deploy config dict for auth
31        result = get_authenticator({})
32        assert result == {}
33        result = get_authenticator({'auth': 'dummy'})
34        assert isinstance(result['auth'], DummyAuthenticator)
35
36
37class AuthenticatorTests(unittest.TestCase):
38
39    def test_create(self):
40        # we can create Authenticator instances
41        auth = Authenticator()
42        assert isinstance(auth, Authenticator)
43
44
45class DummyAuthenticatorTests(unittest.TestCase):
46
47    def test_create(self):
48        # we can create DummyAuthenticator instances
49        auth = DummyAuthenticator()
50        assert isinstance(auth, DummyAuthenticator)
51
52    def test_check_credentials(self):
53        # we can succeed with 'bird'/'bebop'. Others will fail.
54        auth = DummyAuthenticator()
55        result1 = auth.check_credentials('bird', 'bebop')
56        assert result1 == (True, '')
57        result2 = auth.check_credentials('foo', 'bar')
58        assert result2 == (False, 'Invalid username or password.')
59
60
61BACKENDS = dict(
62    inst1=dict(
63        url='http://localhost:6666/app',
64        marker='^M[0-9]+$',
65        )
66    )
67
68
69class KofaAuthenticatorTests(unittest.TestCase):
70
71    def test_create(self):
72        # we can create KofaAuthenticator instances
73        auth = KofaAuthenticator()
74        assert isinstance(auth, KofaAuthenticator)
75
76    def test_options_defaults(self):
77        # all options have sensible defaults
78        auth = KofaAuthenticator()
79        assert auth.backends == {}
80
81    def test_options(self):
82        # we can pass options
83        auth = KofaAuthenticator(auth_backends=str(BACKENDS))
84        assert auth.backends == BACKENDS
85        auth = KofaAuthenticator(auth_backends='{"foo": {"url": "bar"}}')
86        assert auth.backends['foo']['marker'] == '.+'
87        self.assertRaises(
88            ValueError, KofaAuthenticator, auth_backends='not-a-py-expr')
89        self.assertRaises(
90            ValueError, KofaAuthenticator, auth_backends='"Not a dict"')
91        self.assertRaises(
92            ValueError, KofaAuthenticator,
93            auth_backends='{"foo": "not-a-dict"}')
94        self.assertRaises(
95            ValueError, KofaAuthenticator,
96            auth_backends='{"foo": {"no-url-key": "bar"}}')
97        self.assertRaises(
98            ValueError, KofaAuthenticator,
99            auth_backends='{"foo": {"url": "bar", "marker": "inv_re)"}}')
100
101    def test_paste_deploy_options(self):
102        # we can set CAS server-related options via paste.deploy config
103        paste_conf = os.path.join(
104            os.path.dirname(__file__), 'sample3.ini')
105        app = loadapp('config:%s' % paste_conf)
106        assert isinstance(app, CASServer)
107        assert app.db_connection_string == 'sqlite:///:memory:'
108        assert isinstance(app.auth, KofaAuthenticator)
109
110    def DIStest_check_credentials(self):
111        # we get real responses when querying Kofa instances
112        auth = KofaAuthenticator()
113        result1 = auth.check_credentials('bird', 'bebop')
114        assert result1 == (True, '')
115        result2 = auth.check_credentials('foo', 'bar')
116        assert result2 == (False, 'Invalid username or password.')
Note: See TracBrowser for help on using the repository browser.