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

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

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1## $Id: test_authentication.py 7193 2011-11-25 07:21:29Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##import grok
18import unittest
19from zope.component.hooks import setSite, clearSite
20from zope.interface.verify import verifyClass, verifyObject
21from zope.pluggableauth.interfaces import IAuthenticatorPlugin
22from zope.securitypolicy.interfaces import IPrincipalRoleManager
23from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
24from waeup.sirp.authentication import (
25    UserAuthenticatorPlugin, Account, PrincipalInfo, get_principal_role_manager)
26
27class FakeSite(grok.Site, grok.Container):
28    pass
29
30class UserAuthenticatorPluginTests(FunctionalTestCase):
31    # Must be functional because of various utility lookups and the like
32
33    layer = FunctionalLayer
34
35    def setUp(self):
36        super(UserAuthenticatorPluginTests, self).setUp()
37        self.getRootFolder()['app'] = FakeSite()
38        self.site = self.getRootFolder()['app']
39        self.site['users'] = {'bob': Account('bob', 'secret')}
40        setSite(self.site)
41        return
42
43    def tearDown(self):
44        super(UserAuthenticatorPluginTests, self).tearDown()
45        clearSite(self.site)
46        return
47
48    def test_ifaces(self):
49        # make sure, interfaces requirements are met
50        plugin = UserAuthenticatorPlugin()
51        plugin.__parent__ = None # This attribute is required by iface
52        self.assertTrue(
53            verifyClass(IAuthenticatorPlugin, UserAuthenticatorPlugin))
54        self.assertTrue(verifyObject(IAuthenticatorPlugin, plugin))
55        return
56
57    def test_authenticate_credentials(self):
58        # make sure authentication works as expected
59        plugin = UserAuthenticatorPlugin()
60        result1 = plugin.authenticateCredentials(
61            dict(login='bob', password='secret'))
62        result2 = plugin.authenticateCredentials(
63            dict(login='bob', password='nonsense'))
64        self.assertTrue(isinstance(result1, PrincipalInfo))
65        self.assertTrue(result2 is None)
66        return
67
68    def test_principal_info(self):
69        # make sure we can get a principal info
70        plugin = UserAuthenticatorPlugin()
71        result1 = plugin.principalInfo('bob')
72        result2 = plugin.principalInfo('manfred')
73        self.assertTrue(isinstance(result1, PrincipalInfo))
74        self.assertTrue(result2 is None)
75        return
76
77    def test_get_principal_role_manager(self):
78        # make sure we get different role managers for different situations
79        prm1 = get_principal_role_manager()
80        clearSite(None)
81        prm2 = get_principal_role_manager()
82        self.assertTrue(IPrincipalRoleManager.providedBy(prm1))
83        self.assertTrue(IPrincipalRoleManager.providedBy(prm2))
84        self.assertTrue(prm1._context is self.site)
85        self.assertTrue(hasattr(prm2, '_context') is False)
86        return
Note: See TracBrowser for help on using the repository browser.