diff --git a/helpdesk/tests/helpers.py b/helpdesk/tests/helpers.py index 6dccad08..ba73e990 100644 --- a/helpdesk/tests/helpers.py +++ b/helpdesk/tests/helpers.py @@ -31,3 +31,21 @@ def reload_urlconf(urlconf=None): reload(sys.modules[urlconf]) clear_url_caches() + + +def update_user_settings(user, **kwargs): + usersettings = user.usersettings + settings = usersettings.settings + settings.update(kwargs) + usersettings.settings = settings + usersettings.save() + + +def delete_user_settings(user, *args): + usersettings = user.usersettings + settings = usersettings.settings + for setting in args: + if setting in settings: + del settings[setting] + usersettings.settings = settings + usersettings.save() diff --git a/helpdesk/tests/navigation.py b/helpdesk/tests/navigation.py index 32bbdb5c..878d38b3 100644 --- a/helpdesk/tests/navigation.py +++ b/helpdesk/tests/navigation.py @@ -4,7 +4,7 @@ from django.core.urlresolvers import reverse from django.test import TestCase from helpdesk import settings -from helpdesk.tests.helpers import get_staff_user, reload_urlconf, User +from helpdesk.tests.helpers import get_staff_user, reload_urlconf, User, update_user_settings, delete_user_settings class KBDisabledTestCase(TestCase): @@ -120,3 +120,70 @@ class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase): self.client.login(username=user.username, password='frog') response = self.client.get(reverse('helpdesk_dashboard'), follow=True) self.assertTemplateUsed(response, 'helpdesk/registration/login.html') + + +class HomePageAnonymousUserTest(TestCase): + def setUp(self): + self.redirect_to_login = settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT + + def tearDown(self): + settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = self.redirect_to_login + + def test_homepage(self): + settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = True + response = self.client.get(reverse('helpdesk_home')) + self.assertTemplateUsed('helpdesk/public_homepage.html') + + def test_redirect_to_login(self): + """Unauthenticated users are redirected to the login page if HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT is True""" + settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = True + response = self.client.get(reverse('helpdesk_home')) + self.assertRedirects(response, reverse('login')) + + +class HomePageTest(TestCase): + def setUp(self): + self.previous = settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE, settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK + settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False + settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK = None + try: + reload(sys.modules['helpdesk.views.public']) + except KeyError: + pass + + def tearDown(self): + settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE, settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK = self.previous + reload(sys.modules['helpdesk.views.public']) + + def assertUserRedirectedToView(self, user, view_name): + self.client.login(username=user.username, password='password') + response = self.client.get(reverse('helpdesk_home')) + self.assertRedirects(response, reverse(view_name)) + self.client.logout() + + def test_redirect_to_dashboard(self): + """Authenticated users are redirected to the dashboard""" + user = get_staff_user() + + # login_view_ticketlist is False... + update_user_settings(user, login_view_ticketlist=False) + self.assertUserRedirectedToView(user, 'helpdesk_dashboard') + + # ... or missing + delete_user_settings(user, 'login_view_ticketlist') + self.assertUserRedirectedToView(user, 'helpdesk_dashboard') + + def test_no_user_settings_redirect_to_dashboard(self): + """Authenticated users are redirected to the dashboard if user settings are missing""" + from helpdesk.models import UserSettings + user = get_staff_user() + + UserSettings.objects.filter(user=user).delete() + self.assertUserRedirectedToView(user, 'helpdesk_dashboard') + + def test_redirect_to_ticket_list(self): + """Authenticated users are redirected to the ticket list based on their user settings""" + user = get_staff_user() + update_user_settings(user, login_view_ticketlist=True) + + self.assertUserRedirectedToView(user, 'helpdesk_list') diff --git a/helpdesk/views/public.py b/helpdesk/views/public.py index e164f059..e65f9f96 100644 --- a/helpdesk/views/public.py +++ b/helpdesk/views/public.py @@ -14,6 +14,7 @@ from django.template import loader, Context, RequestContext from django.utils.translation import ugettext as _ from helpdesk import settings as helpdesk_settings +from helpdesk.decorators import is_helpdesk_staff from helpdesk.forms import PublicTicketForm from helpdesk.lib import send_templated_mail, text_is_spam from helpdesk.models import Ticket, Queue, UserSettings, KBCategory @@ -23,14 +24,13 @@ def homepage(request): if not request.user.is_authenticated() and helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT: return HttpResponseRedirect(reverse('login')) - if (request.user.is_staff or (request.user.is_authenticated() and helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE)): + if is_helpdesk_staff(request.user): try: - if getattr(request.user.usersettings.settings, 'login_view_ticketlist', False): + if request.user.usersettings.settings.get('login_view_ticketlist', False): return HttpResponseRedirect(reverse('helpdesk_list')) - else: - return HttpResponseRedirect(reverse('helpdesk_dashboard')) except UserSettings.DoesNotExist: - return HttpResponseRedirect(reverse('helpdesk_dashboard')) + pass + return HttpResponseRedirect(reverse('helpdesk_dashboard')) if request.method == 'POST': form = PublicTicketForm(request.POST, request.FILES)