import os import unittest from paste.deploy import loadapp from waeup.cas.authenticators import ( get_all_authenticators, get_authenticator, filter_auth_opts, Authenticator, DummyAuthenticator, KofaAuthenticator ) from waeup.cas.server import CASServer class TestHelpers(unittest.TestCase): def test_get_all_authenticators(self): # we can get authenticators via entry points auths = get_all_authenticators() assert 'dummy' in auths assert auths['dummy'] is DummyAuthenticator def test_filter_auth_opts(self): # we can filter auth opts result = filter_auth_opts( dict(auth='foo', auth_bar='baz', auth_baz='blah') ) assert result == ( {'auth': 'foo'}, {'auth_bar': 'baz', 'auth_baz': 'blah'} ) def test_get_authenticator(self): # we can parse a paste.deploy config dict for auth result = get_authenticator({}) assert result == {} result = get_authenticator({'auth': 'dummy'}) assert isinstance(result['auth'], DummyAuthenticator) class AuthenticatorTests(unittest.TestCase): def test_create(self): # we can create Authenticator instances auth = Authenticator() assert isinstance(auth, Authenticator) class DummyAuthenticatorTests(unittest.TestCase): def test_create(self): # we can create DummyAuthenticator instances auth = DummyAuthenticator() assert isinstance(auth, DummyAuthenticator) def test_check_credentials(self): # we can succeed with 'bird'/'bebop'. Others will fail. auth = DummyAuthenticator() result1 = auth.check_credentials('bird', 'bebop') assert result1 == (True, '') result2 = auth.check_credentials('foo', 'bar') assert result2 == (False, 'Invalid username or password.') BACKENDS = dict( inst1 = dict( url = 'http://localhost:6666/app', marker = '^M[0-9]+$', ) ) class KofaAuthenticatorTests(unittest.TestCase): def test_create(self): # we can create KofaAuthenticator instances auth = KofaAuthenticator() assert isinstance(auth, KofaAuthenticator) def test_options_defaults(self): # all options have sensible defaults auth = KofaAuthenticator() assert auth.backends == {} def test_options(self): # we can pass options auth = KofaAuthenticator(auth_backends=str(BACKENDS)) assert auth.backends == BACKENDS auth = KofaAuthenticator(auth_backends='{"foo": {"url": "bar"}}') assert auth.backends['foo']['marker'] == '.+' self.assertRaises( ValueError, KofaAuthenticator, auth_backends='not-a-py-expr') self.assertRaises( ValueError, KofaAuthenticator, auth_backends='"Not a dict"') self.assertRaises( ValueError, KofaAuthenticator, auth_backends='{"foo": "not-a-dict"}') self.assertRaises( ValueError, KofaAuthenticator, auth_backends='{"foo": {"no-url-key": "bar"}}') self.assertRaises( ValueError, KofaAuthenticator, auth_backends='{"foo": {"url": "bar", "marker": "inv_re)"}}') def test_paste_deploy_options(self): # we can set CAS server-related options via paste.deploy config paste_conf = os.path.join( os.path.dirname(__file__), 'sample3.ini') app = loadapp('config:%s' % paste_conf) assert isinstance(app, CASServer) assert app.db_connection_string == 'sqlite:///:memory:' assert isinstance(app.auth, KofaAuthenticator) def DIStest_check_credentials(self): # we get real responses when querying Kofa instances auth = KofaAuthenticator() result1 = auth.check_credentials('bird', 'bebop') assert result1 == (True, '') result2 = auth.check_credentials('foo', 'bar') assert result2 == (False, 'Invalid username or password.')