Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validate_token_true(self):
header = httplib2.Http(".cache")
url = '%stokens/%s?belongsTo=%s' % (utils.URL, self.token, self.tenant)
resp, content = header.request(url, "GET", body='',
headers={"Content-Type": "application/json",
"X-Auth-Token": self.auth_token})
if int(resp['status']) == 500:
self.fail('Identity Fault')
elif int(resp['status']) == 503:
self.fail('Service Not Available')
self.assertEqual(200, int(resp['status']))
self.assertEqual('application/json', utils.content_type(resp))
#verify content
obj = json.loads(content)
if not "auth" in obj:
raise self.fail("Expecting Auth")
role_refs = obj["auth"]["user"]["roleRefs"]
def delete_token(token, auth_token):
header = httplib2.Http(".cache")
url = '%stoken/%s' % (URL, token)
resp, content = header.request(url, "DELETE", body='',
headers={"Content-Type": "application/json",
"X-Auth-Token": auth_token})
return (resp, content)
def _mock_gce_get_project(projectid):
if projectid in results.GCE_GET_PROJECT:
return results.GCE_GET_PROJECT[projectid]
response = httplib2.Response(
{'status': '403', 'content-type': 'application/json'})
content = results.GCE_API_NOT_ENABLED_TEMPLATE.format(id=projectid)
error_403 = errors.HttpError(response, content)
raise api_errors.ApiNotEnabledError('Access Not Configured.', error_403)
def __init__(self, headers, content):
from httplib2 import Response
self._response = Response(headers)
self._content = content
def test_digest_object_stale():
credentials = ("joe", "password")
host = None
request_uri = "/digest/stale/"
headers = {}
response = httplib2.Response({})
response["www-authenticate"] = (
'Digest realm="myrealm", nonce="bd669f", '
'algorithm=MD5, qop="auth", stale=true'
)
response.status = 401
content = b""
d = httplib2.DigestAuthentication(
credentials, host, request_uri, headers, response, content, None
)
# Returns true to force a retry
assert d.response(response, content)
def test_errors(self):
errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
requestBuilder = RequestMockBuilder({
'plus.activities.list': (errorResponse, '{}')
})
plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
try:
activity = plus.activities().list(collection='public', userId='me').execute()
self.fail('An exception should have been thrown')
except HttpError, e:
self.assertEqual('{}', e.content)
self.assertEqual(500, e.resp.status)
self.assertEqual('Server Error', e.resp.reason)
def test_fetch_tab_not_found(mocker):
# Exception built by pasting content from a real exception generated
# via Ipython + a pdb trace put in the datasheets code
exception = apiclient.errors.HttpError(
resp=httplib2.Response({
'vary': 'Origin, X-Origin, Referer',
'content-type': 'application/json; charset=UTF-8',
'date': 'Tue, 10 Apr 2018 19:17:12 GMT',
'server': 'ESF',
'cache-control': 'private',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'hq=":443"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="42,41,39,35"',
'transfer-encoding': 'chunked',
'status': '400',
'content-length': '271',
'-content-encoding': 'gzip'
}),
content=b"""
{
"error": {
def _mock_permission_denied(parentid):
response = httplib2.Response(
{'status': '403', 'content-type': 'application/json'})
content = results.GCP_PERMISSION_DENIED_TEMPLATE.format(id=parentid).\
encode()
error_403 = errors.HttpError(response, content)
raise api_errors.ApiExecutionError(parentid, error_403)
def test_proper_exception_is_raised_when_cert_validation_fails(self):
http = HTTPClient(token=AUTH_TOKEN, endpoint_url=END_URL)
self.mox.StubOutWithMock(httplib2.Http, 'request')
httplib2.Http.request(
URL, METHOD, headers=mox.IgnoreArg()
).AndRaise(httplib2.SSLHandshakeError)
self.mox.ReplayAll()
self.assertRaises(
exceptions.SslCertificateValidationError,
http._cs_request,
URL, METHOD
)
self.mox.VerifyAll()
def crequest(self, uri, trace=None, **kws):
"""
"""
headers = kws.pop('headers', {})
req = DummyRequest(uri, headers)
self.cookiejar.add_cookie_header(req)
headers = req.headers
if trace:
trace.request("HEADERS: %s" % headers)
try:
(r, body) = Http.request(self, uri, headers=headers,
redirections=0, **kws)
except RedirectLimit, err:
r = err.response
body = err.content
resp = DummyResponse(r)
self.cookiejar.extract_cookies(resp, req)
return r, body