Beispiel #1
0
    def it_sends_the_email_using_next_available_provider_hourly_limit(
            self, mocker, db_session):
        mocker.patch(
            'app.comms.email.dao_get_past_hour_email_count_for_provider',
            mock_get_past_hour_email_count_for_provider_over_first_limit)

        create_email_provider(name='First email provider',
                              daily_limit=0,
                              hourly_limit=30,
                              pos=0)

        create_email_provider(name='Another email provider',
                              daily_limit=0,
                              hourly_limit=30,
                              pos=3)

        next_available_email_provider = create_email_provider(
            name='Next available email provider',
            hourly_limit=30,
            pos=5,
            available=True)

        with requests_mock.mock() as r:
            r.post(next_available_email_provider.api_url, text='OK')
            resp = send_email('*****@*****.**', 'test subject',
                              'test message')

            assert resp == (200, next_available_email_provider.id)
Beispiel #2
0
 def it_triggers_429_when_next_email_provider_hits_limit(
         self, mocker, db_session, sample_email_provider):
     mocker.patch('app.comms.email.dao_get_todays_email_count_for_provider',
                  return_value=30)
     create_email_provider(name='Next email provider', daily_limit=30)
     with pytest.raises(expected_exception=InvalidRequest):
         send_email('*****@*****.**', 'test subject', 'test message')
Beispiel #3
0
    def it_triggers_429_when_minute_limit_reached(self, mocker, db_session):
        create_email_provider(minute_limit=5)
        mocker.patch(
            'app.comms.email.dao_get_last_minute_email_count_for_provider',
            return_value=6)

        with pytest.raises(expected_exception=InvalidRequest):
            send_email('*****@*****.**', 'test subject', 'test message')
Beispiel #4
0
    def it_sends_an_email_to_members_up_to_email_limit(self, mocker,
                                                       db_session,
                                                       sample_email,
                                                       sample_member, monthly,
                                                       daily, hourly, minute):
        mocker.patch.dict('app.application.config', {
            'ENVIRONMENT': 'live',
            'EMAIL_RESTRICT': None
        })
        mocker.patch('requests.post')
        email_provider = create_email_provider(monthly_limit=monthly,
                                               daily_limit=daily,
                                               hourly_limit=hourly,
                                               minute_limit=minute)

        member_1 = create_member(name='Test 1', email='*****@*****.**')
        create_member(name='Test 2', email='*****@*****.**')

        mock_send_email = mocker.patch('app.na_celery.email_tasks.send_email',
                                       return_value=(200, email_provider.id))
        send_emails(sample_email.id)

        assert mock_send_email.call_count == 2
        assert mock_send_email.call_args_list[0][0][0] == sample_member.email
        assert mock_send_email.call_args_list[1][0][0] == member_1.email
        assert sample_email.serialize()['emails_sent_counts'] == {
            'success': 2,
            'failed': 0,
            'total_active_members': 3
        }
Beispiel #5
0
    def it_gets_past_hour_emails_for_provider(self, db, db_session,
                                              sample_member, sample_email,
                                              sample_email_provider):
        email = create_email(send_starts_at="2020-10-30",
                             send_after="2020-10-30T20:30:00",
                             expires="2020-11-07",
                             email_state=APPROVED)
        member = create_member(email="*****@*****.**")
        email_to_member = create_email_to_member(
            email_id=email.id, created_at="2020-10-31T12:00:00")
        create_email_to_member(
            member_id=sample_member.id,
            email_id=email.id,
            created_at="2020-10-31T10:00:00",
            email_provider_id=email_to_member.email_provider_id)
        create_email_to_member(
            member_id=member.id,
            email_id=email.id,
            created_at="2020-10-31T11:31:00",
            email_provider_id=email_to_member.email_provider_id)

        # use another provider to show that it doesn't get that count
        email_provider = create_email_provider(name="Another email provider")
        create_email_to_member(
            member_id=sample_member.id,
            email_id=sample_email.id,
            email_provider_id=email_provider.id,
            created_at="2020-10-31T11:31:00",
        )

        count = dao_get_past_hour_email_count_for_provider(
            email_to_member.email_provider_id)
        assert count == 2
Beispiel #6
0
    def it_gets_email_providers_in_order(self, client, db, db_session,
                                         sample_email_provider):
        next_email_provider = create_email_provider(
            name='Next email provider',
            pos=2,
            smtp_server='http://smtp.test',
            smtp_user='******',
            smtp_password='******')

        response = client.get(url_for('email_provider.get_email_providers'),
                              headers=[('Content-Type', 'application/json'),
                                       create_authorization_header()])
        assert response.status_code == 200
        assert len(response.json) == 2
        assert response.json[0]['id'] == str(sample_email_provider.id)
        assert 'api_key' not in response.json[0]
        assert response.json[0][
            'shortened_api_key'] == sample_email_provider.api_key[-10:]
        assert response.json[1]['id'] == str(next_email_provider.id)
        assert 'api_key' not in response.json[1]
        assert response.json[1][
            'shortened_api_key'] == next_email_provider.api_key[-10:]
        assert response.json[1][
            'shortened_smtp_password'] == next_email_provider.smtp_password[
                -5:]
Beispiel #7
0
    def it_gets_emails_count_only_for_chosen_provider(self, db, db_session):
        email = create_email()
        member = create_member(email='*****@*****.**', name='Test1')
        create_email_to_member(email_id=email.id, member_id=member.id)

        email_provider = create_email_provider(name='another', pos=2)
        email_to_member = create_email_to_member(
            email_provider_id=email_provider.id)

        assert dao_get_todays_email_count_for_provider(
            email_to_member.email_provider_id) == 1
Beispiel #8
0
    def it_sends_the_email_using_next_provider_with_override(
            self, mocker, db_session, sample_email_provider):
        mocker.patch(
            'app.comms.email.dao_get_todays_email_count_for_provider',
            mock_get_todays_email_count_for_provider_over_first_limit)
        next_email_provider = create_email_provider(name='Next email provider',
                                                    daily_limit=30)
        with requests_mock.mock() as r:
            r.post(next_email_provider.api_url, text='OK')
            resp = send_email('*****@*****.**',
                              'test subject',
                              'test message',
                              override=True)

            assert resp == (200, next_email_provider.id)
Beispiel #9
0
def sample_email_provider(db):
    return create_email_provider(
        name='Sample Email Provider', daily_limit=25, monthly_limit=100,
        api_key='sample-api-key', api_url='http://sample-api-url.com', pos=0,
    )