test_smtp.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.modules.smtp as smtp
  16. class SMTPRecipientsRefused(Exception):
  17. '''
  18. Mock SMTPRecipientsRefused class
  19. '''
  20. def __init__(self, msg):
  21. super(SMTPRecipientsRefused, self).__init__(msg)
  22. self.smtp_error = msg
  23. class SMTPHeloError(Exception):
  24. '''
  25. Mock SMTPHeloError class
  26. '''
  27. def __init__(self, msg):
  28. super(SMTPHeloError, self).__init__(msg)
  29. self.smtp_error = msg
  30. class SMTPSenderRefused(Exception):
  31. '''
  32. Mock SMTPSenderRefused class
  33. '''
  34. def __init__(self, msg):
  35. super(SMTPSenderRefused, self).__init__(msg)
  36. self.smtp_error = msg
  37. class SMTPDataError(Exception):
  38. '''
  39. Mock SMTPDataError class
  40. '''
  41. def __init__(self, msg):
  42. super(SMTPDataError, self).__init__(msg)
  43. self.smtp_error = msg
  44. class SMTPException(Exception):
  45. '''
  46. Mock SMTPException class
  47. '''
  48. def __init__(self, msg):
  49. super(SMTPException, self).__init__(msg)
  50. self.smtp_error = msg
  51. class SMTPAuthenticationError(Exception):
  52. '''
  53. Mock SMTPAuthenticationError class
  54. '''
  55. def __init__(self, msg):
  56. super(SMTPAuthenticationError, self).__init__(msg)
  57. self.smtp_error = msg
  58. class MockSMTPSSL(object):
  59. '''
  60. Mock SMTP_SSL class
  61. '''
  62. flag = None
  63. def __init__(self, server):
  64. pass
  65. def sendmail(self, sender, recipient, msg):
  66. '''
  67. Mock sendmail method
  68. '''
  69. if self.flag == 1:
  70. raise SMTPRecipientsRefused('All recipients were refused.')
  71. elif self.flag == 2:
  72. raise SMTPHeloError('Helo error')
  73. elif self.flag == 3:
  74. raise SMTPSenderRefused('Sender Refused')
  75. elif self.flag == 4:
  76. raise SMTPDataError('Data error')
  77. return (sender, recipient, msg)
  78. def login(self, username, password):
  79. '''
  80. Mock login method
  81. '''
  82. if self.flag == 5:
  83. raise SMTPAuthenticationError('SMTP Authentication Failure')
  84. return (username, password)
  85. @staticmethod
  86. def quit():
  87. '''
  88. Mock quit method
  89. '''
  90. return True
  91. class MockSMTP(object):
  92. '''
  93. Mock SMTP class
  94. '''
  95. flag = None
  96. def __init__(self, server):
  97. pass
  98. @staticmethod
  99. def ehlo():
  100. '''
  101. Mock ehlo method
  102. '''
  103. return True
  104. @staticmethod
  105. def has_extn(name):
  106. '''
  107. Mock has_extn method
  108. '''
  109. return name
  110. def starttls(self):
  111. '''
  112. Mock starttls method
  113. '''
  114. if self.flag == 1:
  115. raise SMTPHeloError('Helo error')
  116. elif self.flag == 2:
  117. raise SMTPException('Exception error')
  118. elif self.flag == 3:
  119. raise RuntimeError
  120. return True
  121. def sendmail(self, sender, recipient, msg):
  122. '''
  123. Mock sendmail method
  124. '''
  125. if self.flag == 1:
  126. raise SMTPRecipientsRefused('All recipients were refused.')
  127. elif self.flag == 2:
  128. raise SMTPHeloError('Helo error')
  129. elif self.flag == 3:
  130. raise SMTPSenderRefused('Sender Refused')
  131. elif self.flag == 4:
  132. raise SMTPDataError('Data error')
  133. return (sender, recipient, msg)
  134. @staticmethod
  135. def quit():
  136. '''
  137. Mock quit method
  138. '''
  139. return True
  140. class MockGaierror(Exception):
  141. '''
  142. Mock MockGaierror class
  143. '''
  144. def __init__(self, msg):
  145. super(MockGaierror, self).__init__(msg)
  146. self.smtp_error = msg
  147. class MockSocket(object):
  148. '''
  149. Mock Socket class
  150. '''
  151. def __init__(self):
  152. self.gaierror = MockGaierror
  153. class MockSmtplib(object):
  154. '''
  155. Mock smtplib class
  156. '''
  157. flag = None
  158. def __init__(self):
  159. self.SMTPRecipientsRefused = SMTPRecipientsRefused
  160. self.SMTPHeloError = SMTPHeloError
  161. self.SMTPSenderRefused = SMTPSenderRefused
  162. self.SMTPDataError = SMTPDataError
  163. self.SMTPException = SMTPException
  164. self.SMTPAuthenticationError = SMTPAuthenticationError
  165. self.server = None
  166. def SMTP_SSL(self, server):
  167. '''
  168. Mock SMTP_SSL method
  169. '''
  170. self.server = server
  171. if self.flag == 1:
  172. raise MockGaierror('gaierror')
  173. return MockSMTPSSL('server')
  174. def SMTP(self, server):
  175. '''
  176. Mock SMTP method
  177. '''
  178. self.server = server
  179. if self.flag == 1:
  180. raise MockGaierror('gaierror')
  181. return MockSMTP('server')
  182. class SmtpTestCase(TestCase, LoaderModuleMockMixin):
  183. '''
  184. TestCase for salt.modules.smtp
  185. '''
  186. def setup_loader_modules(self):
  187. return {
  188. smtp: {
  189. 'socket': MockSocket(),
  190. 'smtplib': MockSmtplib()
  191. }
  192. }
  193. # 'send_msg' function tests: 1
  194. def test_send_msg(self):
  195. '''
  196. Tests if it send a message to an SMTP recipient.
  197. '''
  198. mock = MagicMock(return_value={'smtp.server': '', 'smtp.tls': 'True',
  199. 'smtp.sender': '', 'smtp.username': '',
  200. 'smtp.password': ''})
  201. with patch.dict(smtp.__salt__, {'config.option': mock}):
  202. self.assertTrue(smtp.send_msg('admin@example.com',
  203. 'This is a salt module test',
  204. profile='my-smtp-account'))
  205. MockSMTPSSL.flag = 1
  206. self.assertFalse(smtp.send_msg('admin@example.com',
  207. 'This is a salt module test',
  208. profile='my-smtp-account'))
  209. MockSMTPSSL.flag = 2
  210. self.assertFalse(smtp.send_msg('admin@example.com',
  211. 'This is a salt module test',
  212. profile='my-smtp-account'))
  213. MockSMTPSSL.flag = 3
  214. self.assertFalse(smtp.send_msg('admin@example.com',
  215. 'This is a salt module test',
  216. profile='my-smtp-account'))
  217. MockSMTPSSL.flag = 4
  218. self.assertFalse(smtp.send_msg('admin@example.com',
  219. 'This is a salt module test',
  220. profile='my-smtp-account'))
  221. mock = MagicMock(return_value={'smtp.server': '', 'smtp.tls': '',
  222. 'smtp.sender': '', 'smtp.username': '',
  223. 'smtp.password': ''})
  224. with patch.dict(smtp.__salt__, {'config.option': mock}):
  225. MockSMTPSSL.flag = 5
  226. self.assertFalse(smtp.send_msg('admin@example.com',
  227. 'This is a salt module test',
  228. username='myuser',
  229. password='verybadpass',
  230. sender='admin@example.com',
  231. server='smtp.domain.com'))
  232. MockSMTP.flag = 1
  233. self.assertFalse(smtp.send_msg('admin@example.com',
  234. 'This is a salt module test',
  235. profile='my-smtp-account'))
  236. MockSMTP.flag = 2
  237. self.assertFalse(smtp.send_msg('admin@example.com',
  238. 'This is a salt module test',
  239. profile='my-smtp-account'))
  240. MockSMTP.flag = 3
  241. self.assertFalse(smtp.send_msg('admin@example.com',
  242. 'This is a salt module test',
  243. profile='my-smtp-account'))
  244. MockSmtplib.flag = 1
  245. self.assertFalse(smtp.send_msg('admin@example.com',
  246. 'This is a salt module test',
  247. profile='my-smtp-account'))