Exemplo n.º 1
0
 def test_version(self):
     template1 = EmailTemplate(name="test", language="en-us",
                               version=1).save()
     template2 = EmailTemplate(name="test", language="en-us",
                               version=0).save()
     self.assertEqual(EmailTemplate.objects.version("test", 1), template1)
     self.assertEqual(EmailTemplate.objects.version("test", 0), template2)
Exemplo n.º 2
0
 def test_current(self):
     # manually setting the version in the wrong order, so the first
     # template is actually the last, when ordered by version.
     template1 = EmailTemplate(name="test", language="en-us",
                               version=1).save()
     _ = EmailTemplate(name="test", language="en-us", version=0).save()
     self.assertEqual(EmailTemplate.objects.current("test"), template1)
     self.assertEqual(
         EmailTemplate.objects.current("test", language="klingon"), None)
Exemplo n.º 3
0
 def test_clone_template(self):
     template = EmailTemplate(name="Test template",
                              language="en-us",
                              version=0).save()
     pk = template.pk
     clone = template.clone()
     template = EmailTemplate.objects.get(id=pk)
     self.assertEqual(clone.name, template.name)
     self.assertEqual(clone.language, template.language)
     self.assertEqual(clone.version, 1)
     self.assertNotEqual(clone.id, template.id)
Exemplo n.º 4
0
    def test_defaults(self):
        template = EmailTemplate()
        self.assertEqual(template.language, settings.LANGUAGE_CODE)
        self.assertEqual(template.from_email, settings.DEFAULT_FROM_EMAIL)
        self.assertEqual(template.reply_to, settings.DEFAULT_FROM_EMAIL)
        self.assertEqual(template.reply_to_list, [settings.DEFAULT_FROM_EMAIL])
        self.assertEqual(template.version, 0)

        template.reply_to = "[email protected], [email protected]"
        self.assertEqual(template.reply_to_list,
                         ["*****@*****.**", "*****@*****.**"])
Exemplo n.º 5
0
 def test__validate_subject(self, mock_render):
     template = EmailTemplate()
     mock_render.side_effect = TemplateDoesNotExist("foo.html")
     self.assertEqual(
         template._validate_subject(),
         {"subject": "Template does not exist: foo.html"},
     )
     mock_render.side_effect = TemplateSyntaxError("No can do")
     self.assertEqual(template._validate_subject(),
                      {"subject": "No can do"})
     mock_render.side_effect = None
     self.assertEqual(template._validate_subject(), {})
     mock_render.side_effect = Exception("Something else")
     self.assertRaises(Exception, template._validate_subject)
Exemplo n.º 6
0
 def test_init__special_characters__complex_context(self):
     template = EmailTemplate(
         subject=
         "Hello {{ user.first_name }} and welcome to {{ company_name }}",
         body_text=
         "Hello {{ user.first_name }} and welcome to {{ company_name }}",
         body_html=("<h1>Hello {{ user.first_name }}</h1></br><p>"
                    "Welcome to {{ company_name }}</p>"),
     )
     context = {
         "user": {
             "first_name": "Test & Company"
         },
         "company_name": "Me & Co Inc",
     }
     message = AppmailMessage(template, context)
     self.assertIsInstance(message, EmailMultiAlternatives)
     self.assertEqual(message.subject,
                      "Hello Test & Company and welcome to Me & Co Inc")
     self.assertEqual(message.body,
                      "Hello Test & Company and welcome to Me & Co Inc")
     self.assertEqual(
         message.alternatives,
         [(
             "<h1>Hello Test &amp; Company</h1></br><p>"
             "Welcome to Me &amp; Co Inc</p>",
             EmailTemplate.CONTENT_TYPE_HTML,
         )],
     )
Exemplo n.º 7
0
 def test_init__with_attachments__allowed(self):
     template = EmailTemplate(
         subject="Welcome {{ first_name }}",
         body_text="Hello {{ first_name }}",
         body_html="<h1>Hello {{ first_name }}</h1>",
         supports_attachments=True,
     )
     AppmailMessage(template, {}, attachments=[mock.Mock(spec=MIMEImage)])
Exemplo n.º 8
0
 def test__validate_body(self, mock_render):
     template = EmailTemplate()
     mock_render.side_effect = TemplateDoesNotExist("foo.html")
     self.assertEqual(
         template._validate_body(
             content_type=EmailTemplate.CONTENT_TYPE_PLAIN),
         {"body_text": "Template does not exist: foo.html"},
     )
     mock_render.side_effect = TemplateSyntaxError("No can do")
     self.assertEqual(
         template._validate_body(
             content_type=EmailTemplate.CONTENT_TYPE_HTML),
         {"body_html": "No can do"},
     )
     mock_render.side_effect = None
     self.assertEqual(
         template._validate_body(EmailTemplate.CONTENT_TYPE_HTML), {})
     mock_render.side_effect = Exception("Something else")
     self.assertRaises(Exception, template._validate_body)
Exemplo n.º 9
0
 def test_init__with_attachments__disallowed(self):
     template = EmailTemplate(
         subject="Welcome {{ first_name }}",
         body_text="Hello {{ first_name }}",
         body_html="<h1>Hello {{ first_name }}</h1>",
         supports_attachments=False,
     )
     with self.assertRaisesMessage(
             ValueError, "Email template does not support attachments."):
         AppmailMessage(template, {},
                        attachments=[mock.Mock(spec=MIMEImage)])
Exemplo n.º 10
0
 def test_render_body(self):
     template = EmailTemplate(
         body_text="Hello {{ first_name }}",
         body_html="<h1>Hello {{ first_name }}</h1>",
     )
     context = {"first_name": "fråd"}
     self.assertEqual(template.render_body(context), "Hello fråd")
     self.assertEqual(
         template.render_body(
             context, content_type=EmailTemplate.CONTENT_TYPE_PLAIN),
         "Hello fråd",
     )
     self.assertEqual(
         template.render_body(context,
                              content_type=EmailTemplate.CONTENT_TYPE_HTML),
         "<h1>Hello fråd</h1>",
     )
     self.assertRaises(ValueError,
                       template.render_body,
                       context,
                       content_type="foo")
Exemplo n.º 11
0
 def test_save(self, mock_clean):
     template = EmailTemplate(
         subject="test ßmail",
         body_text="this is plain text",
         body_html="this is <b>html</b>",
     )
     with mock.patch("appmail.models.VALIDATE_ON_SAVE", False):
         template.save()
         self.assertEqual(mock_clean.call_count, 0)
     with mock.patch("appmail.models.VALIDATE_ON_SAVE", True):
         template.save()
         self.assertEqual(mock_clean.call_count, 1)
         # test the override
         template.save(validate=False)
         self.assertEqual(mock_clean.call_count, 1)
Exemplo n.º 12
0
 def test__create_message(self):
     form = EmailTestForm()
     form.cleaned_data = {
         "context": {"foo": "bar"},
         "to": ["*****@*****.**"],
         "cc": [],
         "bcc": [],
         "from_email": "*****@*****.**",
     }
     template = EmailTemplate()
     email = form._create_message(template)
     self.assertEqual(email.from_email, "*****@*****.**")
     self.assertEqual(email.to, ["*****@*****.**"])
     self.assertEqual(email.cc, [])
     self.assertEqual(email.bcc, [])
Exemplo n.º 13
0
    def test_init(self):
        template = EmailTemplate(
            subject="Welcome message",
            body_text="Hello {{ first_name }}",
            body_html="<h1>Hello {{ first_name }}</h1>",
        )
        context = {"first_name": "fråd"}
        message = AppmailMessage(template, context)
        self.assertIsInstance(message, EmailMultiAlternatives)
        self.assertEqual(message.subject, "Welcome message")
        self.assertEqual(message.body, "Hello fråd")
        self.assertEqual(
            message.alternatives,
            [("<h1>Hello fråd</h1>", EmailTemplate.CONTENT_TYPE_HTML)],
        )
        self.assertEqual(message.to, [])
        self.assertEqual(message.cc, [])
        self.assertEqual(message.bcc, [])
        self.assertEqual(message.from_email, settings.DEFAULT_FROM_EMAIL)
        self.assertEqual(message.reply_to, [settings.DEFAULT_FROM_EMAIL])

        message = AppmailMessage(
            template,
            context,
            to=["*****@*****.**"],
            cc=["*****@*****.**"],
            bcc=["*****@*****.**"],
            from_email="Fred <*****@*****.**>",
        )
        self.assertEqual(message.to, ["*****@*****.**"])
        self.assertEqual(message.cc, ["*****@*****.**"])
        self.assertEqual(message.bcc, ["*****@*****.**"])
        self.assertEqual(message.from_email, "Fred <*****@*****.**>")
        # and so on - not going to test every property.

        # but we will check the three illegal kwargs
        with self.assertRaisesMessage(
                ValueError,
                "Invalid argument: 'subject' is set from the template."):
            AppmailMessage(template, {}, subject="foo")
        with self.assertRaisesMessage(
                ValueError,
                "Invalid argument: 'body' is set from the template."):
            AppmailMessage(template, {}, body="foo")
        with self.assertRaisesMessage(
                ValueError,
                "Invalid argument: 'alternatives' is set from the template."):
            AppmailMessage(template, {}, alternatives="foo")
Exemplo n.º 14
0
    def test_init__special_characters(self):
        template = EmailTemplate(
            subject="Welcome {{ first_name }}",
            body_text="Hello {{ first_name }}",
            body_html="<h1>Hello {{ first_name }}</h1>",
        )

        context = {"first_name": "Test & Company"}
        message = AppmailMessage(template, context)
        self.assertIsInstance(message, EmailMultiAlternatives)
        self.assertEqual(message.subject, "Welcome Test & Company")
        self.assertEqual(message.body, "Hello Test & Company")
        self.assertEqual(
            message.alternatives,
            [("<h1>Hello Test &amp; Company</h1>",
              EmailTemplate.CONTENT_TYPE_HTML)],
        )
Exemplo n.º 15
0
 def setUp(self):
     # Every test needs access to the request factory.
     self.factory = RequestFactory()
     self.template = EmailTemplate(
         subject="ßello, {{user.first_name}}",
         body_text="ßello,\n{{user.first_name}}",
         body_html="ßello, <b>{{user.first_name}}</b>",
     ).save()
     self.subject_url = reverse(
         "appmail:render_template_subject", kwargs={"template_id": self.template.id}
     )
     self.body_text_url = reverse(
         "appmail:render_template_body_text",
         kwargs={"template_id": self.template.id},
     )
     self.body_html_url = reverse(
         "appmail:render_template_body_html",
         kwargs={"template_id": self.template.id},
     )
Exemplo n.º 16
0
    def test_send_emails(self, mock_send, mock_messages):
        template = EmailTemplate().save()
        form = EmailTestForm()
        form.cleaned_data = {
            "context": {"foo": "bar"},
            "to": ["*****@*****.**"],
            "cc": [],
            "bcc": [],
            "from_email": "*****@*****.**",
            "templates": [template],
        }
        request = HttpRequest()
        form.send_emails(request)
        mock_send.assert_called_once()
        mock_messages.success.assert_called_once()

        # test email failure
        mock_send.side_effect = Exception()
        form.send_emails(request)
        mock_messages.error.assert_called_once()
Exemplo n.º 17
0
 def test_clean(self, mock_body):
     template = EmailTemplate()
     template.clean()
     mock_body.return_value = {"body_text": "Template not found"}
     self.assertRaises(ValidationError, template.clean)
Exemplo n.º 18
0
 def test_render_subject(self):
     template = EmailTemplate(subject="Hello {{ first_name }}")
     subject = template.render_subject({"first_name": "fråd"})
     self.assertEqual(subject, "Hello fråd")
Exemplo n.º 19
0
 def test_active(self):
     template1 = EmailTemplate(name="test1", language="en-us").save()
     _ = EmailTemplate(name="test2", language="en-us",
                       is_active=False).save()
     self.assertEqual(EmailTemplate.objects.active().get(), template1)
Exemplo n.º 20
0
 def test_template_name(self):
     template = EmailTemplate(name="foo")
     message = LoggedMessage(template=template)
     assert message.template_name == "foo"