Esempio n. 1
0
    def test_signal(self, mock_select_advice, unused_mock_select_tips,
                    mock_mail):
        """Test that the batch send fails gracefully on SIGTERM."""
        mock_select_advice.return_value = project_pb2.Advice(
            advice_id='advice-to-send',
            num_stars=2,
        )

        mock_mail.send_template.return_value.status_code = 200
        mock_mail.send_template_to_admins.return_value.status_code = 200
        mock_mail.count_sent_to.return_value = {
            'DeliveredCount': 0,
            'OpenedCount': 0
        }
        self._db.user.insert_many([
            dict(_USER_READY_FOR_EMAIL,
                 _id=mongomock.ObjectId('580f4a4271cd4a0007672a%dd' % i))
            for i in range(10)
        ])

        users = list(self._db.user.find({}))

        db_user = mock.MagicMock()
        db_user.find.return_value = _SigtermAfterNItems(users, 3)
        self._db.user = db_user

        mail_advice.main(self._db, 'http://localhost:3000', self._now)

        self.assertEqual(4, mock_mail.send_template.call_count)
        self.assertEqual(4, db_user.update_one.call_count)
        self.assertTrue(mock_mail.send_template_to_admins.called)
Esempio n. 2
0
    def test_no_dupes(self, mock_select_advice, unused_mock_select_tips,
                      mock_mail):
        """Test that we do not send duplicate emails if we run the script twice."""
        mock_select_advice.return_value = project_pb2.Advice(
            advice_id='advice-to-send',
            num_stars=2,
        )
        self._db.user.insert_one(_USER_READY_FOR_EMAIL)
        mock_mail.send_template.return_value.status_code = 200
        mock_mail.send_template_to_admins.return_value.status_code = 200
        mock_mail.count_sent_to.return_value = {
            'DeliveredCount': 0,
            'OpenedCount': 0
        }

        mail_advice.main(self._db, 'http://localhost:3000', self._now)

        self.assertTrue(mock_mail.send_template.called)
        self.assertTrue(mock_mail.send_template_to_admins.called)
        mock_mail.send_template.reset_mock()
        mock_mail.send_template_to_admins.reset_mock()

        # Running the script again 10 minutes later.
        mock_select_advice.return_value = project_pb2.Advice(
            advice_id='other-advice-to-send',
            num_stars=1,
        )
        mail_advice.main(self._db, 'http://localhost:3000',
                         self._now + datetime.timedelta(minutes=10))
        self.assertFalse(mock_mail.send_template.called)
        self.assertTrue(mock_mail.send_template_to_admins.called)
Esempio n. 3
0
    def test_need_score(self, mock_select_advice, mock_select_tips, mock_mail):
        """Do not send if user never scored any piece of advice."""
        mock_select_advice.return_value = project_pb2.Advice(
            advice_id='advice-to-send',
            num_stars=2,
        )
        mock_select_tips.return_value = [
            action_pb2.Action(title='First tip'),
            action_pb2.Action(title='Second tip'),
            action_pb2.Action(title='Third tip'),
        ]
        self._db.advice_modules.insert_many([
            {
                'adviceId': 'a',
                'title': 'Advice A'
            },
            {
                'adviceId': 'b',
                'title': 'Advice B'
            },
        ])
        self._db.user.insert_one({
            'featuresEnabled': {
                'advisorEmail': 'ACTIVE'
            },
            'profile': {
                'name': 'Pascal',
                'lastName': 'Corpet',
                'email': '*****@*****.**',
                'emailDays': ['MONDAY', 'THURSDAY'],
            },
            'projects': [{
                'advices': [{
                    'adviceId': 'a'
                }, {
                    'adviceId': 'b'
                }],
                'projectId': 'project-id-123',
                'title': 'Project Title',
            }],
        })
        mock_mail.send_template.return_value.status_code = 200
        mock_mail.send_template_to_admins.return_value.status_code = 200
        mock_mail.count_sent_to.return_value = {
            'DeliveredCount': 0,
            'OpenedCount': 0
        }

        mail_advice.main(self._db, 'http://localhost:3000', self._now)
        self.assertFalse(mock_mail.send_template.called)
Esempio n. 4
0
    def _assert_disable_sending(self, mock_mail, delivered, opened, disabled):
        mock_mail.send_template.return_value.status_code = 200
        mock_mail.send_template_to_admins.return_value.status_code = 200
        self._db.user.drop()
        self._db.user.insert_one(_USER_READY_FOR_EMAIL)

        mock_mail.count_sent_to.return_value = {
            'DeliveredCount': delivered,
            'OpenedCount': opened
        }
        mail_advice.main(self._db, 'http://localhost:3000', self._now)

        if disabled:
            self.assertFalse(mock_mail.send_template.called)
            user_in_db = self._db.user.find_one({})
            self.assertEqual([], user_in_db['profile'].get('emailDays', []))
            self.assertTrue(
                user_in_db['featuresEnabled'].get('autoStopEmails'))
        else:
            self.assertTrue(mock_mail.send_template.called)
        mock_mail.send_template.reset_mock()
Esempio n. 5
0
    def test_6_weeks_not_open(self, mock_select_advice,
                              unused_mock_select_tips, mock_mail):
        """Test that we disable emailing if users does not open their email for 5 days."""
        mock_select_advice.return_value = project_pb2.Advice(
            advice_id='advice-to-send',
            num_stars=2,
        )

        mock_mail.send_template.return_value.status_code = 200
        mock_mail.send_template_to_admins.return_value.status_code = 200
        self._db.user.insert_one(_USER_READY_FOR_EMAIL)

        # Running the script 5 weeks in a row.
        for week in range(5):
            mock_mail.count_sent_to.return_value = {
                'DeliveredCount': week,
                'OpenedCount': 0
            }
            mail_advice.main(
                self._db, 'http://localhost:3000',
                self._now + datetime.timedelta(hours=24 * 7 * week))

        self.assertEqual(5, mock_mail.send_template.call_count)
        mock_mail.send_template.reset_mock()

        # Running on week 6.
        mock_mail.count_sent_to.return_value = {
            'DeliveredCount': 5,
            'OpenedCount': 0
        }
        mail_advice.main(self._db, 'http://localhost:3000',
                         self._now + datetime.timedelta(hours=24 * 7 * 6))

        self.assertFalse(mock_mail.send_template.called)
        user_in_db = self._db.user.find_one({})
        self.assertEqual([], user_in_db['profile'].get('emailDays', []))
        self.assertTrue(user_in_db['featuresEnabled'].get('autoStopEmails'))
Esempio n. 6
0
    def test_main(self, mock_select_advice, mock_select_tips, mock_mail):
        """Overall test."""
        mock_select_advice.return_value = project_pb2.Advice(
            advice_id='advice-to-send',
            num_stars=2,
        )
        mock_select_tips.return_value = [
            action_pb2.Action(title='First tip'),
            action_pb2.Action(title='Second tip'),
            action_pb2.Action(title='Third tip'),
        ]
        self._db.advice_modules.insert_many([
            {
                'adviceId': 'a',
                'title': 'Advice A'
            },
            {
                'adviceId': 'b',
                'title': 'Advice B'
            },
        ])
        self._db.user.insert_one({
            'featuresEnabled': {
                'advisorEmail': 'ACTIVE'
            },
            'profile': {
                'name': 'Pascal',
                'lastName': 'Corpet',
                'email': '*****@*****.**',
                'emailDays': ['MONDAY', 'THURSDAY'],
            },
            'projects': [{
                'advices': [{
                    'adviceId': 'a',
                    'score': 1
                }, {
                    'adviceId': 'b'
                }],
                'projectId':
                'project-id-123',
                'title':
                'Project Title',
            }],
        })
        mock_mail.send_template.return_value.status_code = 200
        mock_mail.send_template_to_admins.return_value.status_code = 200
        mock_mail.count_sent_to.return_value = {
            'DeliveredCount': 0,
            'OpenedCount': 0
        }

        mail_advice.main(self._db, 'http://*****:*****@bayes.org', profile.email)
        self.assertEqual(
            {
                'advices': [
                    {
                        'adviceId': 'a',
                        'title': 'Advice A'
                    },
                    {
                        'adviceId': 'b',
                        'title': 'Advice B'
                    },
                ],
                'baseUrl':
                'http://localhost:3000',
                'fact':
                'Inbox 0 is awesome',
                'firstName':
                'Pascal',
                'frequency':
                'deux fois par semaine',
                'nextday':
                'lundi',
                'numStars':
                2,
                'projectId':
                'project-id-123',
                'subject':
                'Advice Email Subject',
                'suggestionSentence':
                'check your inbox',
                'tips_0_title':
                'First tip',
                'tips_1_title':
                'Second tip',
                'tips_2_title':
                'Third tip',
                'title':
                'Advice Email Title',
            }, template_vars)
        self.assertTrue(mock_mail.send_template_to_admins.called)