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

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

Check backend values when initializing Kofa authenticator.

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
68class KofaAuthenticatorTests(unittest.TestCase):
69
70    def test_create(self):
71        # we can create KofaAuthenticator instances
72        auth = KofaAuthenticator()
73        assert isinstance(auth, KofaAuthenticator)
74
75    def test_options_defaults(self):
76        # all options have sensible defaults
77        auth = KofaAuthenticator()
78        assert auth.backends == {}
79
80    def test_options(self):
81        # we can pass options
82        auth = KofaAuthenticator(auth_backends=str(BACKENDS))
83        assert auth.backends == BACKENDS
84        auth = KofaAuthenticator(auth_backends='{"foo": {"url": "bar"}}')
85        assert auth.backends['foo']['marker'] == '.+'
86        self.assertRaises(
87            ValueError, KofaAuthenticator, auth_backends='not-a-py-expr')
88        self.assertRaises(
89            ValueError, KofaAuthenticator, auth_backends='"Not a dict"')
90        self.assertRaises(
91            ValueError, KofaAuthenticator,
92            auth_backends='{"foo": "not-a-dict"}')
93        self.assertRaises(
94            ValueError, KofaAuthenticator,
95            auth_backends='{"foo": {"no-url-key": "bar"}}')
96        self.assertRaises(
97            ValueError, KofaAuthenticator,
98            auth_backends='{"foo": {"url": "bar", "marker": "inv_re)"}}')
99
100    def test_paste_deploy_options(self):
101        # we can set CAS server-related options via paste.deploy config
102        paste_conf = os.path.join(
103            os.path.dirname(__file__), 'sample3.ini')
104        app = loadapp('config:%s' % paste_conf)
105        assert isinstance(app, CASServer)
106        assert app.db_connection_string == 'sqlite:///:memory:'
107        assert isinstance(app.auth, KofaAuthenticator)
108
109    def DIStest_check_credentials(self):
110        # we get real responses when querying Kofa instances
111        auth = KofaAuthenticator()
112        result1 = auth.check_credentials('bird', 'bebop')
113        assert result1 == (True, '')
114        result2 = auth.check_credentials('foo', 'bar')
115        assert result2 == (False, 'Invalid username or password.')
Note: See TracBrowser for help on using the repository browser.