Repository: django-helpdesk/django-helpdesk

Issue:
Error in testsuite: RelatedObjectDoesNotExist: User has no queuemembership.
This is probably for @reduxionist.

Testing helpdesk via `quicktest.py` passes all tests.

However running the testsuite via 
`python manage.py test helpdesk`
gives me a weird error (trace is below) probably related to the staff per queue membership feature.

This is raised in `test_navigation` around `82: queue__in=request.user.queuemembership.queues.all()`. I tried fiddling with the test settings and enabling/disabling `HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP`, to no avail. 

Since this does NOT occur when testing via `quicktest.py` I am bit puzzled - has to do something with the test environments probably. Maybe somebody has some ideas.

---

``` python
Traceback (most recent call last):
  File "django-helpdesk/helpdesk/tests/test_navigation.py", line 31, in test_navigation
    response = self.client.get(reverse('helpdesk_dashboard'))
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/test/client.py", line 500, in get
    **extra)
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/test/client.py", line 303, in get
    return self.generic('GET', path, secure=secure, **r)
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/test/client.py", line 379, in generic
    return self.request(**r)
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/test/client.py", line 466, in request
    six.reraise(*exc_info)
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "django-helpdesk/helpdesk/views/staff.py", line 82, in dashboard
    queue__in=request.user.queuemembership.queues.all(),
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/utils/functional.py", line 226, in inner
    return func(self._wrapped, *args)
  File "/Users/username/.virtualenvs/test_dj18/lib/python2.7/site-packages/django/db/models/fields/related.py", line 470, in __get__
    self.related.get_accessor_name()
RelatedObjectDoesNotExist: User has no queuemembership.
```



Discussion:
@flinz Thanks for letting me know, I'm looking into it right now, and have a strong suspicion as to what the cause is, so there's no need for you to investigate any further.  :) 


Resolving patch:
diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py
--- a/helpdesk/views/staff.py
+++ b/helpdesk/views/staff.py
@@ -39,7 +39,7 @@
 
 from helpdesk.forms import TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, EditFollowUpForm, TicketDependencyForm
 from helpdesk.lib import send_templated_mail, query_to_dict, apply_query, safe_template_context
-from helpdesk.models import Ticket, Queue, FollowUp, TicketChange, PreSetReply, Attachment, SavedSearch, IgnoreEmail, TicketCC, TicketDependency
+from helpdesk.models import Ticket, Queue, FollowUp, TicketChange, PreSetReply, Attachment, SavedSearch, IgnoreEmail, TicketCC, TicketDependency, QueueMembership
 from helpdesk import settings as helpdesk_settings
 
 if helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
@@ -78,9 +78,12 @@ def dashboard(request):
         )
     limit_queues_by_user = helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP and not request.user.is_superuser
     if limit_queues_by_user:
-        unassigned_tickets = unassigned_tickets.filter(
-            queue__in=request.user.queuemembership.queues.all(),
-        )
+        try:
+            unassigned_tickets = unassigned_tickets.filter(
+                queue__in=request.user.queuemembership.queues.all(),
+            )
+        except QueueMembership.DoesNotExist:
+           unassigned_tickets = unassigned_tickets.none()
 
     # all tickets, reported by current user
     all_tickets_reported_by_current_user = ''
@@ -92,9 +95,12 @@ def dashboard(request):
 
     Tickets = Ticket.objects
     if limit_queues_by_user:
-        Tickets = Tickets.filter(
-            queue__in=request.user.queuemembership.queues.all(),
-        )
+        try:
+            Tickets = Tickets.filter(
+                queue__in=request.user.queuemembership.queues.all(),
+            )
+        except QueueMembership.DoesNotExist:
+           Tickets = Tickets.none()
     basic_ticket_stats = calc_basic_ticket_stats(Tickets)
 
     # The following query builds a grid of queues & ticket statuses,
@@ -367,7 +373,7 @@ def update_ticket(request, ticket_id, public=False):
     # if comment contains some django code, like "why does {% if bla %} crash",
     # then the following line will give us a crash, since django expects {% if %}
     # to be closed with an {% endif %} tag.
-    
+
 
     # get_template_from_string was removed in Django 1.8 http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
     try:
@@ -1001,9 +1007,12 @@ def run_report(request, report):
     report_queryset = Ticket.objects.all().select_related()
     limit_queues_by_user = helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP and not request.user.is_superuser
     if limit_queues_by_user:
-        report_queryset = report_queryset.filter(
-            queue__in=request.user.queuemembership.queues.all(),
-        )
+        try:
+            report_queryset = report_queryset.filter(
+                queue__in=request.user.queuemembership.queues.all(),
+            )
+        except QueueMembership.DoesNotExist:
+            report_queryset = report_queryset.none()
 
     from_saved_query = False
     saved_query = None
@@ -1065,9 +1074,12 @@ def run_report(request, report):
         col1heading = _('User')
         queue_options = Queue.objects.all()
         if limit_queues_by_user:
-            queue_options  = queue_options.filter(
-                pk__in=request.user.queuemembership.queues.all(),
-            )
+            try:
+                queue_options = queue_options.filter(
+                    pk__in=request.user.queuemembership.queues.all(),
+                )
+            except QueueMembership.DoesNotExist:
+                queue_options = queue_options.none()
         possible_options = [q.title.encode('utf-8') for q in queue_options]
         charttype = 'bar'
 


Test patch:
diff --git a/helpdesk/tests/test_per_queue_staff_membership.py b/helpdesk/tests/test_per_queue_staff_membership.py
--- a/helpdesk/tests/test_per_queue_staff_membership.py
+++ b/helpdesk/tests/test_per_queue_staff_membership.py
@@ -17,6 +17,7 @@ def setUp(self):
         and    user_2 with access to queue_2 containing 2 tickets
         and superuser who should be able to access both queues
         """
+        self.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP
         settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = True
         self.client = Client()
         User = get_user_model()
@@ -59,6 +60,12 @@ def setUp(self):
                     assigned_to=user,
                 )
 
+    def tearDown(self):
+        """
+        Reset HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP to original value
+        """
+        settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = self.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP
+
     def test_dashboard_ticket_counts(self):
         """
         Check that the regular users' dashboard only shows 1 of the 2 queues,
@@ -213,9 +220,3 @@ def test_ticket_reports_per_queue_user_restrictions(self):
             3,
             'Queue choices were improperly limited by queue membership for a superuser'
         )
-
-    def tearDown(self):
-        """
-        Don't interfere with subsequent tests that do not expect this setting
-        """
-        settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = False