Only add CC emails to Ticket if they were not already included (no duplicates), add testing for it

This commit is contained in:
Garret Wassermann 2017-04-18 23:36:39 -04:00
parent 3b22bb655a
commit 34ce553435
2 changed files with 28 additions and 11 deletions

View File

@ -282,7 +282,8 @@ def ticket_from_message(message, queue, logger):
tempcc = [] tempcc = []
for hdr in cc: for hdr in cc:
tempcc.extend(hdr.split(',')) 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)): for ignore in IgnoreEmail.objects.filter(Q(queues=queue) | Q(queues__isnull=True)):
if ignore.test(sender_email): 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)) logger.debug("Created new ticket %s-%s" % (t.queue.slug, t.id))
if cc: if cc:
for new_cc in cc: # get list of currently CC'd emails
tcc = TicketCC( 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, ticket=t,
email=new_cc, email=ccemail,
can_view=True, can_view=True,
can_update=False can_update=False
) )

View File

@ -229,13 +229,16 @@ class GetEmailParametricTemplate(object):
# example email text from Django docs: https://docs.djangoproject.com/en/1.10/ref/unicode/ # example email text from Django docs: https://docs.djangoproject.com/en/1.10/ref/unicode/
test_email_from = "Arnbjörg Ráðormsdóttir <arnbjorg@example.com>" test_email_from = "Arnbjörg Ráðormsdóttir <arnbjorg@example.com>"
test_email_cc_one = "other@example.com" # NOTE: CC emails are in alphabetical order and must be tested as such!
test_email_cc_two = "someone@example.com" # implementation uses sets, so only way to ensure tickets created
test_email_cc_three = "Alice Ráðormsdóttir <alice@example.com>" # in right order is to change set to list and sort it
test_email_cc_four = "nobody@example.com" test_email_cc_one = "Alice Ráðormsdóttir <alice@example.com>"
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_subject = "My visit to Sør-Trøndelag"
test_email_body = "Unicode helpdesk comment with an s-hat (ŝ) via email." 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) test_mail_len = len(test_email)
if self.socks: if self.socks:
@ -299,6 +302,9 @@ class GetEmailParametricTemplate(object):
cc3 = get_object_or_404(TicketCC, pk=3) cc3 = get_object_or_404(TicketCC, pk=3)
self.assertEqual(cc3.email, test_email_cc_three) self.assertEqual(cc3.email, test_email_cc_three)
cc4 = get_object_or_404(TicketCC, pk=4) 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) self.assertEqual(cc4.email, test_email_cc_four)
ticket2 = get_object_or_404(Ticket, pk=2) ticket2 = get_object_or_404(Ticket, pk=2)
@ -318,8 +324,11 @@ class GetEmailParametricTemplate(object):
me = "my@example.com" me = "my@example.com"
you = "your@example.com" you = "your@example.com"
cc_one = "other@example.com" # NOTE: CC'd emails need to be alphabetical and tested as such!
cc_two = "nobody@example.com" # 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 cc = cc_one + ", " + cc_two
subject = "Link" subject = "Link"