def test_json_request_works(self): self.mock_urlfetch([ ({ 'deadline': 123, 'headers': { 'Authorization': 'Bearer token', 'Content-Type': 'application/json; charset=utf-8', 'Header': 'value', }, 'method': 'POST', 'payload': '{"key":"value"}', 'url': 'http://localhost/123?a=%3D&b=%26', }, Response(200, '{"a":"b"}', {})), ]) response = net.json_request(url='http://localhost/123', method='POST', payload={'key': 'value'}, params={ 'a': '=', 'b': '&' }, headers={'Header': 'value'}, scopes=['scope'], service_account_key=auth.ServiceAccountKey( 'a', 'b', 'c'), deadline=123, max_attempts=5) self.assertEqual({'a': 'b'}, response)
def test_request_works(self): self.mock_urlfetch([ ({ 'deadline': 123, 'headers': { 'Accept': 'text/plain', 'Authorization': 'Bearer token' }, 'method': 'POST', 'payload': 'post body', 'url': 'http://localhost/123?a=%3D&b=%26', }, Response(200, 'response body', {})), ]) response = net.request(url='http://localhost/123', method='POST', payload='post body', params={ 'a': '=', 'b': '&' }, headers={'Accept': 'text/plain'}, scopes=['scope'], service_account_key=auth.ServiceAccountKey( 'a', 'b', 'c'), deadline=123, max_attempts=5) self.assertEqual('response body', response)
def test_fetch(self): service = impl.CASService( '/bucket/real', '/bucket/temp', auth.ServiceAccountKey('*****@*****.**', 'PEM private key', 'id')) # Actual _rsa_sign implementation depends on PyCrypto, that for some reason # is not importable in unit tests. _rsa_sign is small enough to be "tested" # manually on the dev server. calls = [] def fake_sign(pkey, data): calls.append((pkey, data)) return '+signature+' self.mock(service, '_rsa_sign', fake_sign) self.mock_now(utils.timestamp_to_datetime(1416444987 * 1000000.)) # Signature and email should be urlencoded. url = service.generate_fetch_url('SHA1', 'a' * 40) self.assertEqual( 'https://storage.googleapis.com/bucket/real/SHA1/' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?' 'GoogleAccessId=account%40email.com&' 'Expires=1416448587&' 'Signature=%2Bsignature%2B', url) # Since _rsa_sign is mocked out, at least verify it is called as expected. self.assertEqual([( 'PEM private key', 'GET\n\n\n1416448587\n/bucket/real/SHA1/' + 'a'*40 )], calls)
def setUp(self): super(BufferTest, self).setUp() conf = metrics.MonitoringConfig() conf.project_id = '123' conf.service_account_key = auth.ServiceAccountKey( client_email='client_email', private_key='private_key', private_key_id='private_key_id') conf.store(updated_by=auth.Anonymous)
def _get_credentials(credentials): """Obtain Aquisition API credentials as Credentials object.""" if not all((credentials.client_email, credentials.private_key, credentials.private_key_id)): return None return auth.ServiceAccountKey(client_email=credentials.client_email, private_key=credentials.private_key, private_key_id=credentials.private_key_id)
def test_request_project_token_fallback_works(self): @ndb.tasklet def mocked_get_project_access_token_async(*args, **kwargs): mocked_get_project_access_token_async.params = (args, kwargs) mocked_get_project_access_token_async.called = True raise auth.NotFoundError('testing fallback 1') mocked_get_project_access_token_async.called = False @ndb.tasklet def mocked_get_access_token_async(*args, **kwargs): mocked_get_access_token_async.params = (args, kwargs) mocked_get_access_token_async.called = True raise Exception('testing fallback 2') mocked_get_access_token_async.called = False self.mock(auth, 'get_project_access_token_async', mocked_get_project_access_token_async) self.mock(auth, 'get_access_token_async', mocked_get_access_token_async) with self.assertRaises(Exception): _ = net.request(url='http://localhost/123', method='POST', payload='post body', params={ 'a': '=', 'b': '&' }, headers={'Accept': 'text/plain'}, scopes=['scope'], service_account_key=auth.ServiceAccountKey( 'a', 'b', 'c'), deadline=123, max_attempts=5, project_id='project1') self.assertTrue(mocked_get_project_access_token_async.called) self.assertTrue(mocked_get_access_token_async.called)
def get_cas_service(): """Factory method that returns configured CASService instance. If the service is not configured, returns None. Also acts as a mocking point for unit tests. """ conf = config.cached() if not conf.cas_gs_path or not conf.cas_gs_temp: return None try: cloudstorage.validate_file_path(conf.cas_gs_path.rstrip('/')) cloudstorage.validate_file_path(conf.cas_gs_temp.rstrip('/')) except ValueError as err: logging.error("Invalid CAS config: %s", err) return None service_account_key = auth.ServiceAccountKey( client_email=conf.service_account_email, private_key=conf.service_account_pkey, private_key_id=conf.service_account_pkey_id) if utils.is_local_dev_server(): # pragma: no branch from . import hacks hacks.patch_cloudstorage_lib(service_account_key) return CASService(conf.cas_gs_path.rstrip('/'), conf.cas_gs_temp.rstrip('/'), service_account_key)
def test_execute_flush(self): calls = [] @ndb.tasklet def mocked_json_request_async(**kwargs): calls.append(kwargs) self.mock(net, 'json_request_async', mocked_json_request_async) # To test batching. self.mock(metrics, '_MAX_BATCH_SIZE', 3) service_account_key = auth.ServiceAccountKey( client_email='client_email', private_key='private_key', private_key_id='private_key_id') metrics._execute_flush(self.EXPECTED_FLUSH_TASK) self.assertEqual( [ # Register metrics. { 'method': 'POST', 'payload': { 'description': 'Desc 1', 'labels': [ { 'description': 'a', 'key': 'custom.cloudmonitoring.googleapis.com/A', }, { 'description': 'b', 'key': 'custom.cloudmonitoring.googleapis.com/B', }, ], 'name': 'custom.cloudmonitoring.googleapis.com/name1', 'typeDescriptor': { 'metricType': 'gauge', 'valueType': 'int64' }, }, 'scopes': ['https://www.googleapis.com/auth/monitoring'], 'service_account_key': service_account_key, 'url': 'https://www.googleapis.com/cloudmonitoring/v2beta2/' 'projects/123/metricDescriptors', }, { 'method': 'POST', 'payload': { 'description': 'Desc 2', 'labels': [ { 'description': 'c', 'key': 'custom.cloudmonitoring.googleapis.com/C', }, { 'description': 'd', 'key': 'custom.cloudmonitoring.googleapis.com/D', }, ], 'name': 'custom.cloudmonitoring.googleapis.com/name2', 'typeDescriptor': { 'metricType': 'gauge', 'valueType': 'int64' }, }, 'scopes': ['https://www.googleapis.com/auth/monitoring'], 'service_account_key': service_account_key, 'url': 'https://www.googleapis.com/cloudmonitoring/v2beta2/' 'projects/123/metricDescriptors', }, { 'method': 'POST', 'payload': { 'description': 'Desc 3', 'labels': [], 'name': 'custom.cloudmonitoring.googleapis.com/name3', 'typeDescriptor': { 'metricType': 'gauge', 'valueType': 'double' }, }, 'scopes': ['https://www.googleapis.com/auth/monitoring'], 'service_account_key': service_account_key, 'url': 'https://www.googleapis.com/cloudmonitoring/v2beta2/' 'projects/123/metricDescriptors', }, # Sending actual values (first batch). { 'method': 'POST', 'payload': { 'timeseries': [ { 'point': { 'end': '2015-01-02T03:04:05Z', 'int64Value': 456, 'start': '2015-01-02T03:04:05Z', }, 'timeseriesDesc': { 'labels': { 'custom.cloudmonitoring.googleapis.com/A': '1', 'custom.cloudmonitoring.googleapis.com/B': '2', }, 'metric': 'custom.cloudmonitoring.googleapis.com/name1', }, }, { 'point': { 'int64Value': 789, 'end': '2015-01-02T03:04:05Z', 'start': '2015-01-02T03:04:05Z', }, 'timeseriesDesc': { 'labels': { 'custom.cloudmonitoring.googleapis.com/A': '3', 'custom.cloudmonitoring.googleapis.com/B': '4', }, 'metric': 'custom.cloudmonitoring.googleapis.com/name1', }, }, { 'point': { 'int64Value': 555, 'end': '2015-01-02T03:04:05Z', 'start': '2015-01-02T03:04:05Z', }, 'timeseriesDesc': { 'labels': { 'custom.cloudmonitoring.googleapis.com/C': 'x', 'custom.cloudmonitoring.googleapis.com/D': 'z', }, 'metric': 'custom.cloudmonitoring.googleapis.com/name2', }, }, ], }, 'scopes': ['https://www.googleapis.com/auth/monitoring'], 'service_account_key': service_account_key, 'url': 'https://www.googleapis.com/cloudmonitoring/v2beta2/' 'projects/123/timeseries:write', }, # Second batch. { 'method': 'POST', 'payload': { 'timeseries': [ { 'point': { 'doubleValue': 3.0, 'end': '2015-01-02T03:04:05Z', 'start': '2015-01-02T03:04:05Z', }, 'timeseriesDesc': { 'labels': {}, 'metric': 'custom.cloudmonitoring.googleapis.com/name3', }, }, ], }, 'scopes': ['https://www.googleapis.com/auth/monitoring'], 'service_account_key': service_account_key, 'url': 'https://www.googleapis.com/cloudmonitoring/v2beta2/' 'projects/123/timeseries:write', } ], calls)
def service_account_key(self): if not self.client_email: return None return auth.ServiceAccountKey(client_email=self.client_email, private_key=self.private_key, private_key_id=self.private_key_id)