From 9606201200079b04f9dac1568e1e2a11a120e74a Mon Sep 17 00:00:00 2001 From: Garret Wassermann Date: Thu, 20 Oct 2016 02:09:05 -0400 Subject: [PATCH] Fix mock tests for get_email (hopefully for good this time) --- helpdesk/management/commands/get_email.py | 6 +++--- helpdesk/tests/test_get_email.py | 16 +++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 4591fd76..bfe5dce3 100644 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -18,7 +18,7 @@ import poplib import re import socket from os import listdir, unlink -from os.path import isfile, isdir, join +from os.path import isfile, join from datetime import timedelta from email.header import decode_header @@ -97,7 +97,7 @@ def process_email(quiet=False): logger.setLevel(logging.DEBUG) if quiet: logger.propagate = False # do not propagate to root logger that would log to console - logdir = q.logging_dir if isdir(q.logging_dir) else '/var/log/helpdesk/' + logdir = q.logging_dir or '/var/log/helpdesk/' handler = logging.FileHandler(logdir + q.slug + '_get_email.log') logger.addHandler(handler) @@ -212,7 +212,7 @@ def process_queue(q, logger): server.logout() elif email_box_type == 'local': - mail_dir = q.email_box_local_dir if isdir(q.email_box_local_dir) else '/var/lib/mail/helpdesk/' + mail_dir = q.email_box_local_dir or '/var/lib/mail/helpdesk/' mail = [join(mail_dir, f) for f in listdir(mail_dir) if isfile(join(mail_dir, f))] logger.info("Found %s messages in local mailbox directory" % str(len(mail))) for m in mail: diff --git a/helpdesk/tests/test_get_email.py b/helpdesk/tests/test_get_email.py index d0c6a18e..630af619 100644 --- a/helpdesk/tests/test_get_email.py +++ b/helpdesk/tests/test_get_email.py @@ -2,7 +2,7 @@ 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.core.management import call_command from django.test.client import Client from django.core.urlresolvers import reverse from django.shortcuts import get_object_or_404 @@ -23,30 +23,28 @@ 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') + self.queue_public = Queue.objects.create(title='Queue 1', slug='QQ', allow_public_submission=True, allow_email_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.get_email.process_email') as mocked_processemail: - management.call_command('get_email', '--quiet') + call_command('get_email', '--quiet') mocked_processemail.assert_called_with(quiet=True) - management.call_command('get_email') + call_command('get_email') mocked_processemail.assert_called_with(quiet=False) # 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.get_email.isdir') as mocked_isdir, \ - mock.patch('helpdesk.management.commands.get_email.listdir') as mocked_listdir, \ + with mock.patch('helpdesk.management.commands.get_email.listdir') as mocked_listdir, \ mock.patch('helpdesk.management.commands.get_email.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') + call_command('get_email') + mocked_listdir.assert_called_with('/var/lib/mail/helpdesk') mocked_isfile.assert_any_call('/var/lib/mail/helpdesk/filename1') mocked_isfile.assert_any_call('/var/lib/mail/helpdesk/filename2')