mirror of
https://github.com/django-helpdesk/django-helpdesk.git
synced 2025-06-21 01:58:19 +02:00
Unit test multiple user IDs associated with the same email address.
This commit is contained in:
parent
9da65f05eb
commit
1757166638
@ -46,6 +46,7 @@ from typing import List
|
||||
# import User model, which may be a custom model
|
||||
User = get_user_model()
|
||||
|
||||
MULTIPLE_USERS_SAME_EMAIL_MSG = "CC email address is linked to more than 1 active user"
|
||||
STRIPPED_SUBJECT_STRINGS = [
|
||||
"Re: ",
|
||||
"Fw: ",
|
||||
@ -531,7 +532,7 @@ def is_autoreply(message):
|
||||
return any(any_if_this)
|
||||
|
||||
|
||||
def create_ticket_cc(ticket, cc_list):
|
||||
def create_ticket_cc(ticket, cc_list, logger):
|
||||
if not cc_list:
|
||||
return []
|
||||
# Local import to deal with non-defined / circular reference problem
|
||||
@ -544,16 +545,34 @@ def create_ticket_cc(ticket, cc_list):
|
||||
if cced_email == ticket.queue.email_address:
|
||||
continue
|
||||
|
||||
user = None
|
||||
user_id = None
|
||||
|
||||
try:
|
||||
user = User.objects.get(email=cced_email) # @UndefinedVariable
|
||||
except User.DoesNotExist:
|
||||
pass
|
||||
user_list = User.objects.filter(email=cced_email, is_active=True).values_list(
|
||||
"id", flat=True
|
||||
)
|
||||
count = user_list.count()
|
||||
if count == 0:
|
||||
if getattr(
|
||||
helpdesk_settings, "LOG_WARN_WHEN_CC_EMAIL_NOT_LINKED_TO_A_USER", False
|
||||
):
|
||||
logger.warning(
|
||||
f"CC email address is not linked to an active user: {cced_email}"
|
||||
)
|
||||
elif count > 1:
|
||||
if getattr(
|
||||
helpdesk_settings,
|
||||
"LOG_WARN_WHEN_CC_EMAIL_LINKED_TO_MORE_THAN_1_USER",
|
||||
True,
|
||||
):
|
||||
logger.warning(
|
||||
f"{MULTIPLE_USERS_SAME_EMAIL_MSG}: {cced_email}"
|
||||
)
|
||||
else:
|
||||
user_id = user_list[0]
|
||||
|
||||
try:
|
||||
ticket_cc = subscribe_to_ticket_updates(
|
||||
ticket=ticket, user=user, email=cced_email
|
||||
ticket=ticket, user_id=user_id, email=cced_email
|
||||
)
|
||||
new_ticket_ccs.append(ticket_cc)
|
||||
except ValidationError:
|
||||
@ -673,7 +692,7 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
|
||||
context = safe_template_context(ticket)
|
||||
|
||||
new_ticket_ccs = []
|
||||
new_ticket_ccs.append(create_ticket_cc(ticket, to_list + cc_list))
|
||||
new_ticket_ccs.append(create_ticket_cc(ticket, to_list + cc_list, logger))
|
||||
|
||||
autoreply = is_autoreply(message)
|
||||
if autoreply:
|
||||
|
@ -4,7 +4,7 @@ from django.test import TestCase
|
||||
from django.test.client import Client
|
||||
from django.urls import reverse
|
||||
import email
|
||||
from helpdesk.email import extract_email_metadata
|
||||
from helpdesk.email import extract_email_metadata, MULTIPLE_USERS_SAME_EMAIL_MSG
|
||||
from helpdesk.models import (
|
||||
CustomField,
|
||||
FollowUp,
|
||||
@ -14,10 +14,13 @@ from helpdesk.models import (
|
||||
Ticket,
|
||||
TicketCC,
|
||||
)
|
||||
from . import utils
|
||||
import logging
|
||||
from urllib.parse import urlparse
|
||||
import uuid
|
||||
import mock
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
logger = logging.getLogger("helpdesk")
|
||||
|
||||
@ -46,7 +49,7 @@ class TicketBasicsTestCase(TestCase):
|
||||
"description": "Some Test Ticket",
|
||||
}
|
||||
|
||||
self.user = get_user_model().objects.create(
|
||||
self.user = User.objects.create(
|
||||
username="User_1",
|
||||
)
|
||||
|
||||
@ -480,6 +483,39 @@ class EmailInteractionsTestCase(TestCase):
|
||||
self.assertTrue(ticket_cc.ticket, ticket)
|
||||
self.assertTrue(ticket_cc.email, cc_email)
|
||||
|
||||
def test_create_ticket_from_email_with_cc_duplicate_users_same_email(self):
|
||||
"""
|
||||
Verify that the duplicated email warning log is invoked
|
||||
"""
|
||||
dup_email = "dup@helpdesk.test"
|
||||
user_dup1 = User.objects.create(
|
||||
username="User_dup1", email=dup_email,
|
||||
)
|
||||
user_dup2 = User.objects.create(
|
||||
username="User_dup2", email=dup_email,
|
||||
)
|
||||
message, _, _ = utils.generate_text_email(locale="fr_FR")
|
||||
|
||||
submitter_email = "foo@bar.py"
|
||||
cc_list = [dup_email, "alpha@acme.test", "beta@acme.test", "gamma@delta.test"]
|
||||
message_id = uuid.uuid4().hex
|
||||
|
||||
message["Message-ID"] = message_id
|
||||
message["Cc"] = ",".join(cc_list)
|
||||
|
||||
with self.assertLogs('helpdesk', level='WARNING') as dup_warn:
|
||||
extract_email_metadata(str(message), self.queue_public, logger=logger)
|
||||
self.assertTrue(MULTIPLE_USERS_SAME_EMAIL_MSG in dup_warn.output[0], "The duplicated email across user ID's was was not logged.")
|
||||
# Check that the CC duplicate was still sent a notification email
|
||||
email_count = len(mail.outbox)
|
||||
found = False
|
||||
for i in range(0, email_count-1):
|
||||
if dup_email in mail.outbox[i].to:
|
||||
found = True
|
||||
break
|
||||
self.assertTrue(found, "The duplicated email across user ID's was not sent a n otification.")
|
||||
|
||||
|
||||
def test_create_followup_from_email_with_valid_message_id_with_no_initial_cc_list(
|
||||
self,
|
||||
):
|
||||
|
Loading…
x
Reference in New Issue
Block a user