Beispiel #1
0
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
Beispiel #2
0
def test_doesnt_send_case_study_email_when_user_has_case_studies():
    eight_days_ago = timezone.now() - timedelta(days=8)
    company = SupplierFactory(date_joined=eight_days_ago).company
    CompanyCaseStudyFactory(company=company)
    mail.outbox = []  # reset after emails sent by signals

    notifications.no_case_studies()

    assert len(mail.outbox) == 0
    assert SupplierEmailNotification.objects.all().count() == 0
Beispiel #3
0
def test_sends_case_study_email_when_8_days_ago_but_not_to_the_minute(
        mock_task, settings):
    supplier1 = SupplierFactory(date_joined=datetime(2016, 12, 8, 0, 0, 1))
    supplier2 = SupplierFactory(date_joined=datetime(2016, 12, 8, 23, 59, 59))
    SupplierFactory(date_joined=datetime(2016, 12, 7, 23, 59, 59))
    SupplierFactory(date_joined=datetime(2016, 12, 9, 0, 0, 1))
    notifications.no_case_studies()

    assert len(mock_task.delay.call_args_list) == 2
    first_email_call_args = mock_task.delay.call_args_list[0][1]
    second_email_call_args = mock_task.delay.call_args_list[1][1]
    assert first_email_call_args['from_email'] == settings.FAB_FROM_EMAIL
    assert first_email_call_args['recipient_email'] == supplier1.company_email
    assert second_email_call_args['from_email'] == settings.FAB_FROM_EMAIL
    assert second_email_call_args['recipient_email'] == supplier2.company_email
Beispiel #4
0
def test_case_study_email_uses_settings_for_no_of_days_and_subject(
        mock_task, settings):
    settings.NO_CASE_STUDIES_DAYS = 1
    expected_subject = email.NoCaseStudiesNotification.subject
    one_day_ago = timezone.now() - timedelta(days=1)
    eight_days_ago = timezone.now() - timedelta(days=8)
    SupplierFactory(date_joined=eight_days_ago)
    supplier = SupplierFactory(date_joined=one_day_ago)

    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'] == supplier.company_email
    assert call_args['subject'] == expected_subject
def test_sends_case_study_email_when_8_days_ago_but_not_to_the_minute(
        settings):
    supplier1 = SupplierFactory(date_joined=datetime(2016, 12, 8, 0, 0, 1))
    supplier2 = SupplierFactory(date_joined=datetime(2016, 12, 8, 23, 59, 59))
    SupplierFactory(date_joined=datetime(2016, 12, 7, 23, 59, 59))
    SupplierFactory(date_joined=datetime(2016, 12, 9, 0, 0, 1))
    mail.outbox = []  # reset after emails sent by signals

    notifications.no_case_studies()

    assert len(mail.outbox) == 2
    assert mail.outbox[0].from_email == settings.FAB_FROM_EMAIL
    assert mail.outbox[0].to == [supplier1.company_email]
    assert mail.outbox[1].from_email == settings.FAB_FROM_EMAIL
    assert mail.outbox[1].to == [supplier2.company_email]
    assert SupplierEmailNotification.objects.all().count() == 2
def test_case_study_email_uses_settings_for_no_of_days_and_subject(settings):
    settings.NO_CASE_STUDIES_DAYS = 1
    expected_subject = email.NoCaseStudiesNotification.subject
    one_day_ago = timezone.now() - timedelta(days=1)
    eight_days_ago = timezone.now() - timedelta(days=8)
    SupplierFactory(date_joined=eight_days_ago)
    supplier = SupplierFactory(date_joined=one_day_ago)
    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 == [supplier.company_email]
    assert mail.outbox[0].subject == expected_subject
    assert SupplierEmailNotification.objects.all().count() == 1
Beispiel #7
0
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_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
Beispiel #9
0
def test_sends_case_study_email_only_when_registered_8_days_ago(
        mock_task, settings):
    expected_subject = email.NoCaseStudiesNotification.subject
    seven_days_ago = timezone.now() - timedelta(days=7)
    eight_days_ago = timezone.now() - timedelta(days=8)
    nine_days_ago = timezone.now() - timedelta(days=9)
    SupplierFactory(date_joined=seven_days_ago)
    supplier = SupplierFactory(date_joined=eight_days_ago)
    SupplierFactory(date_joined=nine_days_ago)
    notifications.no_case_studies()

    mock_task.delay.assert_called_once_with(
        from_email=settings.FAB_FROM_EMAIL,
        html_body=ANY,
        text_body=ANY,
        recipient_email=supplier.company_email,
        subject=expected_subject,
    )
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
Beispiel #11
0
def test_case_study_email_has_expected_vars_in_template(mock_task, settings):
    settings.NO_CASE_STUDIES_URL = 'http://great.gov.uk/case-studies/add'
    settings.NO_CASE_STUDIES_UTM = 'utm=1'

    eight_days_ago = timezone.now() - timedelta(days=8)
    supplier = SupplierFactory(date_joined=eight_days_ago)
    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 supplier.name in call_args['text_body']
    assert supplier.name in call_args['html_body']
    assert ('http://great.gov.uk/case-studies/add?utm=1'
            in call_args['text_body'])
    assert ('http://great.gov.uk/case-studies/add?utm=1'
            in call_args['html_body'])
    assert 'http://help.zendesk.com' in call_args['text_body']
    assert 'http://help.zendesk.com' in call_args['html_body']
def test_case_study_email_has_expected_vars_in_template(settings):
    settings.NO_CASE_STUDIES_URL = 'http://great.gov.uk/case-studies/add'
    settings.NO_CASE_STUDIES_UTM = 'utm=1'

    eight_days_ago = timezone.now() - timedelta(days=8)
    supplier = SupplierFactory(date_joined=eight_days_ago)
    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 supplier.name in mail.outbox[0].body
    assert supplier.name in mail.outbox[0].alternatives[0][0]
    assert 'http://great.gov.uk/case-studies/add?utm=1' in mail.outbox[0].body
    assert ('http://great.gov.uk/case-studies/add?utm=1'
            in mail.outbox[0].alternatives[0][0])
    assert 'http://help.zendesk.com' in mail.outbox[0].body
    assert 'http://help.zendesk.com' in mail.outbox[0].alternatives[0][0]
def test_sends_case_study_email_only_when_registered_8_days_ago(settings):
    expected_subject = email.NoCaseStudiesNotification.subject
    seven_days_ago = timezone.now() - timedelta(days=7)
    eight_days_ago = timezone.now() - timedelta(days=8)
    nine_days_ago = timezone.now() - timedelta(days=9)
    SupplierFactory(date_joined=seven_days_ago)
    supplier = SupplierFactory(date_joined=eight_days_ago)
    SupplierFactory(date_joined=nine_days_ago)
    mail.outbox = []  # reset after emails sent by signals

    notifications.no_case_studies()

    assert len(mail.outbox) == 1
    assert mail.outbox[0].to == [supplier.company_email]
    assert mail.outbox[0].subject == expected_subject
    assert mail.outbox[0].from_email == settings.FAB_FROM_EMAIL
    assert SupplierEmailNotification.objects.all().count() == 1
    instance = SupplierEmailNotification.objects.get()
    assert instance.supplier == supplier
    assert instance.category == 'no_case_studies'
    assert instance.date_sent == timezone.now()
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
Beispiel #15
0
def no_case_studies():
    if lock_acquired('no_case_studies'):
        notifications.no_case_studies()
Beispiel #16
0
 def run_daily(self):
     notifications.no_case_studies()
     notifications.hasnt_logged_in()
     notifications.verification_code_not_given()