1 | import grok |
---|
2 | import unittest |
---|
3 | from zope.component.hooks import setSite, clearSite |
---|
4 | from zope.interface.verify import verifyClass, verifyObject |
---|
5 | from zope.pluggableauth.interfaces import IAuthenticatorPlugin |
---|
6 | from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer |
---|
7 | from waeup.sirp.authentication import ( |
---|
8 | UserAuthenticatorPlugin, Account, PrincipalInfo) |
---|
9 | |
---|
10 | class FakeSite(dict): |
---|
11 | def getSiteManager(self): |
---|
12 | return None |
---|
13 | |
---|
14 | class FakeSite(grok.Site, grok.Container): |
---|
15 | pass |
---|
16 | class 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 |
---|