Rev 2979: Fix bug #162494, 'bzr register-branch' needs proper auth handling. in http://bzr.arbash-meinel.com/branches/bzr/0.93-dev/launchpad_plugin_162494

John Arbash Meinel john at arbash-meinel.com
Tue Nov 13 22:03:11 GMT 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.93-dev/launchpad_plugin_162494

------------------------------------------------------------
revno: 2979
revision-id:john at arbash-meinel.com-20071113220221-6kkzbczr91r0gmbt
parent: pqm at pqm.ubuntu.com-20071109195036-5o5bwu0a01uniqwg
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: launchpad_plugin_162494
timestamp: Tue 2007-11-13 16:02:21 -0600
message:
  Fix bug #162494, 'bzr register-branch' needs proper auth handling.
  The recent fix to use ~/.bazaar/authentication.conf had some small bugs.
  It was not passing a user to auth.get_password(), and it was accidentally
  using a local variable with the same name as a global.
  Add tests that gather_user_credentials() gets proper values.
modified:
  bzrlib/plugins/launchpad/lp_registration.py lp_registration.py-20060315190948-daa617eafe3a8d48
  bzrlib/plugins/launchpad/test_register.py test_register.py-20060315182712-40f5dda945c829a8
  bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
-------------- next part --------------
=== modified file 'bzrlib/plugins/launchpad/lp_registration.py'
--- a/bzrlib/plugins/launchpad/lp_registration.py	2007-10-23 14:31:10 +0000
+++ b/bzrlib/plugins/launchpad/lp_registration.py	2007-11-13 22:02:21 +0000
@@ -95,8 +95,8 @@
 
     def gather_user_credentials(self):
         """Get the password from the user."""
-        config = config.GlobalConfig()
-        self.registrant_email = config.user_email()
+        the_config = config.GlobalConfig()
+        self.registrant_email = the_config.user_email()
         if self.registrant_password is None:
             auth = config.AuthenticationConfig()
             scheme, hostinfo = urlsplit(self.service_url)[:2]
@@ -105,6 +105,7 @@
             # We will reuse http[s] credentials if we can, prompt user
             # otherwise
             self.registrant_password = auth.get_password(scheme, hostinfo,
+                                                         self.registrant_email,
                                                          prompt=prompt)
 
     def send_request(self, method_name, method_params):

=== modified file 'bzrlib/plugins/launchpad/test_register.py'
--- a/bzrlib/plugins/launchpad/test_register.py	2007-10-12 03:18:30 +0000
+++ b/bzrlib/plugins/launchpad/test_register.py	2007-11-13 22:02:21 +0000
@@ -17,8 +17,15 @@
 import base64
 import os
 from StringIO import StringIO
+import urlparse
 import xmlrpclib
 
+from bzrlib import (
+    config,
+    osutils,
+    tests,
+    ui,
+    )
 from bzrlib.tests import TestCase, TestSkipped
 
 # local import
@@ -124,8 +131,7 @@
     def setUp(self):
         super(TestBranchRegistration, self).setUp()
         # make sure we have a reproducible standard environment
-        if 'BZR_LP_XMLRPC_URL' in os.environ:
-            del os.environ['BZR_LP_XMLRPC_URL']
+        self._captureVar('BZR_LP_XMLRPC_URL', None)
 
     def test_register_help(self):
         """register-branch accepts --help"""
@@ -224,3 +230,55 @@
         rego = BranchBugLinkRequest('http://server/branch', 1234)
         result = rego.submit(service)
         self.assertEquals(result, 'http://launchpad.net/bug/1234')
+
+
+class TestGatherUserCredentials(tests.TestCaseInTempDir):
+
+    def setUp(self):
+        super(TestGatherUserCredentials, self).setUp()
+        # make sure we have a reproducible standard environment
+        self._captureVar('BZR_LP_XMLRPC_URL', None)
+
+    def test_gather_user_credentials_has_password(self):
+        service = LaunchpadService()
+        service.registrant_password = 'mypassword'
+        # This should be a basic no-op, since we already have the password
+        service.gather_user_credentials()
+        self.assertEqual('mypassword', service.registrant_password)
+
+    def test_gather_user_credentials_from_auth_conf(self):
+        auth_path = config.authentication_config_filename()
+        service = LaunchpadService()
+        g_conf = config.GlobalConfig()
+        g_conf.set_user_option('email', 'Test User <test at user.com>')
+        f = open(auth_path, 'wb')
+        try:
+            scheme, hostinfo = urlparse.urlsplit(service.service_url)[:2]
+            f.write('[section]\n'
+                    'scheme=%s\n'
+                    'host=%s\n'
+                    'user=test at user.com\n'
+                    'password=testpass\n'
+                    % (scheme, hostinfo))
+        finally:
+            f.close()
+        self.assertIs(None, service.registrant_password)
+        service.gather_user_credentials()
+        self.assertEqual('test at user.com', service.registrant_email)
+        self.assertEqual('testpass', service.registrant_password)
+
+    def test_gather_user_credentials_prompts(self):
+        service = LaunchpadService()
+        self.assertIs(None, service.registrant_password)
+        g_conf = config.GlobalConfig()
+        g_conf.set_user_option('email', 'Test User <test at user.com>')
+        stdout = tests.StringIOWrapper()
+        ui.ui_factory = tests.TestUIFactory(stdin='userpass\n',
+                                            stdout=stdout)
+        self.assertIs(None, service.registrant_password)
+        service.gather_user_credentials()
+        self.assertEqual('test at user.com', service.registrant_email)
+        self.assertEqual('userpass', service.registrant_password)
+        self.assertContainsRe(stdout.getvalue(),
+                             'launchpad.net password for test at user\\.com')
+

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2007-10-23 14:31:10 +0000
+++ b/bzrlib/tests/test_config.py	2007-11-13 22:02:21 +0000
@@ -1133,7 +1133,7 @@
         self.assertEquals(expected_user, user)
         self.assertEquals(expected_password, password)
 
-    def  test_empty_config(self):
+    def test_empty_config(self):
         conf = config.AuthenticationConfig(_file=StringIO())
         self.assertEquals({}, conf._get_config())
         self._got_user_passwd(None, None, conf, 'http', 'foo.net')



More information about the bazaar-commits mailing list