def test_sends_case_study_email_to_expected_users(mock_task): eight_days_ago = timezone.now() - timedelta(days=8) twelve_days_ago = timezone.now() - timedelta(days=12) suppliers = SupplierFactory.create_batch(10, date_joined=eight_days_ago) SupplierFactory.create_batch(3, date_joined=twelve_days_ago) for supplier in suppliers[:4]: CompanyCaseStudyFactory(company=supplier.company) SupplierEmailNotificationFactory(supplier=suppliers[9], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[8], category='hasnt_logged_in') notifications.no_case_studies() assert len(mock_task.delay.call_args_list) == 5 supplier1 = mock_task.delay.call_args_list[0][1]['recipient_email'] supplier2 = mock_task.delay.call_args_list[1][1]['recipient_email'] supplier3 = mock_task.delay.call_args_list[2][1]['recipient_email'] supplier4 = mock_task.delay.call_args_list[3][1]['recipient_email'] supplier5 = mock_task.delay.call_args_list[4][1]['recipient_email'] assert supplier1 == suppliers[4].company_email assert supplier2 == suppliers[5].company_email assert supplier3 == suppliers[6].company_email assert supplier4 == suppliers[7].company_email assert supplier5 == suppliers[8].company_email objs = SupplierEmailNotification.objects.all() assert objs.count() == 7
def test_doesnt_send_log_in_email_if_log_in_email_already_sent(): suppliers = SupplierFactory.create_batch(2) SupplierEmailNotificationFactory(supplier=suppliers[0], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[1], category='hasnt_logged_in') mocked_json = [ { 'id': suppliers[0].sso_id, 'last_login': '******' }, { 'id': suppliers[1].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) mail.outbox = [] # reset after emails sent by signals with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() assert len(mail.outbox) == 1 assert mail.outbox[0].to == [suppliers[0].company_email] # 2 in data setup + 1 new assert SupplierEmailNotification.objects.all().count() == 3
def test_sends_log_in_email_to_expected_users(mock_task, settings): suppliers = SupplierFactory.create_batch(4) mocked_json = [ { 'id': suppliers[0].sso_id, 'last_login': '******' }, { 'id': suppliers[1].sso_id, 'last_login': '******' }, { 'id': suppliers[2].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) SupplierEmailNotificationFactory(supplier=suppliers[1], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[0], category='hasnt_logged_in') with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() assert mock_task.delay.call_count == 2 call_args = mock_task.delay.call_args_list assert len(call_args) == 2 assert call_args[0][1]['recipient_email'] == suppliers[1].company_email assert call_args[1][1]['recipient_email'] == suppliers[2].company_email expected_url = settings.FAB_NOTIFICATIONS_UNSUBSCRIBE_URL assert expected_url in call_args[0][1]['text_body'] objs = SupplierEmailNotification.objects.all() assert objs.count() == 4
def test_sends_log_in_email_to_expected_users(settings): suppliers = SupplierFactory.create_batch(4) mocked_json = [ { 'id': suppliers[0].sso_id, 'last_login': '******' }, { 'id': suppliers[1].sso_id, 'last_login': '******' }, { 'id': suppliers[2].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) SupplierEmailNotificationFactory(supplier=suppliers[1], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[0], category='hasnt_logged_in') mail.outbox = [] # reset after emails sent by signals with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() assert len(mail.outbox) == 2 assert mail.outbox[0].to == [suppliers[1].company_email] assert mail.outbox[1].to == [suppliers[2].company_email] objs = SupplierEmailNotification.objects.all() assert objs.count() == 4 # 2 + 2 created in setup assert settings.FAB_NOTIFICATIONS_UNSUBSCRIBE_URL in mail.outbox[0].body
def test_doesnt_send_log_in_email_if_log_in_email_already_sent(mock_task): suppliers = SupplierFactory.create_batch(2) SupplierEmailNotificationFactory(supplier=suppliers[0], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[1], category='hasnt_logged_in') mocked_json = [ { 'id': suppliers[0].sso_id, 'last_login': '******' }, { 'id': suppliers[1].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() assert mock_task.delay.call_count == 1 call_args = mock_task.delay.call_args_list assert len(call_args) == 1 assert call_args[0][1]['recipient_email'] == suppliers[0].company_email assert SupplierEmailNotification.objects.all().count() == 3
def test_upload_suppliers_csv_to_s3(mocked_upload_file_object_to_s3): SupplierFactory.create_batch(5) SupplierFactory( sso_id=123, name='foobar', company_email='*****@*****.**', company=None, is_company_owner=True, ) call_command('generate_suppliers_csv_dump') assert mocked_upload_file_object_to_s3.called assert mocked_upload_file_object_to_s3.call_args == mock.call( file_object=mock.ANY, key=settings.SUPPLIERS_CSV_FILE_NAME, bucket=settings.CSV_DUMP_BUCKET_NAME, )
def test_if_log_in_email_send_fails_previous_info_still_written_to_db(): suppliers = SupplierFactory.create_batch(3) mocked_json = [ { 'id': suppliers[0].sso_id, 'last_login': '******' }, { 'id': suppliers[1].sso_id, 'last_login': '******' }, { 'id': suppliers[2].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) send_method = 'django.core.mail.EmailMultiAlternatives.send' def mocked_send(self): # This will be the last email that will be sent to if self.to == [suppliers[2].company_email]: raise Exception with patch(send_method, mocked_send): try: with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() except: pass # should have created the two objects before the email exception assert SupplierEmailNotification.objects.all().count() == 2
def test_sends_log_in_email_when_not_logged_in_for_30_days(): expected_subject = email.HasNotLoggedInRecentlyNotification.subject suppliers = SupplierFactory.create_batch(3) mocked_json = [ { 'id': suppliers[1].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) mail.outbox = [] # reset after emails sent by signals with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() mocked_api.assert_called_once_with(start=datetime(2017, 1, 1, 0, 0, 0, 0), end=datetime(2017, 1, 1, 23, 59, 59, 999999)) assert len(mail.outbox) == 1 assert mail.outbox[0].to == [suppliers[1].company_email] assert mail.outbox[0].subject == expected_subject assert suppliers[1].name in mail.outbox[0].body assert suppliers[1].name in mail.outbox[0].alternatives[0][0] assert SupplierEmailNotification.objects.all().count() == 1 instance = SupplierEmailNotification.objects.get() assert instance.supplier == suppliers[1] assert instance.category == 'hasnt_logged_in' assert instance.date_sent == timezone.now()
def test_if_ver_code_email_send_fails_previous_info_still_written_to_db_16(): sixteen_days_ago = timezone.now() - timedelta(days=16) suppliers = SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=sixteen_days_ago) for supplier in suppliers: SupplierEmailNotificationFactory( supplier=supplier, category='verification_code_not_given', date_sent=(timezone.now() - timedelta(days=8))) send_method = 'django.core.mail.EmailMultiAlternatives.send' def mocked_send(self): # This will be the last email that will be sent to if self.to == [suppliers[0].company_email]: raise Exception with patch(send_method, mocked_send): try: notifications.verification_code_not_given() except: pass # 2 created (before email exception on 3rd) + 3 in set up assert SupplierEmailNotification.objects.all().count() == 5
def test_sends_ver_code_email_to_expected_users(mock_task): eight_days_ago = timezone.now() - timedelta(days=8) twelve_days_ago = timezone.now() - timedelta(days=12) sixteen_days_ago = timezone.now() - timedelta(days=16) SupplierFactory.create_batch( 3, company__verified_with_code=True, company__date_verification_letter_sent=eight_days_ago) suppliers8 = SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=eight_days_ago) SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=twelve_days_ago) suppliers16 = SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=sixteen_days_ago) SupplierFactory.create_batch( 3, company__verified_with_code=True, company__date_verification_letter_sent=sixteen_days_ago) SupplierEmailNotificationFactory(supplier=suppliers8[2], category='verification_code_not_given') SupplierEmailNotificationFactory(supplier=suppliers16[2], category='verification_code_2nd_email') for supplier in suppliers16: SupplierEmailNotificationFactory( supplier=supplier, category='verification_code_not_given', date_sent=eight_days_ago) SupplierEmailNotificationFactory(supplier=suppliers8[1], category='hasnt_logged_in') SupplierEmailNotificationFactory(supplier=suppliers16[1], category='hasnt_logged_in') notifications.verification_code_not_given() assert mock_task.delay.call_count == 4 call_args = mock_task.delay.call_args_list assert len(call_args) == 4 assert call_args[0][1]['recipient_email'] == suppliers8[1].company_email assert call_args[1][1]['recipient_email'] == suppliers8[0].company_email assert call_args[2][1]['recipient_email'] == suppliers16[1].company_email assert call_args[3][1]['recipient_email'] == suppliers16[0].company_email objs = SupplierEmailNotification.objects.all() assert objs.count() == 11
def test_sends_ver_code_email_to_expected_users(): eight_days_ago = timezone.now() - timedelta(days=8) twelve_days_ago = timezone.now() - timedelta(days=12) sixteen_days_ago = timezone.now() - timedelta(days=16) SupplierFactory.create_batch( 3, company__verified_with_code=True, company__date_verification_letter_sent=eight_days_ago) suppliers8 = SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=eight_days_ago) SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=twelve_days_ago) suppliers16 = SupplierFactory.create_batch( 3, company__verified_with_code=False, company__date_verification_letter_sent=sixteen_days_ago) SupplierFactory.create_batch( 3, company__verified_with_code=True, company__date_verification_letter_sent=sixteen_days_ago) SupplierEmailNotificationFactory(supplier=suppliers8[2], category='verification_code_not_given') SupplierEmailNotificationFactory(supplier=suppliers16[2], category='verification_code_2nd_email') for supplier in suppliers16: SupplierEmailNotificationFactory( supplier=supplier, category='verification_code_not_given', date_sent=eight_days_ago) SupplierEmailNotificationFactory(supplier=suppliers8[1], category='hasnt_logged_in') SupplierEmailNotificationFactory(supplier=suppliers16[1], category='hasnt_logged_in') mail.outbox = [] # reset after emails sent by signals notifications.verification_code_not_given() assert len(mail.outbox) == 4 assert mail.outbox[0].to == [suppliers8[1].company_email] assert mail.outbox[1].to == [suppliers8[0].company_email] assert mail.outbox[2].to == [suppliers16[1].company_email] assert mail.outbox[3].to == [suppliers16[0].company_email] objs = SupplierEmailNotification.objects.all() assert objs.count() == 11 # 4 + 7 created in setup
def test_sends_case_study_email_to_expected_users(): eight_days_ago = timezone.now() - timedelta(days=8) twelve_days_ago = timezone.now() - timedelta(days=12) suppliers = SupplierFactory.create_batch(10, date_joined=eight_days_ago) SupplierFactory.create_batch(3, date_joined=twelve_days_ago) for supplier in suppliers[:4]: CompanyCaseStudyFactory(company=supplier.company) SupplierEmailNotificationFactory(supplier=suppliers[9], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[8], category='hasnt_logged_in') mail.outbox = [] # reset after emails sent by signals notifications.no_case_studies() assert len(mail.outbox) == 5 assert mail.outbox[0].to == [suppliers[4].company_email] assert mail.outbox[1].to == [suppliers[5].company_email] assert mail.outbox[2].to == [suppliers[6].company_email] assert mail.outbox[3].to == [suppliers[7].company_email] assert mail.outbox[4].to == [suppliers[8].company_email] objs = SupplierEmailNotification.objects.all() assert objs.count() == 7 # 5 + 2 created in setup
def test_doesnt_send_case_study_email_if_case_study_email_already_sent( settings): eight_days_ago = timezone.now() - timedelta(days=8) suppliers = SupplierFactory.create_batch(2, date_joined=eight_days_ago) SupplierEmailNotificationFactory(supplier=suppliers[0], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[1], category='hasnt_logged_in') mail.outbox = [] # reset after emails sent by signals notifications.no_case_studies() assert len(mail.outbox) == 1 assert mail.outbox[0].from_email == settings.FAB_FROM_EMAIL assert mail.outbox[0].to == [suppliers[1].company_email] # what we created in data setup + 1 new assert SupplierEmailNotification.objects.all().count() == 3
def test_doesnt_send_case_study_email_if_case_study_email_already_sent( mock_task, settings): eight_days_ago = timezone.now() - timedelta(days=8) suppliers = SupplierFactory.create_batch(2, date_joined=eight_days_ago) SupplierEmailNotificationFactory(supplier=suppliers[0], category='no_case_studies') SupplierEmailNotificationFactory(supplier=suppliers[1], category='hasnt_logged_in') notifications.no_case_studies() assert len(mock_task.delay.call_args_list) == 1 call_args = mock_task.delay.call_args[1] assert call_args['from_email'] == settings.FAB_FROM_EMAIL assert call_args['recipient_email'] == suppliers[1].company_email assert SupplierEmailNotification.objects.all().count() == 3
def test_if_case_study_email_send_fails_previous_info_still_written_to_db(): eight_days_ago = timezone.now() - timedelta(days=8) suppliers = SupplierFactory.create_batch(3, date_joined=eight_days_ago) send_method = 'django.core.mail.EmailMultiAlternatives.send' def mocked_send(self): # This will be the last email that will be sent to if self.to == [suppliers[0].company_email]: raise Exception with patch(send_method, mocked_send): try: notifications.no_case_studies() except: pass # should have created the two objects before the email exception assert SupplierEmailNotification.objects.all().count() == 2
def test_sends_log_in_email_when_not_logged_in_for_30_days(mock_task): expected_subject = email.HasNotLoggedInRecentlyNotification.subject suppliers = SupplierFactory.create_batch(3) mocked_json = [ { 'id': suppliers[1].sso_id, 'last_login': '******' }, ] mocked_api = MagicMock(return_value=MagicMock(json=MagicMock( return_value=mocked_json))) with patch(LAST_LOGIN_API_METHOD, mocked_api): notifications.hasnt_logged_in() mocked_api.assert_called_once_with(start=datetime(2017, 1, 1, 0, 0, 0, 0), end=datetime(2017, 1, 1, 23, 59, 59, 999999)) assert len(mock_task.delay.call_args_list) == 1 call_args = mock_task.delay.call_args[1] assert call_args['recipient_email'] == suppliers[1].company_email assert call_args['subject'] == expected_subject assert suppliers[1].name in call_args['text_body'] assert suppliers[1].name in call_args['html_body']
def new_suppliers(): return SupplierFactory.create_batch(10)