[10462] | 1 | import os |
---|
[10394] | 2 | import unittest |
---|
[10462] | 3 | from paste.deploy import loadapp |
---|
[10394] | 4 | from waeup.cas.authenticators import ( |
---|
| 5 | get_all_authenticators, get_authenticator, filter_auth_opts, |
---|
[10462] | 6 | Authenticator, DummyAuthenticator, KofaAuthenticator |
---|
[10394] | 7 | ) |
---|
[10462] | 8 | from waeup.cas.server import CASServer |
---|
[10394] | 9 | |
---|
| 10 | |
---|
| 11 | class 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 | |
---|
| 37 | class 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 | |
---|
| 45 | class 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') |
---|
[10396] | 56 | assert result1 == (True, '') |
---|
[10394] | 57 | result2 = auth.check_credentials('foo', 'bar') |
---|
[10396] | 58 | assert result2 == (False, 'Invalid username or password.') |
---|
[10462] | 59 | |
---|
| 60 | |
---|
| 61 | SERVICES = dict( |
---|
| 62 | inst1 = dict(auth_url = 'http://localhost:6666/app', |
---|
| 63 | service_url = 'http://example.com/foo/bar') |
---|
| 64 | ) |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | class KofaAuthenticatorTests(unittest.TestCase): |
---|
| 68 | |
---|
| 69 | def test_create(self): |
---|
| 70 | # we can create KofaAuthenticator instances |
---|
| 71 | auth = KofaAuthenticator() |
---|
| 72 | assert isinstance(auth, KofaAuthenticator) |
---|
| 73 | |
---|
| 74 | def test_options_defaults(self): |
---|
| 75 | # all options have sensible defaults |
---|
| 76 | auth = KofaAuthenticator() |
---|
| 77 | assert auth.services is None |
---|
| 78 | |
---|
| 79 | def test_options(self): |
---|
| 80 | # we can pass options |
---|
| 81 | auth = KofaAuthenticator(auth_services=SERVICES) |
---|
| 82 | assert auth.services == SERVICES |
---|
| 83 | |
---|
| 84 | def test_paste_deploy_options(self): |
---|
| 85 | # we can set CAS server-related options via paste.deploy config |
---|
| 86 | paste_conf = os.path.join( |
---|
| 87 | os.path.dirname(__file__), 'sample3.ini') |
---|
| 88 | app = loadapp('config:%s' % paste_conf) |
---|
| 89 | assert isinstance(app, CASServer) |
---|
| 90 | assert app.db_connection_string == 'sqlite:///:memory:' |
---|
| 91 | assert isinstance(app.auth, KofaAuthenticator) |
---|
| 92 | |
---|
| 93 | def DIStest_check_credentials(self): |
---|
| 94 | # we get real responses when querying Kofa instances |
---|
| 95 | auth = KofaAuthenticator() |
---|
| 96 | result1 = auth.check_credentials('bird', 'bebop') |
---|
| 97 | assert result1 == (True, '') |
---|
| 98 | result2 = auth.check_credentials('foo', 'bar') |
---|
| 99 | assert result2 == (False, 'Invalid username or password.') |
---|