def test_send_new_briefs_email_fails_with_empty_list_id(app, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = ''

    with app.app_context():
        try:
            send_new_briefs_email()
            assert False
        except MailChimpConfigException as e:
            assert str(e) == 'Failed to get MAILCHIMP_SELLER_EMAIL_LIST_ID from the environment variables.'
def test_send_new_briefs_email_fails_no_new_briefs_in_past_24hrs(app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    with app.app_context():
        send_new_briefs_email()

        assert not client.campaigns.create.called
        assert not client.campaigns.content.update.called
        assert not client.campaigns.actions.schedule.called
def test_send_new_briefs_email_fails_no_new_briefs_in_past_24hrs(
        app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    with app.app_context():
        send_new_briefs_email()

        assert not client.campaigns.create.called
        assert not client.campaigns.content.update.called
        assert not client.campaigns.actions.schedule.called
def test_send_new_briefs_email_fails_with_empty_list_id(app, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = ''

    with app.app_context():
        try:
            send_new_briefs_email()
            assert False
        except MailChimpConfigException as e:
            assert str(
                e
            ) == 'Failed to get MAILCHIMP_SELLER_EMAIL_LIST_ID from the environment variables.'
def test_send_new_briefs_email_fails_mailchimp_api_call_with_requests_error(app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    requestEx = mocker.patch('app.tasks.mailchimp.RequestException')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    client.campaigns.create.side_effect = RequestException

    with app.app_context():
        try:
            send_new_briefs_email()
            assert False
        except RequestException as e:
            assert True
def test_send_new_briefs_email_fails_mailchimp_api_call_with_requests_error(
        app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    requestEx = mocker.patch('app.tasks.mailchimp.RequestException')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    client.campaigns.create.side_effect = RequestException

    with app.app_context():
        try:
            send_new_briefs_email()
            assert False
        except RequestException as e:
            assert True
def test_send_new_briefs_email_success(app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    with app.app_context():
        send_new_briefs_email()

        assert client.campaigns.create.called
        assert client.campaigns.content.update.called
        assert client.campaigns.actions.schedule.called

        audit_event = AuditEvent.query.filter(
            AuditEvent.type ==
            AuditTypes.send_seller_opportunities_campaign.value).first()

        assert audit_event
        assert audit_event.data['briefs_sent'] == len(briefs)
def test_send_new_briefs_email_success(app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    with app.app_context():
        send_new_briefs_email()

        assert client.campaigns.create.called
        assert client.campaigns.content.update.called
        assert client.campaigns.actions.schedule.called

        audit_event = AuditEvent.query.filter(
            AuditEvent.type == AuditTypes.send_seller_opportunities_campaign.value
        ).first()

        assert audit_event
        assert audit_event.data['briefs_sent'] == len(briefs)
def test_send_new_briefs_email_fails_no_new_briefs_since_last_run(
        app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    with app.app_context():

        audit = AuditEvent(
            audit_type=AuditTypes.send_seller_opportunities_campaign,
            user=None,
            data={},
            db_object=None)
        db.session.add(audit)
        db.session.commit()

        send_new_briefs_email()

        assert not client.campaigns.create.called
        assert not client.campaigns.content.update.called
        assert not client.campaigns.actions.schedule.called
def test_send_new_briefs_email_fails_no_new_briefs_since_last_run(app, briefs, mocker):
    mailchimp = mocker.patch('app.tasks.mailchimp.MailChimp')
    client = MagicMock()

    mailchimp.return_value = client
    environ['MAILCHIMP_SELLER_EMAIL_LIST_ID'] = '123456'

    with app.app_context():

        audit = AuditEvent(
            audit_type=AuditTypes.send_seller_opportunities_campaign,
            user=None,
            data={},
            db_object=None
        )
        db.session.add(audit)
        db.session.commit()

        send_new_briefs_email()

        assert not client.campaigns.create.called
        assert not client.campaigns.content.update.called
        assert not client.campaigns.actions.schedule.called