[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testPrincipalRegistry.py:1.1.2.3

Guido van Rossum guido@python.org
Thu, 13 Dec 2001 12:22:47 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv31148/tests

Modified Files:
      Tag: Zope-3x-branch
	testPrincipalRegistry.py 
Log Message:
Implement authenticate() method that uses HTTP basic authentication.

=== Zope3/lib/python/Zope/App/Security/tests/testPrincipalRegistry.py 1.1.2.2 => 1.1.2.3 ===
 from Zope.App.Security.PrincipalRegistry import DuplicateLogin, DuplicateId
 from Zope.Exceptions import NotFoundError
+from Zope.ComponentArchitecture import _clear as clearCA
 
 class Test(unittest.TestCase):
 
     def setUp(self):
+        clearCA()
         self.reg = PrincipalRegistry()
 
         self.reg.definePrincipal('1', 'Tim Peters', 'Sir Tim Peters',
@@ -25,6 +27,9 @@
         self.reg.definePrincipal('2', 'Jim Fulton', 'Sir Jim Fulton',
                                  'jim', '456')
 
+    def tearDown(self):
+        clearCA()
+
     def testRegistered(self):
         p = self.reg.getPrincipal('1')
         self.assertEqual(p.getId(), '1')
@@ -72,6 +77,28 @@
         self.failIf(tim.validate(''))
         self.failIf(tim.validate('1234'))
         self.failIf(tim.validate('12'))
+
+    def testAuthenticate(self):
+        from Zope.Publisher.HTTP.IHTTPCredentials import IHTTPCredentials
+        from Zope.ComponentArchitecture import provideAdapter
+        class Request:
+            __implements__ = IHTTPCredentials
+            def __init__(self, lpw):
+                self.__lpw = lpw
+            def _authUserPW(self):
+                return self.__lpw
+        from Zope.App.Security.BasicAuthAdapter import BasicAuthAdapter
+        from Zope.App.Security.ILoginPassword import ILoginPassword
+        provideAdapter(IHTTPCredentials, ILoginPassword, BasicAuthAdapter)
+        req = Request(('tim', '123'))
+        pid = self.reg.authenticate(req)
+        self.assertEquals(pid, '1')
+        req = Request(('tim', '1234'))
+        pid = self.reg.authenticate(req)
+        self.assertEquals(pid, None)
+        req = Request(('kim', '123'))
+        pid = self.reg.authenticate(req)
+        self.assertEquals(pid, None)
 
 def test_suite():
     loader=unittest.TestLoader()