def test_create_inspect_operation_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_create_inspect_operation_exception',
            done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        name = 'EMAIL_ADDRESS'
        info_types_element = {'name': name}
        info_types = [info_types_element]
        inspect_config = {'info_types': info_types}
        url = 'gs://example_bucket/example_file.png'
        file_set = {'url': url}
        cloud_storage_options = {'file_set': file_set}
        storage_config = {'cloud_storage_options': cloud_storage_options}
        output_config = {}

        response = client.create_inspect_operation(inspect_config,
                                                   storage_config,
                                                   output_config)
        exception = response.exception()
        assert exception.errors[0] == error
    def test_inspect_content(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = dlp_pb2.InspectContentResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        name = 'EMAIL_ADDRESS'
        info_types_element = {'name': name}
        info_types = [info_types_element]
        inspect_config = {'info_types': info_types}
        type_ = 'text/plain'
        value = 'My email is [email protected].'
        items_element = {'type': type_, 'value': value}
        items = [items_element]

        response = client.inspect_content(inspect_config, items)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = dlp_pb2.InspectContentRequest(
            inspect_config=inspect_config, items=items)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_analyze_data_source_risk(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = dlp_pb2.RiskAnalysisOperationResult(
            **expected_response)
        operation = operations_pb2.Operation(
            name='operations/test_analyze_data_source_risk', done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        privacy_metric = {}
        source_table = {}

        response = client.analyze_data_source_risk(privacy_metric,
                                                   source_table)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = dlp_pb2.AnalyzeDataSourceRiskRequest(
            privacy_metric=privacy_metric, source_table=source_table)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_deidentify_content(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = dlp_pb2.DeidentifyContentResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        deidentify_config = {}
        inspect_config = {}
        items = []

        response = client.deidentify_content(deidentify_config, inspect_config,
                                             items)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = dlp_pb2.DeidentifyContentRequest(
            deidentify_config=deidentify_config,
            inspect_config=inspect_config,
            items=items)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_redact_content_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup request
        name = 'EMAIL_ADDRESS'
        info_types_element = {'name': name}
        info_types = [info_types_element]
        inspect_config = {'info_types': info_types}
        type_ = 'text/plain'
        value = 'My email is [email protected].'
        items_element = {'type': type_, 'value': value}
        items = [items_element]
        name_2 = 'EMAIL_ADDRESS'
        info_type = {'name': name_2}
        replace_with = 'REDACTED'
        replace_configs_element = {
            'info_type': info_type,
            'replace_with': replace_with
        }
        replace_configs = [replace_configs_element]

        with pytest.raises(CustomException):
            client.redact_content(inspect_config,
                                  items,
                                  replace_configs=replace_configs)
    def test_list_root_categories_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup request
        language_code = 'en'

        with pytest.raises(CustomException):
            client.list_root_categories(language_code)
    def test_list_inspect_findings_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup request
        name = client.result_path('[RESULT]')

        with pytest.raises(CustomException):
            client.list_inspect_findings(name)
    def test_inspect_content(self):

        client = dlp_v2beta1.DlpServiceClient()
        min_likelihood = enums.Likelihood.POSSIBLE
        inspect_config = {'min_likelihood': min_likelihood}
        type_ = 'text/plain'
        value = 'my phone number is 215-512-1212'
        items_element = {'type': type_, 'value': value}
        items = [items_element]
        response = client.inspect_content(inspect_config, items)
    def test_deidentify_content_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup request
        deidentify_config = {}
        inspect_config = {}
        items = []

        with pytest.raises(CustomException):
            client.deidentify_content(deidentify_config, inspect_config, items)
Example #10
0
def main():

    # Build the service , add credentials from Services JSON file , as default Environment variables didn't work

    client = dlp_v2beta1.DlpServiceClient(credentials=credentials)
    # Data for DLP Inspection just for test, used Google Example
    name = 'EMAIL_ADDRESS'
    info_types_element = {'name': name}
    info_types = [info_types_element]
    inspect_config = {'info_types': info_types}
    type_ = 'text/plain'
    value = 'My email is [email protected].'
    items_element = {'type': type_, 'value': value}
    items = [items_element]
    #DLP Findings
    response = client.inspect_content(inspect_config, items)
    print(response)
    def test_analyze_data_source_risk_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_analyze_data_source_risk_exception',
            done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        privacy_metric = {}
        source_table = {}

        response = client.analyze_data_source_risk(privacy_metric,
                                                   source_table)
        exception = response.exception()
        assert exception.errors[0] == error
    def test_list_root_categories(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = dlp_pb2.ListRootCategoriesResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        language_code = 'en'

        response = client.list_root_categories(language_code)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = dlp_pb2.ListRootCategoriesRequest(
            language_code=language_code)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_inspect_findings(self):
        # Setup Expected Response
        next_page_token = 'nextPageToken-1530815211'
        expected_response = {'next_page_token': next_page_token}
        expected_response = dlp_pb2.ListInspectFindingsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        name = client.result_path('[RESULT]')

        response = client.list_inspect_findings(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = dlp_pb2.ListInspectFindingsRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_inspect_operation(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        expected_response = {'name': name_2}
        expected_response = dlp_pb2.InspectOperationResult(**expected_response)
        operation = operations_pb2.Operation(
            name='operations/test_create_inspect_operation', done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = dlp_v2beta1.DlpServiceClient(channel=channel)

        # Setup Request
        name = 'EMAIL_ADDRESS'
        info_types_element = {'name': name}
        info_types = [info_types_element]
        inspect_config = {'info_types': info_types}
        url = 'gs://example_bucket/example_file.png'
        file_set = {'url': url}
        cloud_storage_options = {'file_set': file_set}
        storage_config = {'cloud_storage_options': cloud_storage_options}
        output_config = {}

        response = client.create_inspect_operation(inspect_config,
                                                   storage_config,
                                                   output_config)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = dlp_pb2.CreateInspectOperationRequest(
            inspect_config=inspect_config,
            storage_config=storage_config,
            output_config=output_config)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request