def send_mail(self, message: MIMEMultipart): if not self._is_active: log = 'Not sending email to {} (service disabled)' logger.info(self, log.format(message['To'])) else: self.connect() # Actually, this connects to SMTP only if required logger.info(self, 'Sending email to {}'.format(message['To'])) self._smtp_connection.send_message(message) from tracim.lib.notifications import EmailNotifier EmailNotifier.log_notification( action=' SENT', recipient=message['To'], subject=message['Subject'], )
def test_unit__log_notification(self): """Check file and format of notification log.""" log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH pattern = '\|{act}\|{rec}\|{subj}$\\n' line_1_act = 'CREATED' line_1_rec = 'user 1 <*****@*****.**>' line_1_subj = 'notification 1' line_1_pattern = pattern.format( act=line_1_act, rec=line_1_rec, subj=line_1_subj, ) line_2_act = ' SENT' line_2_rec = 'user 2 <*****@*****.**>' line_2_subj = 'notification 2' line_2_pattern = pattern.format( act=line_2_act, rec=line_2_rec, subj=line_2_subj, ) EmailNotifier.log_notification( action=line_1_act, recipient=line_1_rec, subject=line_1_subj, ) EmailNotifier.log_notification( action=line_2_act, recipient=line_2_rec, subject=line_2_subj, ) with open(log_path, 'rt') as log_file: line_1 = log_file.readline() line_2 = log_file.readline() os.remove(path=log_path) ok_(re.search(pattern=line_1_pattern, string=line_1)) ok_(re.search(pattern=line_2_pattern, string=line_2))