Skip to content

Commit

Permalink
Merge pull request #885 from alligatorbait/master
Browse files Browse the repository at this point in the history
email tests updated to correct references to mock functions
  • Loading branch information
gwasser committed Oct 14, 2020
2 parents 3054407 + 14b53bb commit 52ab38a
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions helpdesk/tests/test_get_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
from django.contrib.auth.hashers import make_password

from helpdesk.models import Queue, Ticket, TicketCC, FollowUp, FollowUpAttachment
from helpdesk.management.commands.get_email import Command
import helpdesk.email

import six
import itertools
from shutil import rmtree
import sys
import os
from tempfile import mkdtemp
import logging


from urllib.parse import urlparse

from unittest import mock


THIS_DIR = os.path.dirname(os.path.abspath(__file__))

# class A addresses can't have first octet of 0
Expand All @@ -39,11 +37,13 @@ def setUp(self):
# tests correct syntax for command line option
def test_get_email_quiet_option(self):
"""Test quiet option is properly propagated"""
with mock.patch('helpdesk.management.commands.get_email.process_email') as mocked_processemail:
call_command('get_email', quiet=True)
mocked_processemail.assert_called_with(quiet=True)
call_command('get_email')
mocked_processemail.assert_called_with(quiet=False)
# Test get_email with quiet set to True and also False, and verify handle receives quiet option set properly
for quiet_test_value in [True, False]:
with mock.patch.object(Command, 'handle', return_value=None) as mocked_handle:
call_command('get_email', quiet=quiet_test_value)
mocked_handle.assert_called_once()
for args, kwargs in mocked_handle.call_args_list:
self.assertEqual(quiet_test_value, (kwargs['quiet']))

def test_email_with_blank_body_and_attachment(self):
"""
Expand Down Expand Up @@ -224,8 +224,8 @@ def test_commas_in_mail_headers(self):
else:
# Test local email reading
if self.method == 'local':
with mock.patch('helpdesk.management.commands.get_email.listdir') as mocked_listdir, \
mock.patch('helpdesk.management.commands.get_email.isfile') as mocked_isfile, \
with mock.patch('helpdesk.email.listdir') as mocked_listdir, \
mock.patch('helpdesk.email.isfile') as mocked_isfile, \
mock.patch('builtins.open' if six.PY3 else '__builtin__.open', mock.mock_open(read_data=test_email)):
mocked_isfile.return_value = True
mocked_listdir.return_value = ['filename1', 'filename2']
Expand All @@ -246,7 +246,7 @@ def test_commas_in_mail_headers(self):
mocked_poplib_server = mock.Mock()
mocked_poplib_server.list = mock.Mock(return_value=pop3_mail_list)
mocked_poplib_server.retr = mock.Mock(side_effect=lambda x: pop3_emails[x])
with mock.patch('helpdesk.management.commands.get_email.poplib', autospec=True) as mocked_poplib:
with mock.patch('helpdesk.email.poplib', autospec=True) as mocked_poplib:
mocked_poplib.POP3 = mock.Mock(return_value=mocked_poplib_server)
call_command('get_email')

Expand All @@ -262,7 +262,7 @@ def test_commas_in_mail_headers(self):

# we ignore the second arg as the data item/mime-part is constant (RFC822)
mocked_imaplib_server.fetch = mock.Mock(side_effect=lambda x, _: imap_emails[x])
with mock.patch('helpdesk.management.commands.get_email.imaplib', autospec=True) as mocked_imaplib:
with mock.patch('helpdesk.email.imaplib', autospec=True) as mocked_imaplib:
mocked_imaplib.IMAP4 = mock.Mock(return_value=mocked_imaplib_server)
call_command('get_email')

Expand Down Expand Up @@ -576,7 +576,6 @@ class GetEmailCCHandling(TestCase):
"""TestCase that checks CC handling in email. Needs its own test harness."""

def setUp(self):

self.temp_logdir = mkdtemp()

kwargs = {
Expand Down Expand Up @@ -640,7 +639,6 @@ def setUp(self):
self.original_cc = TicketCC.objects.create(**cc_kwargs)

def tearDown(self):

rmtree(self.temp_logdir)

def test_read_email_cc(self):
Expand Down Expand Up @@ -675,7 +673,6 @@ def test_read_email_cc(self):
with mock.patch('helpdesk.email.listdir') as mocked_listdir, \
mock.patch('helpdesk.email.isfile') as mocked_isfile, \
mock.patch('builtins.open', mock.mock_open(read_data=test_email)):

mocked_isfile.return_value = True
mocked_listdir.return_value = ['filename1']

Expand Down

0 comments on commit 52ab38a

Please sign in to comment.