Exemple #1
0
def test_smtpaction_run(mocker, mock_config, dry_run, auth, ssl, starttls):
    mock_config({'debug': False, 'dry_run': dry_run})
    mocker.patch('logstapo.actions.debug_echo')
    mocker.patch('logstapo.actions.SMTPAction._build_msg', return_value='...')
    smtplib = mocker.patch('logstapo.actions.smtplib', autospec=True)
    # XXX: why doesn't autospec handle this?
    smtplib.SMTP.__name__ = 'SMTP'
    smtplib.SMTP_SSL.__name__ = 'SMTP_SSL'
    data = {'host': 'somehost', 'port': 12345, 'ssl': ssl, 'starttls': starttls,
            'from': '*****@*****.**', 'to': '*****@*****.**', 'subject': 'log stuff'}
    if auth:
        data.update({'username': '******', 'password': '******'})
    action = SMTPAction(data)
    action.run({})
    cls = smtplib.SMTP_SSL if ssl else smtplib.SMTP
    if dry_run:
        assert not cls.called
    else:
        cls.assert_called_once_with('somehost', 12345)
        smtp = cls()
        assert smtp.starttls.called == starttls
        if auth:
            smtp.login.assert_called_once_with('user', 'pass')
        assert smtp.send_message.called
        msg = smtp.send_message.call_args[0][0]
        assert msg['To'] == '*****@*****.**'
        assert msg['From'] == '*****@*****.**'
        assert msg['Subject'] == 'log stuff'
        assert msg.get_payload() == '...'
Exemple #2
0
def test_smtpaction_build_msg(group_by_source):
    action = SMTPAction({'to': '*****@*****.**', 'group': group_by_source})
    data = {
        'a': ([('a1', {'source': 'sa1'}),
               ('a2', {'source': 'sa2'}),
               ('a3', {'source': 'sa1'})],
              ['uA']),
        'b': ([], ['uB']),
        'c': ([('c1', {'source': 'sc'})], []),
        'd': ([], [])
    }
    msg = action._build_msg(data)
    if group_by_source:
        assert msg == textwrap.dedent('''
            Logstapo results for 'a'
            =-=-=-=-=-=-=-=-=-=-=-=-

            Unparsable lines
            ~~~~~~~~~~~~~~~~
            uA

            Unusual lines
            -------------
            a1
            a3
            a2



            Logstapo results for 'b'
            =-=-=-=-=-=-=-=-=-=-=-=-

            Unparsable lines
            ~~~~~~~~~~~~~~~~
            uB



            Logstapo results for 'c'
            =-=-=-=-=-=-=-=-=-=-=-=-

            Unusual lines
            -------------
            c1
        ''').strip()
    else:
        assert msg == textwrap.dedent('''
            Logstapo results for 'a'
            =-=-=-=-=-=-=-=-=-=-=-=-

            Unparsable lines
            ~~~~~~~~~~~~~~~~
            uA

            Unusual lines
            -------------
            a1
            a2
            a3



            Logstapo results for 'b'
            =-=-=-=-=-=-=-=-=-=-=-=-

            Unparsable lines
            ~~~~~~~~~~~~~~~~
            uB



            Logstapo results for 'c'
            =-=-=-=-=-=-=-=-=-=-=-=-

            Unusual lines
            -------------
            c1
        ''').strip()