email_tests.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. """Unit tests for email service in Superset"""
  19. import logging
  20. import tempfile
  21. import unittest
  22. from email.mime.application import MIMEApplication
  23. from email.mime.image import MIMEImage
  24. from email.mime.multipart import MIMEMultipart
  25. from unittest import mock
  26. from superset import app
  27. from superset.utils import core as utils
  28. from tests.base_tests import SupersetTestCase
  29. from .utils import read_fixture
  30. send_email_test = mock.Mock()
  31. logger = logging.getLogger(__name__)
  32. class EmailSmtpTest(SupersetTestCase):
  33. def setUp(self):
  34. app.config["smtp_ssl"] = False
  35. @mock.patch("superset.utils.core.send_MIME_email")
  36. def test_send_smtp(self, mock_send_mime):
  37. attachment = tempfile.NamedTemporaryFile()
  38. attachment.write(b"attachment")
  39. attachment.seek(0)
  40. utils.send_email_smtp(
  41. "to", "subject", "content", app.config, files=[attachment.name]
  42. )
  43. assert mock_send_mime.called
  44. call_args = mock_send_mime.call_args[0]
  45. logger.debug(call_args)
  46. assert call_args[0] == app.config["SMTP_MAIL_FROM"]
  47. assert call_args[1] == ["to"]
  48. msg = call_args[2]
  49. assert msg["Subject"] == "subject"
  50. assert msg["From"] == app.config["SMTP_MAIL_FROM"]
  51. assert len(msg.get_payload()) == 2
  52. mimeapp = MIMEApplication("attachment")
  53. assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
  54. @mock.patch("superset.utils.core.send_MIME_email")
  55. def test_send_smtp_data(self, mock_send_mime):
  56. utils.send_email_smtp(
  57. "to", "subject", "content", app.config, data={"1.txt": b"data"}
  58. )
  59. assert mock_send_mime.called
  60. call_args = mock_send_mime.call_args[0]
  61. logger.debug(call_args)
  62. assert call_args[0] == app.config["SMTP_MAIL_FROM"]
  63. assert call_args[1] == ["to"]
  64. msg = call_args[2]
  65. assert msg["Subject"] == "subject"
  66. assert msg["From"] == app.config["SMTP_MAIL_FROM"]
  67. assert len(msg.get_payload()) == 2
  68. mimeapp = MIMEApplication("data")
  69. assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
  70. @mock.patch("superset.utils.core.send_MIME_email")
  71. def test_send_smtp_inline_images(self, mock_send_mime):
  72. image = read_fixture("sample.png")
  73. utils.send_email_smtp(
  74. "to", "subject", "content", app.config, images=dict(blah=image)
  75. )
  76. assert mock_send_mime.called
  77. call_args = mock_send_mime.call_args[0]
  78. logger.debug(call_args)
  79. assert call_args[0] == app.config["SMTP_MAIL_FROM"]
  80. assert call_args[1] == ["to"]
  81. msg = call_args[2]
  82. assert msg["Subject"] == "subject"
  83. assert msg["From"] == app.config["SMTP_MAIL_FROM"]
  84. assert len(msg.get_payload()) == 2
  85. mimeapp = MIMEImage(image)
  86. assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
  87. @mock.patch("superset.utils.core.send_MIME_email")
  88. def test_send_bcc_smtp(self, mock_send_mime):
  89. attachment = tempfile.NamedTemporaryFile()
  90. attachment.write(b"attachment")
  91. attachment.seek(0)
  92. utils.send_email_smtp(
  93. "to",
  94. "subject",
  95. "content",
  96. app.config,
  97. files=[attachment.name],
  98. cc="cc",
  99. bcc="bcc",
  100. )
  101. assert mock_send_mime.called
  102. call_args = mock_send_mime.call_args[0]
  103. assert call_args[0] == app.config["SMTP_MAIL_FROM"]
  104. assert call_args[1] == ["to", "cc", "bcc"]
  105. msg = call_args[2]
  106. assert msg["Subject"] == "subject"
  107. assert msg["From"] == app.config["SMTP_MAIL_FROM"]
  108. assert len(msg.get_payload()) == 2
  109. mimeapp = MIMEApplication("attachment")
  110. assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
  111. @mock.patch("smtplib.SMTP_SSL")
  112. @mock.patch("smtplib.SMTP")
  113. def test_send_mime(self, mock_smtp, mock_smtp_ssl):
  114. mock_smtp.return_value = mock.Mock()
  115. mock_smtp_ssl.return_value = mock.Mock()
  116. msg = MIMEMultipart()
  117. utils.send_MIME_email("from", "to", msg, app.config, dryrun=False)
  118. mock_smtp.assert_called_with(app.config["SMTP_HOST"], app.config["SMTP_PORT"])
  119. assert mock_smtp.return_value.starttls.called
  120. mock_smtp.return_value.login.assert_called_with(
  121. app.config["SMTP_USER"], app.config["SMTP_PASSWORD"]
  122. )
  123. mock_smtp.return_value.sendmail.assert_called_with(
  124. "from", "to", msg.as_string()
  125. )
  126. assert mock_smtp.return_value.quit.called
  127. @mock.patch("smtplib.SMTP_SSL")
  128. @mock.patch("smtplib.SMTP")
  129. def test_send_mime_ssl(self, mock_smtp, mock_smtp_ssl):
  130. app.config["SMTP_SSL"] = True
  131. mock_smtp.return_value = mock.Mock()
  132. mock_smtp_ssl.return_value = mock.Mock()
  133. utils.send_MIME_email("from", "to", MIMEMultipart(), app.config, dryrun=False)
  134. assert not mock_smtp.called
  135. mock_smtp_ssl.assert_called_with(
  136. app.config["SMTP_HOST"], app.config["SMTP_PORT"]
  137. )
  138. @mock.patch("smtplib.SMTP_SSL")
  139. @mock.patch("smtplib.SMTP")
  140. def test_send_mime_noauth(self, mock_smtp, mock_smtp_ssl):
  141. app.config["SMTP_USER"] = None
  142. app.config["SMTP_PASSWORD"] = None
  143. mock_smtp.return_value = mock.Mock()
  144. mock_smtp_ssl.return_value = mock.Mock()
  145. utils.send_MIME_email("from", "to", MIMEMultipart(), app.config, dryrun=False)
  146. assert not mock_smtp_ssl.called
  147. mock_smtp.assert_called_with(app.config["SMTP_HOST"], app.config["SMTP_PORT"])
  148. assert not mock_smtp.login.called
  149. @mock.patch("smtplib.SMTP_SSL")
  150. @mock.patch("smtplib.SMTP")
  151. def test_send_mime_dryrun(self, mock_smtp, mock_smtp_ssl):
  152. utils.send_MIME_email("from", "to", MIMEMultipart(), app.config, dryrun=True)
  153. assert not mock_smtp.called
  154. assert not mock_smtp_ssl.called
  155. if __name__ == "__main__":
  156. unittest.main()