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