[6615] | 1 | import grok |
---|
| 2 | import unittest |
---|
| 3 | from zope.component.hooks import setSite, clearSite |
---|
| 4 | from zope.interface.verify import verifyClass, verifyObject |
---|
| 5 | from zope.pluggableauth.interfaces import IAuthenticatorPlugin |
---|
[6673] | 6 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[6615] | 7 | from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer |
---|
| 8 | from waeup.sirp.authentication import ( |
---|
[6673] | 9 | UserAuthenticatorPlugin, Account, PrincipalInfo, get_principal_role_manager) |
---|
[6615] | 10 | |
---|
| 11 | class FakeSite(grok.Site, grok.Container): |
---|
| 12 | pass |
---|
[6617] | 13 | |
---|
[6615] | 14 | class UserAuthenticatorPluginTests(FunctionalTestCase): |
---|
| 15 | # Must be functional because of various utility lookups and the like |
---|
| 16 | |
---|
| 17 | layer = FunctionalLayer |
---|
| 18 | |
---|
| 19 | def setUp(self): |
---|
| 20 | super(UserAuthenticatorPluginTests, self).setUp() |
---|
| 21 | self.getRootFolder()['app'] = FakeSite() |
---|
| 22 | self.site = self.getRootFolder()['app'] |
---|
| 23 | self.site['users'] = {'bob': Account('bob', 'secret')} |
---|
| 24 | setSite(self.site) |
---|
| 25 | return |
---|
| 26 | |
---|
| 27 | def tearDown(self): |
---|
| 28 | super(UserAuthenticatorPluginTests, self).tearDown() |
---|
| 29 | clearSite(self.site) |
---|
| 30 | return |
---|
| 31 | |
---|
| 32 | def test_ifaces(self): |
---|
| 33 | # make sure, interfaces requirements are met |
---|
| 34 | plugin = UserAuthenticatorPlugin() |
---|
| 35 | plugin.__parent__ = None # This attribute is required by iface |
---|
| 36 | self.assertTrue( |
---|
| 37 | verifyClass(IAuthenticatorPlugin, UserAuthenticatorPlugin)) |
---|
| 38 | self.assertTrue(verifyObject(IAuthenticatorPlugin, plugin)) |
---|
| 39 | return |
---|
| 40 | |
---|
| 41 | def test_authenticate_credentials(self): |
---|
| 42 | # make sure authentication works as expected |
---|
| 43 | plugin = UserAuthenticatorPlugin() |
---|
| 44 | result1 = plugin.authenticateCredentials( |
---|
| 45 | dict(login='bob', password='secret')) |
---|
| 46 | result2 = plugin.authenticateCredentials( |
---|
| 47 | dict(login='bob', password='nonsense')) |
---|
| 48 | self.assertTrue(isinstance(result1, PrincipalInfo)) |
---|
| 49 | self.assertTrue(result2 is None) |
---|
| 50 | return |
---|
| 51 | |
---|
| 52 | def test_principal_info(self): |
---|
| 53 | # make sure we can get a principal info |
---|
| 54 | plugin = UserAuthenticatorPlugin() |
---|
| 55 | result1 = plugin.principalInfo('bob') |
---|
| 56 | result2 = plugin.principalInfo('manfred') |
---|
| 57 | self.assertTrue(isinstance(result1, PrincipalInfo)) |
---|
| 58 | self.assertTrue(result2 is None) |
---|
| 59 | return |
---|
[6673] | 60 | |
---|
| 61 | def test_get_principal_role_manager(self): |
---|
| 62 | # make sure we get different role managers for different situations |
---|
| 63 | prm1 = get_principal_role_manager() |
---|
| 64 | clearSite(None) |
---|
| 65 | prm2 = get_principal_role_manager() |
---|
| 66 | self.assertTrue(IPrincipalRoleManager.providedBy(prm1)) |
---|
| 67 | self.assertTrue(IPrincipalRoleManager.providedBy(prm2)) |
---|
| 68 | self.assertTrue(prm1._context is self.site) |
---|
| 69 | self.assertTrue(hasattr(prm2, '_context') is False) |
---|
| 70 | return |
---|