1 | ## $Id: test_authentication.py 7233 2011-11-28 21:04:57Z 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 | ## |
---|
18 | import grok |
---|
19 | import unittest |
---|
20 | from zope.component.hooks import setSite, clearSite |
---|
21 | from zope.interface.verify import verifyClass, verifyObject |
---|
22 | from zope.pluggableauth.interfaces import IAuthenticatorPlugin |
---|
23 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
24 | from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer |
---|
25 | from waeup.sirp.authentication import ( |
---|
26 | UserAuthenticatorPlugin, Account, SIRPPrincipalInfo, get_principal_role_manager) |
---|
27 | |
---|
28 | class FakeSite(grok.Site, grok.Container): |
---|
29 | pass |
---|
30 | |
---|
31 | class UserAuthenticatorPluginTests(FunctionalTestCase): |
---|
32 | # Must be functional because of various utility lookups and the like |
---|
33 | |
---|
34 | layer = FunctionalLayer |
---|
35 | |
---|
36 | def setUp(self): |
---|
37 | super(UserAuthenticatorPluginTests, self).setUp() |
---|
38 | self.getRootFolder()['app'] = FakeSite() |
---|
39 | self.site = self.getRootFolder()['app'] |
---|
40 | self.site['users'] = {'bob': Account('bob', 'secret')} |
---|
41 | setSite(self.site) |
---|
42 | return |
---|
43 | |
---|
44 | def tearDown(self): |
---|
45 | super(UserAuthenticatorPluginTests, self).tearDown() |
---|
46 | clearSite(self.site) |
---|
47 | return |
---|
48 | |
---|
49 | def test_ifaces(self): |
---|
50 | # make sure, interfaces requirements are met |
---|
51 | plugin = UserAuthenticatorPlugin() |
---|
52 | plugin.__parent__ = None # This attribute is required by iface |
---|
53 | self.assertTrue( |
---|
54 | verifyClass(IAuthenticatorPlugin, UserAuthenticatorPlugin)) |
---|
55 | self.assertTrue(verifyObject(IAuthenticatorPlugin, plugin)) |
---|
56 | return |
---|
57 | |
---|
58 | def test_authenticate_credentials(self): |
---|
59 | # make sure authentication works as expected |
---|
60 | plugin = UserAuthenticatorPlugin() |
---|
61 | result1 = plugin.authenticateCredentials( |
---|
62 | dict(login='bob', password='secret')) |
---|
63 | result2 = plugin.authenticateCredentials( |
---|
64 | dict(login='bob', password='nonsense')) |
---|
65 | self.assertTrue(isinstance(result1, SIRPPrincipalInfo)) |
---|
66 | self.assertTrue(result2 is None) |
---|
67 | return |
---|
68 | |
---|
69 | def test_principal_info(self): |
---|
70 | # make sure we can get a principal info |
---|
71 | plugin = UserAuthenticatorPlugin() |
---|
72 | result1 = plugin.principalInfo('bob') |
---|
73 | result2 = plugin.principalInfo('manfred') |
---|
74 | self.assertTrue(isinstance(result1, SIRPPrincipalInfo)) |
---|
75 | self.assertTrue(result2 is None) |
---|
76 | return |
---|
77 | |
---|
78 | def test_get_principal_role_manager(self): |
---|
79 | # make sure we get different role managers for different situations |
---|
80 | prm1 = get_principal_role_manager() |
---|
81 | clearSite(None) |
---|
82 | prm2 = get_principal_role_manager() |
---|
83 | self.assertTrue(IPrincipalRoleManager.providedBy(prm1)) |
---|
84 | self.assertTrue(IPrincipalRoleManager.providedBy(prm2)) |
---|
85 | self.assertTrue(prm1._context is self.site) |
---|
86 | self.assertTrue(hasattr(prm2, '_context') is False) |
---|
87 | return |
---|