def test_send(self): with patch("smtplib.SMTP") as smtp: notify.send(self.config) instance = smtp.return_value # verify that sendmail() was called self.assertTrue(instance.sendmail.called) self.assertEqual(instance.sendmail.call_count, 1) # verify that the correct email is being sent to sendmail() self.assertEqual( config_parser.get_smtp_email(config=self.config), instance.sendmail.mock_calls[0][2]["from_addr"], ) self.assertEqual( config_parser.get_smtp_to_email(config=self.config), instance.sendmail.mock_calls[0][2]["to_addrs"], ) # verify that the message was passed to sendmail() self.assertIn( "Subject: icloud-docker: Two step authentication required", instance.sendmail.mock_calls[0][2]["msg"], )
def send(config, last_send=None, dry_run=False): sent_on = None email = config_parser.get_smtp_email(config=config) host = config_parser.get_smtp_host(config=config) port = config_parser.get_smtp_port(config=config) no_tls = config_parser.get_smtp_no_tls(config=config) password = config_parser.get_smtp_password(config=config) if (last_send and last_send > datetime.datetime.now() - datetime.timedelta(hours=24)): print("Throttling email to once a day") sent_on = last_send elif email and host and port: try: sent_on = datetime.datetime.now() if not dry_run: smtp = smtplib.SMTP(host, port) smtp.set_debuglevel(0) smtp.connect(host, port) if not no_tls: smtp.starttls() if password: smtp.login(email, password) msg = build_message(email) smtp.sendmail(email, email, msg.as_string()) smtp.quit() except (Exception) as e: sent_on = None print("Error: failed to send email:" + str(e)) logging.exception(e) else: print("Not sending 2FA notification because SMTP is not configured") return sent_on
def send(config, last_send=None, dry_run=False): sent_on = None email = config_parser.get_smtp_email(config=config) to_email = config_parser.get_smtp_to_email(config=config) host = config_parser.get_smtp_host(config=config) port = config_parser.get_smtp_port(config=config) no_tls = config_parser.get_smtp_no_tls(config=config) password = config_parser.get_smtp_password(config=config) if last_send and last_send > datetime.datetime.now() - datetime.timedelta(hours=24): LOGGER.info("Throttling email to once a day") sent_on = last_send elif email and host and port: try: sent_on = datetime.datetime.now() if not dry_run: smtp = smtplib.SMTP(host, port) smtp.set_debuglevel(0) smtp.connect(host, port) if not no_tls: smtp.starttls() if password: smtp.login(email, password) msg = build_message(email) smtp.sendmail(from_addr=email, to_addrs=to_email, msg=msg.as_string()) smtp.quit() except (Exception) as e: sent_on = None LOGGER.error(f"Failed to send email: {str(e)}.") else: LOGGER.warning("Not sending 2FA notification because SMTP is not configured") return sent_on
def test_smtp_email_invalids(self): config = config_parser.read_config() self.assertIsNone(config_parser.get_smtp_email(config=None))
def test_get_smtp_email_valids(self): # Given email config = {'smtp': {'email': '*****@*****.**'}} self.assertEqual(config['smtp']['email'], config_parser.get_smtp_email(config=config))
def test_smtp_email_none_config(self): self.assertIsNone(config_parser.get_smtp_email(config=None))
def test_get_smtp_email(self): # Given email config = {"app": {"smtp": {"email": "*****@*****.**"}}} self.assertEqual(config["app"]["smtp"]["email"], config_parser.get_smtp_email(config=config))