def test_lookups(self): """Ensure that short service names turn into long service names.""" # If the service name is a known alias, lookup methods convert # it to a URL. with self.edge_deprecation_error(): for alias in self.aliases: self.assertEqual( uris.lookup_service_root(alias), uris.service_roots[alias]) with self.edge_deprecation_error(): for alias in self.aliases: self.assertEqual( uris.lookup_web_root(alias), uris.web_roots[alias]) # If the service name is a valid URL, lookup methods let it # through. other_root = "http://some-other-server.com" self.assertEqual(uris.lookup_service_root(other_root), other_root) self.assertEqual(uris.lookup_web_root(other_root), other_root) # Otherwise, lookup methods raise an exception. not_a_url = "not-a-url" self.assertRaises(ValueError, uris.lookup_service_root, not_a_url) self.assertRaises(ValueError, uris.lookup_web_root, not_a_url)
def test_lookups(self): """Ensure that short service names turn into long service names.""" # If the service name is a known alias, lookup methods convert # it to a URL. with self.edge_deprecation_error(): for alias in self.aliases: self.assertEqual( uris.lookup_service_root(alias), uris.service_roots[alias]) with self.edge_deprecation_error(): for alias in self.aliases: self.assertEqual( uris.lookup_web_root(alias), uris.web_roots[alias]) # If the service name is a valid URL, lookup methods let it # through. other_root = "http://some-other-server.com" self.assertEqual(uris.lookup_service_root(other_root), other_root) self.assertEqual(uris.lookup_web_root(other_root), other_root) # Otherwise, lookup methods raise an exception. not_a_url = "not-a-url" self.assertRaises(ValueError, uris.lookup_service_root, not_a_url) self.assertRaises(ValueError, uris.lookup_web_root, not_a_url)
def exchange_request_token_for_access_token( self, web_root=uris.STAGING_WEB_ROOT): """Exchange the previously obtained request token for an access token. This method must not be called unless get_request_token() has been called and completed successfully. The access token will be stored as self.access_token. :param web_root: The base URL of the website that granted the request token. """ assert self._request_token is not None, ( "get_request_token() doesn't seem to have been called.") web_root = uris.lookup_web_root(web_root) params = dict( oauth_consumer_key=self.consumer.key, oauth_signature_method='PLAINTEXT', oauth_token=self._request_token.key, oauth_signature='&%s' % self._request_token.secret) url = web_root + access_token_page headers = {'Referer' : web_root} response, content = httplib2.Http().request( url, method='POST', headers=headers, body=urlencode(params)) if response.status != 200: raise HTTPError(response, content) self.access_token = AccessToken.from_string(content)
def __init__(self, web_root, consumer_name, request_token, allow_access_levels=[], max_failed_attempts=3): self.web_root = uris.lookup_web_root(web_root) self.consumer_name = consumer_name self.request_token = request_token self.browser = SimulatedLaunchpadBrowser(self.web_root) self.max_failed_attempts = max_failed_attempts self.allow_access_levels = allow_access_levels self.text_wrapper = textwrap.TextWrapper( replace_whitespace=False, width=78)
def get_request_token(self, context=None, web_root=uris.STAGING_WEB_ROOT, token_format=URI_TOKEN_FORMAT): """Request an OAuth token to Launchpad. Also store the token in self._request_token. This method must not be called on an object with no consumer specified or if an access token has already been obtained. :param context: The context of this token, that is, its scope of validity within Launchpad. :param web_root: The URL of the website on which the token should be requested. :token_format: How the token should be presented. URI_TOKEN_FORMAT means just return the URL to the page that authorizes the token. DICT_TOKEN_FORMAT means return a dictionary describing the token and the site's authentication policy. :return: If token_format is URI_TOKEN_FORMAT, the URL for the user to authorize the `AccessToken` provided by Launchpad. If token_format is DICT_TOKEN_FORMAT, a dict of information about the new access token. """ assert self.consumer is not None, "Consumer not specified." assert self.access_token is None, "Access token already obtained." web_root = uris.lookup_web_root(web_root) params = dict( oauth_consumer_key=self.consumer.key, oauth_signature_method='PLAINTEXT', oauth_signature='&') url = web_root + request_token_page headers = {'Referer' : web_root} if token_format == self.DICT_TOKEN_FORMAT: headers['Accept'] = 'application/json' response, content = httplib2.Http().request( url, method='POST', headers=headers, body=urlencode(params)) if response.status != 200: raise HTTPError(response, content) if token_format == self.DICT_TOKEN_FORMAT: params = simplejson.loads(content) if context is not None: params["lp.context"] = context self._request_token = AccessToken.from_params(params) return params else: self._request_token = AccessToken.from_string(content) url = '%s%s?oauth_token=%s' % (web_root, authorize_token_page, self._request_token.key) if context is not None: self._request_token.context = context url += "&lp.context=%s" % context return url
def __init__(self, web_root, consumer_name, request_token, allow_access_levels=[], max_failed_attempts=3): web_root = uris.lookup_web_root(web_root) page = "+authorize-token?oauth_token=%s" % request_token if len(allow_access_levels) > 0: page += ("&allow_permission=" + "&allow_permission=".join(allow_access_levels)) self.authorization_url = urljoin(web_root, page) super(AuthorizeRequestTokenWithBrowser, self).__init__( web_root, consumer_name, request_token, allow_access_levels, max_failed_attempts)
def get_request_token(self, context=None, web_root=uris.STAGING_WEB_ROOT, token_format=URI_TOKEN_FORMAT): """Request an OAuth token to Launchpad. Also store the token in self._request_token. This method must not be called on an object with no consumer specified or if an access token has already been obtained. :param context: The context of this token, that is, its scope of validity within Launchpad. :param web_root: The URL of the website on which the token should be requested. :token_format: How the token should be presented. URI_TOKEN_FORMAT means just return the URL to the page that authorizes the token. DICT_TOKEN_FORMAT means return a dictionary describing the token and the site's authentication policy. :return: If token_format is URI_TOKEN_FORMAT, the URL for the user to authorize the `AccessToken` provided by Launchpad. If token_format is DICT_TOKEN_FORMAT, a dict of information about the new access token. """ assert self.consumer is not None, "Consumer not specified." assert self.access_token is None, "Access token already obtained." web_root = uris.lookup_web_root(web_root) params = dict(oauth_consumer_key=self.consumer.key, oauth_signature_method='PLAINTEXT', oauth_signature='&') url = web_root + request_token_page headers = {'Referer': web_root} if token_format == self.DICT_TOKEN_FORMAT: headers['Accept'] = 'application/json' response, content = _http_post(url, headers, params) if isinstance(content, binary_type): content = content.decode('utf-8') if token_format == self.DICT_TOKEN_FORMAT: params = json.loads(content) if context is not None: params["lp.context"] = context self._request_token = AccessToken.from_params(params) return params else: self._request_token = AccessToken.from_string(content) url = '%s%s?oauth_token=%s' % (web_root, authorize_token_page, self._request_token.key) if context is not None: self._request_token.context = context url += "&lp.context=%s" % context return url
def exchange_request_token_for_access_token(self, web_root=uris.STAGING_WEB_ROOT ): """Exchange the previously obtained request token for an access token. This method must not be called unless get_request_token() has been called and completed successfully. The access token will be stored as self.access_token. :param web_root: The base URL of the website that granted the request token. """ assert self._request_token is not None, ( "get_request_token() doesn't seem to have been called.") web_root = uris.lookup_web_root(web_root) params = dict(oauth_consumer_key=self.consumer.key, oauth_signature_method='PLAINTEXT', oauth_token=self._request_token.key, oauth_signature='&%s' % self._request_token.secret) url = web_root + access_token_page headers = {'Referer': web_root} response, content = _http_post(url, headers, params) self.access_token = AccessToken.from_string(content)
def test_edge_web_server_equivalent_string_becomes_production(self): with self.edge_deprecation_error(): self.assertEqual( uris.lookup_web_root('https://edge.launchpad.net/'), uris.lookup_web_root('production'))
def test_edge_web_root_url_becomes_production(self): with self.edge_deprecation_error(): self.assertEqual(uris.lookup_web_root(uris.EDGE_WEB_ROOT), uris.lookup_web_root('production'))
def test_edge_service_root_is_production(self): # The edge server no longer exists, so if the client wants # edge we give them production. with self.edge_deprecation_error(): self.assertEqual(uris.lookup_web_root('edge'), uris.lookup_web_root('production'))
def test_edge_web_server_equivalent_string_becomes_production(self): with self.edge_deprecation_error(): self.assertEqual( uris.lookup_web_root('https://edge.launchpad.net/'), uris.lookup_web_root('production'))
def test_edge_web_root_url_becomes_production(self): with self.edge_deprecation_error(): self.assertEqual(uris.lookup_web_root(uris.EDGE_WEB_ROOT), uris.lookup_web_root('production'))
def test_edge_service_root_is_production(self): # The edge server no longer exists, so if the client wants # edge we give them production. with self.edge_deprecation_error(): self.assertEqual(uris.lookup_web_root('edge'), uris.lookup_web_root('production'))
def __init__(self, web_root=uris.STAGING_WEB_ROOT): self.web_root = uris.lookup_web_root(web_root) self.http = httplib2.Http()
def __init__(self, web_root, consumer_name, context): """Initialize.""" self.web_root = lookup_web_root(web_root) self.credentials = Credentials(consumer_name) self.context = context
def __init__(self, web_root, consumer_name, context): """Initialize.""" self.web_root = lookup_web_root(web_root) self.credentials = Credentials(consumer_name) self.context = context