From 26fc6cb1de849cefe519afc1c08d4b9356503763 Mon Sep 17 00:00:00 2001 From: Bruno Tikami Date: Tue, 1 Mar 2016 16:49:34 -0300 Subject: [PATCH] ADDED: Flag to enable notifications on every interaction that occurs over email. --- helpdesk/management/commands/get_email.py | 2 +- ...notifications_on_email_events_to_ticket.py | 20 ++++ helpdesk/models.py | 9 ++ helpdesk/tests/test_ticket_submission.py | 99 ++++++++++++++++--- 4 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 helpdesk/migrations/0014_add_enable_notifications_on_email_events_to_ticket.py diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 5028c098..0335dad6 100644 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -348,7 +348,7 @@ def create_object_from_email_message(message, ticket_id, payload, files, quiet): for email in ticket_cc_list : notifications_to_be_sent.append(email) - if len(notifications_to_be_sent): + if queue.enable_notifications_on_email_events and len(notifications_to_be_sent): send_templated_mail( notification_template, diff --git a/helpdesk/migrations/0014_add_enable_notifications_on_email_events_to_ticket.py b/helpdesk/migrations/0014_add_enable_notifications_on_email_events_to_ticket.py new file mode 100644 index 00000000..deb1ef04 --- /dev/null +++ b/helpdesk/migrations/0014_add_enable_notifications_on_email_events_to_ticket.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-03-01 19:43 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0013_add_submitter_email_id_field_to_ticket'), + ] + + operations = [ + migrations.AddField( + model_name='queue', + name='enable_notifications_on_email_events', + field=models.BooleanField(default=False, help_text='When an email arrives to either create a ticket or to interact with an existing discussion. Should email notifications be sent ? Note: the new_ticket_cc and updated_ticket_cc work independently of this feature', verbose_name='Notify contacts when email updates arrive'), + ), + ] diff --git a/helpdesk/models.py b/helpdesk/models.py index 1db8a923..374aa1af 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -112,6 +112,15 @@ class Queue(models.Model): 'multiple addresses with a comma.'), ) + enable_notifications_on_email_events = models.BooleanField( + _('Notify contacts when email updates arrive'), + blank=True, + default=False, + help_text=_('When an email arrives to either create a ticket or to ' + 'interact with an existing discussion. Should email notifications be sent ? ' + 'Note: the new_ticket_cc and updated_ticket_cc work independently of this feature'), + ) + email_box_type = models.CharField( _('E-Mail Box Type'), max_length=5, diff --git a/helpdesk/tests/test_ticket_submission.py b/helpdesk/tests/test_ticket_submission.py index b9b389bd..2ef6cbb1 100644 --- a/helpdesk/tests/test_ticket_submission.py +++ b/helpdesk/tests/test_ticket_submission.py @@ -123,22 +123,17 @@ class EmailInteractionsTestCase(TestCase): self.queue_public = Queue.objects.create(title='Mail Queue 1', slug='mq1', allow_public_submission=True, - new_ticket_cc='new.public@example.com', - updated_ticket_cc='update.public@example.com' + new_ticket_cc='new.public.with.notifications@example.com', + updated_ticket_cc='update.public.with.notifications@example.com', + enable_notifications_on_email_events=True, ) - self.queue_public_with_notification_enabled = Queue.objects.create(title='Mail Queue 2', + self.queue_public_with_notifications_disabled = Queue.objects.create(title='Mail Queue 2', slug='mq2', allow_public_submission=True, - new_ticket_cc='new.public.with.notifications@example.com', - updated_ticket_cc='update.public.with.notifications@example.com' - ) - - self.queue_public_with_notification_disabled = Queue.objects.create(title='Mail Queue 3', - slug='mq3', - allow_public_submission=True, new_ticket_cc='new.public.without.notifications@example.com', - updated_ticket_cc='update.public.without.notifications@example.com' + updated_ticket_cc='update.public.without.notifications@example.com', + enable_notifications_on_email_events=False, ) self.ticket_data = { @@ -508,3 +503,85 @@ class EmailInteractionsTestCase(TestCase): # the new and update queues (+2) and contacts on the cc_list (+1 as it's # treated as a list) self.assertEqual(email_count + 1 + 2 + 1, len(mail.outbox)) + + def test_create_ticket_from_email_to_a_notification_enabled_queue(self): + + """ + Ensure that when an email is sent to a Queue with notifications_enabled turned ON, all contacts + on the TicketCC list are notified. + """ + + msg = email.message.Message() + + message_id = uuid.uuid4().hex + submitter_email = 'foo@bar.py' + cc_list = ['bravo@example.net', 'charlie@foobar.com'] + + msg.__setitem__('Message-ID', message_id) + msg.__setitem__('Subject', self.ticket_data['title']) + msg.__setitem__('From', submitter_email) + msg.__setitem__('To', self.queue_public.email_address) + msg.__setitem__('Cc', ','.join(cc_list)) + msg.__setitem__('Content-Type', 'text/plain;') + msg.set_payload(self.ticket_data['description']) + + email_count = len(mail.outbox) + + object_from_message(str(msg), self.queue_public, quiet=True) + + followup = FollowUp.objects.get(message_id=message_id) + ticket = Ticket.objects.get(id=followup.ticket.id) + self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id) + + # As we have created an Ticket from an email, we notify the sender (+1), + # the new and update queues (+2) and contacts on the cc_list (+1 as it's + # treated as a list) + self.assertEqual(email_count + 1 + 2 + 1, len(mail.outbox)) + + # Ensure that is created + for cc_email in cc_list: + ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email) + self.assertTrue(ticket_cc.ticket, ticket) + self.assertTrue(ticket_cc.email, cc_email) + + def test_create_ticket_from_email_to_a_notification_disabled_queue(self): + + """ + Ensure that when an email is sent to a Queue with notifications_enabled turned OFF, only the + new_ticket_cc and updated_ticket_cc contacts (if they are set) are notified. No contact + from the TicketCC list should be notified. + """ + + msg = email.message.Message() + + message_id = uuid.uuid4().hex + submitter_email = 'foo@bar.py' + cc_list = ['bravo@example.net', 'charlie@foobar.com'] + + msg.__setitem__('Message-ID', message_id) + msg.__setitem__('Subject', self.ticket_data['title']) + msg.__setitem__('From', submitter_email) + msg.__setitem__('To', self.queue_public.email_address) + msg.__setitem__('Cc', ','.join(cc_list)) + msg.__setitem__('Content-Type', 'text/plain;') + msg.set_payload(self.ticket_data['description']) + + email_count = len(mail.outbox) + + object_from_message(str(msg), self.queue_public_with_notifications_disabled, quiet=True) + + followup = FollowUp.objects.get(message_id=message_id) + ticket = Ticket.objects.get(id=followup.ticket.id) + self.assertEqual(ticket.ticket_for_url, "mq2-%s" % ticket.id) + + # As we have created an Ticket from an email, we notify the sender (+1), + # the new and update queues (+2) and that's all + self.assertEqual(email_count + 1 + 2, len(mail.outbox)) + + # Ensure that is created even if the Queue notifications are disabled + # so when staff members interact with the , they get notified + for cc_email in cc_list: + ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email) + self.assertTrue(ticket_cc.ticket, ticket) + self.assertTrue(ticket_cc.email, cc_email) +