[Zope3-checkins] CVS: Zope3/src/zope/app/services/pluggableauth/tests - test_pluggableauth.py:1.1.2.2

Chris McDonough chrism@zope.com
Mon, 23 Jun 2003 16:17:35 -0400


Update of /cvs-repository/Zope3/src/zope/app/services/pluggableauth/tests
In directory cvs.zope.org:/tmp/cvs-serv8385/src/zope/app/services/pluggableauth/tests

Modified Files:
      Tag: pluggable_authentication_service-branch
	test_pluggableauth.py 
Log Message:
Change earmark style.


=== Zope3/src/zope/app/services/pluggableauth/tests/test_pluggableauth.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/services/pluggableauth/tests/test_pluggableauth.py:1.1.2.1	Sun Jun 22 21:08:23 2003
+++ Zope3/src/zope/app/services/pluggableauth/tests/test_pluggableauth.py	Mon Jun 23 16:17:35 2003
@@ -32,15 +32,15 @@
 
 from zope.app.services.pluggableauth import BTreePrincipalSource, \
      SimplePrincipal, PluggableAuthenticationService, \
-     PrincipalAuthenticationView
+     PrincipalAuthenticationView, PrincipalWrapper
 
 from zope.publisher.browser import TestRequest as Request
 
 import base64
 
 
-class AuthServiceTest(placefulsetup.PlacefulSetup, TestCase):
-
+class Setup(placefulsetup.PlacefulSetup, TestCase):
+    
     def setUp(self):
         from zope.component.view import viewService
         from zope.app.interfaces.services.pluggableauth import IPrincipalSource
@@ -68,7 +68,11 @@
         default = traverse(folder, '++etc++site/default')
         key = default.setObject("AuthenticationService",
                                 PluggableAuthenticationService())
+
         auth = traverse(default, key)
+        auth.earmark = 'earmark'
+        # XXX - not sure why this is needed, it should be assigned by
+        # afterAddHook
 
         path = getPath(auth)
 
@@ -93,12 +97,6 @@
         self._two.setObject('slinkp2', self._slinkp2)
         self._two.setObject('chrism2', self._chrism2)
 
-
-    def testGetPrincipal(self):
-        auth = self._auth
-        self.assertEqual(self._slinkp,
-            auth.getPrincipal((auth.earmark, 'one', 'slinkp')))
-
     def getRequest(self, uid=None, passwd=None):
         if uid is None:
             return Request()
@@ -109,8 +107,11 @@
             "Basic %s" % base64.encodestring('%s:%s' % (uid, passwd))
          }
         return Request(**dict)
+
+
+class AuthServiceTest(Setup):
         
-    def testAuthenticate(self):
+    def testAuthServiceAuthenticate(self):
         auth = self._auth
         req = self.getRequest('slinkp', '123')
         pid = auth.authenticate(req).getLogin()
@@ -126,28 +127,70 @@
         auth = self._auth
         self.assertEqual(None, auth.unauthenticatedPrincipal())
 
-##     def testGetPrincipal(self):
-##         auth = self._auth
-##         self.assertEqual(auth['srichter'], auth.getPrincipal('srichter'))
-##         self.assertRaises(NotFoundError, auth.getPrincipal, 'srichter2')
+    def testUnauthorized(self):
+        auth = self._auth
+        req = self.getRequest('nobody', 'nopass')
+        self.assertEqual(None, auth.unauthorized((None, None, None), req))
+        
+    def _fail_NoSourceId(self):
+        self._auth.getPrincipal((self._auth.earmark, None, None))
+
+    def _fail_BadIdType(self):
+        self._auth.getPrincipal((self._auth.earmark, None, None))
+
+    def _fail_BadIdLength(self):
+        self._auth.getPrincipal((self._auth.earmark, None, None))
+
+    def testAuthServiceGetPrincipal(self):
+        auth = self._auth
+        id = '\t'.join((auth.earmark, 'one', str(self._slinkp.getId())))
+        self.assertEqual(self._slinkp, auth.getPrincipal(id))
+        self.assertRaises(NotFoundError, self._fail_NoSourceId)
+        self.assertRaises(NotFoundError, self._fail_BadIdType)
+        self.assertRaises(NotFoundError, self._fail_BadIdLength)
 
     def testGetPrincipals(self):
         auth = self._auth
         self.failUnless(self._slinkp in auth.getPrincipals('slinkp'))
         self.failUnless(self._slinkp2 in auth.getPrincipals('slinkp'))
 
-    def _fail(self):
-        self._auth.getPrincipal((self._auth.earmark, None, None))
+    
+    def testPrincipalWrapper(self):
+        wrapper = PrincipalWrapper(self._slinkp, self._auth, id='wrong')
+        self.assertEqual(wrapper.getId(), 'wrong')
+
+
+class BTreePrincipalSourceTest(Setup):
+    
+    def test_authenticate(self):
+        one = self._one
+        self.assertEqual(None, one.authenticate('bogus', 'bogus'))
+        self.assertEqual(self._slinkp, one.authenticate('slinkp', '123'))
+        self.assertEqual(None, one.authenticate('slinkp', 'not really'))
+
+    def test_getPrincipal(self):
+        one = self._one
+        p = self._slinkp
+        self.assertEqual(p, one.getPrincipal(p.getId()))
+        self.assertRaises(NotFoundError, one.getPrincipal, None)
+
+class PrincipalAuthenticationViewTest(Setup):
+
+    def test_authenticate(self):
+        request = self.getRequest('chrism', '123')
+        view = PrincipalAuthenticationView(self._one, request)
+        self.assertEqual(self._chrism, view.authenticate())
+        
         
-    def testGetPrincipalId(self):
-        auth = self._auth
-        self.assertRaises(NotFoundError, self._fail)
-        p = auth.getPrincipal((auth.earmark, "one", self._slinkp.getId()))
-        self.assertEqual(p, self._slinkp)
-                         
 def test_suite():
     t1 = makeSuite(AuthServiceTest)
-    return TestSuite((t1,))
+    from zope.testing.doctestunit import DocTestSuite
+    t2 = DocTestSuite('zope.app.services.pluggableauth')
+    t3 = makeSuite(BTreePrincipalSourceTest)
+    t4 = makeSuite(PrincipalAuthenticationViewTest)
+    return TestSuite((t1, t2, t3, t4))
+    
 
 if __name__=='__main__':
     main(defaultTest='test_suite')
+