def test_bad_events_deactivate_user(self): now = datetime.datetime.now() for event in STOP_SENDING + BAD_EMAIL: u = UserFactory(email="*****@*****.**", is_verified=True, opt_in_myjobs=True) self.make_email_logs(u.email, event, now, False, 3) process_batch_events() u = User.objects.get(pk=u.pk) self.assertEqual(u.deactivate_type, event) # Users start this test case with is_verified=True # is_verified should only change if the modifying event # is a block or drop self.assertEqual(u.is_verified, event in STOP_SENDING) self.assertFalse(u.opt_in_myjobs) infos = u.messageinfo_set.all() self.assertEqual(len(infos), 1) message = infos[0].message if u.deactivate_type in STOP_SENDING: text = 'stop communications' else: text = 'Attempts to send messages to' self.assertTrue(text in message.body) EmailLog.objects.all().delete() u.delete()
def test_batch_month_old_message_digest_no_searches(self): """ Posting data created a month ago should result in no emails being sent if the user has no saved searches """ # Create activation profile for user custom_signals.create_activation_profile(sender=self, user=self.user, email=self.user.email) month_ago = date.today() - timedelta(days=30) self.user.last_response = month_ago - timedelta(days=1) self.user.save() messages = self.make_messages(month_ago) response = self.client.post(reverse('batch_message_digest'), data=messages, content_type="text/json", HTTP_AUTHORIZATION='BASIC %s' % base64.b64encode( 'accounts%40my.jobs:secret')) self.assertTrue(response.status_code, 200) self.assertEqual(EmailLog.objects.count(), 3) self.assertEqual( EmailLog.objects.filter( received=month_ago ).count(), 3 ) process_batch_events() self.assertEqual(len(mail.outbox), 0)
def test_batch_recent_message_digest(self): """ Posting data created recently should result in one EmailLog instance being created per message and no emails being sent """ # Create activation profile for user; Used when disabling an account custom_signals.create_activation_profile(sender=self, user=self.user, email=self.user.email) now = date.today() # Submit a batch of three events created recently messages = self.make_messages(now) response = self.client.post(reverse('batch_message_digest'), data=messages, content_type="text/json", HTTP_AUTHORIZATION='BASIC %s' % base64.b64encode( 'accounts%40my.jobs:secret')) self.assertEqual(response.status_code, 200) self.assertEqual(EmailLog.objects.count(), 3) process_batch_events() self.assertEqual(len(mail.outbox), 0) for log in EmailLog.objects.all(): self.assertTrue(log.event in self.events)
def test_batch_month_old_message_digest_with_searhes(self): """ Posting data created a month ago should result in one EmailLog instance being created per message and one email being sent per user """ # Create activation profile for user; Used when disabling an account custom_signals.create_activation_profile(sender=self, user=self.user, email=self.user.email) now = date.today() month_ago = date.today() - timedelta(days=30) self.user.last_response = month_ago - timedelta(days=1) self.user.save() SavedSearch(user=self.user).save() # Submit a batch of events created a month ago # The owners of these addresses should be sent an email messages = self.make_messages(month_ago) response = self.client.post( reverse('batch_message_digest'), data=messages, content_type="text/json", HTTP_AUTHORIZATION='BASIC %s' % base64.b64encode('accounts%40my.jobs:secret')) self.assertTrue(response.status_code, 200) self.assertEqual(EmailLog.objects.count(), 3) self.assertEqual( EmailLog.objects.filter(received=month_ago).count(), 3) process_batch_events() self.assertEqual(len(mail.outbox), 1) user = User.objects.get(pk=self.user.pk) self.assertEqual(user.last_response, month_ago)
def test_event_with_no_user(self): EmailLog.objects.create( email="*****@*****.**", event=STOP_SENDING[0], received=datetime.datetime.now(), processed=False ) process_batch_events() log = EmailLog.objects.get() self.assertTrue(log.processed)
def test_event_with_no_user(self): EmailLog.objects.create(email='*****@*****.**', event=STOP_SENDING[0], received=datetime.datetime.now(), processed=False) process_batch_events() log = EmailLog.objects.get() self.assertTrue(log.processed)
def test_required_number_of_bad_events(self): now = datetime.datetime.now() event = BAD_EMAIL[0] u = UserFactory(is_verified=True, opt_in_myjobs=True) self.make_email_logs(u.email, event, now, False, 2) process_batch_events() u = User.objects.get(pk=u.pk) self.assertEqual(u.deactivate_type, "none") self.make_email_logs(u.email, event, now, False, 3) process_batch_events() u = User.objects.get(pk=u.pk) self.assertEqual(u.deactivate_type, event)
def test_required_number_of_bad_events(self): now = datetime.datetime.now() event = BAD_EMAIL[0] u = UserFactory(email="*****@*****.**", is_verified=True, opt_in_myjobs=True) self.make_email_logs(u.email, event, now, False, 2) process_batch_events() u = User.objects.get(pk=u.pk) self.assertEqual(u.deactivate_type, 'none') self.make_email_logs(u.email, event, now, False, 3) process_batch_events() u = User.objects.get(pk=u.pk) self.assertEqual(u.deactivate_type, event)
def test_batch_with_one_event(self): """ Version 1 and Version 2 posts that contain a single event are valid JSON and do not play well with our batch digest method. This tests both forms of post to ensure they work. """ now = date.today() # make_messages makes len(self.events) messages. We only want one self.events = ['open'] for api_ver in [2, 3]: messages = self.make_messages(now, api_ver) response = self.client.post(reverse('batch_message_digest'), data=messages, content_type='text/json', HTTP_AUTHORIZATION='BASIC %s' % self.auth) self.assertEqual(response.status_code, 200) process_batch_events() self.assertEqual(EmailLog.objects.count(), 2)
def test_batch_month_and_week_old_message_digest(self): """ Posting data created a month and a week ago should result in one EmailLog instance being created per message, no emails being sent, and the user's opt-in status being set to False """ # Create activation profile for user; Used when disabling an account custom_signals.create_activation_profile(sender=self, user=self.user, email=self.user.email) month_and_week_ago = date.today() - timedelta(days=37) self.user.last_response = month_and_week_ago - timedelta(days=1) self.user.save() # Submit a batch of events created a month and a week ago # The owners of these addresses should no longer receive email messages = self.make_messages(month_and_week_ago) response = self.client.post(reverse('batch_message_digest'), data=messages, content_type="text/json", HTTP_AUTHORIZATION='BASIC %s' % base64.b64encode( 'accounts%40my.jobs:secret')) self.assertTrue(response.status_code, 200) self.assertEqual(EmailLog.objects.count(), 3) self.assertEqual( EmailLog.objects.filter( received__lte=(date.today() - timedelta(days=37)) ).count(), 3 ) process_batch_events() self.assertEqual(len(mail.outbox), 0) user = User.objects.get(pk=self.user.pk) self.assertFalse(user.opt_in_myjobs) self.assertTrue(user.last_response, month_and_week_ago)
def test_batch_month_old_message_digest_with_searhes(self): """ Posting data created a month ago should result in one EmailLog instance being created per message and one email being sent per user """ # Create activation profile for user; Used when disabling an account custom_signals.create_activation_profile(sender=self, user=self.user, email=self.user.email) now = date.today() month_ago = date.today() - timedelta(days=30) self.user.last_response = month_ago - timedelta(days=1) self.user.save() SavedSearch(user=self.user).save() # Submit a batch of events created a month ago # The owners of these addresses should be sent an email messages = self.make_messages(month_ago) response = self.client.post(reverse('batch_message_digest'), data=messages, content_type="text/json", HTTP_AUTHORIZATION='BASIC %s' % base64.b64encode( 'accounts%40my.jobs:secret')) self.assertTrue(response.status_code, 200) self.assertEqual(EmailLog.objects.count(), 3) self.assertEqual( EmailLog.objects.filter( received=month_ago ).count(), 3 ) process_batch_events() self.assertEqual(len(mail.outbox), 1) user = User.objects.get(pk=self.user.pk) self.assertEqual(user.last_response, month_ago)