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

Last change on this file since 6615 was 6615, checked in by uli, 13 years ago

Start tests for authenticator plugin and other authentication
components.

File size: 2.3 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 waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
7from waeup.sirp.authentication import (
8    UserAuthenticatorPlugin, Account, PrincipalInfo)
9
10class FakeSite(dict):
11    def getSiteManager(self):
12        return None
13
14class FakeSite(grok.Site, grok.Container):
15    pass
16class UserAuthenticatorPluginTests(FunctionalTestCase):
17    # Must be functional because of various utility lookups and the like
18
19    layer = FunctionalLayer
20
21    def setUp(self):
22        super(UserAuthenticatorPluginTests, self).setUp()
23        #site = FakeSite()
24        self.getRootFolder()['app'] = FakeSite()
25        self.site = self.getRootFolder()['app']
26        self.site['users'] = {'bob': Account('bob', 'secret')}
27        #self.site = FakeSite(
28        #    users = {'bob': Account('bob', 'secret')},
29        #    )
30        setSite(self.site)
31        return
32
33    def tearDown(self):
34        super(UserAuthenticatorPluginTests, self).tearDown()
35        clearSite(self.site)
36        return
37
38    def test_ifaces(self):
39        # make sure, interfaces requirements are met
40        plugin = UserAuthenticatorPlugin()
41        plugin.__parent__ = None # This attribute is required by iface
42        self.assertTrue(
43            verifyClass(IAuthenticatorPlugin, UserAuthenticatorPlugin))
44        self.assertTrue(verifyObject(IAuthenticatorPlugin, plugin))
45        return
46
47    def test_authenticate_credentials(self):
48        # make sure authentication works as expected
49        plugin = UserAuthenticatorPlugin()
50        result1 = plugin.authenticateCredentials(
51            dict(login='bob', password='secret'))
52        result2 = plugin.authenticateCredentials(
53            dict(login='bob', password='nonsense'))
54        self.assertTrue(isinstance(result1, PrincipalInfo))
55        self.assertTrue(result2 is None)
56        return
57
58    def test_principal_info(self):
59        # make sure we can get a principal info
60        plugin = UserAuthenticatorPlugin()
61        result1 = plugin.principalInfo('bob')
62        result2 = plugin.principalInfo('manfred')
63        self.assertTrue(isinstance(result1, PrincipalInfo))
64        self.assertTrue(result2 is None)
65        return
Note: See TracBrowser for help on using the repository browser.