Exemplo n.º 1
0
    def it_only_sends_to_3_emails_if_not_live_environment(
            self, mocker, db_session, sample_email, sample_member,
            sample_email_provider):
        mocker.patch.dict('app.application.config', {
            'ENVIRONMENT': 'test',
            'EMAIL_RESTRICT': None
        })

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

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

        assert mock_send_email.call_count == 3
        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 mock_send_email.call_args_list[2][0][0] == member_2.email
        assert sample_email.serialize()['emails_sent_counts'] == {
            'success': 3,
            'failed': 0,
            'total_active_members': 4
        }

        emails_to_members = EmailToMember.query.all()
        assert len(emails_to_members) == 3
        assert emails_to_members[
            0].email_provider_id == sample_email_provider.id
        assert emails_to_members[0].created_at == datetime.strptime(
            "2020-10-09 19:00", "%Y-%m-%d %H:%M")
Exemplo n.º 2
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
        }
Exemplo n.º 3
0
    def it_only_sends_to_unsent_members_and_shows_failed_stat(
            self, mocker, db, db_session, sample_email, sample_member,
            sample_email_provider):
        mocker.patch.dict('app.application.config', {
            'ENVIRONMENT': 'test',
            'EMAIL_RESTRICT': None
        })

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

        create_email_to_member(sample_email.id,
                               sample_member.id,
                               status_code=500)

        # respond with 201 on 2nd call
        mock_send_email = mocker.patch('app.na_celery.email_tasks.send_email',
                                       side_effect=[
                                           (200, sample_email_provider.id),
                                           (201, sample_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] == member_1.email
        assert mock_send_email.call_args_list[1][0][0] == member_2.email
        assert sample_email.serialize()['emails_sent_counts'] == {
            'success': 2,
            'failed': 1,
            'total_active_members': 3
        }
    def it_calls_send_email_to_task(self, mocker, db, db_session, sample_email,
                                    sample_member):
        mock_send_email = mocker.patch('app.na_celery.email_tasks.send_email',
                                       return_value=200)
        send_emails(sample_email.id)

        assert mock_send_email.call_args[0][0] == sample_member.email
        assert mock_send_email.call_args[0][1] == 'workshop: test title'
Exemplo n.º 5
0
    def it_reraises_if_not_429_status_code_response(self, mocker, db_session,
                                                    sample_email,
                                                    sample_member):
        mocker.patch('app.na_celery.email_tasks.send_email',
                     side_effect=InvalidRequest('Unknown', 400))

        with pytest.raises(InvalidRequest):
            send_emails(sample_email.id)
Exemplo n.º 6
0
    def it_only_sends_to_3_emails_if_not_live_environment(
            self, mocker, db, db_session, sample_email, sample_member):
        member_1 = create_member(name='Test 1', email='*****@*****.**')
        member_2 = create_member(name='Test 2', email='*****@*****.**')
        member_3 = create_member(name='Test 3', email='*****@*****.**')

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

        assert mock_send_email.call_count == 3
        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 mock_send_email.call_args_list[2][0][0] == member_2.email
Exemplo n.º 7
0
 def it_logs_429_status_code_response(self, mocker, db_session,
                                      sample_email, sample_member):
     mocker.patch('app.na_celery.email_tasks.send_email',
                  side_effect=InvalidRequest('Minute limit reached', 429))
     mock_logger_error = mocker.patch(
         'app.na_celery.email_tasks.current_app.logger.error')
     mock_send_periodic_task = mocker.patch(
         'app.na_celery.email_tasks.send_periodic_emails.apply_async')
     with pytest.raises(expected_exception=InvalidRequest):
         send_emails(sample_email.id)
     assert mock_logger_error.called
     args = mock_logger_error.call_args[0]
     assert args[0] == 'Email limit reached: %r'
     assert args[1] == 'Minute limit reached'
     assert mock_send_periodic_task.called
     assert mock_send_periodic_task.call_args == call(countdown=60)
Exemplo n.º 8
0
    def it_calls_send_email_to_task(self, mocker, db, db_session, sample_email,
                                    sample_member):
        mock_send_email = mocker.patch('app.na_celery.email_tasks.send_email',
                                       return_value=200)
        send_emails(sample_email.id)

        assert mock_send_email.call_args[0][0] == sample_member.email
        assert mock_send_email.call_args[0][1] == 'workshop: test title'
        page = BeautifulSoup(mock_send_email.call_args[0][2], 'html.parser')
        assert 'http://frontend-test/member/unsubscribe' in str(page)

        unsubcode = page.select_one('#unsublink')['href'].split('/')[-1]
        tokens = get_tokens(
            decrypt(unsubcode, current_app.config['EMAIL_UNSUB_SALT']))
        assert tokens[current_app.config['EMAIL_TOKENS']['member_id']] == str(
            sample_member.id)
Exemplo n.º 9
0
    def it_doesnt_send_unapproved_emails(self, mocker, db, db_session,
                                         sample_email, sample_member):
        mocker.patch.dict('app.application.config', {
            'ENVIRONMENT': 'test',
            'EMAIL_RESTRICT': 1
        })
        sample_email.email_state = DRAFT

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

        mock_send_email = mocker.patch('app.na_celery.email_tasks.send_email',
                                       return_value=200)
        mock_logger = mocker.patch(
            'app.na_celery.email_tasks.current_app.logger.info')
        send_emails(sample_email.id)

        assert not mock_send_email.called
        assert mock_logger.called
Exemplo n.º 10
0
    def it_only_sends_to_1_emails_if_restrict_email(self, mocker, db,
                                                    db_session, sample_email,
                                                    sample_member,
                                                    sample_email_provider):
        mocker.patch.dict('app.application.config', {
            'ENVIRONMENT': 'test',
            'EMAIL_RESTRICT': 1
        })

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

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

        assert mock_send_email.call_count == 1
        assert mock_send_email.call_args_list[0][0][0] == sample_member.email
Exemplo n.º 11
0
    def it_sends_a_magazine_email(self, mocker, db_session,
                                  sample_magazine_email, sample_member,
                                  sample_email_provider):
        mocker.patch.dict('app.application.config', {
            'ENVIRONMENT': 'test',
            'EMAIL_RESTRICT': 1
        })

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

        mock_send_email = mocker.patch(
            'app.na_celery.email_tasks.send_email',
            return_value=(200, str(sample_email_provider.id)))
        send_emails(sample_magazine_email.id)

        magazine = Magazine.query.filter_by(
            id=sample_magazine_email.magazine_id).first()
        assert magazine

        assert magazine.title in mock_send_email.call_args_list[0][0][1]
        assert magazine.filename in mock_send_email.call_args_list[0][0][2]
        assert mock_send_email.call_count == 1
        assert mock_send_email.call_args_list[0][0][0] == sample_member.email