[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/app/tests/ If a username:password is passed as the Authorization header, use base64.encodestring() on it.

Sidnei da Silva sidnei at awkly.org
Thu Oct 7 15:30:55 EDT 2004


Log message for revision 27787:
  If a username:password is passed as the Authorization header, use base64.encodestring() on it.


Changed:
  U   Zope3/branches/ZopeX3-3.0/src/zope/app/tests/functional.py
  U   Zope3/branches/ZopeX3-3.0/src/zope/app/tests/test_functional.py


-=-
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/tests/functional.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/tests/functional.py	2004-10-07 19:20:01 UTC (rev 27786)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/tests/functional.py	2004-10-07 19:30:55 UTC (rev 27787)
@@ -491,6 +491,10 @@
             name = 'HTTP_' + name
         environment[name] = value.rstrip()
 
+    auth_key = 'HTTP_AUTHORIZATION'
+    if environment.has_key(auth_key):
+        environment[auth_key] = auth_header(environment[auth_key])
+
     outstream = HTTPTaskStub()
 
 
@@ -532,6 +536,20 @@
 def split_header(header):
     return headerre.match(header).group(1, 2)
 
+basicre = re.compile('Basic (.+)?:(.+)?$')
+def auth_header(header):
+    match = basicre.match(header)
+    if match:
+        import base64
+        u, p = match.group(1, 2)
+        if u is None:
+            u = ''
+        if p is None:
+            p = ''
+        auth = base64.encodestring('%s:%s' % (u, p))
+        return 'Basic %s' % auth[:-1]
+    return header
+
 def getRootFolder():
     return FunctionalTestSetup().getRootFolder()
 

Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/tests/test_functional.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/tests/test_functional.py	2004-10-07 19:20:01 UTC (rev 27786)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/tests/test_functional.py	2004-10-07 19:30:55 UTC (rev 27787)
@@ -55,6 +55,39 @@
         self.body_output.write(BODY)
         self.assertEqual(self.wrapper.getOutput(), BODY)
 
+class AuthHeaderTestCase(unittest.TestCase):
 
+    def test_auth_encoded(self):
+        auth_header = zope.app.tests.functional.auth_header
+        header = 'Basic Z2xvYmFsbWdyOmdsb2JhbG1ncnB3'
+        self.assertEquals(auth_header(header), header)
+
+    def test_auth_non_encoded(self):
+        auth_header = zope.app.tests.functional.auth_header
+        header = 'Basic globalmgr:globalmgrpw'
+        expected = 'Basic Z2xvYmFsbWdyOmdsb2JhbG1ncnB3'
+        self.assertEquals(auth_header(header), expected)
+
+    def test_auth_non_encoded_empty(self):
+        auth_header = zope.app.tests.functional.auth_header
+        header = 'Basic globalmgr:'
+        expected = 'Basic Z2xvYmFsbWdyOg=='
+        self.assertEquals(auth_header(header), expected)
+        header = 'Basic :pass'
+        expected = 'Basic OnBhc3M='
+        self.assertEquals(auth_header(header), expected)
+
+    def test_auth_non_encoded_colon(self):
+        auth_header = zope.app.tests.functional.auth_header
+        header = 'Basic globalmgr:pass:pass'
+        expected = 'Basic Z2xvYmFsbWdyOnBhc3M6cGFzcw=='
+        self.assertEquals(auth_header(header), expected)
+
 def test_suite():
-    return unittest.makeSuite(DocResponseWrapperTestCase)
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(DocResponseWrapperTestCase))
+    suite.addTest(unittest.makeSuite(AuthHeaderTestCase))
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')



More information about the Zope3-Checkins mailing list