2014-07-21 18:44:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-07-22 03:26:41 +02:00
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.utils import override_settings
|
|
|
|
from django.urls import reverse
|
2017-10-30 10:07:44 +01:00
|
|
|
from helpdesk import settings as helpdesk_settings
|
2018-08-21 16:23:25 +02:00
|
|
|
from helpdesk.models import Queue
|
2023-11-14 20:07:25 +01:00
|
|
|
from helpdesk.tests.helpers import create_ticket, get_staff_user, reload_urlconf, User
|
2022-07-22 03:26:41 +02:00
|
|
|
from importlib import reload
|
|
|
|
import sys
|
2014-07-21 18:44:54 +02:00
|
|
|
|
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
class KBDisabledTestCase(TestCase):
|
2014-07-21 18:44:54 +02:00
|
|
|
def setUp(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
self.HELPDESK_KB_ENABLED = helpdesk_settings.HELPDESK_KB_ENABLED
|
2014-07-21 18:44:54 +02:00
|
|
|
if self.HELPDESK_KB_ENABLED:
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_KB_ENABLED = False
|
2014-07-21 18:44:54 +02:00
|
|
|
reload_urlconf()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self.HELPDESK_KB_ENABLED:
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_KB_ENABLED = True
|
2014-07-21 18:44:54 +02:00
|
|
|
reload_urlconf()
|
|
|
|
|
|
|
|
def test_navigation(self):
|
|
|
|
"""Test proper rendering of navigation.html by accessing the dashboard"""
|
2017-12-28 15:11:34 +01:00
|
|
|
from django.urls import NoReverseMatch
|
2014-07-21 18:44:54 +02:00
|
|
|
|
2022-07-12 12:34:19 +02:00
|
|
|
self.client.login(username=get_staff_user(
|
|
|
|
).get_username(), password='password')
|
2015-12-22 11:20:52 +01:00
|
|
|
self.assertRaises(NoReverseMatch, reverse, 'helpdesk:kb_index')
|
2014-07-21 18:44:54 +02:00
|
|
|
try:
|
2015-12-22 10:10:00 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'))
|
2015-11-18 10:41:02 +01:00
|
|
|
except NoReverseMatch as e:
|
2015-12-22 11:20:52 +01:00
|
|
|
if 'helpdesk:kb_index' in e.message:
|
2022-07-12 12:34:19 +02:00
|
|
|
self.fail(
|
|
|
|
"Please verify any unchecked references to helpdesk_kb_index (start with navigation.html)")
|
2014-07-21 18:44:54 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2014-07-28 06:47:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StaffUserTestCaseMixin(object):
|
|
|
|
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
self.original_setting = helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE
|
|
|
|
helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = self.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE
|
2014-07-28 06:47:19 +02:00
|
|
|
self.reload_views()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = self.original_setting
|
2014-07-28 06:47:19 +02:00
|
|
|
self.reload_views()
|
|
|
|
|
|
|
|
def reload_views(self):
|
|
|
|
try:
|
2014-07-28 11:46:02 +02:00
|
|
|
reload(sys.modules['helpdesk.decorators'])
|
2014-07-28 06:47:19 +02:00
|
|
|
reload(sys.modules['helpdesk.views.staff'])
|
|
|
|
reload_urlconf()
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_anonymous_user(self):
|
|
|
|
"""Access to the dashboard always requires a login"""
|
2017-10-30 10:07:44 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'), follow=True)
|
2014-07-29 20:55:25 +02:00
|
|
|
self.assertTemplateUsed(response, 'helpdesk/registration/login.html')
|
2014-07-28 06:47:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NonStaffUsersAllowedTestCase(StaffUserTestCaseMixin, TestCase):
|
|
|
|
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = True
|
|
|
|
|
|
|
|
def test_non_staff_allowed(self):
|
|
|
|
"""If HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is True,
|
|
|
|
authenticated, non-staff users should be able to access
|
|
|
|
the dashboard.
|
|
|
|
"""
|
2014-07-29 22:41:09 +02:00
|
|
|
from helpdesk.decorators import is_helpdesk_staff
|
|
|
|
|
2022-07-12 12:34:19 +02:00
|
|
|
user = User.objects.create_user(
|
|
|
|
username='henry.wensleydale', password='gouda', email='wensleydale@example.com')
|
2014-07-28 06:47:19 +02:00
|
|
|
|
2014-07-29 22:41:09 +02:00
|
|
|
self.assertTrue(is_helpdesk_staff(user))
|
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
self.client.login(username=user.username, password='gouda')
|
2017-10-30 10:07:44 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'), follow=True)
|
2014-07-28 06:47:19 +02:00
|
|
|
self.assertTemplateUsed(response, 'helpdesk/dashboard.html')
|
|
|
|
|
|
|
|
|
|
|
|
class StaffUsersOnlyTestCase(StaffUserTestCaseMixin, TestCase):
|
|
|
|
# Use default values
|
|
|
|
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False
|
|
|
|
|
2018-08-21 16:23:25 +02:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2022-06-20 17:36:32 +02:00
|
|
|
self.non_staff_user_password = "gouda"
|
2022-07-12 12:34:19 +02:00
|
|
|
self.non_staff_user = User.objects.create_user(
|
|
|
|
username='henry.wensleydale', password=self.non_staff_user_password, email='wensleydale@example.com')
|
2014-07-29 22:41:09 +02:00
|
|
|
|
2018-08-21 16:23:25 +02:00
|
|
|
def test_staff_user_detection(self):
|
|
|
|
"""Staff and non-staff users are correctly identified"""
|
|
|
|
from helpdesk.decorators import is_helpdesk_staff
|
2014-07-29 22:41:09 +02:00
|
|
|
|
2018-08-21 16:23:25 +02:00
|
|
|
self.assertFalse(is_helpdesk_staff(self.non_staff_user))
|
|
|
|
self.assertTrue(is_helpdesk_staff(get_staff_user()))
|
2014-07-29 22:41:09 +02:00
|
|
|
|
2018-08-21 16:23:25 +02:00
|
|
|
def test_staff_can_access_dashboard(self):
|
|
|
|
"""When HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is False,
|
|
|
|
staff users should be able to access the dashboard.
|
2014-07-28 06:47:19 +02:00
|
|
|
"""
|
2014-07-29 22:41:09 +02:00
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
user = get_staff_user()
|
|
|
|
self.client.login(username=user.username, password='password')
|
2017-10-30 10:07:44 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'), follow=True)
|
2014-07-28 06:47:19 +02:00
|
|
|
self.assertTemplateUsed(response, 'helpdesk/dashboard.html')
|
|
|
|
|
2018-08-21 16:23:25 +02:00
|
|
|
def test_non_staff_cannot_access_dashboard(self):
|
|
|
|
"""When HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is False,
|
|
|
|
non-staff users should not be able to access the dashboard.
|
|
|
|
"""
|
|
|
|
user = self.non_staff_user
|
2022-07-12 12:34:19 +02:00
|
|
|
self.client.login(username=user.username,
|
|
|
|
password=self.non_staff_user_password)
|
2018-08-21 16:23:25 +02:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'), follow=True)
|
|
|
|
self.assertTemplateUsed(response, 'helpdesk/registration/login.html')
|
|
|
|
|
|
|
|
def test_staff_rss(self):
|
|
|
|
"""If HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is False,
|
|
|
|
staff users should be able to access rss feeds.
|
|
|
|
"""
|
|
|
|
user = get_staff_user()
|
2022-06-20 17:36:32 +02:00
|
|
|
self.client.login(username=user.username, password="password")
|
2022-07-12 12:34:19 +02:00
|
|
|
response = self.client.get(
|
|
|
|
reverse('helpdesk:rss_unassigned'), follow=True)
|
2018-08-21 16:23:25 +02:00
|
|
|
self.assertContains(response, 'Unassigned Open and Reopened tickets')
|
|
|
|
|
2022-06-20 17:36:32 +02:00
|
|
|
@override_settings(HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE=False)
|
2018-08-21 16:23:25 +02:00
|
|
|
def test_non_staff_cannot_rss(self):
|
|
|
|
"""If HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is False,
|
|
|
|
non-staff users should not be able to access rss feeds.
|
|
|
|
"""
|
|
|
|
user = self.non_staff_user
|
2022-07-12 12:34:19 +02:00
|
|
|
self.client.login(username=user.username,
|
|
|
|
password=self.non_staff_user_password)
|
2018-08-21 16:23:25 +02:00
|
|
|
queue = Queue.objects.create(
|
|
|
|
title="Foo",
|
|
|
|
slug="test_queue",
|
|
|
|
)
|
|
|
|
rss_urls = [
|
|
|
|
reverse('helpdesk:rss_user', args=[user.username]),
|
2022-07-12 12:34:19 +02:00
|
|
|
reverse('helpdesk:rss_user_queue', args=[
|
|
|
|
user.username, 'test_queue']),
|
2018-08-21 16:23:25 +02:00
|
|
|
reverse('helpdesk:rss_queue', args=['test_queue']),
|
|
|
|
reverse('helpdesk:rss_unassigned'),
|
|
|
|
reverse('helpdesk:rss_activity'),
|
|
|
|
]
|
|
|
|
for rss_url in rss_urls:
|
|
|
|
response = self.client.get(rss_url, follow=True)
|
2022-07-12 12:34:19 +02:00
|
|
|
self.assertTemplateUsed(
|
|
|
|
response, 'helpdesk/registration/login.html')
|
2018-08-21 16:23:25 +02:00
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
|
|
|
|
class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase):
|
|
|
|
@staticmethod
|
2014-08-01 07:20:43 +02:00
|
|
|
def custom_staff_filter(user):
|
2014-07-28 06:47:19 +02:00
|
|
|
"""Arbitrary user validation function"""
|
2018-03-04 10:24:30 +01:00
|
|
|
return user.is_authenticated and user.is_active and user.username.lower().endswith('wensleydale')
|
2014-07-28 06:47:19 +02:00
|
|
|
|
2014-08-01 07:20:43 +02:00
|
|
|
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = custom_staff_filter
|
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
def test_custom_staff_pass(self):
|
2014-08-01 07:20:43 +02:00
|
|
|
"""If HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is callable,
|
2014-07-28 06:47:19 +02:00
|
|
|
a custom access rule is applied.
|
|
|
|
"""
|
2014-07-29 22:41:09 +02:00
|
|
|
from helpdesk.decorators import is_helpdesk_staff
|
|
|
|
|
2022-07-12 12:34:19 +02:00
|
|
|
user = User.objects.create_user(
|
|
|
|
username='henry.wensleydale', password='gouda', email='wensleydale@example.com')
|
2014-07-28 06:47:19 +02:00
|
|
|
|
2014-07-29 22:41:09 +02:00
|
|
|
self.assertTrue(is_helpdesk_staff(user))
|
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
self.client.login(username=user.username, password='gouda')
|
2017-10-30 10:07:44 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'), follow=True)
|
2014-07-28 06:47:19 +02:00
|
|
|
self.assertTemplateUsed(response, 'helpdesk/dashboard.html')
|
|
|
|
|
|
|
|
def test_custom_staff_fail(self):
|
2014-07-29 22:41:09 +02:00
|
|
|
from helpdesk.decorators import is_helpdesk_staff
|
|
|
|
|
2022-07-12 12:34:19 +02:00
|
|
|
user = User.objects.create_user(
|
|
|
|
username='terry.milton', password='frog', email='milton@example.com')
|
2014-07-28 06:47:19 +02:00
|
|
|
|
2014-07-29 22:41:09 +02:00
|
|
|
self.assertFalse(is_helpdesk_staff(user))
|
|
|
|
|
2014-07-28 06:47:19 +02:00
|
|
|
self.client.login(username=user.username, password='frog')
|
2017-10-30 10:07:44 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:dashboard'), follow=True)
|
2014-07-29 20:55:25 +02:00
|
|
|
self.assertTemplateUsed(response, 'helpdesk/registration/login.html')
|
2014-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
|
2014-07-30 06:07:08 +02:00
|
|
|
class HomePageAnonymousUserTestCase(TestCase):
|
2014-07-29 22:03:47 +02:00
|
|
|
def setUp(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
self.redirect_to_login = helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT
|
2014-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = self.redirect_to_login
|
2014-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
def test_homepage(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = True
|
|
|
|
response = self.client.get(reverse('helpdesk:home'))
|
2014-07-29 22:03:47 +02:00
|
|
|
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"""
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = True
|
|
|
|
response = self.client.get(reverse('helpdesk:home'))
|
|
|
|
self.assertRedirects(response, reverse('helpdesk:login'))
|
2014-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
|
2014-07-30 06:07:08 +02:00
|
|
|
class HomePageTestCase(TestCase):
|
2014-07-29 22:03:47 +02:00
|
|
|
def setUp(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
self.original_setting = helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE
|
|
|
|
helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False
|
2014-07-29 22:03:47 +02:00
|
|
|
try:
|
|
|
|
reload(sys.modules['helpdesk.views.public'])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
2017-10-30 10:07:44 +01:00
|
|
|
helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = self.original_setting
|
2014-07-29 22:03:47 +02:00
|
|
|
reload(sys.modules['helpdesk.views.public'])
|
|
|
|
|
|
|
|
def assertUserRedirectedToView(self, user, view_name):
|
|
|
|
self.client.login(username=user.username, password='password')
|
2017-10-30 10:07:44 +01:00
|
|
|
response = self.client.get(reverse('helpdesk:home'))
|
2014-07-29 22:03:47 +02:00
|
|
|
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...
|
2018-10-05 14:54:22 +02:00
|
|
|
user.usersettings_helpdesk.login_view_ticketlist = False
|
|
|
|
user.usersettings_helpdesk.save()
|
2017-10-30 10:07:44 +01:00
|
|
|
self.assertUserRedirectedToView(user, 'helpdesk:dashboard')
|
2014-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
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()
|
2017-10-30 10:07:44 +01:00
|
|
|
self.assertUserRedirectedToView(user, 'helpdesk:dashboard')
|
2014-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
def test_redirect_to_ticket_list(self):
|
|
|
|
"""Authenticated users are redirected to the ticket list based on their user settings"""
|
|
|
|
user = get_staff_user()
|
2018-10-05 14:54:22 +02:00
|
|
|
user.usersettings_helpdesk.login_view_ticketlist = True
|
|
|
|
user.usersettings_helpdesk.save()
|
2014-07-29 22:03:47 +02:00
|
|
|
|
2017-10-30 10:07:44 +01:00
|
|
|
self.assertUserRedirectedToView(user, 'helpdesk:list')
|
2014-07-30 06:07:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ReturnToTicketTestCase(TestCase):
|
|
|
|
def test_staff_user(self):
|
|
|
|
from helpdesk.views.staff import return_to_ticket
|
|
|
|
|
|
|
|
user = get_staff_user()
|
|
|
|
ticket = create_ticket()
|
2017-10-30 10:07:44 +01:00
|
|
|
response = return_to_ticket(user, helpdesk_settings, ticket)
|
2014-07-30 06:07:08 +02:00
|
|
|
self.assertEqual(response['location'], ticket.get_absolute_url())
|
|
|
|
|
|
|
|
def test_non_staff_user(self):
|
|
|
|
from helpdesk.views.staff import return_to_ticket
|
|
|
|
|
2022-07-12 12:34:19 +02:00
|
|
|
user = User.objects.create_user(
|
|
|
|
username='henry.wensleydale', password='gouda', email='wensleydale@example.com')
|
2014-07-30 06:07:08 +02:00
|
|
|
ticket = create_ticket()
|
2017-10-30 10:07:44 +01:00
|
|
|
response = return_to_ticket(user, helpdesk_settings, ticket)
|
2014-07-30 06:07:08 +02:00
|
|
|
self.assertEqual(response['location'], ticket.ticket_url)
|