def test_token_refresh_failure(self): http = HttpMockSequence([({"status": "401"}, ""), ({"status": "400"}, '{"error":"access_denied"}')]) http = self.credentials.authorize(http) try: http.request("http://example.com") self.fail("should raise AccessTokenRefreshError exception") except AccessTokenRefreshError: pass
def test_token_refresh_failure(self): http = HttpMockSequence([ ({'status': '401'}, ''), ({'status': '400'}, '{"error":"access_denied"}'), ]) http = self.credentials.authorize(http) try: http.request("http://example.com") self.fail("should raise AccessTokenRefreshError exception") except AccessTokenRefreshError: pass
def test_token_refresh_failure(self): http = HttpMockSequence([ ({'status': '401'}, ''), ({'status': '400'}, '{"error":"access_denied"}'), ]) http = self.credentials.authorize(http) try: http.request("http://example.com") self.fail("should raise AccessTokenRefreshError exception") except AccessTokenRefreshError: pass self.assertTrue(self.credentials.access_token_expired)
def test_token_refresh_failure(self): for status_code in REFRESH_STATUS_CODES: http = HttpMockSequence([ ({'status': status_code}, ''), ({'status': '400'}, '{"error":"access_denied"}'), ]) http = self.credentials.authorize(http) try: http.request('http://example.com') self.fail('should raise AccessTokenRefreshError exception') except AccessTokenRefreshError: pass self.assertTrue(self.credentials.access_token_expired) self.assertEqual(None, self.credentials.token_response)
def testPost(self): request = webapp2.Request.blank("/step1", POST={ 'domain': 'demo-123.resold.richieforeman.net' }) from mock import MagicMock http = HttpMockSequence([ ({'status': '200'}, RESELLER_DISCOVERY), ({'status': '200'}, json.dumps({})) ]) # wrap http.request so we can spy on it. http.request = MagicMock(wraps=http.request) with patch('main.get_authorized_http', return_value=http): response = request.get_response(app) self.assertTrue(http.request.called) discovery_call = http.request.call_args_list[0] create_customer = http.request.call_args_list[1] # ensure that a discovery call occured. (url,), args = discovery_call self.assertEqual(url, RESELLER_DISCOVERY_URL) # ensure that a customer was created via the POST api. (url, method), args = create_customer self.assertEqual(url, 'https://www.googleapis' '.com/apps/reseller/v1/customers?alt=json') self.assertEqual(method, 'POST')
def testPost(self): request = webapp2.Request.blank( "/step1", POST={'domain': 'demo-123.resold.richieforeman.net'}) from mock import MagicMock http = HttpMockSequence([({ 'status': '200' }, RESELLER_DISCOVERY), ({ 'status': '200' }, json.dumps({}))]) # wrap http.request so we can spy on it. http.request = MagicMock(wraps=http.request) with patch('main.get_authorized_http', return_value=http): response = request.get_response(app) self.assertTrue(http.request.called) discovery_call = http.request.call_args_list[0] create_customer = http.request.call_args_list[1] # ensure that a discovery call occured. (url, ), args = discovery_call self.assertEqual(url, RESELLER_DISCOVERY_URL) # ensure that a customer was created via the POST api. (url, method), args = create_customer self.assertEqual( url, 'https://www.googleapis.com/apps/reseller/v1/customers?alt=json' ) self.assertEqual(method, 'POST')
def test_assertion_refresh(self): http = HttpMockSequence( [({"status": "200"}, '{"access_token":"1/3w"}'), ({"status": "200"}, "echo_request_headers")] ) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual(content["authorization"], "OAuth 1/3w")
def test_non_401_error_response(self): http = HttpMockSequence([ ({'status': '400'}, ''), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual(400, resp.status)
def test_auth_header_sent(self): http = HttpMockSequence([ ({'status': '200'}, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual('Bearer foo', content['Authorization'])
def test_set_user_agent_nested(self): http = HttpMockSequence([({"status": "200"}, "echo_request_headers")]) http = set_user_agent(http, "my_app/5.5") http = set_user_agent(http, "my_library/0.1") resp, content = http.request("http://example.com") self.assertEqual("my_app/5.5 my_library/0.1", content["user-agent"])
def test_non_401_error_response(self): http = HttpMockSequence([ ({'status': '400'}, ''), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual(400, resp.status)
def test_set_user_agent(self): http = HttpMockSequence([ ({'status': '200'}, 'echo_request_headers'), ]) http = set_user_agent(http, "my_app/5.5") resp, content = http.request("http://example.com") self.assertEqual(content['user-agent'], 'my_app/5.5')
def test_add_requestor_to_uri(self): http = HttpMockSequence([ ({'status': '200'}, 'echo_request_uri'), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual('http://example.com?xoauth_requestor_id=test%40example.org', content)
def test_assertion_refresh(self): http = HttpMockSequence([ ({'status': '200'}, '{"access_token":"1/3w"}'), ({'status': '200'}, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual('Bearer 1/3w', content['Authorization'])
def test_auth_header_sent(self): http = HttpMockSequence([ ({ 'status': '200' }, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual(content['authorization'], 'OAuth foo')
def test_token_refresh_failure(self): for status_code in REFRESH_STATUS_CODES: http = HttpMockSequence([ ({ 'status': status_code }, ''), ({ 'status': '400' }, '{"error":"access_denied"}'), ]) http = self.credentials.authorize(http) try: http.request('http://example.com') self.fail('should raise AccessTokenRefreshError exception') except AccessTokenRefreshError: pass self.assertTrue(self.credentials.access_token_expired) self.assertEqual(None, self.credentials.token_response)
def test_token_refresh_success(self): http = HttpMockSequence([ ({'status': '401'}, ''), ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual(content['authorization'], 'OAuth 1/3w')
def test_token_refresh_failure(self): # Older API (GData) respond with 403 for status_code in ['401', '403']: http = HttpMockSequence([ ({ 'status': status_code }, ''), ({ 'status': '400' }, '{"error":"access_denied"}'), ]) http = self.credentials.authorize(http) try: http.request("http://example.com") self.fail("should raise AccessTokenRefreshError exception") except AccessTokenRefreshError: pass self.assertTrue(self.credentials.access_token_expired)
def test_token_refresh_success(self): http = HttpMockSequence([ ({'status': '401'}, ''), ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual('Bearer 1/3w', content['Authorization'])
def test_set_user_agent_nested(self): http = HttpMockSequence([ ({'status': '200'}, 'echo_request_headers'), ]) http = set_user_agent(http, "my_app/5.5") http = set_user_agent(http, "my_library/0.1") resp, content = http.request("http://example.com") content = simplejson.loads(content.decode()) self.assertEqual('my_app/5.5 my_library/0.1', content['user-agent'])
def test_token_refresh_success(self): http = HttpMockSequence([({"status": "401"}, "")]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail("should throw exception if token expires") except AccessTokenCredentialsError: pass except Exception: self.fail("should only throw AccessTokenCredentialsError")
def _credentials_refresh(self, credentials): http = HttpMockSequence([ ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '401'}, ''), ({'status': '200'}, '{"access_token":"3/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = credentials.authorize(http) resp, content = http.request('http://example.org') return content
def test_invalid_token(self): http = HttpMockSequence([ ({'status': '401'}, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail('should raise CredentialsInvalidError') except CredentialsInvalidError: pass
def test_auth_header_sent(self): http = HttpMockSequence([ ({ 'status': '200' }, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual('Bearer foo', simplejson.loads(content.decode())['Authorization'])
def test_add_requestor_to_uri(self): http = HttpMockSequence([ ({ 'status': '200' }, 'echo_request_uri'), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual( 'http://example.com?xoauth_requestor_id=test%40example.org', content)
def test_no_requestor(self): self.credentials.requestor = None http = HttpMockSequence([ ({'status': '401'}, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail('should raise MissingParameter') except MissingParameter: pass
def _credentials_refresh(self, credentials): http = HttpMockSequence( [ ({"status": "200"}, '{"access_token":"1/3w","expires_in":3600}'), ({"status": "401"}, ""), ({"status": "200"}, '{"access_token":"3/3w","expires_in":3600}'), ({"status": "200"}, "echo_request_headers"), ] ) http = credentials.authorize(http) resp, content = http.request("http://example.org") return content
def test_token_refresh_success(self): http = HttpMockSequence([ ({'status': '401'}, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail("should throw exception if token expires") except AccessTokenCredentialsError: pass except Exception: self.fail("should only throw AccessTokenCredentialsError")
def test_set_user_agent_nested(self): http = HttpMockSequence([ ({ 'status': '200' }, 'echo_request_headers'), ]) http = set_user_agent(http, "my_app/5.5") http = set_user_agent(http, "my_library/0.1") resp, content = http.request("http://example.com") content = simplejson.loads(content.decode()) self.assertEqual('my_app/5.5 my_library/0.1', content['user-agent'])
def test_token_refresh_success(self): # Older API (GData) respond with 403 for status_code in ['401', '403']: http = HttpMockSequence([ ({'status': status_code}, ''), ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual('Bearer 1/3w', content['Authorization']) self.assertFalse(self.credentials.access_token_expired)
def test_assertion_refresh(self): http = HttpMockSequence([ ({ 'status': '200' }, '{"access_token":"1/3w"}'), ({ 'status': '200' }, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual('Bearer 1/3w', content['Authorization'])
def test_invalid_token(self): http = HttpMockSequence([ ({ 'status': '401' }, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail('should raise CredentialsInvalidError') except CredentialsInvalidError: pass
def test_token_refresh_success(self): for status_code in REFRESH_STATUS_CODES: http = HttpMockSequence([ ({'status': status_code}, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request('http://example.com') self.fail('should throw exception if token expires') except AccessTokenCredentialsError: pass except Exception: self.fail('should only throw AccessTokenCredentialsError')
def test_token_refresh_success(self): for status_code in REFRESH_STATUS_CODES: token_response = {'access_token': '1/3w', 'expires_in': 3600} http = HttpMockSequence([ ({'status': status_code}, ''), ({'status': '200'}, simplejson.dumps(token_response)), ({'status': '200'}, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual('Bearer 1/3w', content['Authorization']) self.assertFalse(self.credentials.access_token_expired) self.assertEqual(token_response, self.credentials.token_response)
def test_no_requestor(self): self.credentials.requestor = None http = HttpMockSequence([ ({ 'status': '401' }, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail('should raise MissingParameter') except MissingParameter: pass
def test_credentials_good(self): private_key = datafile('privatekey.%s' % self.format) credentials = SignedJwtAssertionCredentials( '*****@*****.**', private_key, scope='read+write', sub='*****@*****.**') http = HttpMockSequence([ ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = credentials.authorize(http) resp, content = http.request('http://example.org') self.assertEqual('Bearer 1/3w', content['Authorization'])
def test_signed_jwt_assertion_credentials(self): private_key = datafile('privatekey.p12') credentials = SignedJwtAssertionCredentials( '*****@*****.**', private_key, scope='read+write', prn='*****@*****.**') http = HttpMockSequence([ ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = credentials.authorize(http) resp, content = http.request('http://example.org') self.assertEquals(content['authorization'], 'OAuth 1/3w')
def test_token_refresh_success(self): # Older API (GData) respond with 403 for status_code in ['401', '403']: http = HttpMockSequence([ ({'status': status_code}, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail("should throw exception if token expires") except AccessTokenCredentialsError: pass except Exception: self.fail("should only throw AccessTokenCredentialsError")
def __enter__(self): http = HttpMockSequence(self._responses) native_request_method = http.request # Collecting requests to validate at __exit__. def _request_wrapper(*args, **kwargs): self._actual_requests.append(args + (kwargs['body'],)) return native_request_method(*args, **kwargs) http.request = _request_wrapper service_mock = build('ml', 'v1', http=http) with mock.patch.object( hook.CloudMLHook, 'get_conn', return_value=service_mock): return hook.CloudMLHook()
def test_credentials_good(self): private_key = datafile("privatekey.p12") credentials = SignedJwtAssertionCredentials( "*****@*****.**", private_key, scope="read+write", prn="*****@*****.**" ) http = HttpMockSequence( [ ({"status": "200"}, '{"access_token":"1/3w","expires_in":3600}'), ({"status": "200"}, "echo_request_headers"), ] ) http = credentials.authorize(http) resp, content = http.request("http://example.org") self.assertEqual("Bearer 1/3w", content["Authorization"])
def test_token_refresh_success(self): for status_code in REFRESH_STATUS_CODES: http = HttpMockSequence([ ({ 'status': status_code }, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request('http://example.com') self.fail('should throw exception if token expires') except AccessTokenCredentialsError: pass except Exception: self.fail('should only throw AccessTokenCredentialsError')
def __enter__(self): http = HttpMockSequence(self._responses) native_request_method = http.request # Collecting requests to validate at __exit__. def _request_wrapper(*args, **kwargs): self._actual_requests.append(args + (kwargs['body'], )) return native_request_method(*args, **kwargs) http.request = _request_wrapper service_mock = build('ml', 'v1', http=http) with mock.patch.object(hook.CloudMLHook, 'get_conn', return_value=service_mock): return hook.CloudMLHook()
def test_token_refresh_success(self): # Older API (GData) respond with 403 for status_code in ['401', '403']: http = HttpMockSequence([ ({ 'status': status_code }, ''), ]) http = self.credentials.authorize(http) try: resp, content = http.request("http://example.com") self.fail("should throw exception if token expires") except AccessTokenCredentialsError: pass except Exception: self.fail("should only throw AccessTokenCredentialsError")
def __enter__(self): http = HttpMockSequence(self._responses) native_request_method = http.request # Collecting requests to validate at __exit__. def _request_wrapper(*args, **kwargs): self._actual_requests.append(args + (kwargs.get('body', ''),)) return native_request_method(*args, **kwargs) http.request = _request_wrapper discovery = requests.get( 'https://www.googleapis.com/discovery/v1/apis/ml/v1/rest') service_mock = build_from_document(discovery.json(), http=http) with mock.patch.object( hook.MLEngineHook, 'get_conn', return_value=service_mock): return hook.MLEngineHook()
def __enter__(self): http = HttpMockSequence(self._responses) native_request_method = http.request # Collecting requests to validate at __exit__. def _request_wrapper(*args, **kwargs): self._actual_requests.append(args + (kwargs.get('body', ''), )) return native_request_method(*args, **kwargs) http.request = _request_wrapper discovery = requests.get( 'https://www.googleapis.com/discovery/v1/apis/ml/v1/rest') service_mock = build_from_document(discovery.json(), http=http) with mock.patch.object(hook.MLEngineHook, 'get_conn', return_value=service_mock): return hook.MLEngineHook()
def test_signed_jwt_assertion_credentials(self): private_key = datafile('privatekey.p12') credentials = SignedJwtAssertionCredentials('*****@*****.**', private_key, scope='read+write', prn='*****@*****.**') http = HttpMockSequence([ ({ 'status': '200' }, '{"access_token":"1/3w","expires_in":3600}'), ({ 'status': '200' }, 'echo_request_headers'), ]) http = credentials.authorize(http) resp, content = http.request('http://example.org') self.assertEquals(content['authorization'], 'OAuth 1/3w')
def test_token_refresh_success(self): # Older API (GData) respond with 403 for status_code in ['401', '403']: http = HttpMockSequence([ ({ 'status': status_code }, ''), ({ 'status': '200' }, '{"access_token":"1/3w","expires_in":3600}'), ({ 'status': '200' }, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual('Bearer 1/3w', content['Authorization']) self.assertFalse(self.credentials.access_token_expired)
def test_token_refresh_success(self): for status_code in REFRESH_STATUS_CODES: token_response = {'access_token': '1/3w', 'expires_in': 3600} http = HttpMockSequence([ ({ 'status': status_code }, ''), ({ 'status': '200' }, simplejson.dumps(token_response)), ({ 'status': '200' }, 'echo_request_headers'), ]) http = self.credentials.authorize(http) resp, content = http.request('http://example.com') self.assertEqual('Bearer 1/3w', content['Authorization']) self.assertFalse(self.credentials.access_token_expired) self.assertEqual(token_response, self.credentials.token_response)
def test_non_401_error_response(self): http = HttpMockSequence([({"status": "400"}, "")]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual(400, resp.status)
def test_auth_header_sent(self): http = HttpMockSequence([({"status": "200"}, "echo_request_headers")]) http = self.credentials.authorize(http) resp, content = http.request("http://example.com") self.assertEqual(content["authorization"], "OAuth foo")