Rev 5487: (mbp) avoid "KeyError: port" in urllib authentication callback (Martin Pool) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Oct 12 10:03:00 BST 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5487 [merge]
revision-id: pqm at pqm.ubuntu.com-20101012090258-dxr8yxh0wnpv8jh2
parent: pqm at pqm.ubuntu.com-20101012080045-hz4dpwaq7tq8f5k1
parent: mbp at sourcefrog.net-20101012072446-cnxz39kdn4ig9to8
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2010-10-12 10:02:58 +0100
message:
  (mbp) avoid "KeyError: port" in urllib authentication callback (Martin Pool)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/tests/test_http.py      testhttp.py-20051018020158-b2eef6e867c514d9
  bzrlib/transport/http/_urllib2_wrappers.py _urllib2_wrappers.py-20060913231729-ha9ugi48ktx481ao-1
=== modified file 'NEWS'
--- a/NEWS	2010-10-12 08:00:45 +0000
+++ b/NEWS	2010-10-12 09:02:58 +0000
@@ -35,6 +35,10 @@
 * Don't force openssh to use protocol=2, since that is now the default.
   (Neil Martinsen-Burrell, #561061)
 
+* Fix ``KeyError: 'port'`` when getting the stored password for an http
+  URL.
+  (Martin Pool, #654684)
+
 * Make ``bzr tag --quiet`` really quiet. (Neil Martinsen-Burrell, #239523)
 
 Improvements

=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py	2010-10-11 22:40:23 +0000
+++ b/bzrlib/tests/test_http.py	2010-10-12 07:24:46 +0000
@@ -1604,11 +1604,11 @@
         ui.ui_factory = tests.TestUIFactory(stdin=stdin_content,
                                             stderr=tests.StringIOWrapper())
         # Create a minimal config file with the right password
-        conf = config.AuthenticationConfig()
-        conf._get_config().update(
-            {'httptest': {'scheme': 'http', 'port': self.server.port,
-                          'user': user, 'password': password}})
-        conf._save()
+        _setup_authentication_config(
+            scheme='http', 
+            port=self.server.port,
+            user=user,
+            password=password)
         # Issue a request to the server to connect
         self.assertEqual('contents of a\n',t.get('a').read())
         # stdin should have  been left untouched
@@ -1616,25 +1616,6 @@
         # Only one 'Authentication Required' error should occur
         self.assertEqual(1, self.server.auth_required_errors)
 
-    def test_user_from_auth_conf(self):
-        if self._testing_pycurl():
-            raise tests.TestNotApplicable(
-                'pycurl does not support authentication.conf')
-        user = 'joe'
-        password = 'foo'
-        self.server.add_user(user, password)
-        # Create a minimal config file with the right password
-        conf = config.AuthenticationConfig()
-        conf._get_config().update(
-            {'httptest': {'scheme': 'http', 'port': self.server.port,
-                          'user': user, 'password': password}})
-        conf._save()
-        t = self.get_user_transport(None, None)
-        # Issue a request to the server to connect
-        self.assertEqual('contents of a\n', t.get('a').read())
-        # Only one 'Authentication Required' error should occur
-        self.assertEqual(1, self.server.auth_required_errors)
-
     def test_changing_nonce(self):
         if self._auth_server not in (http_utils.HTTPDigestAuthServer,
                                      http_utils.ProxyDigestAuthServer):
@@ -1656,6 +1637,56 @@
         # initial 'who are you' and a second 'who are you' with the new nonce)
         self.assertEqual(2, self.server.auth_required_errors)
 
+    def test_user_from_auth_conf(self):
+        if self._testing_pycurl():
+            raise tests.TestNotApplicable(
+                'pycurl does not support authentication.conf')
+        user = 'joe'
+        password = 'foo'
+        self.server.add_user(user, password)
+        _setup_authentication_config(
+            scheme='http', 
+            port=self.server.port,
+            user=user,
+            password=password)
+        t = self.get_user_transport(None, None)
+        # Issue a request to the server to connect
+        self.assertEqual('contents of a\n', t.get('a').read())
+        # Only one 'Authentication Required' error should occur
+        self.assertEqual(1, self.server.auth_required_errors)
+
+
+def _setup_authentication_config(**kwargs):
+    conf = config.AuthenticationConfig()
+    conf._get_config().update({'httptest': kwargs})
+    conf._save()
+
+
+
+class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
+    """Unit tests for glue by which urllib2 asks us for authentication"""
+
+    def test_get_user_password_without_port(self):
+        """We cope if urllib2 doesn't tell us the port.
+
+        See https://bugs.launchpad.net/bzr/+bug/654684
+        """
+        user = 'joe'
+        password = 'foo'
+        _setup_authentication_config(
+            scheme='http', 
+            host='localhost',
+            user=user,
+            password=password)
+        handler = _urllib2_wrappers.HTTPAuthHandler()
+        got_pass = handler.get_user_password(dict(
+            user='joe',
+            protocol='http',
+            host='localhost',
+            path='/',
+            realm='Realm',
+            ))
+        self.assertEquals((user, password), got_pass)
 
 
 class TestProxyAuth(TestAuth):

=== modified file 'bzrlib/transport/http/_urllib2_wrappers.py'
--- a/bzrlib/transport/http/_urllib2_wrappers.py	2010-10-08 10:50:51 +0000
+++ b/bzrlib/transport/http/_urllib2_wrappers.py	2010-10-12 07:24:46 +0000
@@ -1202,15 +1202,17 @@
         user = auth.get('user', None)
         password = auth.get('password', None)
         realm = auth['realm']
+        port = auth.get('port', None)
 
         if user is None:
             user = auth_conf.get_user(auth['protocol'], auth['host'],
-                                      port=auth['port'], path=auth['path'],
+                                      port=port, path=auth['path'],
                                       realm=realm, ask=True,
                                       prompt=self.build_username_prompt(auth))
         if user is not None and password is None:
             password = auth_conf.get_password(
-                auth['protocol'], auth['host'], user, port=auth['port'],
+                auth['protocol'], auth['host'], user,
+                port=port,
                 path=auth['path'], realm=realm,
                 prompt=self.build_password_prompt(auth))
 




More information about the bazaar-commits mailing list