Add test for the add_email_to_ticketcc_if_not_in ticket function

This commit is contained in:
bbe 2020-10-31 17:54:29 +01:00
parent 391cc000b8
commit 177ba8f963
2 changed files with 57 additions and 8 deletions

View File

@ -813,6 +813,7 @@ class Ticket(models.Model):
:param str email:
:param User user:
:param TicketCC ticketcc:
:rtype: TicketCC|None
"""
if ticketcc:
email = ticketcc.display
@ -820,23 +821,28 @@ class Ticket(models.Model):
if user.email:
email = user.email
else:
# Ignore if user has no email address
return
elif not email:
return
raise ValueError('You must provide at least one parameter to get the email from')
# Prepare all emails already into the ticket
ticket_emails = [x.display for x in self.ticketcc_set.all()]
if self.submitter_email:
ticket_emails.append(self.submitter_email)
if self.assigned_to and self.assigned_to.email:
ticket_emails.append(self.assigned_to.email)
# Check that email is not already part of the ticket
if (
email != self.submitter_email and
(self.assigned_to and email != self.assigned_to.email) and
email not in [x.display for x in self.ticketcc_set.all()]
):
if email not in ticket_emails:
if ticketcc:
ticketcc.ticket = self
ticketcc.save(update_fields=['ticket'])
elif user:
self.ticketcc_set.create(user=user)
ticketcc = self.ticketcc_set.create(user=user)
else:
self.ticketcc_set.create(email=email)
ticketcc = self.ticketcc_set.create(email=email)
return ticketcc
class FollowUpManager(models.Manager):

View File

@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import TestCase
from helpdesk.models import Ticket, Queue
from django.test.utils import override_settings
User = get_user_model()
@override_settings(
HELPDESK_VIEW_A_TICKET_PUBLIC=True
)
@ -50,3 +54,42 @@ class TestTicketLookupPublicEnabled(TestCase):
# confirm that we can still get to a url which was emailed earlier
response = self.client.get(url, params)
self.assertNotContains(response, "Invalid ticket ID")
def test_add_email_to_ticketcc_if_not_in(self):
staff_email = 'staff@mail.com'
staff_user = User.objects.create(username='staff', email=staff_email, is_staff=True)
self.ticket.assigned_to = staff_user
self.ticket.save()
email_1 = 'user1@mail.com'
ticketcc_1 = self.ticket.ticketcc_set.create(email=email_1)
# Add new email to CC
email_2 = 'user2@mail.com'
ticketcc_2 = self.ticket.add_email_to_ticketcc_if_not_in(email=email_2)
self.assertEqual(list(self.ticket.ticketcc_set.all()), [ticketcc_1, ticketcc_2])
# Add existing email, doesn't change anything
self.ticket.add_email_to_ticketcc_if_not_in(email=email_1)
self.assertEqual(list(self.ticket.ticketcc_set.all()), [ticketcc_1, ticketcc_2])
# Add mail from assigned user, doesn't change anything
self.ticket.add_email_to_ticketcc_if_not_in(email=staff_email)
self.assertEqual(list(self.ticket.ticketcc_set.all()), [ticketcc_1, ticketcc_2])
self.ticket.add_email_to_ticketcc_if_not_in(user=staff_user)
self.assertEqual(list(self.ticket.ticketcc_set.all()), [ticketcc_1, ticketcc_2])
# Move a ticketCC from ticket 1 to ticket 2
ticket_2 = Ticket.objects.create(queue=self.ticket.queue, title='Ticket 2', submitter_email=email_2)
self.assertEqual(ticket_2.ticketcc_set.count(), 0)
ticket_2.add_email_to_ticketcc_if_not_in(ticketcc=ticketcc_1)
self.assertEqual(ticketcc_1.ticket, ticket_2)
self.assertEqual(ticket_2.ticketcc_set.count(), 1)
# Adding email_2 doesn't change since it is already submitter email
ticket_2.add_email_to_ticketcc_if_not_in(email=email_2)
self.assertEqual(ticket_2.ticketcc_set.get(), ticketcc_1)
ticket_2.add_email_to_ticketcc_if_not_in(ticketcc=ticketcc_2)
self.assertEqual(ticket_2.ticketcc_set.get(), ticketcc_1)
# Finally test function raises a Value error when no parameter is given
self.assertRaises(ValueError, ticket_2.add_email_to_ticketcc_if_not_in)