test_smtp.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.smtp as smtp
  16. class SmtpTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.smtp
  19. '''
  20. def setup_loader_modules(self):
  21. return {smtp: {}}
  22. # 'send_msg' function tests: 1
  23. def test_send_msg(self):
  24. '''
  25. Test to send a message via SMTP
  26. '''
  27. name = 'This is a salt states module'
  28. comt = ('Need to send message to admin@example.com:'
  29. ' This is a salt states module')
  30. ret = {'name': name,
  31. 'changes': {},
  32. 'result': None,
  33. 'comment': comt}
  34. with patch.dict(smtp.__opts__, {'test': True}):
  35. self.assertDictEqual(smtp.send_msg(name,
  36. 'admin@example.com',
  37. 'Message from Salt',
  38. 'admin@example.com',
  39. 'my-smtp-account'), ret)
  40. comt = ('Sent message to admin@example.com: '
  41. 'This is a salt states module')
  42. with patch.dict(smtp.__opts__, {'test': False}):
  43. mock = MagicMock(return_value=True)
  44. with patch.dict(smtp.__salt__, {'smtp.send_msg': mock}):
  45. ret['comment'] = comt
  46. ret['result'] = True
  47. self.assertDictEqual(smtp.send_msg(name,
  48. 'admin@example.com',
  49. 'Message from Salt',
  50. 'admin@example.com',
  51. 'my-smtp-account'), ret)