def test_main_notifies_about_briefs_closed_on_date_8_weeks_ago_using_offset_days( self, logger, get_briefs_closed_on_date, send_email_to_brief_user_via_notify, notify_client): get_briefs_closed_on_date.return_value = [self.brief1, self.brief2] send_email_to_brief_user_via_notify.return_value = [] with freeze_time('2016-02-26 03:04:05'): assert notify_buyers_to_award_closed_briefs.main( 'URL', 'API_KEY', 'NOTIFY_KEY', 'NOTIFY_TEMPLATE_ID', offset_days=56, date_closed=None, dry_run=None) get_briefs_closed_on_date.assert_called_once_with( mock.ANY, datetime.date(2016, 1, 1)) notify_client.assert_called_once_with('NOTIFY_KEY', logger=mock.ANY) assert send_email_to_brief_user_via_notify.call_args_list == [ mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief1['users'][0], self.brief1, None, None), mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief2['users'][0], self.brief2, None, None), ]
def test_main_sends_email_to_brief_user_via_notify_for_each_user_on_each_closed_brief( self, logger, get_briefs_closed_on_date, send_email_to_brief_user_via_notify, notify_client): get_briefs_closed_on_date.return_value = [self.brief1, self.brief2] send_email_to_brief_user_via_notify.return_value = [] with freeze_time('2016-01-29 03:04:05'): assert notify_buyers_to_award_closed_briefs.main( 'URL', 'API_KEY', 'NOTIFY_KEY', 'NOTIFY_TEMPLATE_ID', self.OFFSET_DAYS, date_closed='2016-01-01', dry_run=None) get_briefs_closed_on_date.assert_called_once_with( mock.ANY, datetime.date(2016, 1, 1)) notify_client.assert_called_once_with('NOTIFY_KEY', logger=mock.ANY) assert send_email_to_brief_user_via_notify.call_args_list == [ mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief1['users'][0], self.brief1, None, None), mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief2['users'][0], self.brief2, None, None), ] assert logger.info.call_args_list == [ mock.call("Data API URL: {data_api_url}", extra={'data_api_url': 'URL'}), mock.call( "{briefs_count} closed brief(s) found with closing date {date_closed}", extra={ 'briefs_count': 2, 'date_closed': datetime.date(2016, 1, 1) }) ]
def test_main_fails_when_send_email_to_brief_user_via_notify_fails( self, logger, get_briefs_closed_on_date, send_email_to_brief_user_via_notify, notify_client): get_briefs_closed_on_date.return_value = [ self.brief1, self.brief2, self.brief3 ] send_email_to_brief_user_via_notify.side_effect = [9, None, 999, 9999] assert not notify_buyers_to_award_closed_briefs.main( 'URL', 'API_KEY', 'NOTIFY_KEY', 'NOTIFY_TEMPLATE_ID', self.OFFSET_DAYS, date_closed="2017-01-01", dry_run=None) notify_client.assert_called_with('NOTIFY_KEY', logger=mock.ANY) assert send_email_to_brief_user_via_notify.call_args_list == [ mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief1['users'][0], self.brief1, None, None), mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief2['users'][0], self.brief2, None, None), mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief3['users'][0], self.brief3, None, None), mock.call(notify_client.return_value, 'NOTIFY_TEMPLATE_ID', self.brief3['users'][1], self.brief3, None, None), ] assert logger.error.call_args_list == [ mock.call( 'Email sending failed for the following buyer users of brief ID {brief_id}: {buyer_ids}', extra={ 'brief_id': 100, 'buyer_ids': '9' }), mock.call( 'Email sending failed for the following buyer users of brief ID {brief_id}: {buyer_ids}', extra={ 'brief_id': 300, 'buyer_ids': '999,9999' }), mock.call( 'All failures for award closed briefs notification on closing date {date_closed}: {all_failed_users}', extra={ 'date_closed': '2017-01-01', 'all_failed_users': '9,999,9999' }) ]
def test_main_doesnt_allow_date_closed_to_be_less_than_x_days_ago_by_default( self, logger, get_briefs_closed_on_date, send_email_to_brief_user_via_notify, notify_client, offset_days, date_closed): get_briefs_closed_on_date.return_value = [self.brief1, self.brief2] with freeze_time('2016-01-29 08:34:05'): assert not notify_buyers_to_award_closed_briefs.main( 'URL', 'API_KEY', 'NOTIFY_KEY', 'NOTIFY_TEMPLATE_ID', offset_days, date_closed=date_closed, dry_run=None, ) notify_client.assert_not_called() send_email_to_brief_user_via_notify.assert_not_called() logger.error.assert_called_with( 'Not allowed to notify about briefs that closed less than {} days ago', offset_days)
def test_main_with_no_briefs_logs_and_returns_true( self, logger, get_briefs_closed_on_date, send_email_to_brief_user_via_notify, notify_client): get_briefs_closed_on_date.return_value = [] assert notify_buyers_to_award_closed_briefs.main( 'URL', 'API_KEY', 'NOTIFY_KEY', 'NOTIFY_TEMPLATE_ID', self.OFFSET_DAYS, date_closed='2017-01-01', dry_run=None) assert logger.info.call_args_list == [ mock.call("Data API URL: {data_api_url}", extra={'data_api_url': 'URL'}), mock.call("No briefs closed on {date_closed}", extra={"date_closed": datetime.date(2017, 1, 1)}) ] notify_client.assert_called_with('NOTIFY_KEY', logger=mock.ANY) send_email_to_brief_user_via_notify.assert_not_called()
from docopt import docopt from dmscripts.helpers.auth_helpers import get_auth_token from dmscripts.notify_buyers_to_award_closed_briefs import main from dmutils.env_helpers import get_api_endpoint_from_stage if __name__ == '__main__': arguments = docopt(__doc__) list_of_buyer_ids = [] if arguments['--buyer-ids']: list_of_buyer_ids = list(map(int, arguments['--buyer-ids'].split(','))) if arguments['--offset-days']: offset_days = int(arguments['--offset-days']) else: offset_days = 28 ok = main(data_api_url=get_api_endpoint_from_stage(arguments['<stage>'], 'api'), data_api_access_token=get_auth_token('api', arguments['<stage>']), notify_api_key=arguments['<govuk_notify_api_key>'], notify_template_id=arguments['<govuk_notify_template_id>'], date_closed=arguments['--date-closed'], dry_run=arguments['--dry-run'], user_id_list=list_of_buyer_ids, offset_days=offset_days) if not ok: sys.exit(1)