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