Exemplo n.º 1
0
 def setUp(self):
     self.mixin_object = TemplatedEmailFormViewMixin()
Exemplo n.º 2
0
class TemplatedEmailFormViewMixinUnitTestCase(TestCase):
    def setUp(self):
        self.mixin_object = TemplatedEmailFormViewMixin()

    def test_templated_email_get_template_names_raises_exception(self):
        self.assertRaises(ImproperlyConfigured,
                          self.mixin_object.templated_email_get_template_names,
                          valid=True)
        self.assertRaises(ImproperlyConfigured,
                          self.mixin_object.templated_email_get_template_names,
                          valid=False)

    def test_templated_email_get_template_names_with_template_name(self):
        self.mixin_object.templated_email_template_name = 'template_name'
        self.assertEquals(
            self.mixin_object.templated_email_get_template_names(valid=True),
            ['template_name']
        )
        self.assertEquals(
            self.mixin_object.templated_email_get_template_names(valid=False),
            ['template_name']
        )

    def test_templated_email_get_context_data(self):
        context = self.mixin_object.templated_email_get_context_data()
        self.assertEqual(context, {})
        context = self.mixin_object.templated_email_get_context_data(foo='bar')
        self.assertEqual(context, {'foo': 'bar'})

    def test_templated_email_get_recipients(self):
        self.assertRaises(NotImplementedError,
                          self.mixin_object.templated_email_get_recipients,
                          form=None)

    @mock.patch.object(TemplatedEmailFormViewMixin,
                       'templated_email_get_template_names',
                       return_value=['template'])
    @mock.patch.object(TemplatedEmailFormViewMixin,
                       'templated_email_get_recipients',
                       return_value=['*****@*****.**'])
    def test_templated_email_get_send_email_kwargs_valid(
            self,
            mocked_get_templated_email_recipients,
            mocked_get_templated_email_template_names):
        class FakeForm(object):
            data = 'foo'
        form = FakeForm()
        kwargs = self.mixin_object.templated_email_get_send_email_kwargs(
            valid=True, form=form)
        self.assertEquals(len(kwargs), 4)
        self.assertEquals(kwargs['template_name'], ['template'])
        self.assertEquals(kwargs['from_email'], None)
        self.assertEquals(kwargs['recipient_list'], ['*****@*****.**'])
        self.assertEquals(kwargs['context'], {'form_data': 'foo'})

    @mock.patch.object(TemplatedEmailFormViewMixin,
                       'templated_email_get_template_names',
                       return_value=['template'])
    @mock.patch.object(TemplatedEmailFormViewMixin,
                       'templated_email_get_recipients',
                       return_value=['*****@*****.**'])
    def test_templated_email_get_send_email_kwargs_not_valid(
            self,
            mocked_get_templated_email_recipients,
            mocked_get_templated_email_template_names):
        class FakeForm(object):
            errors = 'errors foo'
        form = FakeForm()
        kwargs = self.mixin_object.templated_email_get_send_email_kwargs(
            valid=False, form=form)
        self.assertEquals(len(kwargs), 4)
        self.assertEquals(kwargs['template_name'], ['template'])
        self.assertEquals(kwargs['from_email'], None)
        self.assertEquals(kwargs['recipient_list'], ['*****@*****.**'])
        self.assertEquals(kwargs['context'], {'form_errors': 'errors foo'})