From c9c76427249fe5a320f64ccbd866ad436da6ff1f Mon Sep 17 00:00:00 2001 From: Garret Wassermann Date: Wed, 19 Oct 2016 21:37:23 -0400 Subject: [PATCH] New unit test for get_email command --- helpdesk/tests/test_get_email.py | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 helpdesk/tests/test_get_email.py diff --git a/helpdesk/tests/test_get_email.py b/helpdesk/tests/test_get_email.py new file mode 100644 index 00000000..d186a008 --- /dev/null +++ b/helpdesk/tests/test_get_email.py @@ -0,0 +1,60 @@ +from helpdesk.models import Queue, Ticket +from helpdesk.management.commands.get_email import process_email +from django.test import TestCase +from django.core import mail +from django.core import management +from django.test.client import Client +from django.core.urlresolvers import reverse +from django.shortcuts import get_object_or_404 + +try: # python 3 + from urllib.parse import urlparse +except ImportError: # python 2 + from urlparse import urlparse + +try: + # Python >= 3.3 + from unittest import mock +except ImportError: + # Python < 3.3 + import mock + +class GetEmailTestCase(TestCase): + fixtures = ['emailtemplate.json'] + + def setUp(self): + self.queue_public = Queue.objects.create(title='Queue 1', slug='QQ', allow_public_submission=True, new_ticket_cc='new.public@example.com', updated_ticket_cc='update.public@example.com', + email_box_type='local', email_box_local_dir='/var/lib/mail/helpdesk') + + # tests correct syntax for command line option + def test_get_email_quiet_option(self): + with mock.patch('helpdesk.management.commands.process_email') as mocked_processemail: + management.call_command('get_email', '-q') + mocked_processemail.assert_called_with(quiet=True) + + # tests reading emails from a queue and creating tickets + def test_read_email(self): + test_email = "To: update.public@example.com\nFrom: comment@example.com\nSubject: Some Comment\n\nThis is the helpdesk comment via email." + with mock.patch('helpdesk.management.commands.isdir') as mocked_isdir, \ + mock.patch('helpdesk.management.commands.listdir') as mocked_listdir, \ + mock.patch('helpdesk.management.commands.isfile') as mocked_isfile, \ + mock.patch('builtins.open', mock.mock_open(read_data=test_email)): + mocked_isdir.return_value = True + mocked_isfile.return_value = True + mocked_listdir.return_value = ['filename1', 'filename2'] + + management.call_command('get_email') + + mocked_isfile.assert_any_call('/var/lib/mail/helpdesk/filename1') + mocked_isfile.assert_any_call('/var/lib/mail/helpdesk/filename2') + + ticket1 = get_object_or_404(Ticket, pk=1) + self.assertEqual(ticket1.ticket_for_url, "QQ-%s" % ticket1.id) + self.assertEqual(ticket1.description, "This is the helpdesk comment via email.") + + ticket2 = get_object_or_404(Ticket, pk=2) + self.assertEqual(ticket2.ticket_for_url, "QQ-%s" % ticket2.id) + self.assertEqual(ticket2.description, "This is the helpdesk comment via email.") + + +