replace HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE in homepage view and test behaviour

This commit is contained in:
Stefano Brentegani 2014-07-29 22:03:47 +02:00
parent 581e43a3b0
commit 82df965d5f
3 changed files with 91 additions and 6 deletions

View File

@ -31,3 +31,21 @@ def reload_urlconf(urlconf=None):
reload(sys.modules[urlconf]) reload(sys.modules[urlconf])
clear_url_caches() 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()

View File

@ -4,7 +4,7 @@ from django.core.urlresolvers import reverse
from django.test import TestCase from django.test import TestCase
from helpdesk import settings 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): class KBDisabledTestCase(TestCase):
@ -120,3 +120,70 @@ class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase):
self.client.login(username=user.username, password='frog') self.client.login(username=user.username, password='frog')
response = self.client.get(reverse('helpdesk_dashboard'), follow=True) response = self.client.get(reverse('helpdesk_dashboard'), follow=True)
self.assertTemplateUsed(response, 'helpdesk/registration/login.html') 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')

View File

@ -14,6 +14,7 @@ from django.template import loader, Context, RequestContext
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from helpdesk import settings as helpdesk_settings from helpdesk import settings as helpdesk_settings
from helpdesk.decorators import is_helpdesk_staff
from helpdesk.forms import PublicTicketForm from helpdesk.forms import PublicTicketForm
from helpdesk.lib import send_templated_mail, text_is_spam from helpdesk.lib import send_templated_mail, text_is_spam
from helpdesk.models import Ticket, Queue, UserSettings, KBCategory from helpdesk.models import Ticket, Queue, UserSettings, KBCategory
@ -23,13 +24,12 @@ def homepage(request):
if not request.user.is_authenticated() and helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT: if not request.user.is_authenticated() and helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT:
return HttpResponseRedirect(reverse('login')) 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: 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')) return HttpResponseRedirect(reverse('helpdesk_list'))
else:
return HttpResponseRedirect(reverse('helpdesk_dashboard'))
except UserSettings.DoesNotExist: except UserSettings.DoesNotExist:
pass
return HttpResponseRedirect(reverse('helpdesk_dashboard')) return HttpResponseRedirect(reverse('helpdesk_dashboard'))
if request.method == 'POST': if request.method == 'POST':