From 34ce5534352ada0509714032e9c53337486059a9 Mon Sep 17 00:00:00 2001 From: Garret Wassermann Date: Tue, 18 Apr 2017 23:36:39 -0400 Subject: [PATCH] Only add CC emails to Ticket if they were not already included (no duplicates), add testing for it --- helpdesk/management/commands/get_email.py | 16 ++++++++++++---- helpdesk/tests/test_get_email.py | 23 ++++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 3fa3d38a..a7baa956 100644 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -282,7 +282,8 @@ def ticket_from_message(message, queue, logger): tempcc = [] for hdr in cc: tempcc.extend(hdr.split(',')) - cc = [x.strip() for x in tempcc] + # use a set to ensure no duplicates + cc = set([x.strip() for x in tempcc]) for ignore in IgnoreEmail.objects.filter(Q(queues=queue) | Q(queues__isnull=True)): if ignore.test(sender_email): @@ -369,10 +370,17 @@ def ticket_from_message(message, queue, logger): logger.debug("Created new ticket %s-%s" % (t.queue.slug, t.id)) if cc: - for new_cc in cc: - tcc = TicketCC( + # get list of currently CC'd emails + current_cc = TicketCC.objects.filter(ticket=ticket) + current_cc = set([x.email for x in current_cc]) + # add any email in cc that's not already in current_cc (set difference) + new_cc = cc.difference(current_cc) + # add emails alphabetically, makes testing easy + new_cc = sorted(list(new_cc)) + for ccemail in new_cc: + tcc = TicketCC.objects.create( ticket=t, - email=new_cc, + email=ccemail, can_view=True, can_update=False ) diff --git a/helpdesk/tests/test_get_email.py b/helpdesk/tests/test_get_email.py index c86e1f17..100cd01c 100644 --- a/helpdesk/tests/test_get_email.py +++ b/helpdesk/tests/test_get_email.py @@ -229,13 +229,16 @@ class GetEmailParametricTemplate(object): # example email text from Django docs: https://docs.djangoproject.com/en/1.10/ref/unicode/ test_email_from = "Arnbjörg Ráðormsdóttir " - test_email_cc_one = "other@example.com" - test_email_cc_two = "someone@example.com" - test_email_cc_three = "Alice Ráðormsdóttir " - test_email_cc_four = "nobody@example.com" + # NOTE: CC emails are in alphabetical order and must be tested as such! + # implementation uses sets, so only way to ensure tickets created + # in right order is to change set to list and sort it + test_email_cc_one = "Alice Ráðormsdóttir " + test_email_cc_two = "nobody@example.com" + test_email_cc_three = "other@example.com" + test_email_cc_four = "someone@example.com" test_email_subject = "My visit to Sør-Trøndelag" test_email_body = "Unicode helpdesk comment with an s-hat (ŝ) via email." - test_email = "To: helpdesk@example.com\nCc: " + test_email_cc_one + ", " + test_email_cc_two + ", " + test_email_cc_three + "\nCC: " + test_email_cc_four + "\nFrom: " + test_email_from + "\nSubject: " + test_email_subject + "\n\n" + test_email_body + test_email = "To: helpdesk@example.com\nCc: " + test_email_cc_one + ", " + test_email_cc_one + ", " + test_email_cc_two + ", " + test_email_cc_three + "\nCC: " + test_email_cc_one + ", " + test_email_cc_three + ", " + test_email_cc_four + "\nFrom: " + test_email_from + "\nSubject: " + test_email_subject + "\n\n" + test_email_body test_mail_len = len(test_email) if self.socks: @@ -299,6 +302,9 @@ class GetEmailParametricTemplate(object): cc3 = get_object_or_404(TicketCC, pk=3) self.assertEqual(cc3.email, test_email_cc_three) cc4 = get_object_or_404(TicketCC, pk=4) + # the second CC has an extra test_email_cc_one and three that + # should not be saved again, so the 4th email should be + # test_email_cc_four self.assertEqual(cc4.email, test_email_cc_four) ticket2 = get_object_or_404(Ticket, pk=2) @@ -318,8 +324,11 @@ class GetEmailParametricTemplate(object): me = "my@example.com" you = "your@example.com" - cc_one = "other@example.com" - cc_two = "nobody@example.com" + # NOTE: CC'd emails need to be alphabetical and tested as such! + # implementation uses sets, so only way to ensure tickets created + # in right order is to change set to list and sort it + cc_one = "nobody@example.com" + cc_two = "other@example.com" cc = cc_one + ", " + cc_two subject = "Link"