Fix mock tests for get_email (hopefully for good this time)

This commit is contained in:
Garret Wassermann 2016-10-20 02:09:05 -04:00
parent b7ef83f7d2
commit 9606201200
2 changed files with 10 additions and 12 deletions

View File

@ -18,7 +18,7 @@ import poplib
import re import re
import socket import socket
from os import listdir, unlink from os import listdir, unlink
from os.path import isfile, isdir, join from os.path import isfile, join
from datetime import timedelta from datetime import timedelta
from email.header import decode_header from email.header import decode_header
@ -97,7 +97,7 @@ def process_email(quiet=False):
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
if quiet: if quiet:
logger.propagate = False # do not propagate to root logger that would log to console 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') handler = logging.FileHandler(logdir + q.slug + '_get_email.log')
logger.addHandler(handler) logger.addHandler(handler)
@ -212,7 +212,7 @@ def process_queue(q, logger):
server.logout() server.logout()
elif email_box_type == 'local': 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))] 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))) logger.info("Found %s messages in local mailbox directory" % str(len(mail)))
for m in mail: for m in mail:

View File

@ -2,7 +2,7 @@ from helpdesk.models import Queue, Ticket
from helpdesk.management.commands.get_email import process_email from helpdesk.management.commands.get_email import process_email
from django.test import TestCase from django.test import TestCase
from django.core import mail 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.test.client import Client
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -23,30 +23,28 @@ class GetEmailTestCase(TestCase):
fixtures = ['emailtemplate.json'] fixtures = ['emailtemplate.json']
def setUp(self): 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', 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/')
email_box_type='local', email_box_local_dir='/var/lib/mail/helpdesk')
# tests correct syntax for command line option # tests correct syntax for command line option
def test_get_email_quiet_option(self): def test_get_email_quiet_option(self):
with mock.patch('helpdesk.management.commands.get_email.process_email') as mocked_processemail: 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) mocked_processemail.assert_called_with(quiet=True)
management.call_command('get_email') call_command('get_email')
mocked_processemail.assert_called_with(quiet=False) mocked_processemail.assert_called_with(quiet=False)
# tests reading emails from a queue and creating tickets # tests reading emails from a queue and creating tickets
def test_read_email(self): 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." 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, \ with mock.patch('helpdesk.management.commands.get_email.listdir') as mocked_listdir, \
mock.patch('helpdesk.management.commands.get_email.listdir') as mocked_listdir, \
mock.patch('helpdesk.management.commands.get_email.isfile') as mocked_isfile, \ mock.patch('helpdesk.management.commands.get_email.isfile') as mocked_isfile, \
mock.patch('builtins.open', mock.mock_open(read_data=test_email)): mock.patch('builtins.open', mock.mock_open(read_data=test_email)):
mocked_isdir.return_value = True
mocked_isfile.return_value = True mocked_isfile.return_value = True
mocked_listdir.return_value = ['filename1', 'filename2'] 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/filename1')
mocked_isfile.assert_any_call('/var/lib/mail/helpdesk/filename2') mocked_isfile.assert_any_call('/var/lib/mail/helpdesk/filename2')