def test_set_iam_policy(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = database_admin_client.DatabaseAdminClient() # Mock request resource = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') policy = policy_pb2.Policy() # Mock response version = 351608024 etag = b'21' expected_response = policy_pb2.Policy(version=version, etag=etag) grpc_stub.SetIamPolicy.return_value = expected_response response = client.set_iam_policy(resource, policy) self.assertEqual(expected_response, response) grpc_stub.SetIamPolicy.assert_called_once() args, kwargs = grpc_stub.SetIamPolicy.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) self.assertEqual(expected_request, actual_request)
def test_set_iam_policy(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock(spec=iam_policy_pb2.IAMPolicyStub) mock_create_stub.return_value = grpc_stub client = publisher_client.PublisherClient() # Mock request resource = client.topic_path('[PROJECT]', '[TOPIC]') policy = policy_pb2.Policy() # Mock response version = 351608024 etag = b'21' expected_response = policy_pb2.Policy(version, etag) grpc_stub.SetIamPolicy.return_value = expected_response response = client.set_iam_policy(resource, policy) self.assertEqual(expected_response, response) grpc_stub.SetIamPolicy.assert_called_once() request = grpc_stub.SetIamPolicy.call_args[0] self.assertEqual(resource, request.resource) self.assertEqual(policy, request.policy)
def test_set_iam_policy_from_dict(): client = CloudBillingClient(credentials=credentials.AnonymousCredentials()) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.set_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = policy.Policy() response = client.set_iam_policy( request={"resource": "resource_value", "policy": policy.Policy(version=774)} ) call.assert_called()
def test_policy_to_pb_empty(): from google.iam.v1 import policy_pb2 policy = _make_policy() expected = policy_pb2.Policy() assert policy.to_pb() == expected
def test_policy_from_pb_w_non_empty(): from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE from google.cloud.bigtable.policy import Policy ETAG = b"ETAG" VERSION = 1 members = ["serviceAccount:[email protected]", "user:[email protected]"] empty = frozenset() message = policy_pb2.Policy( etag=ETAG, version=VERSION, bindings=[{ "role": BIGTABLE_ADMIN_ROLE, "members": members }], ) policy = Policy.from_pb(message) assert policy.etag == ETAG assert policy.version == VERSION assert policy.bigtable_admins == set(members) assert policy.bigtable_readers == empty assert policy.bigtable_users == empty assert policy.bigtable_viewers == empty assert len(policy) == 1 assert dict(policy) == {BIGTABLE_ADMIN_ROLE: set(members)}
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = kms_v1.KeyManagementServiceClient() # Setup Request resource = client.key_ring_path("[PROJECT]", "[LOCATION]", "[KEY_RING]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = kms_v1.KeyManagementServiceClient() # Setup Request resource = "resource-341064690" policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_policy_to_pb_w_condition(): from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE VERSION = 3 ETAG = b"ETAG" members = ["serviceAccount:[email protected]", "user:[email protected]"] condition = { "title": "request_time", "description": "Requests made before 2021-01-01T00:00:00Z", "expression": 'request.time < timestamp("2021-01-01T00:00:00Z")', } policy = _make_policy(ETAG, VERSION) policy.bindings = [{ "role": BIGTABLE_ADMIN_ROLE, "members": set(members), "condition": condition }] expected = policy_pb2.Policy( etag=ETAG, version=VERSION, bindings=[ policy_pb2.Binding( role=BIGTABLE_ADMIN_ROLE, members=sorted(members), condition=condition, ) ], ) assert policy.to_pb() == expected
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_admin_database_v1.DatabaseAdminClient() # Setup Request resource = "resource-341064690" response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request resource = client.source_path('[ORGANIZATION]', '[SOURCE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(transport: str = "grpc"): client = CloudBillingClient( credentials=credentials.AnonymousCredentials(), transport=transport ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = iam_policy.SetIamPolicyRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.set_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = policy.Policy(version=774, etag=b"etag_blob") response = client.set_iam_policy(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the response is the type that we expect. assert isinstance(response, policy.Policy) assert response.version == 774 assert response.etag == b"etag_blob"
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = subscriber_client.SubscriberClient() # Setup Request resource = client.subscription_path("[PROJECT]", "[SUBSCRIPTION]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = subscriber_client.SubscriberClient() # Setup Request resource = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]') policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_from_pb_non_empty(self): from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE ETAG = b"ETAG" VERSION = 17 members = [ "serviceAccount:[email protected]", "user:[email protected]" ] empty = frozenset() message = policy_pb2.Policy( etag=ETAG, version=VERSION, bindings=[{ "role": BIGTABLE_ADMIN_ROLE, "members": members }], ) klass = self._get_target_class() policy = klass.from_pb(message) self.assertEqual(policy.etag, ETAG) self.assertEqual(policy.version, VERSION) self.assertEqual(policy.bigtable_admins, set(members)) self.assertEqual(policy.bigtable_readers, empty) self.assertEqual(policy.bigtable_users, empty) self.assertEqual(policy.bigtable_viewers, empty) self.assertEqual(len(policy), 1) self.assertEqual(dict(policy), {BIGTABLE_ADMIN_ROLE: set(members)})
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request resource = client.source_path("[ORGANIZATION]", "[SOURCE]") policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"etag3123477" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = bigtable_admin_v2.BigtableInstanceAdminClient() # Setup Request resource = client.instance_path("[PROJECT]", "[INSTANCE]") policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta3.CloudTasksClient() # Setup Request resource = client.queue_path("[PROJECT]", "[LOCATION]", "[QUEUE]") policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = datacatalog_v1beta1.DataCatalogClient() # Setup Request resource = client.tag_template_path("[PROJECT]", "[LOCATION]", "[TAG_TEMPLATE]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = iot_v1.DeviceManagerClient() # Setup Request resource = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = publisher_client.PublisherClient() # Mock request resource = client.topic_path('[PROJECT]', '[TOPIC]') # Mock response version = 351608024 etag = b'21' expected_response = policy_pb2.Policy(version=version, etag=etag) grpc_stub.GetIamPolicy.return_value = expected_response response = client.get_iam_policy(resource) self.assertEqual(expected_response, response) grpc_stub.GetIamPolicy.assert_called_once() args, kwargs = grpc_stub.GetIamPolicy.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) self.assertEqual(expected_request, actual_request)
def test_instance_get_iam_policy_w_requested_policy_version(): from google.iam.v1 import policy_pb2, options_pb2 from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) instance = _make_instance(INSTANCE_ID, client) version = 1 etag = b"etag_v1" members = ["serviceAccount:[email protected]", "user:[email protected]"] bindings = [{"role": BIGTABLE_ADMIN_ROLE, "members": members}] iam_policy = policy_pb2.Policy(version=version, etag=etag, bindings=bindings) api = client._instance_admin_client = _make_instance_admin_api() api.get_iam_policy.return_value = iam_policy result = instance.get_iam_policy(requested_policy_version=3) assert result.version == version assert result.etag == etag admins = result.bigtable_admins assert len(admins) == len(members) for found, expected in zip(sorted(admins), sorted(members)): assert found == expected api.get_iam_policy.assert_called_once_with( request={ "resource": instance.name, "options_": options_pb2.GetPolicyOptions( requested_policy_version=3), })
def test_get_iam_policy(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_admin_instance_v1.InstanceAdminClient() # Mock request resource = client.instance_path('[PROJECT]', '[INSTANCE]') # Mock response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) grpc_stub.GetIamPolicy.return_value = expected_response response = client.get_iam_policy(resource) self.assertEqual(expected_response, response) grpc_stub.GetIamPolicy.assert_called_once() args, kwargs = grpc_stub.GetIamPolicy.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) self.assertEqual(expected_request, actual_request)
def test_to_pb_empty(self): from google.iam.v1 import policy_pb2 policy = self._make_one() expected = policy_pb2.Policy() self.assertEqual(policy.to_pb(), expected)
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = spanner_admin_instance_v1.InstanceAdminClient() # Setup Request resource = client.instance_path('[PROJECT]', '[INSTANCE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def set_iam_policy(self, request, context): policy = None if context is not None: policy = request.iam_request.policy else: data = json.loads(request.data) data.pop("kind", None) policy = json_format.ParseDict(data, policy_pb2.Policy()) self.iam_policy = policy self.iam_policy.etag = datetime.datetime.now().isoformat().encode("utf-8") return self.iam_policy
def test_set_iam_policy(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import Policy from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) version = 1 etag = b'etag_v1' members = [ 'serviceAccount:[email protected]', 'user:[email protected]', ] bindings = [{'role': BIGTABLE_ADMIN_ROLE, 'members': members}] iam_policy_pb = policy_pb2.Policy(version=version, etag=etag, bindings=bindings) # Patch the stub used by the API method. instance_api = mock.create_autospec( bigtable_instance_admin_client.BigtableInstanceAdminClient) instance_api.set_iam_policy.return_value = iam_policy_pb client._instance_admin_client = instance_api # Perform the method and check the result. iam_policy = Policy(etag=etag, version=version) iam_policy[BIGTABLE_ADMIN_ROLE] = [ Policy.user("*****@*****.**"), Policy.service_account("*****@*****.**"), ] result = instance.set_iam_policy(iam_policy) instance_api.set_iam_policy.assert_called_once_with( resource=instance.name, policy={ 'version': version, 'etag': etag, 'bindings': bindings, }, ) self.assertEqual(result.version, version) self.assertEqual(result.etag, etag) admins = result.bigtable_admins self.assertEqual(len(admins), len(members)) for found, expected in zip(sorted(admins), sorted(members)): self.assertEqual(found, expected)
def test_set_iam_policy(self): from google.cloud.bigtable.client import Client from google.cloud.bigtable_admin_v2.services.bigtable_table_admin import ( BigtableTableAdminClient, ) from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import Policy from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE credentials = _make_credentials() client = Client(project=self.PROJECT_ID, credentials=credentials, admin=True) instance = client.instance(instance_id=self.INSTANCE_ID) backup = self._make_one(self.BACKUP_ID, instance, cluster_id=self.CLUSTER_ID) version = 1 etag = b"etag_v1" members = [ "serviceAccount:[email protected]", "user:[email protected]" ] bindings = [{"role": BIGTABLE_ADMIN_ROLE, "members": sorted(members)}] iam_policy_pb = policy_pb2.Policy(version=version, etag=etag, bindings=bindings) table_api = mock.create_autospec(BigtableTableAdminClient) client._table_admin_client = table_api table_api.set_iam_policy.return_value = iam_policy_pb iam_policy = Policy(etag=etag, version=version) iam_policy[BIGTABLE_ADMIN_ROLE] = [ Policy.user("*****@*****.**"), Policy.service_account("*****@*****.**"), ] result = backup.set_iam_policy(iam_policy) table_api.set_iam_policy.assert_called_once_with( request={ "resource": backup.name, "policy": iam_policy_pb }) self.assertEqual(result.version, version) self.assertEqual(result.etag, etag) admins = result.bigtable_admins self.assertEqual(len(admins), len(members)) for found, expected in zip(sorted(admins), sorted(members)): self.assertEqual(found, expected)
def test_policy_from_pb_w_empty(): from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import Policy empty = frozenset() message = policy_pb2.Policy() policy = Policy.from_pb(message) assert policy.etag == b"" assert policy.version == 0 assert policy.bigtable_admins == empty assert policy.bigtable_readers == empty assert policy.bigtable_users == empty assert policy.bigtable_viewers == empty assert len(policy) == 0 assert dict(policy) == {}
def test_from_pb_empty(self): from google.iam.v1 import policy_pb2 empty = frozenset() message = policy_pb2.Policy() klass = self._get_target_class() policy = klass.from_pb(message) self.assertEqual(policy.etag, b"") self.assertEqual(policy.version, 0) self.assertEqual(policy.bigtable_admins, empty) self.assertEqual(policy.bigtable_readers, empty) self.assertEqual(policy.bigtable_users, empty) self.assertEqual(policy.bigtable_viewers, empty) self.assertEqual(len(policy), 0) self.assertEqual(dict(policy), {})
def test_set_iam_policy_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = publisher_client.PublisherClient() # Mock request resource = client.topic_path('[PROJECT]', '[TOPIC]') policy = policy_pb2.Policy() # Mock exception response grpc_stub.SetIamPolicy.side_effect = CustomException() self.assertRaises(errors.GaxError, client.set_iam_policy, resource, policy)