def web_root_for_service_root(service_root): """Turn a service root URL into a web root URL. This is done heuristically, not with a lookup. """ service_root = lookup_service_root(service_root) web_root_uri = URI(service_root) web_root_uri.path = "" web_root_uri.host = web_root_uri.host.replace("api.", "", 1) web_root = str(web_root_uri.ensureSlash()) return web_root
def test_uriToPath(self): # uriToPath only supports our own URLs with certain schemes. uri = URI(config.codehosting.git_anon_root) uri.path = "/~foo/bar/baz" # Test valid schemes. for scheme in ("git", "git+ssh", "https", "ssh"): uri.scheme = scheme self.assertEqual("~foo/bar/baz", self.lookup.uriToPath(uri)) # Test an invalid scheme. uri.scheme = "ftp" self.assertIsNone(self.lookup.uriToPath(uri)) # Test valid scheme but invalid domain. uri.scheme = 'sftp' uri.host = 'example.com' self.assertIsNone(self.lookup.uriToPath(uri))
def get_token_and_login(cls, consumer_name, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, authorizer_class=AuthorizeRequestTokenWithBrowser, allow_access_levels=[], max_failed_attempts=3, version=DEFAULT_VERSION): """Get credentials from Launchpad and log into the service root. This is a convenience method which will open up the user's preferred web browser and thus should not be used by most applications. Applications should, instead, use Credentials.get_request_token() to obtain the authorization URL and Credentials.exchange_request_token_for_access_token() to obtain the actual OAuth access token. This method will negotiate an OAuth access token with the service provider, but to complete it we will need the user to log into Launchpad and authorize us, so we'll open the authorization page in a web browser and ask the user to come back here and tell us when they finished the authorization process. :param consumer_name: The consumer name, as appropriate for the `Consumer` constructor :type consumer_name: string :param service_root: The URL to the root of the web service. :type service_root: string :return: The web service root :rtype: `Launchpad` """ credentials = Credentials(consumer_name) service_root = uris.lookup_service_root(service_root) web_root_uri = URI(service_root) web_root_uri.path = "" web_root_uri.host = web_root_uri.host.replace("api.", "", 1) web_root = str(web_root_uri.ensureSlash()) authorization_json = credentials.get_request_token( web_root=web_root, token_format=Credentials.DICT_TOKEN_FORMAT) authorizer = authorizer_class( web_root, authorization_json['oauth_token_consumer'], authorization_json['oauth_token'], allow_access_levels, max_failed_attempts) authorizer() credentials.exchange_request_token_for_access_token(web_root) return cls(credentials, service_root, cache, timeout, proxy_info, version)
def test_uriToHostingPath(self): """Ensure uriToHostingPath works. Only codehosting-based using http, sftp or bzr+ssh URLs will be handled. If any other URL gets passed (including lp), the return value will be None. """ branch_set = getUtility(IBranchLookup) uri = URI(config.codehosting.supermirror_root) uri.path = '/~foo/bar/baz' # Test valid schemes uri.scheme = 'http' self.assertEqual('~foo/bar/baz', branch_set.uriToHostingPath(uri)) uri.scheme = 'sftp' self.assertEqual('~foo/bar/baz', branch_set.uriToHostingPath(uri)) uri.scheme = 'bzr+ssh' self.assertEqual('~foo/bar/baz', branch_set.uriToHostingPath(uri)) # Test invalid scheme uri.scheme = 'ftp' self.assertIs(None, branch_set.uriToHostingPath(uri)) # Test valid scheme, invalid domain uri.scheme = 'sftp' uri.host = 'example.com' self.assertIs(None, branch_set.uriToHostingPath(uri))