def test_get_lp_login_updates_authentication_conf(self): account._set_global_option('foo') self.assertIs(None, account._get_auth_user()) account.get_lp_login() auth = config.AuthenticationConfig() self.assertEqual('foo', account._get_auth_user(auth)) self.assertEqual('foo', auth.get_user('ssh', 'bazaar.launchpad.net')) self.assertEqual('foo', auth.get_user('ssh', 'bazaar.staging.launchpad.net'))
def test_get_lp_login_leaves_existing_credentials(self): auth = config.AuthenticationConfig() auth.set_credentials('Foo', 'bazaar.launchpad.net', 'foo', 'ssh') auth.set_credentials('Bar', 'bazaar.staging.launchpad.net', 'foo', 'ssh') account._set_global_option('foo') account.get_lp_login() auth = config.AuthenticationConfig() credentials = auth.get_credentials('ssh', 'bazaar.launchpad.net') self.assertEqual('Foo', credentials['name'])
def run(self, name=None, no_check=False, verbose=False): # This is totally separate from any launchpadlib login system. from bzrlib.plugins.launchpad import account check_account = not no_check if name is None: username = account.get_lp_login() if username: if check_account: account.check_lp_login(username) if verbose: self.outf.write(gettext( "Launchpad user ID exists and has SSH keys.\n")) self.outf.write(username + '\n') else: self.outf.write(gettext('No Launchpad user ID configured.\n')) return 1 else: name = name.lower() if check_account: account.check_lp_login(name) if verbose: self.outf.write(gettext( "Launchpad user ID exists and has SSH keys.\n")) account.set_lp_login(name) if verbose: self.outf.write(gettext("Launchpad user ID set to '%s'.\n") % (name,))
def run(self, name=None, no_check=False, verbose=False): # This is totally separate from any launchpadlib login system. from bzrlib.plugins.launchpad import account check_account = not no_check if name is None: username = account.get_lp_login() if username: if check_account: account.check_lp_login(username) if verbose: self.outf.write( gettext( "Launchpad user ID exists and has SSH keys.\n") ) self.outf.write(username + '\n') else: self.outf.write(gettext('No Launchpad user ID configured.\n')) return 1 else: name = name.lower() if check_account: account.check_lp_login(name) if verbose: self.outf.write( gettext( "Launchpad user ID exists and has SSH keys.\n")) account.set_lp_login(name) if verbose: self.outf.write( gettext("Launchpad user ID set to '%s'.\n") % (name, ))
def test_tilde_fails_no_login(self): factory = FakeResolveFactory( self, '~username/apt/test', dict(urls=[ 'bzr+ssh://bazaar.launchpad.net/~username/apt/test'])) self.assertIs(None, get_lp_login()) directory = LaunchpadDirectory() self.assertRaises(errors.InvalidURL, directory._resolve, 'lp:~/apt/test', factory)
def test_no_rewrite_of_other_bzr_ssh(self): # Test that we don't rewrite bzr+ssh URLs for other self.assertEqual(None, get_lp_login()) factory = FakeResolveFactory( self, 'apt', dict(urls=[ 'bzr+ssh://example.com/~apt/apt/devel', 'http://bazaar.launchpad.net/~apt/apt/devel'])) directory = LaunchpadDirectory() self.assertEquals('bzr+ssh://example.com/~apt/apt/devel', directory._resolve('lp:///apt', factory))
def test_no_rewrite_of_other_bzr_ssh(self): # Test that we don't rewrite bzr+ssh URLs for other self.assertEqual(None, get_lp_login()) factory = FakeResolveFactory( self, 'apt', dict(urls=[ 'bzr+ssh://example.com/~apt/apt/devel', 'http://bazaar.launchpad.net/~apt/apt/devel'])) directory = LaunchpadDirectory() self.assertEqual('bzr+ssh://example.com/~apt/apt/devel', directory._resolve('lp:///apt', factory))
def test_skip_sftp_launchpad_net_when_anonymous(self): # Test that sftp://bazaar.launchpad.net gets skipped if # Bazaar does not know the user's Launchpad ID: self.assertEqual(None, get_lp_login()) factory = FakeResolveFactory( self, 'apt', dict(urls=[ 'sftp://bazaar.launchpad.net/~apt/apt/devel', 'http://bazaar.launchpad.net/~apt/apt/devel'])) directory = LaunchpadDirectory() self.assertEqual('http://bazaar.launchpad.net/~apt/apt/devel', directory._resolve('lp:///apt', factory))
def test_skip_sftp_launchpad_net_when_anonymous(self): # Test that sftp://bazaar.launchpad.net gets skipped if # Bazaar does not know the user's Launchpad ID: self.assertEqual(None, get_lp_login()) factory = FakeResolveFactory( self, 'apt', dict(urls=[ 'sftp://bazaar.launchpad.net/~apt/apt/devel', 'http://bazaar.launchpad.net/~apt/apt/devel'])) directory = LaunchpadDirectory() self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel', directory._resolve('lp:///apt', factory))
def _resolve(self, url, _request_factory=ResolveLaunchpadPathRequest, _lp_login=None): """Resolve the base URL for this transport.""" url, path = self._update_url_scheme(url) if _lp_login is None: _lp_login = get_lp_login() path = path.strip('/') path = self._expand_user(path, url, _lp_login) if _lp_login is not None: result = self._resolve_locally(path, url, _request_factory) if 'launchpad' in debug.debug_flags: local_res = result result = self._resolve_via_xmlrpc(path, url, _request_factory) trace.note( gettext('resolution for {0}\n local: {1}\n remote: {2}'). format(url, local_res['urls'], result['urls'])) else: result = self._resolve_via_xmlrpc(path, url, _request_factory) if 'launchpad' in debug.debug_flags: trace.mutter("resolve_lp_path(%r) == %r", url, result) _warned_login = False for url in result['urls']: scheme, netloc, path, query, fragment = urlsplit(url) if self._requires_launchpad_login(scheme, netloc, path, query, fragment): # Only accept launchpad.net bzr+ssh URLs if we know # the user's Launchpad login: if _lp_login is not None: break if _lp_login is None: if not _warned_login: trace.warning( 'You have not informed bzr of your Launchpad ID, and you must do this to\n' 'write to Launchpad or access private data. See "bzr help launchpad-login".' ) _warned_login = True else: # Use the URL if we can create a transport for it. try: transport.get_transport(url) except (errors.PathError, errors.TransportError): pass else: break else: raise errors.InvalidURL(path=url, extra='no supported schemes') return url
def _resolve(self, url, _request_factory=ResolveLaunchpadPathRequest, _lp_login=None): """Resolve the base URL for this transport.""" url, path = self._update_url_scheme(url) if _lp_login is None: _lp_login = get_lp_login() path = path.strip('/') path = self._expand_user(path, url, _lp_login) if _lp_login is not None: result = self._resolve_locally(path, url, _request_factory) if 'launchpad' in debug.debug_flags: local_res = result result = self._resolve_via_xmlrpc(path, url, _request_factory) trace.note(gettext( 'resolution for {0}\n local: {1}\n remote: {2}').format( url, local_res['urls'], result['urls'])) else: result = self._resolve_via_xmlrpc(path, url, _request_factory) if 'launchpad' in debug.debug_flags: trace.mutter("resolve_lp_path(%r) == %r", url, result) _warned_login = False for url in result['urls']: scheme, netloc, path, query, fragment = urlsplit(url) if self._requires_launchpad_login(scheme, netloc, path, query, fragment): # Only accept launchpad.net bzr+ssh URLs if we know # the user's Launchpad login: if _lp_login is not None: break if _lp_login is None: if not _warned_login: trace.warning( 'You have not informed bzr of your Launchpad ID, and you must do this to\n' 'write to Launchpad or access private data. See "bzr help launchpad-login".') _warned_login = True else: # Use the URL if we can create a transport for it. try: transport.get_transport(url) except (errors.PathError, errors.TransportError): pass else: break else: raise errors.InvalidURL(path=url, extra='no supported schemes') return url
def _build_lp_push_suggestion(self, master_url): try: from bzrlib.plugins.launchpad import account except ImportError: # yes, ImportError is possible with bzr.exe, # because user has option to not install launchpad plugin at all return '' from bzrlib.plugins.qbzr.lib.util import launchpad_project_from_url user_name = account.get_lp_login() project_name = launchpad_project_from_url(master_url) branch_name = urlutils.basename(self.branch.base) if user_name and project_name and branch_name: return "lp:~%s/%s/%s" % (user_name, project_name, branch_name) else: return ''
def run(self, name=None, no_check=False): from bzrlib.plugins.launchpad import account check_account = not no_check if name is None: username = account.get_lp_login() if username: if check_account: account.check_lp_login(username) self.outf.write(username + '\n') else: self.outf.write('No Launchpad user ID configured.\n') return 1 else: if check_account: account.check_lp_login(name) account.set_lp_login(name)
class LaunchpadDirectory(object): def _requires_launchpad_login(self, scheme, netloc, path, query, fragment): """Does the URL require a Launchpad login in order to be reached? The URL is specified by its parsed components, as returned from urlsplit. """ return (scheme in ('bzr+ssh', 'sftp') and (netloc.endswith('launchpad.net') or netloc.endswith('launchpad.dev'))) def look_up(self, name, url): """See DirectoryService.look_up""" return self._resolve(url) def _resolve(self, url, _request_factory=ResolveLaunchpadPathRequest, _lp_login=None): """Resolve the base URL for this transport.""" result = urlsplit(url) # Perform an XMLRPC request to resolve the path lp_instance = result[1] if lp_instance == '': lp_instance = None elif lp_instance not in LaunchpadService.LAUNCHPAD_INSTANCE: raise errors.InvalidURL(path=url) resolve = _request_factory(result[2].strip('/')) service = LaunchpadService(lp_instance=lp_instance) try: result = resolve.submit(service) except xmlrpclib.Fault, fault: raise errors.InvalidURL(path=url, extra=fault.faultString) if 'launchpad' in debug.debug_flags: trace.mutter("resolve_lp_path(%r) == %r", path, result) if _lp_login is None: _lp_login = get_lp_login() _warned_login = False for url in result['urls']: scheme, netloc, path, query, fragment = urlsplit(url) if self._requires_launchpad_login(scheme, netloc, path, query, fragment): # Only accept launchpad.net bzr+ssh URLs if we know # the user's Launchpad login: if _lp_login is None: if not _warned_login: trace.warning( 'You have not informed bzr of your ' 'launchpad login. If you are attempting a\n' 'write operation and it fails, run ' '"bzr launchpad-login YOUR_ID" and try again.') _warned_login = True continue url = urlunsplit((scheme, '%s@%s' % (_lp_login, netloc), path, query, fragment)) break else: # Use the URL if we can create a transport for it. try: get_transport(url) except (errors.PathError, errors.TransportError): pass else: break else: raise errors.InvalidURL(path=url, extra='no supported schemes') return url
def test_login_with_name_sets_login(self): # lp-login with a 'name' parameter sets the Launchpad login. self.run_bzr(['launchpad-login', '--no-check', 'foo']) self.assertEqual('foo', account.get_lp_login())
def test_get_lp_login_does_not_update_for_none_user(self): account.get_lp_login() self.assertIs(None, account._get_auth_user())
def test_get_lp_login_unconfigured(self): # Test that get_lp_login() returns None if no username has # been configured. my_config = self.setup_config('') self.assertEqual(None, account.get_lp_login(my_config))
def test_get_lp_login(self): # Test that get_lp_login() returns the configured username my_config = self.setup_config( '[DEFAULT]\nlaunchpad_username=test-user\n') self.assertEqual('test-user', account.get_lp_login(my_config))