def mocked_trigger_send(email_template, fields): # raise an error no matter what attempted_emails.append(fields['EMAIL_ADDRESS_']) raise exacttarget.NewsletterException('error') if fields['EMAIL_ADDRESS_'].endswith('@mail.com.'): raise exacttarget.NewsletterException('error') else: return True
def test_cron_job(self, exacttarget_mock): config_manager = self._setup_config_manager() et_mock = exacttarget_mock.return_value # Make get_subscriber raise an exception list_service = et_mock.list.return_value = mock.Mock() list_service.get_subscriber = mock.Mock( side_effect=exacttarget.NewsletterException() ) with config_manager.context() as config: tab = crontabber.CronTabber(config) tab.run_all() information = self._load_structure() assert information['automatic-emails'] assert not information['automatic-emails']['last_error'] assert information['automatic-emails']['last_success'] self.assertEqual(et_mock.trigger_send.call_count, 4) last_email = u'z\[email protected]' # Verify the last call to trigger_send fields = { 'EMAIL_ADDRESS_': last_email, 'EMAIL_FORMAT_': 'H', 'TOKEN': last_email } et_mock.trigger_send.assert_called_with('socorro_dev_test', fields) # Verify that user's data was updated conf = config.crontabber['class-AutomaticEmailsCronApp'] es = SuperS().es( urls=conf.elasticsearch.elasticsearch_urls, timeout=conf.elasticsearch.elasticsearch_timeout, ) search = es.indexes(conf.elasticsearch.elasticsearch_emails_index) search = search.doctypes('emails') es.get_es().refresh() emails_list = ( '*****@*****.**', '"Quidam" <*****@*****.**>', '*****@*****.**' ) search = search.filter(_id__in=emails_list) res = search.values_list('last_sending') self.assertEqual(len(res), 3) now = utc_now() for row in res: date = string_to_datetime(row[0]) self.assertEqual(date.year, now.year) self.assertEqual(date.month, now.month) self.assertEqual(date.day, now.day)
def test_cron_job(self, exacttarget_mock): (config_manager, json_file) = self._setup_config_manager() et_mock = exacttarget_mock.return_value # Make get_subscriber raise an exception list_service = et_mock.list.return_value = mock.Mock() list_service.get_subscriber = mock.Mock( side_effect=exacttarget.NewsletterException() ) with config_manager.context() as config: tab = crontabber.CronTabber(config) tab.run_all() information = json.load(open(json_file)) assert information['automatic-emails'] assert not information['automatic-emails']['last_error'] assert information['automatic-emails']['last_success'] self.assertEqual(et_mock.trigger_send.call_count, 3) # Verify the last call to trigger_send fields = { 'EMAIL_ADDRESS_': '*****@*****.**', 'EMAIL_FORMAT_': 'H', 'TOKEN': '*****@*****.**' } et_mock.trigger_send.assert_called_with('socorro_dev_test', fields) # Verify that user's data was updated cursor = self.conn.cursor() emails_list = ( '*****@*****.**', '*****@*****.**', '*****@*****.**' ) sql = """ SELECT last_sending FROM emails WHERE email IN %s """ % (emails_list,) cursor.execute(sql) self.assertEqual(cursor.rowcount, 3) now = utc_now() for row in cursor.fetchall(): self.assertEqual(row[0].year, now.year) self.assertEqual(row[0].month, now.month) self.assertEqual(row[0].day, now.day)
def mocked_trigger_send(email_template, fields): # raise an error no matter what attempted_emails.append(fields['EMAIL_ADDRESS_']) raise exacttarget.NewsletterException('error')
def test_error_in_send_email(self, exacttarget_mock): list_service_mock = exacttarget_mock.return_value.list.return_value list_service_mock.get_subscriber.return_value = { 'token': '*****@*****.**' } exacttarget_mock.return_value.trigger_send.side_effect = ( exacttarget.NewsletterException('error')) config_manager = self._setup_simple_config() with config_manager.context() as config: job = automatic_emails.AutomaticEmailsCronApp(config, '') email = '*****@*****.**' job.send_email({ 'processed_crash.email': email, 'email_template': 'socorro_dev_test', }) fields = { 'EMAIL_ADDRESS_': email, 'EMAIL_FORMAT_': 'H', 'TOKEN': email } exacttarget_mock.return_value.trigger_send.assert_called_with( 'socorro_dev_test', fields) eq_(config.logger.error.call_count, 1) config.logger.error.assert_called_with( 'Unable to send an email to %s, error is: %s', email, 'error', exc_info=True) list_service = exacttarget_mock.return_value.list.return_value list_service.get_subscriber.side_effect = (Exception( 404, 'Bad Request')) exacttarget_mock.return_value.trigger_send.side_effect = (Exception( 404, 'Bad Request')) with config_manager.context() as config: job = automatic_emails.AutomaticEmailsCronApp(config, '') email = '*****@*****.**' job.send_email({ 'processed_crash.email': email, 'email_template': 'socorro_dev_test', }) fields = { 'EMAIL_ADDRESS_': '*****@*****.**', 'EMAIL_FORMAT_': 'H', 'TOKEN': '*****@*****.**' } exacttarget_mock.return_value.trigger_send.assert_called_with( 'socorro_dev_test', fields) eq_(config.logger.error.call_count, 2) config.logger.error.assert_called_with( 'Unable to send an email to %s, fields are %s, error is: %s', '*****@*****.**', str(fields), "(404, 'Bad Request')", exc_info=True)
def test_error_in_send_email(self, exacttarget_mock): list_service_mock = exacttarget_mock.return_value.list.return_value list_service_mock.get_subscriber.return_value = { 'token': '*****@*****.**' } exacttarget_mock.return_value.trigger_send.side_effect = ( exacttarget.NewsletterException('error')) config_manager = self._setup_simple_config() with config_manager.context() as config: job = automatic_emails.AutomaticEmailsCronApp(config, '') report = { 'email': '*****@*****.**', 'product': 'WaterWolf', 'version': '20.0', 'release_channel': 'Release', } job.send_email(report) fields = { 'EMAIL_ADDRESS_': '*****@*****.**', 'EMAIL_FORMAT_': 'H', 'TOKEN': '*****@*****.**' } exacttarget_mock.return_value.trigger_send.assert_called_with( 'socorro_dev_test', fields) self.assertEqual(config.logger.error.call_count, 1) config.logger.error.assert_called_with( 'Unable to send an email to %s, error is: %s', '*****@*****.**', 'error', exc_info=True) list_service = exacttarget_mock.return_value.list.return_value list_service.get_subscriber.side_effect = (Exception( 404, 'Bad Request')) exacttarget_mock.return_value.trigger_send.side_effect = (Exception( 404, 'Bad Request')) with config_manager.context() as config: job = automatic_emails.AutomaticEmailsCronApp(config, '') report = { 'email': '*****@*****.**', 'product': 'WaterWolf', 'version': '20.0', 'release_channel': 'Release', } job.send_email(report) fields = { 'EMAIL_ADDRESS_': u'*****@*****.**', 'EMAIL_FORMAT_': 'H', 'TOKEN': u'*****@*****.**' } exacttarget_mock.return_value.trigger_send.assert_called_with( 'socorro_dev_test', fields) self.assertEqual(config.logger.error.call_count, 2) config.logger.error.assert_called_with( 'Unable to send an email to %s, fields are %s, error is: %s', u'*****@*****.**', str(fields), "(404, 'Bad Request')", exc_info=True)