source: main/waeup.sirp/trunk/src/waeup/sirp/tests/test_authentication.py @ 7162

Last change on this file since 7162 was 7137, checked in by Henrik Bettermann, 13 years ago

Set value Id for property svn:keywords in all Python files.

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1import grok
2import unittest
3from zope.component.hooks import setSite, clearSite
4from zope.interface.verify import verifyClass, verifyObject
5from zope.pluggableauth.interfaces import IAuthenticatorPlugin
6from zope.securitypolicy.interfaces import IPrincipalRoleManager
7from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
8from waeup.sirp.authentication import (
9    UserAuthenticatorPlugin, Account, PrincipalInfo, get_principal_role_manager)
10
11class FakeSite(grok.Site, grok.Container):
12    pass
13
14class 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
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
Note: See TracBrowser for help on using the repository browser.