def test_does_notification_batching(self): with execute_scheduled_tasks_immediately(): recent_message = self.conversation.messages.create( author=self.author, content='first reply') self.assertEqual(len(mail.outbox), 1) self.assertIn(recent_message.content, mail.outbox[0].body) # and another three messages, to check if notified_up_to is updated mail.outbox = [] with suppressed_notifications(): two = self.conversation.messages.create(author=self.author, content='second message') self.conversation.messages.create(author=self.author, content='three') self.conversation.conversationparticipant_set.filter( user=self.user).update(seen_up_to=two) with execute_scheduled_tasks_immediately(): recent_message = self.conversation.messages.create( author=self.author, content='four') self.assertEqual(len(mail.outbox), 1) user1_email = next(email for email in mail.outbox if email.to[0] == self.user.email) self.assertNotIn('second message', user1_email.body) self.assertIn('three', user1_email.body) self.assertIn('four', user1_email.body) self.assertIn(self.author.display_name, mail.outbox[0].from_email) participant = ConversationParticipant.objects.get( conversation=self.conversation, user=self.user) self.assertEqual(participant.notified_up_to.id, recent_message.id)
def test_message_email_notifications(self): with execute_scheduled_tasks_immediately(): message = self.conversation.messages.create(author=self.user, content='asdf') self.assertEqual(len(mail.outbox), 1) self.assertIn(self.place.name, mail.outbox[0].subject) self.assertIn(message.content, mail.outbox[0].body)
def test_send_reply_notifications(self): mail.outbox = [] with execute_scheduled_tasks_immediately(): self.create_reply(author=self.user2) self.assertEqual(len(mail.outbox), 1) self.assertIn(self.thread.content, mail.outbox[0].subject) self.assertIn('In reply to', mail.outbox[0].body)
def test_exclude_thread_replies_from_conversation_notification(self): with suppressed_notifications(): self.group.conversation.messages.create( author=self.user, thread=self.message, content='first thread reply' ) with execute_scheduled_tasks_immediately(): self.group.conversation.messages.create(author=self.author, content='conversation') self.assertNotIn('first thread reply', mail.outbox[0].body)
def test_notify_about_unseen_message(self): self.group.conversation.conversationparticipant_set.filter(user=self.user).update(seen_up_to=self.message) with execute_scheduled_tasks_immediately(): self.group.conversation.messages.create(author=self.author, content='this should be sent') self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to[0], self.user.email) self.assertIn('this should be sent', mail.outbox[0].body) self.assertNotIn('initial message', mail.outbox[0].body)
def test_reply_email_notifications(self): with execute_scheduled_tasks_immediately(): message = self.conversation.messages.create(author=self.user, content='asdf') mail.outbox = [] reply = self.conversation.messages.create(author=self.user2, thread=message, content='my reply') self.assertEqual(len(mail.outbox), 1) self.assertIn(message.content, mail.outbox[0].subject) self.assertIn(reply.content, mail.outbox[0].body)
def test_exclude_participants_without_access(self): self.group.remove_member(self.author) with execute_scheduled_tasks_immediately(): self.group.conversation.messages.create( author=self.user, thread=self.message, content='first thread reply') self.assertEqual(len(mail.outbox), 0)
def test_send_email_notifications(self): with execute_scheduled_tasks_immediately(): ConversationMessage.objects.create(author=self.user, conversation=self.conversation, content='asdf') self.assertEqual(len(mail.outbox), 2) actual_recipients = set(m.to[0] for m in mail.outbox) expected_recipients = set(u.email for u in self.more_users) self.assertEqual(actual_recipients, expected_recipients)
def test_does_not_notify_user_about_own_offer(self): mail.outbox = [] with execute_scheduled_tasks_immediately(): with transaction.atomic(): self.offer = OfferFactory(group=self.group, user=self.user, images=[image_path]) email_addresses = [address for m in mail.outbox for address in m.to] self.assertEqual(len(mail.outbox), 1) self.assertEqual(email_addresses, [self.other_user.email])
def test_only_notifies_active_group_members(self): self.group.add_member(UserFactory()) inactive_user = VerifiedUserFactory() self.group.add_member(inactive_user) self.group.groupmembership_set.filter(user=inactive_user).update(inactive_at=timezone.now()) mail.outbox = [] with execute_scheduled_tasks_immediately(): self.group.conversation.messages.create(author=self.author, content='foo') self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user.email])
def test_member_replies_in_conversation(self): self.client.force_login(user=self.member) chat_message = faker.sentence() with execute_scheduled_tasks_immediately(): response = self.client.post('/api/messages/', { 'conversation': self.conversation.id, 'content': chat_message, }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) notification = mail.outbox[0] self.assertEqual(notification.to[0], self.applicant.email) self.assertIn('New message in', notification.subject) self.assertIn(chat_message, notification.body)
def test_exclude_already_notified_in_thread(self): with execute_scheduled_tasks_immediately(): self.group.conversation.messages.create( author=self.user, thread=self.message, content='first thread reply' ) mail.outbox = [] self.group.conversation.messages.create( author=self.user, thread=self.message, content='second thread reply' ) self.assertEqual(len(mail.outbox), 1) self.assertIn('second thread reply', mail.outbox[0].body) self.assertNotIn('first thread reply', mail.outbox[0].body)
def test_send_email_notifications(self): conversation = Conversation.objects.get_or_create_for_two_users(self.user, self.user2) mail.outbox = [] with execute_scheduled_tasks_immediately(): ConversationMessage.objects.create(author=self.user, conversation=conversation, content='asdf') self.assertEqual(len(mail.outbox), 1) actual_recipient = mail.outbox[0].to[0] expected_recipient = self.user2.email self.assertEqual(actual_recipient, expected_recipient) self.assertEqual(len(mail.outbox), 1)
def test_send_email_notifications(self): users = [VerifiedUserFactory() for _ in range(2)] [self.activity.add_participant(u) for u in users] mail.outbox = [] with execute_scheduled_tasks_immediately(): ConversationMessage.objects.create(author=self.user, conversation=self.conversation, content='asdf') actual_recipients = sorted(m.to[0] for m in mail.outbox) expected_recipients = sorted(u.email for u in users) self.assertEqual(actual_recipients, expected_recipients) self.assertEqual(len(mail.outbox), 2)
def test_newcomer_replies_in_conversation(self): newcomer = UserFactory() self.group.groupmembership_set.create(user=newcomer) mail.outbox = [] self.client.force_login(user=newcomer) chat_message = faker.sentence() with execute_scheduled_tasks_immediately(): response = self.client.post('/api/messages/', { 'conversation': self.conversation.id, 'content': chat_message, }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) notification = mail.outbox[0] self.assertIn('New message in', notification.subject) self.assertIn(chat_message, notification.body)
def test_does_notification_batching_in_threads(self): with suppressed_notifications(): self.group.conversation.messages.create( author=self.user, thread=self.message, content='first thread reply' ) with execute_scheduled_tasks_immediately(): recent_message = self.group.conversation.messages.create( author=self.user, thread=self.message, content='second thread reply' ) self.assertEqual(len(mail.outbox), 1) self.assertIn('first thread reply', mail.outbox[0].body) self.assertIn('second thread reply', mail.outbox[0].body) self.assertEqual(mail.outbox[0].to[0], self.author.email) participant = ConversationThreadParticipant.objects.get(thread=self.message, user=self.author) self.assertEqual(participant.notified_up_to.id, recent_message.id)
def test_email_notification_reply_to_has_thread_id(self): user = VerifiedUserFactory() user2 = VerifiedUserFactory() group = GroupFactory(members=[user, user2]) conversation = Conversation.objects.get_or_create_for_target(group) mail.outbox = [] with execute_scheduled_tasks_immediately(): message = ConversationMessage.objects.create(author=user, conversation=conversation, content='asdf') reply_to = parseaddr(mail.outbox[0].reply_to[0])[1] local_part = reply_to.split('@')[0] conversation_id, user_id, thread_id = parse_local_part(local_part) self.assertEqual(conversation_id, conversation.id) self.assertEqual(user_id, user2.id) self.assertEqual(thread_id, message.id)
def test_not_exclude_bounced_addresses_if_too_old(self): bounce_user = VerifiedUserFactory() self.group.add_member(bounce_user) some_months_ago = timezone.now() - relativedelta(months=4) for _ in range(5): EmailEvent.objects.create(created_at=some_months_ago, address=bounce_user.email, event='bounce', payload={}, version=1) mail.outbox = [] with execute_scheduled_tasks_immediately(): ConversationMessage.objects.create(author=self.user, conversation=self.conversation, content='asdf') self.assertEqual(len(mail.outbox), 1)