2019-10-15 05:31:13 +02:00
|
|
|
from unittest import mock
|
2018-03-09 16:36:39 -05:00
|
|
|
|
2018-03-09 15:35:38 -05:00
|
|
|
from django.core.management import call_command, CommandError
|
|
|
|
|
from django.test import TestCase
|
2023-12-10 04:01:53 +01:00
|
|
|
|
2018-03-09 15:35:38 -05:00
|
|
|
|
|
|
|
|
class CommandsTestCase(TestCase):
|
|
|
|
|
def test_processincomingmessage_no_args(self):
|
|
|
|
|
"""Check that processincomingmessage works with no args"""
|
2023-12-10 04:01:53 +01:00
|
|
|
|
2018-03-09 16:36:39 -05:00
|
|
|
mailbox_name = None
|
2018-03-09 15:35:38 -05:00
|
|
|
# Mock handle so that the test doesn't hang waiting for input. Note that we are only testing
|
|
|
|
|
# the argument parsing here -- functionality should be tested elsewhere
|
|
|
|
|
with mock.patch('django_mailbox.management.commands.processincomingmessage.Command.handle') as handle:
|
|
|
|
|
# Don't care about the return value
|
|
|
|
|
handle.return_value = None
|
2023-12-10 04:01:53 +01:00
|
|
|
|
2018-03-09 15:35:38 -05:00
|
|
|
call_command('processincomingmessage')
|
2018-03-09 16:36:39 -05:00
|
|
|
args, kwargs = handle.call_args
|
|
|
|
|
|
2018-03-09 15:35:38 -05:00
|
|
|
# Make sure that we called with the right arguments
|
2023-12-10 04:01:53 +01:00
|
|
|
self.assertEqual(kwargs['mailbox_name'], mailbox_name)
|
2018-03-09 15:35:38 -05:00
|
|
|
|
|
|
|
|
def test_processincomingmessage_with_arg(self):
|
|
|
|
|
"""Check that processincomingmessage works with mailbox_name given"""
|
|
|
|
|
|
2018-03-09 16:36:39 -05:00
|
|
|
mailbox_name = 'foo_mailbox'
|
|
|
|
|
|
2018-03-09 15:35:38 -05:00
|
|
|
with mock.patch('django_mailbox.management.commands.processincomingmessage.Command.handle') as handle:
|
|
|
|
|
handle.return_value = None
|
2023-12-10 04:01:53 +01:00
|
|
|
|
2018-03-09 16:36:39 -05:00
|
|
|
call_command('processincomingmessage', mailbox_name)
|
|
|
|
|
args, kwargs = handle.call_args
|
2023-12-10 04:01:53 +01:00
|
|
|
|
|
|
|
|
self.assertEqual(kwargs['mailbox_name'], mailbox_name)
|
2018-03-09 15:35:38 -05:00
|
|
|
|
|
|
|
|
def test_processincomingmessage_too_many_args(self):
|
|
|
|
|
"""Check that processincomingmessage raises an error if too many args"""
|
2023-12-10 04:01:53 +01:00
|
|
|
|
|
|
|
|
with self.assertRaises(CommandError):
|
|
|
|
|
call_command('processincomingmessage', 'foo_mailbox', 'invalid_arg')
|