Esempio n. 1
0
def test_email_format_html():
    rule = {
        'name': 'test alert',
        'email': ['*****@*****.**', '*****@*****.**'],
        'smtp_ssl': True,
        'smtp_port': 455,
        'email_format': 'html',
        'from_addr': '*****@*****.**',
        'type': mock_rule(),
        'timestamp_field': '@timestamp',
        'email_reply_to': '*****@*****.**',
        'owner': 'owner_value',
        'alert_subject': 'Test alert for {0}, owned by {1}',
        'alert_subject_args': ['test_term', 'owner'],
        'snowman': '☃'
    }
    with mock.patch('elastalert.alerters.email.SMTP_SSL') as mock_smtp:
        mock_smtp.return_value = mock.Mock()

        alert = EmailAlerter(rule)
        alert.alert([{'test_term': 'test_value'}])
        expected = [
            mock.call('localhost', 455, certfile=None, keyfile=None),
            mock.call().sendmail(mock.ANY,
                                 ['*****@*****.**', '*****@*****.**'],
                                 mock.ANY),
            mock.call().quit()
        ]
        assert mock_smtp.mock_calls == expected

        body = mock_smtp.mock_calls[1][1][2]

        assert 'Reply-To: [email protected]' in body
        assert 'To: [email protected]' in body
        assert 'From: [email protected]' in body
        assert 'Subject: Test alert for test_value, owned by owner_value' in body
        assert 'Content-Type: text/html; charset="utf-8"' in body
Esempio n. 2
0
def test_email_smtp_port():
    rule = {
        'name': 'test alert',
        'email': ['*****@*****.**', '*****@*****.**'],
        'smtp_port': 35,
        'from_addr': '*****@*****.**',
        'type': mock_rule(),
        'timestamp_field': '@timestamp',
        'email_reply_to': '*****@*****.**',
        'owner': 'owner_value',
        'alert_subject': 'Test alert for {0}, owned by {1}',
        'alert_subject_args': ['test_term', 'owner'],
        'snowman': '☃'
    }
    with mock.patch('elastalert.alerters.email.SMTP') as mock_smtp:
        mock_smtp.return_value = mock.Mock()

        alert = EmailAlerter(rule)
        alert.alert([{'test_term': 'test_value'}])
        expected = [
            mock.call('localhost', 35),
            mock.call().ehlo(),
            mock.call().has_extn('STARTTLS'),
            mock.call().starttls(certfile=None, keyfile=None),
            mock.call().sendmail(mock.ANY,
                                 ['*****@*****.**', '*****@*****.**'],
                                 mock.ANY),
            mock.call().quit()
        ]
        assert mock_smtp.mock_calls == expected

        body = mock_smtp.mock_calls[4][1][2]

        assert 'Reply-To: [email protected]' in body
        assert 'To: [email protected]' in body
        assert 'From: [email protected]' in body
        assert 'Subject: Test alert for test_value, owned by owner_value' in body