import grok import unittest from zope.component.hooks import setSite, clearSite from zope.interface.verify import verifyClass, verifyObject from zope.pluggableauth.interfaces import IAuthenticatorPlugin from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer from waeup.sirp.authentication import ( UserAuthenticatorPlugin, Account, PrincipalInfo) class FakeSite(grok.Site, grok.Container): pass class UserAuthenticatorPluginTests(FunctionalTestCase): # Must be functional because of various utility lookups and the like layer = FunctionalLayer def setUp(self): super(UserAuthenticatorPluginTests, self).setUp() self.getRootFolder()['app'] = FakeSite() self.site = self.getRootFolder()['app'] self.site['users'] = {'bob': Account('bob', 'secret')} setSite(self.site) return def tearDown(self): super(UserAuthenticatorPluginTests, self).tearDown() clearSite(self.site) return def test_ifaces(self): # make sure, interfaces requirements are met plugin = UserAuthenticatorPlugin() plugin.__parent__ = None # This attribute is required by iface self.assertTrue( verifyClass(IAuthenticatorPlugin, UserAuthenticatorPlugin)) self.assertTrue(verifyObject(IAuthenticatorPlugin, plugin)) return def test_authenticate_credentials(self): # make sure authentication works as expected plugin = UserAuthenticatorPlugin() result1 = plugin.authenticateCredentials( dict(login='bob', password='secret')) result2 = plugin.authenticateCredentials( dict(login='bob', password='nonsense')) self.assertTrue(isinstance(result1, PrincipalInfo)) self.assertTrue(result2 is None) return def test_principal_info(self): # make sure we can get a principal info plugin = UserAuthenticatorPlugin() result1 = plugin.principalInfo('bob') result2 = plugin.principalInfo('manfred') self.assertTrue(isinstance(result1, PrincipalInfo)) self.assertTrue(result2 is None) return