diff --git a/helpdesk/forms.py b/helpdesk/forms.py index b196da90..bcb01eea 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -317,7 +317,7 @@ class TicketForm(AbstractTicketForm): self.fields['assigned_to'].choices = [('', '--------')] + [(u.id, u.get_username()) for u in assignable_users] self._add_form_custom_fields() - def save(self, user=None): + def save(self, user): """ Writes and returns a Ticket() object """ @@ -412,7 +412,7 @@ class PublicTicketForm(AbstractTicketForm): # get the queue user entered return Queue.objects.get(id=int(self.cleaned_data['queue'])) - def save(self): + def save(self, user): """ Writes and returns a Ticket() object """ @@ -423,7 +423,8 @@ class PublicTicketForm(AbstractTicketForm): self._create_custom_fields(ticket) - followup = self._create_follow_up(ticket, title=_('Ticket Opened Via Web')) + followup = self._create_follow_up( + ticket, title=_('Ticket Opened Via Web'), user=user) followup.save() files = self._attach_files_to_follow_up(followup) diff --git a/helpdesk/tests/test_ticket_submission.py b/helpdesk/tests/test_ticket_submission.py index 41efcca3..55674df5 100644 --- a/helpdesk/tests/test_ticket_submission.py +++ b/helpdesk/tests/test_ticket_submission.py @@ -4,6 +4,7 @@ import uuid from helpdesk.models import Queue, CustomField, FollowUp, Ticket, TicketCC, KBCategory, KBItem from django.test import TestCase +from django.contrib.auth import get_user_model from django.core import mail from django.core.exceptions import ObjectDoesNotExist from django.forms import ValidationError @@ -43,6 +44,10 @@ class TicketBasicsTestCase(TestCase): 'description': 'Some Test Ticket', } + self.user = get_user_model().objects.create( + username='User_1', + ) + self.client = Client() def test_create_ticket_instance_from_payload(self): @@ -85,6 +90,45 @@ class TicketBasicsTestCase(TestCase): # Ensure submitter, new-queue + update-queue were all emailed. self.assertEqual(email_count + 3, len(mail.outbox)) + ticket = Ticket.objects.last() + self.assertEqual(ticket.followup_set.count(), 1) + # Follow up is anonymous + self.assertIsNone(ticket.followup_set.first().user) + + def test_create_ticket_authorized(self): + email_count = len(mail.outbox) + self.client.force_login(self.user) + + response = self.client.get(reverse('helpdesk:home')) + self.assertEqual(response.status_code, 200) + + post_data = { + 'title': 'Test ticket title', + 'queue': self.queue_public.id, + 'submitter_email': 'ticket1.submitter@example.com', + 'body': 'Test ticket body', + 'priority': 3, + } + + response = self.client.post(reverse('helpdesk:home'), post_data, follow=True) + last_redirect = response.redirect_chain[-1] + last_redirect_url = last_redirect[0] + # last_redirect_status = last_redirect[1] + + # Ensure we landed on the "View" page. + # Django 1.9 compatible way of testing this + # https://docs.djangoproject.com/en/1.9/releases/1.9/#http-redirects-no-longer-forced-to-absolute-uris + urlparts = urlparse(last_redirect_url) + self.assertEqual(urlparts.path, reverse('helpdesk:public_view')) + + # Ensure submitter, new-queue + update-queue were all emailed. + self.assertEqual(email_count + 3, len(mail.outbox)) + + ticket = Ticket.objects.last() + self.assertEqual(ticket.followup_set.count(), 1) + # Follow up is for registered user + self.assertEqual(ticket.followup_set.first().user, self.user) + def test_create_ticket_private(self): email_count = len(mail.outbox) post_data = { diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 7518d468..0957e4e8 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1046,7 +1046,8 @@ class CreateTicketView(MustBeStaffMixin, abstract_views.AbstractCreateTicketMixi return kwargs def form_valid(self, form): - self.ticket = form.save() + self.ticket = form.save( + user=self.request.user if self.request.user.is_authenticated else None) return super().form_valid(form) def get_success_url(self):