test_smtp.py 8.5 KB

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