def test_rules_evaluation(alert_spec, input_rules, expected_contacts): ''' Test that rules are properly evaluated ''' with patch.dict(mailer.OPTIONS, mailer.DEFAULT_OPTIONS): mailer.OPTIONS['mail_to'] = [] mailer.OPTIONS['group_rules'] = input_rules mail_sender = mailer.MailSender() with patch.object(mail_sender, '_send_email_message') as _sem: alert = Alert.parse(alert_spec) _, emailed_contacts = mail_sender.send_email(alert) assert _sem.call_count == 1 assert emailed_contacts == expected_contacts
def test_rule_matches_string(): ''' Test regex matching is working properly for a string ''' # Mock options to instantiate mailer with patch.dict(mailer.OPTIONS, mailer.DEFAULT_OPTIONS): mail_sender = mailer.MailSender() with patch.object(mailer, 're') as regex: regex.search.side_effect = [MagicMock(), None] assert mail_sender._rule_matches('regex', 'value1') is True regex.search.assert_called_with('regex', 'value1') assert mail_sender._rule_matches('regex', 'value2') is False regex.search.assert_called_with('regex', 'value2')
if early is not None: if early: print ' - Sending early notices.' else: print ' - Sending late notices.' #Process the data and generate emails. try: emails = secretary.generate_emails(cfg['citywidearea'], emailtemplate, cfg['templatepath']) except mailer.MailError as e: print ' - Error generating emails: {0}'.format(e.message) sys.exit(1) #Now, send the emails. try: postman = mailer.MailSender(cfg['mailserver'], cfg['fromaddress']) #For testing, save them to disk before sending. if args.save: sentemails, totaltime = postman.save_emails(emails) else: sentemails, totaltime = postman.send_emails(emails) except Exception as e: print ' - Error sending emails: {0}'.format(e.message) sys.exit(1) print ' - {0} emails sent in {1:0.2f} seconds.'.format( sentemails, totaltime) if sentemails != len(emails): currdate = datetime.date.today().strftime('%Y%m%d') errfile = 'email_errors_{0}.txt'.format(currdate) print ' - errors encountered sending some emails.'