Esempio n. 1
0
def test_mail_content_from_host_context(mocker):
    mocker.patch("cmk.notification_plugins.mail.socket.getfqdn", lambda: 'mysite.com')

    context = mock_host_context()
    assert "EVENT_TXT" not in context
    assert "EVENT_HTML" not in context
    assert "HOSTOUTPUT_HTML" not in context
    assert "SERVICEOUTPUT_HTML" not in context
    assert "LONGSERVICEOUTPUT_HTML" not in context
    assert "LINKEDHOSTNAME" not in context
    assert "LINKEDSERVICEDESC" not in context

    content = mail.SingleEmailContent(mock_host_context)

    assert content.context["EVENT_TXT"] == "DOWN -> UP"
    assert content.context[
        "EVENT_HTML"] == '<span class="stateDOWN">DOWN</span> &rarr; <span class="stateUP">UP</span>'
    assert content.context["HOSTOUTPUT"] == 'Packet received via smart PING'
    assert content.context["HOSTOUTPUT_HTML"] == 'Packet received via smart PING'
    assert "SERVICEOUTPUT" not in content.context
    assert "SERVICEOUTPUT_HTML" not in content.context
    assert "LONGSERVICEOUTPUT" not in content.context
    assert "LONGSERVICEOUTPUT_HTML" not in content.context

    assert content.context[
        'LINKEDHOSTNAME'] == '<a href="https://Klappschloss/heute/check_mk/index.py?start_url=view.py%3Fview_name%3Dhoststatus%26host%3Dheute%26site%3Dheute">heute</a>'
    assert content.context['LINKEDSERVICEDESC'] == ''

    assert content.mailto == '*****@*****.**'
    assert content.subject == 'Check_MK: heute - DOWN -> UP'
    assert content.from_address == u'*****@*****.**'
    assert content.reply_to == u''
    assert content.content_txt == HOST_CONTENT_TXT
    assert content.attachments == []
Esempio n. 2
0
def test_mail_content_from_service_context(mocker):
    # The items below are added by the mail plugin
    context = mock_service_context()
    assert "EVENT_TXT" not in context
    assert "EVENT_HTML" not in context
    assert "HOSTOUTPUT_HTML" not in context
    assert "SERVICEOUTPUT_HTML" not in context
    assert "LONGSERVICEOUTPUT_HTML" not in context
    assert "LINKEDHOSTNAME" not in context
    assert "LINKEDSERVICEDESC" not in context

    content = mail.SingleEmailContent(mock_service_context)

    # The state markers (!) and (!!) as well as the states in EVENT_TXT have to be
    # replaced with HTML, but raw input from plugins has to be escaped.
    # LONGSERVICEOUTPUT_HTML additionally replaces '\n' and '\\n' by '<br>'.
    assert content.context["EVENT_TXT"] == "OK -> WARN"
    assert (
        content.context["EVENT_HTML"] ==
        '<span class="stateOK">OK</span> &rarr; <span class="stateWARNING">WARNING</span>'
    )
    assert (
        content.context["HOSTOUTPUT"] ==
        "&lt;script&gt;console.log(&quot;evil&quot;);&lt;/script&gt;Packet received via smart PING (!)"
    )
    assert (
        content.context["HOSTOUTPUT_HTML"] ==
        '&lt;script&gt;console.log(&quot;evil&quot;);&lt;/script&gt;Packet received via smart PING <b class="stmarkWARNING">WARN</b>'
    )
    assert (
        content.context["SERVICEOUTPUT"] ==
        "&lt;script&gt;console.log(&quot;evil&quot;);&lt;/script&gt; Ok (!)")
    assert (
        content.context["SERVICEOUTPUT_HTML"] ==
        '&lt;script&gt;console.log(&quot;evil&quot;);&lt;/script&gt; Ok <b class="stmarkWARNING">WARN</b>'
    )
    assert (
        content.context["LONGSERVICEOUTPUT"] ==
        "&lt;script&gt;console.log(&quot;evil&quot;);&lt;/script&gt;(!)\nanother line\\nlast line"
    )
    assert (
        content.context["LONGSERVICEOUTPUT_HTML"] ==
        '&lt;script&gt;console.log(&quot;evil&quot;);&lt;/script&gt;<b class="stmarkWARNING">WARN</b><br>another line<br>last line'
    )

    assert (
        content.context["LINKEDHOSTNAME"] ==
        '<a href="http://Klappschloss/heute/check_mk/index.py?start_url=view.py%3Fview_name%3Dhoststatus%26host%3Dheute%26site%3Dheute">heute</a>'
    )
    assert (
        content.context["LINKEDSERVICEDESC"] ==
        '<a href="http://Klappschloss/heute/check_mk/index.py?start_url=view.py%3Fview_name%3Dservice%26host%3Dheute%26service%3DCPU%20utilization%26site%3Dheute">CPU utilization</a>'
    )

    assert content.mailto == "*****@*****.**"
    assert content.subject == "Check_MK: heute/CPU utilization OK -> WARN"
    assert content.from_address == "*****@*****.**"
    assert content.reply_to == "*****@*****.**"
    assert content.content_txt == SERVICE_CONTENT_TXT
    assert content.attachments == []