From fb3b02459209cb840586a290939c1be0920f1fa6 Mon Sep 17 00:00:00 2001 From: Daryl Date: Thu, 30 Jun 2016 10:31:46 +1200 Subject: [PATCH 1/4] Add test for saving a query, fix shared checkbox issue My browser sends 'on' when a checkbox is ticked, django ORM only recognises '1', 'true' or 't' as valid 'True' responses. This throws an error ValidationError at /helpdesk/save_query/ [u"'on' value must be either True or False."] This could be fixed with " value='1'" in the template, but testing that is harder My fix is to add a check in the view. 2 more lines, but easier to unittest. Core devs need to make a call as to which solution is best. D --- helpdesk/tests/test_savequery.py | 27 +++++++++++++++++++++++++++ helpdesk/views/staff.py | 2 ++ 2 files changed, 29 insertions(+) create mode 100644 helpdesk/tests/test_savequery.py diff --git a/helpdesk/tests/test_savequery.py b/helpdesk/tests/test_savequery.py new file mode 100644 index 00000000..6412c02a --- /dev/null +++ b/helpdesk/tests/test_savequery.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +from django.core.urlresolvers import reverse +from django.test import TestCase +from helpdesk.models import Ticket, Queue +from helpdesk.tests.helpers import get_staff_user, reload_urlconf + +class TestKBDisabled(TestCase): + def setUp(self): + q = Queue(title='Q1', slug='q1') + q.save() + self.q = q + + def test_cansavequery(self): + """Can a query be saved""" + # get the ticket from models + url = reverse('helpdesk_savequery') + self.client.login(username=get_staff_user().get_username(), + password='password') + response = self.client.post(url, + data={'title': 'ticket on my queue', + 'queue':self.q, + 'shared':'on', + 'query_encoded':'KGRwMApWZmlsdGVyaW5nCnAxCihkcDIKVnN0YXR1c19faW4KcDMKKGxwNApJMQphSTIKYUkzCmFzc1Zzb3J0aW5nCnA1ClZjcmVhdGVkCnA2CnMu'}) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, '/helpdesk/tickets/?saved_query=1') + + diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 0a652ebb..7d86c420 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1213,6 +1213,8 @@ run_report = staff_member_required(run_report) def save_query(request): title = request.POST.get('title', None) shared = request.POST.get('shared', False) + if shared == 'on': # django only translates '1', 'true', 't' into True + shared = True query_encoded = request.POST.get('query_encoded', None) if not title or not query_encoded: From a842d1a9c3b6fbc096c6ccbaa60d43b26b1dbe14 Mon Sep 17 00:00:00 2001 From: Daryl Date: Thu, 30 Jun 2016 10:42:05 +1200 Subject: [PATCH 2/4] Error in class name Copied a test, forgot to change the test class name --- helpdesk/tests/test_savequery.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/helpdesk/tests/test_savequery.py b/helpdesk/tests/test_savequery.py index 6412c02a..416fd4d0 100644 --- a/helpdesk/tests/test_savequery.py +++ b/helpdesk/tests/test_savequery.py @@ -4,7 +4,7 @@ from django.test import TestCase from helpdesk.models import Ticket, Queue from helpdesk.tests.helpers import get_staff_user, reload_urlconf -class TestKBDisabled(TestCase): +class TestSavingSharedQuery(TestCase): def setUp(self): q = Queue(title='Q1', slug='q1') q.save() @@ -12,7 +12,6 @@ class TestKBDisabled(TestCase): def test_cansavequery(self): """Can a query be saved""" - # get the ticket from models url = reverse('helpdesk_savequery') self.client.login(username=get_staff_user().get_username(), password='password') From 3ffe505f290b23eee68d7ecd5398abead3ec7bf6 Mon Sep 17 00:00:00 2001 From: Daryl Date: Thu, 30 Jun 2016 11:29:35 +1200 Subject: [PATCH 3/4] Wrong method to test the url Forgot that Travis urls have the 'http://testserver' part at the front --- helpdesk/tests/test_savequery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpdesk/tests/test_savequery.py b/helpdesk/tests/test_savequery.py index 416fd4d0..2e0786b1 100644 --- a/helpdesk/tests/test_savequery.py +++ b/helpdesk/tests/test_savequery.py @@ -21,6 +21,6 @@ class TestSavingSharedQuery(TestCase): 'shared':'on', 'query_encoded':'KGRwMApWZmlsdGVyaW5nCnAxCihkcDIKVnN0YXR1c19faW4KcDMKKGxwNApJMQphSTIKYUkzCmFzc1Zzb3J0aW5nCnA1ClZjcmVhdGVkCnA2CnMu'}) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, '/helpdesk/tickets/?saved_query=1') + self.assertTrue('/helpdesk/tickets/?saved_query=1' in response.url) From 0ac7183875bd827078d41ee2d576d48d713f022a Mon Sep 17 00:00:00 2001 From: Daryl Date: Thu, 30 Jun 2016 10:31:46 +1200 Subject: [PATCH 4/4] Add test for saving a query, fix shared checkbox issue My browser sends 'on' when a checkbox is ticked, django ORM only recognises '1', 'true' or 't' as valid 'True' responses. This throws an error ValidationError at /helpdesk/save_query/ [u"'on' value must be either True or False."] This could be fixed with " value='1'" in the template, but testing that is harder My fix is to add a check in the view. 2 more lines, but easier to unittest. Core devs need to make a call as to which solution is best. D --- helpdesk/tests/test_savequery.py | 26 ++++++++++++++++++++++++++ helpdesk/views/staff.py | 2 ++ 2 files changed, 28 insertions(+) create mode 100644 helpdesk/tests/test_savequery.py diff --git a/helpdesk/tests/test_savequery.py b/helpdesk/tests/test_savequery.py new file mode 100644 index 00000000..df95b9e7 --- /dev/null +++ b/helpdesk/tests/test_savequery.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from django.core.urlresolvers import reverse +from django.test import TestCase +from helpdesk.models import Ticket, Queue +from helpdesk.tests.helpers import get_staff_user, reload_urlconf + +class TestSavingSharedQuery(TestCase): + def setUp(self): + q = Queue(title='Q1', slug='q1') + q.save() + self.q = q + + def test_cansavequery(self): + """Can a query be saved""" + url = reverse('helpdesk_savequery') + self.client.login(username=get_staff_user().get_username(), + password='password') + response = self.client.post(url, + data={'title': 'ticket on my queue', + 'queue':self.q, + 'shared':'on', + 'query_encoded':'KGRwMApWZmlsdGVyaW5nCnAxCihkcDIKVnN0YXR1c19faW4KcDMKKGxwNApJMQphSTIKYUkzCmFzc1Zzb3J0aW5nCnA1ClZjcmVhdGVkCnA2CnMu'}) + self.assertEqual(response.status_code, 302) + self.assertTrue('tickets/?saved_query=1' in response.url) + + diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 0a652ebb..7d86c420 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1213,6 +1213,8 @@ run_report = staff_member_required(run_report) def save_query(request): title = request.POST.get('title', None) shared = request.POST.get('shared', False) + if shared == 'on': # django only translates '1', 'true', 't' into True + shared = True query_encoded = request.POST.get('query_encoded', None) if not title or not query_encoded: