# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Mike Place (mp@saltstack.com)`


    tests.unit.returners.smtp_return_test
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'''

# Import Python libs
from __future__ import absolute_import

# Import Salt Testing libs
from salttesting import TestCase, skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch

ensure_in_syspath('../../')

# Import salt libs
from salt.returners import smtp_return as smtp

smtp.__salt__ = {}
smtp.__opts__ = {}

try:
    import gnupg  # pylint: disable=unused-import
    HAS_GNUPG = True
except ImportError:
    HAS_GNUPG = False


@skipIf(NO_MOCK, NO_MOCK_REASON)
class SMTPReturnerTestCase(TestCase):
    '''
    Test SMTP returner
    '''
    def _test_returner(self, mocked_smtplib, *args):  # pylint: disable=unused-argument
        '''
        Test to see if the SMTP returner sends a message
        '''
        ret = {'id': '12345',
               'fun': 'mytest.func',
               'fun_args': 'myfunc args',
               'jid': '54321',
               'return': 'The room is on fire as shes fixing her hair'
               }

        with patch.dict(smtp.__salt__, {'config.option': MagicMock()}):
            smtp.returner(ret)
            self.assertTrue(mocked_smtplib.return_value.sendmail.called)

if HAS_GNUPG:
    @patch('salt.returners.smtp_return.gnupg')
    @patch('salt.returners.smtp_return.smtplib.SMTP')
    def test_returner(self, mocked_smtplib, *args):
        self._test_returner(mocked_smtplib, *args)

else:
    @patch('salt.returners.smtp_return.smtplib.SMTP')
    def test_returner(self, mocked_smtplib, *args):
        self._test_returner(mocked_smtplib, *args)

SMTPReturnerTestCase.test_returner = test_returner
if __name__ == '__main__':
    from integration import run_tests
    run_tests(SMTPReturnerTestCase, needs_daemon=False)

@@ -1185,65 +1185,453 @@
   %7D%0A
-%0A        with patch.dict(smtp.__salt__, %7B'config.
+        options = %7B'username': '',%0A                  'tls': '',%0A                  'from': '',%0A                  'fields': 'id,fun,fun_args,jid,return',%0A                  'to': '',%0A                  'host': '',%0A                  'renderer': 'yaml',%0A                  'template': '',%0A                  'password': '',%0A                  'gpgowner': '',%0A                  'subject': ''%7D%0A%0A        with patch('salt.returners.smtp_return._get_
 option
-':
+s',
  Mag
@@ -1641,10 +1641,29 @@
 ock(
+return_value=options
 )
-%7D
 ):%0A 
@@ -1925,32 +1925,163 @@
 mtplib, *args):%0A
+        with patch.dict(smtp.__opts__, %7B'extension_modules': '',%0A                                        'renderer': 'yaml'%7D):%0A    
         self._te
@@ -2220,32 +2220,163 @@
 mtplib, *args):%0A
+        with patch.dict(smtp.__opts__, %7B'extension_modules': '',%0A                                        'renderer': 'yaml'%7D):%0A    
         self._te

update smtp returner unit tests