Esempio n. 1
0
    def test_refresh_old_status(self, mock_now: mock.MagicMock) -> None:
        """Test refreshing old status."""

        # On Nov. the 5th, the email had been opened.
        message_id = self._send_email('*****@*****.**')
        mailjetmock.get_message(message_id).open()
        mock_now.get.return_value = datetime.datetime(2017, 11, 5, 15, 13)
        self.database.user.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-11-01T09:25:46.145001Z',
                'mailjetMessageId': message_id,
            }],
        })
        update_email_sent_status.main(['--disable-sentry'])

        # A week later the email link had been clicked.
        mock_now.get.return_value = datetime.datetime(2017, 11, 13, 15, 13)
        mailjetmock.get_message(message_id).click()
        update_email_sent_status.main(['--disable-sentry'])

        updated_data = self.database.user.find_one()
        self.assertEqual('EMAIL_SENT_CLICKED',
                         updated_data.get('emailsSent')[0].get('status'))
Esempio n. 2
0
    def test_update_helper(self) -> None:
        """Test updating the sent emails for the helper collection."""

        message_id = self._send_email('*****@*****.**')
        mailjetmock.get_message(message_id).open()

        self.database.helper.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-09-08T09:25:46.145001Z',
                'mailjetMessageId': message_id,
            }],
        })
        update_email_sent_status.main(
            ['--mongo-collection', 'helper', '--disable-sentry'])
        updated_data = self.database.helper.find_one()
        self.assertEqual('field', updated_data.get('other'))
        self.assertEqual(
            message_id,
            int(updated_data.get('emailsSent')[0].get('mailjetMessageId')))
        self.assertEqual('EMAIL_SENT_OPENED',
                         updated_data.get('emailsSent')[0].get('status'))
Esempio n. 3
0
    def test_with_message_id(self) -> None:
        """Test retrieving info when message ID is present."""

        message_id = self._send_email('*****@*****.**')

        self.database.user.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-09-08T09:25:46.145001Z',
                'mailjetMessageId': message_id,
            }],
        })

        # Mark the message as opened.
        mailjetmock.get_message(message_id).open()

        update_email_sent_status.main(['--disable-sentry'])

        updated_data = self.database.user.find_one()
        self.assertEqual('field', updated_data.get('other'))
        self.assertEqual(
            message_id,
            int(updated_data.get('emailsSent')[0].get('mailjetMessageId')))
        self.assertEqual('EMAIL_SENT_OPENED',
                         updated_data.get('emailsSent')[0].get('status'))
    def test_with_message_id(self, mock_mail):
        """Test retrieving info when message ID is present."""

        mock_mail.get_message.return_value = {
            'ArrivedAt': '2017-09-08T09:25:48Z',
            'ID': 6789,
            'Comment': 'Right message, arrived 2 seconds after being sent',
            'Status': 'opened',
        }
        database = mongomock.MongoClient().test
        database.user.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-09-08T09:25:46.145001Z',
                'mailjetMessageId': 6789,
            }],
        })
        update_email_sent_status.main(database)
        updated_data = database.user.find_one()
        self.assertEqual('field', updated_data.get('other'))
        self.assertEqual(
            6789,
            int(updated_data.get('emailsSent')[0].get('mailjetMessageId')))
        self.assertEqual('EMAIL_SENT_OPENED',
                         updated_data.get('emailsSent')[0].get('status'))
    def test_campaign_specific(self, mock_campaigns: mock.MagicMock) -> None:
        """Test retrieving info for a specific campaign."""

        message_id = self._send_email('*****@*****.**')
        mailjetmock.get_message(message_id).open()

        mock_campaigns.list_all_campaigns.return_value = [
            'this-campaign', 'other-campaign'
        ]
        self.database.user.insert_many([
            {
                'profile': {
                    'email': '*****@*****.**'
                },
                'emailsSent': [
                    {
                        'campaignId': 'this-campaign',
                        'sentAt': '2017-09-08T09:25:46.145001Z',
                        'mailjetMessageId': message_id,
                    },
                    {
                        'campaignId': 'other-campaign',
                        'sentAt': '2017-09-08T09:25:46.145001Z',
                        'mailjetMessageId':
                        self._send_email('*****@*****.**'),
                    },
                ],
            },
            {
                'profile': {
                    'email': '*****@*****.**'
                },
                'emailsSent': [{
                    'campaignId':
                    'other-campaign',
                    'sentAt':
                    '2017-09-08T09:25:46.145001Z',
                    'mailjetMessageId':
                    self._send_email('*****@*****.**'),
                }],
            },
        ])
        update_email_sent_status.main(
            ['--campaigns', 'this-campaign', '--disable-sentry'])
        updated_user = self.database.user.find_one(
            {'profile.email': '*****@*****.**'})
        assert updated_user
        self.assertEqual('EMAIL_SENT_OPENED',
                         updated_user.get('emailsSent')[0].get('status'))
        self.assertIsNone(updated_user.get('emailsSent')[1].get('status'))
        not_updated_user = self.database.user.find_one(
            {'profile.email': '*****@*****.**'})
        assert not_updated_user
        self.assertIsNone(not_updated_user.get('emailsSent')[0].get('status'))
Esempio n. 6
0
    def test_mailjet_unknown(self) -> None:
        """Test retrieving info but MailJet never heard of the message."""

        self.database.user.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-09-08T09:25:46.145001Z',
                'mailjetMessageId': 9876554,
            }],
        })

        update_email_sent_status.main(['--disable-sentry'])

        updated_data = self.database.user.find_one()
        self.assertEqual('field', updated_data.get('other'))
        self.assertEqual(
            9876554,
            int(updated_data.get('emailsSent')[0].get('mailjetMessageId')))
        self.assertNotIn('status', updated_data.get('emailsSent')[0])
    def test_refresh_old_status(self, mock_mail, mock_now):
        """Test retrieving info when message ID is present."""

        database = mongomock.MongoClient().test

        # On Nov. the 5th, the email had been opened.
        mock_now.get.return_value = datetime.datetime(2017, 11, 5, 15, 13)
        mock_mail.get_message.return_value = {
            'ArrivedAt': '2017-10-28T09:25:48Z',
            'ID': 6789,
            'Status': 'opened',
        }
        database.user.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-10-28T09:25:46.145001Z',
                'mailjetMessageId': 6789,
            }],
        })
        update_email_sent_status.main(database)

        # A week later the email link had been clicked.
        mock_now.get.return_value = datetime.datetime(2017, 11, 13, 15, 13)
        mock_mail.get_message.return_value = {
            'ArrivedAt': '2017-10-28T09:25:48Z',
            'ID': 6789,
            'Status': 'clicked',
        }
        update_email_sent_status.main(database)

        updated_data = database.user.find_one()
        self.assertEqual('EMAIL_SENT_CLICKED',
                         updated_data.get('emailsSent')[0].get('status'))
Esempio n. 8
0
    def test_multiple_checks(self, mock_mail: mock.MagicMock,
                             mock_now: mock.MagicMock) -> None:
        """Test checking the status of an email several times."""

        # Note that in this test we do not use mailjetmock because what's
        # important is to check when calls to Mailjet are made (i.e. not too often).

        mock_now.get.return_value = datetime.datetime(2017, 9, 8, 15, 13)
        mock_mail.get_message.return_value = {
            'ArrivedAt': '2017-09-08T09:25:48Z',
            'ID': 6789,
            'Comment': 'Right message, arrived 2 seconds after being sent',
            'Status': 'opened',
        }
        self.database.user.insert_one({
            'other':
            'field',
            'profile': {
                'email': '*****@*****.**'
            },
            'emailsSent': [{
                'sentAt': '2017-09-08T09:25:46.145001Z',
                'mailjetMessageId': 6789,
            }],
        })
        update_email_sent_status.main(['--disable-sentry'])
        mock_mail.get_message.reset_mock()

        # Check again, an hour later.
        mock_now.get.return_value = datetime.datetime(2017, 9, 8, 16, 13)
        update_email_sent_status.main(['--disable-sentry'])

        mock_mail.get_message.assert_called_once()
        mock_mail.get_message.reset_mock()

        # Check again the next day.
        mock_now.get.return_value = datetime.datetime(2017, 9, 9, 17, 13)
        update_email_sent_status.main(['--disable-sentry'])

        mock_mail.get_message.assert_called_once()
        mock_mail.get_message.reset_mock()

        # Check again an hour later the next day.
        mock_now.get.return_value = datetime.datetime(2017, 9, 9, 18, 13)
        update_email_sent_status.main(['--disable-sentry'])

        mock_mail.get_message.assert_not_called()

        # Check again 15 days later.
        mock_now.get.return_value = datetime.datetime(2017, 9, 24, 18, 14)
        update_email_sent_status.main(['--disable-sentry'])

        mock_mail.get_message.assert_called_once()
        mock_mail.get_message.reset_mock()

        # Check again the next day.
        mock_now.get.return_value = datetime.datetime(2017, 9, 25, 18, 14)
        update_email_sent_status.main(['--disable-sentry'])

        mock_mail.get_message.assert_not_called()