<filename>core/tests/test_email.py
from django.test import TestCase, override_settings
import mock

from core.email import resource_request_email, email_admin, email_from_admin
from api.tests.factories import UserFactory


class EmailTests(TestCase):
    def test_resource_request_email(self):
        """
        Assert general expected behavior of resource_request_email
        """
        user = UserFactory(
            username="user0",
            first_name="First",
            last_name="Last",
            email="<EMAIL>"
        )

        # Mock out a request
        request = mock.Mock()
        request.user = user
        request.POST = {}
        request.session = None

        with mock.patch('core.tasks.EmailMessage') as MockMessage, \
                override_settings(ATMO_SUPPORT=('Support', '<EMAIL>')):
            resource_request_email(
                request, user.username, "All the resources", "Did I stutter?"
            )
            kwargs = MockMessage.call_args[1]

            # Assert that Django's EmailMessage is called with the right
            # paramaters
            # Note: We are not testing the body of the email here
            MockMessage.assert_called_with(
                cc=None,
                to=['Support <<EMAIL>>'],
                subject='Atmosphere Resource Request - user0',
                from_email='First Last <<EMAIL>>',
                body=kwargs['body']
            )

    def test_email_admin(self):
        """
        Assert that emails to admin correctly set from, to, and cc fields
        """
        with mock.patch('core.tasks.EmailMessage') as MockMessage, \
                override_settings(ADMINS=[
                    ('Admin1', '<EMAIL>'),
                    ('Admin2', '<EMAIL>')]):
            email_admin('Subject', 'Body', 'First Last <<EMAIL>>')
            kwargs = MockMessage.call_args[1]

            MockMessage.assert_called_with(
                to=['Admin1 <<EMAIL>>', 'Admin2 <<EMAIL>>'],
                from_email='First Last <<EMAIL>>',
                cc=None,
                subject=kwargs['subject'],
                body=kwargs['body'],
            )

    def test_email_from_admin(self):
        """
        Assert that emails from admin to user correctly set from, to, and cc fields
        """
        UserFactory(
            username="user0",
            first_name="First",
            last_name="Last",
            email="<EMAIL>"
        )
        with mock.patch('core.tasks.EmailMessage') as MockMessage, \
                override_settings(ATMO_DAEMON=('AtmoAdmin', '<EMAIL>')):
            email_from_admin("user0", 'Subject', 'Body')
            kwargs = MockMessage.call_args[1]

            MockMessage.assert_called_with(
                to=['First Last <<EMAIL>>'],
                from_email='AtmoAdmin <<EMAIL>>',
                cc=['AtmoAdmin <<EMAIL>>'],
                subject=kwargs['subject'],
                body=kwargs['body'],
            )

<filename>core/tests/test_email.py
from django.test import TestCase, override_settings
import mock

from core.email import resource_request_email, email_admin, email_from_admin
from api.tests.factories import UserFactory


class EmailTests(TestCase):
    def test_resource_request_email(self):
        """
        Assert general expected behavior of resource_request_email
        """
        user = UserFactory(
            username="user0",
            first_name="First",
            last_name="Last",
            email="<EMAIL>"
        )

        # Mock out a request
        request = mock.Mock()
        request.user = user
        request.POST = {}
        request.session = None

        with mock.patch('core.tasks.EmailMessage') as MockMessage, \
                override_settings(ATMO_SUPPORT=('Support', '<EMAIL>')):
            resource_request_email(
                request, user.username, "All the resources", "Did I stutter?"
            )
            kwargs = MockMessage.call_args[1]

            # Assert that Django's EmailMessage is called with the right
            # paramaters
            # Note: We are not testing the body of the email here
            MockMessage.assert_called_with(
                cc=None,
                to=['Support <<EMAIL>>'],
                subject='Atmosphere Resource Request - user0',
                from_email='First Last <<EMAIL>>',
                body=kwargs['body']
            )

    def test_email_admin(self):
        """
        Assert that emails to admin correctly set from, to, and cc fields
        """
        with mock.patch('core.tasks.EmailMessage') as MockMessage, \
                override_settings(ADMINS=[
                    ('Admin1', '<EMAIL>'),
                    ('Admin2', '<EMAIL>')]):
            email_admin('Subject', 'Body', 'First Last <<EMAIL>>')
            kwargs = MockMessage.call_args[1]

            MockMessage.assert_called_with(
                to=['Admin1 <<EMAIL>>', 'Admin2 <<EMAIL>>'],
                from_email='First Last <<EMAIL>>',
                cc=None,
                subject=kwargs['subject'],
                body=kwargs['body'],
            )

    def test_email_from_admin(self):
        """
        Assert that emails from admin to user correctly set from, to, and cc fields
        """
        UserFactory(
            username="user0",
            first_name="First",
            last_name="Last",
            email="<EMAIL>"
        )
        with mock.patch('core.tasks.EmailMessage') as MockMessage, \
                override_settings(ATMO_DAEMON=('AtmoAdmin', '<EMAIL>')):
            email_from_admin("user0", 'Subject', 'Body')
            kwargs = MockMessage.call_args[1]

            MockMessage.assert_called_with(
                to=['First Last <<EMAIL>>'],
                from_email='AtmoAdmin <<EMAIL>>',
                cc=['AtmoAdmin <<EMAIL>>'],
                subject=kwargs['subject'],
                body=kwargs['body'],
            )

Assert general expected behavior of resource_request_email # Mock out a request # Assert that Django's EmailMessage is called with the right # paramaters # Note: We are not testing the body of the email here Assert that emails to admin correctly set from, to, and cc fields Assert that emails from admin to user correctly set from, to, and cc fields