def test_list_sources(self): # Setup Expected Response next_page_token = "" sources_element = {} sources = [sources_element] expected_response = {"next_page_token": next_page_token, "sources": sources} expected_response = securitycenter_service_pb2.ListSourcesResponse( **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_v1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") paged_list_response = client.list_sources(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.sources[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListSourcesRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_source(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" description = "description-1724546052" expected_response = { "name": name_2, "display_name": display_name, "description": description, } expected_response = source_pb2.Source(**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_v1.SecurityCenterClient() # Setup Request name = client.source_path("[ORGANIZATION]", "[SOURCE]") response = client.get_source(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetSourceRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_run_asset_discovery(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name="operations/test_run_asset_discovery", done=True ) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") response = client.run_asset_discovery(parent) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.RunAssetDiscoveryRequest( parent=parent ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_organization_settings(self): # Setup Expected Response name_2 = "name2-1052831874" enable_asset_discovery = False expected_response = { "name": name_2, "enable_asset_discovery": enable_asset_discovery, } expected_response = organization_settings_pb2.OrganizationSettings( **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_v1.SecurityCenterClient() # Setup Request name = client.organization_settings_path("[ORGANIZATION]") response = client.get_organization_settings(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetOrganizationSettingsRequest( name=name ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_finding(self): # Setup Expected Response name = "name3373707" parent = "parent-995424086" resource_name = "resourceName979421212" category = "category50511102" external_uri = "externalUri-1385596168" expected_response = { "name": name, "parent": parent, "resource_name": resource_name, "category": category, "external_uri": external_uri, } expected_response = finding_pb2.Finding(**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_v1.SecurityCenterClient() # Setup Request finding = {} response = client.update_finding(finding) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateFindingRequest( finding=finding ) 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 = securitycenter_v1.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_test_iam_permissions(self): # Setup Expected Response expected_response = {} expected_response = iam_policy_pb2.TestIamPermissionsResponse( **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_v1.SecurityCenterClient() # Setup Request resource = client.source_path("[ORGANIZATION]", "[SOURCE]") permissions = [] response = client.test_iam_permissions(resource, permissions) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.TestIamPermissionsRequest( resource=resource, permissions=permissions ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_organization_settings(self): # Setup Expected Response name = "name3373707" enable_asset_discovery = False expected_response = { "name": name, "enable_asset_discovery": enable_asset_discovery, } expected_response = organization_settings_pb2.OrganizationSettings( **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_v1.SecurityCenterClient() # Setup Request organization_settings = {} response = client.update_organization_settings(organization_settings) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateOrganizationSettingsRequest( organization_settings=organization_settings ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_source(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" description = "description-1724546052" expected_response = { "name": name, "display_name": display_name, "description": description, } expected_response = source_pb2.Source(**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_v1.SecurityCenterClient() # Setup Request source = {} response = client.update_source(source) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateSourceRequest(source=source) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_security_marks(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = security_marks_pb2.SecurityMarks(**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_v1.SecurityCenterClient() # Setup Request security_marks = {} response = client.update_security_marks(security_marks) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateSecurityMarksRequest( security_marks=security_marks ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_findings(self): # Setup Expected Response next_page_token = "" total_size = 705419236 list_findings_results_element = {} list_findings_results = [list_findings_results_element] expected_response = { "next_page_token": next_page_token, "total_size": total_size, "list_findings_results": list_findings_results, } expected_response = securitycenter_service_pb2.ListFindingsResponse( **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_v1.SecurityCenterClient() # Setup Request parent = client.source_path("[ORGANIZATION]", "[SOURCE]") paged_list_response = client.list_findings(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.list_findings_results[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListFindingsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_notification_config(self): # Setup Expected Response name = "name3373707" description = "description-1724546052" pubsub_topic = "pubsubTopic-338126829" service_account = "serviceAccount-1948028253" expected_response = { "name": name, "description": description, "pubsub_topic": pubsub_topic, "service_account": service_account, } expected_response = notification_config_pb2.NotificationConfig( **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_v1.SecurityCenterClient() # Setup Request notification_config = {} response = client.update_notification_config(notification_config) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateNotificationConfigRequest( notification_config=notification_config ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request resource = "resource-341064690" with pytest.raises(CustomException): client.get_iam_policy(resource)
def test_get_source_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request name = client.source_path("[ORGANIZATION]", "[SOURCE]") with pytest.raises(CustomException): client.get_source(name)
def get_asset(org_id, resource_name): """Retrieves the asset corresponding to `resource_name` from SCC.""" client = securitycenter_v1.SecurityCenterClient() maybe_asset = list( client.list_assets( securitycenter_v1.ListAssetsRequest( parent=f"organizations/{org_id}", filter= f'security_center_properties.resource_name = "{resource_name}"', ))) if len(maybe_asset) == 1: return maybe_asset[0].asset return securitycenter_v1.Asset()
def test_update_security_marks_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request security_marks = {} with pytest.raises(CustomException): client.update_security_marks(security_marks)
def test_list_sources_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request parent = client.organization_path("[ORGANIZATION]") paged_list_response = client.list_sources(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_group_findings_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request parent = client.source_path("[ORGANIZATION]", "[SOURCE]") group_by = "groupBy506361367" paged_list_response = client.group_findings(parent, group_by) with pytest.raises(CustomException): list(paged_list_response)
def test_create_notification_config_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request parent = client.organization_path("[ORGANIZATION]") config_id = "configId-804450504" notification_config = {} with pytest.raises(CustomException): client.create_notification_config(parent, config_id, notification_config)
def test_delete_notification_config_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request name = client.notification_config_path( "[ORGANIZATION]", "[NOTIFICATION_CONFIG]" ) with pytest.raises(CustomException): client.delete_notification_config(name)
def test_set_finding_state_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request name = client.finding_path("[ORGANIZATION]", "[SOURCE]", "[FINDING]") state = enums.Finding.State.STATE_UNSPECIFIED start_time = {} with pytest.raises(CustomException): client.set_finding_state(name, state, start_time)
def test_create_finding_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup request parent = client.source_path("[ORGANIZATION]", "[SOURCE]") finding_id = "findingId728776081" finding = {} with pytest.raises(CustomException): client.create_finding(parent, finding_id, finding)
def test_delete_notification_config(self): channel = ChannelStub() patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup Request name = client.notification_config_path( "[ORGANIZATION]", "[NOTIFICATION_CONFIG]" ) client.delete_notification_config(name) assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.DeleteNotificationConfigRequest( name=name ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_run_asset_discovery_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_run_asset_discovery_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") response = client.run_asset_discovery(parent) exception = response.exception() assert exception.errors[0] == error
def test_group_assets(self): # Setup Expected Response next_page_token = "" total_size = 705419236 group_by_results_element = {} group_by_results = [group_by_results_element] expected_response = { "next_page_token": next_page_token, "total_size": total_size, "group_by_results": group_by_results, } expected_response = securitycenter_service_pb2.GroupAssetsResponse( **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_v1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") group_by = "groupBy506361367" paged_list_response = client.group_assets(parent, group_by) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.group_by_results[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GroupAssetsRequest( parent=parent, group_by=group_by ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_notification_config(self): # Setup Expected Response name_2 = "name2-1052831874" description = "description-1724546052" pubsub_topic = "pubsubTopic-338126829" service_account = "serviceAccount-1948028253" expected_response = { "name": name_2, "description": description, "pubsub_topic": pubsub_topic, "service_account": service_account, } expected_response = notification_config_pb2.NotificationConfig( **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_v1.SecurityCenterClient() # Setup Request name = client.notification_config_path( "[ORGANIZATION]", "[NOTIFICATION_CONFIG]" ) response = client.get_notification_config(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetNotificationConfigRequest( name=name ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_finding(self): # Setup Expected Response name = "name3373707" parent_2 = "parent21175163357" resource_name = "resourceName979421212" category = "category50511102" external_uri = "externalUri-1385596168" expected_response = { "name": name, "parent": parent_2, "resource_name": resource_name, "category": category, "external_uri": external_uri, } expected_response = finding_pb2.Finding(**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_v1.SecurityCenterClient() # Setup Request parent = client.source_path("[ORGANIZATION]", "[SOURCE]") finding_id = "findingId728776081" finding = {} response = client.create_finding(parent, finding_id, finding) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.CreateFindingRequest( parent=parent, finding_id=finding_id, finding=finding ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_finding_state(self): # Setup Expected Response name_2 = "name2-1052831874" parent = "parent-995424086" resource_name = "resourceName979421212" category = "category50511102" external_uri = "externalUri-1385596168" expected_response = { "name": name_2, "parent": parent, "resource_name": resource_name, "category": category, "external_uri": external_uri, } expected_response = finding_pb2.Finding(**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_v1.SecurityCenterClient() # Setup Request name = client.finding_path("[ORGANIZATION]", "[SOURCE]", "[FINDING]") state = enums.Finding.State.STATE_UNSPECIFIED start_time = {} response = client.set_finding_state(name, state, start_time) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.SetFindingStateRequest( name=name, state=state, start_time=start_time ) actual_request = channel.requests[0][1] assert expected_request == actual_request