2015-04-08 12:24:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
2015-04-08 12:24:52 +00:00
|
|
|
'''
|
|
|
|
# Import Python libs
|
2018-01-24 20:47:14 +00:00
|
|
|
from __future__ import absolute_import, unicode_literals, print_function
|
2015-04-08 12:24:52 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 22:28:46 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import (
|
2015-04-08 12:24:52 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.smtp as smtp
|
2015-04-08 12:24:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 22:28:46 +00:00
|
|
|
class SmtpTestCase(TestCase, LoaderModuleMockMixin):
|
2015-04-08 12:24:52 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.states.smtp
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {smtp: {}}
|
|
|
|
|
2015-04-08 12:24:52 +00:00
|
|
|
# 'send_msg' function tests: 1
|
|
|
|
|
|
|
|
def test_send_msg(self):
|
|
|
|
'''
|
|
|
|
Test to send a message via SMTP
|
|
|
|
'''
|
|
|
|
name = 'This is a salt states module'
|
|
|
|
|
|
|
|
comt = ('Need to send message to admin@example.com:'
|
|
|
|
' This is a salt states module')
|
|
|
|
|
|
|
|
ret = {'name': name,
|
|
|
|
'changes': {},
|
|
|
|
'result': None,
|
|
|
|
'comment': comt}
|
|
|
|
|
|
|
|
with patch.dict(smtp.__opts__, {'test': True}):
|
|
|
|
self.assertDictEqual(smtp.send_msg(name,
|
|
|
|
'admin@example.com',
|
|
|
|
'Message from Salt',
|
|
|
|
'admin@example.com',
|
|
|
|
'my-smtp-account'), ret)
|
|
|
|
|
|
|
|
comt = ('Sent message to admin@example.com: '
|
|
|
|
'This is a salt states module')
|
|
|
|
|
|
|
|
with patch.dict(smtp.__opts__, {'test': False}):
|
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.dict(smtp.__salt__, {'smtp.send_msg': mock}):
|
|
|
|
ret['comment'] = comt
|
|
|
|
ret['result'] = True
|
|
|
|
self.assertDictEqual(smtp.send_msg(name,
|
|
|
|
'admin@example.com',
|
|
|
|
'Message from Salt',
|
|
|
|
'admin@example.com',
|
|
|
|
'my-smtp-account'), ret)
|