Esempio n. 1
0
def test_parse_error_response_forbidden(demisto_client_configure, mocker):
    """
    Given
        - An empty (no given input path) Uploader object
        - An API exception raised by forbidden failure

    When
        - Parsing error response

    Then
        - Ensure a error message is parsed successfully
        - Verify forbidden error message printed as expected
    """
    mocker.patch("builtins.print")
    file_type = "incident field"
    file_name = "SomeIncidentFieldName.json"
    api_exception = ApiException(reason="Forbidden", )
    api_exception.body = json.dumps({"status": 403, "error": "Error message"})
    uploader = Uploader(input="", insecure=False, verbose=False)
    uploader._parse_error_response(error=api_exception,
                                   file_type=file_type,
                                   file_name=file_name)
    upload_failed_message = u"{}{}{}".format(
        LOG_COLORS.RED, f"\nUpload {file_type}: {file_name} failed:",
        LOG_COLORS.NATIVE)
    api_exception_message = u'{}{}{}'.format(
        LOG_COLORS.RED,
        "Error message\nTry checking your API key configuration.",
        LOG_COLORS.NATIVE)
    # verify exactly 2 calls to print_error
    assert len(print.call_args_list) == 2
    assert print.call_args_list[0][0][0] == upload_failed_message
    assert print.call_args_list[1][0][0] == api_exception_message
Esempio n. 2
0
def test_parse_error_response_connection(demisto_client_configure, mocker):
    """
    Given
        - An empty (no given input path) Uploader object
        - An API exception raised by connection failure

    When
        - Parsing error response

    Then
        - Ensure a error message is parsed successfully
        - Verify connection error message printed as expected
    """
    mocker.patch("builtins.print")
    file_type = "widget"
    file_name = "SomeWidgetName.json"
    api_exception = ApiException(reason="Failed to establish a new connection:")
    uploader = Uploader(input="", insecure=False, verbose=False)
    uploader._parse_error_response(error=api_exception, file_type=file_type, file_name=file_name)
    upload_failed_message = u"{}{}{}".format(
        LOG_COLORS.RED, f"\nUpload {file_type}: {file_name} failed:", LOG_COLORS.NATIVE
    )
    api_exception_message = u'{}{}{}'.format(
        LOG_COLORS.RED,
        "Failed to establish a new connection: Connection refused.\n"
        "Try checking your BASE url configuration.",
        LOG_COLORS.NATIVE
    )
    # verify exactly 2 calls to print_error
    assert len(print.call_args_list) == 2
    assert print.call_args_list[0][0][0] == upload_failed_message
    assert print.call_args_list[1][0][0] == api_exception_message
Esempio n. 3
0
def test_parse_error_response_ssl(demisto_client_configure, mocker):
    """
    Given
        - An empty (no given input path) Uploader object
        - An API exception raised by SSL failure

    When
        - Parsing error response

    Then
        - Ensure a error message is parsed successfully
        - Verify SSL error message printed as expected
    """
    mocker.patch("builtins.print")
    file_type = "playbook"
    file_name = "SomePlaybookName.yml"
    api_exception = ApiException(reason="[SSL: CERTIFICATE_VERIFY_FAILED]")
    uploader = Uploader(input="", insecure=False, verbose=False)
    uploader._parse_error_response(error=api_exception, file_type=file_type, file_name=file_name)
    upload_failed_message = u"{}{}{}".format(
        LOG_COLORS.RED, f"\nUpload {file_type}: {file_name} failed:", LOG_COLORS.NATIVE
    )
    api_exception_message = u'{}{}{}'.format(
        LOG_COLORS.RED,
        "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate.\n"
        "Try running the command with --insecure flag.",
        LOG_COLORS.NATIVE
    )
    # verify exactly 2 calls to print_error
    assert len(print.call_args_list) == 2
    assert print.call_args_list[0][0][0] == upload_failed_message
    assert print.call_args_list[1][0][0] == api_exception_message
Esempio n. 4
0
def test_parse_error_response_forbidden(demisto_client_configure, mocker):
    """
    Given
        - An API exception raised by forbidden failure

    When
        - Parsing error response

    Then
        - Ensure a error message is parsed successfully
        - Verify forbidden error message printed as expected
    """
    file_type = "incident field"
    file_name = "SomeIncidentFieldName.json"
    api_exception = ApiException(reason="Forbidden", )
    api_exception.body = json.dumps({"status": 403, "error": "Error message"})
    message = parse_error_response(error=api_exception,
                                   file_type=file_type,
                                   file_name=file_name)
    assert message == "Error message\nTry checking your API key configuration."
Esempio n. 5
0
def test_parse_error_response_connection(demisto_client_configure, mocker):
    """
    Given
        - An API exception raised by connection failure

    When
        - Parsing error response

    Then
        - Ensure a error message is parsed successfully
        - Verify connection error message printed as expected
    """
    file_type = "widget"
    file_name = "SomeWidgetName.json"
    api_exception = ApiException(reason="Failed to establish a new connection:")
    error_message = parse_error_response(error=api_exception, file_type=file_type, file_name=file_name)
    assert error_message == 'Failed to establish a new connection: Connection refused.\n' \
                            'Try checking your BASE url configuration.'
Esempio n. 6
0
def test_parse_error_response_ssl(demisto_client_configure, mocker):
    """
    Given
        - An API exception raised by SSL failure

    When
        - Parsing error response

    Then
        - Ensure a error message is parsed successfully
        - Verify SSL error message printed as expected
    """
    file_type = "playbook"
    file_name = "SomePlaybookName.yml"
    api_exception = ApiException(reason="[SSL: CERTIFICATE_VERIFY_FAILED]")
    message = parse_error_response(error=api_exception, file_type=file_type, file_name=file_name)
    assert message == '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate.\n' \
                      'Try running the command with --insecure flag.'
Esempio n. 7
0
    def test_malformed_pack_exception(self, mocker):
        """

        Given:
            An error response noting that the installation failed due to malformed pack
        When:
            installing packs on servers
        Then:
            Retry without failing pack.
            Fail completely if reoccurs after removing.

        """
        http_resp = MockHttpRequest(MALFORMED_PACK_RESPONSE_BODY)
        mocker.patch.object(demisto_client,
                            'generic_request_func',
                            side_effect=ApiException(http_resp=http_resp))
        client = MockClient()
        assert not script.install_packs(client,
                                        'my_host',
                                        packs_to_install=[{
                                            'id': 'pack1'
                                        }, {
                                            'id': 'pack2'
                                        }])
Esempio n. 8
0
    def test_gcp_timeout_exception(self, mocker):
        """

            Given:
                An error response noting that the installation failed due to gcp timeout
            When:
                installing packs on servers
            Then:
                Retry once again.
                Fail completely if reoccurs after retry.

            """
        http_resp = MockHttpRequest(GCP_TIMEOUT_EXCEPTION_RESPONSE_BODY)
        mocker.patch.object(demisto_client,
                            'generic_request_func',
                            side_effect=ApiException(http_resp=http_resp))
        client = MockClient()
        assert not script.install_packs(client,
                                        'my_host',
                                        packs_to_install=[{
                                            'id': 'pack1'
                                        }, {
                                            'id': 'pack3'
                                        }])