def test_message_arguments(self): u""" Tests arguments passed directly to the message constructor. Checks for ``from_email``, ``to``, ``cc``, ``bcc``, ``headers`` and ``attachments`` arguments. Tests them with all three message forms: text only, html only and with both text and html parts. """ kwargs = { u'from_email': u'*****@*****.**', u'to': [u'*****@*****.**', u'*****@*****.**'], u'cc': [u'John Shmith <*****@*****.**>'], u'bcc': [u'*****@*****.**'], u'headers': { 'Reply-To': '*****@*****.**' }, u'attachments': [(u'filename', u'content', u'application/octet-stream')], } for template in [u'first', u'second', u'third']: msg = render_mail(template, **kwargs) self.assertEqual(msg.from_email, kwargs[u'from_email']) self.assertEqual(msg.to, kwargs[u'to']) self.assertEqual(msg.cc, kwargs[u'cc']) self.assertEqual(msg.bcc, kwargs[u'bcc']) self.assertEqual(msg.extra_headers, kwargs[u'headers']) self.assertEqual(msg.attachments, kwargs[u'attachments'])
def test_message_with_html_only(self): # existing: third_subject.txt, third_message.html # missing: third_message.txt msg = render_mail(u'third') alternatives = self._get_alternatives(msg) self.assertEqual(alternatives, [ (u'text/html', u'<p>Third message with only HTML</p>'), ])
def test_message_with_text_only(self): # existing: second_subject.txt, second_message.txt # missing: second_message.html msg = render_mail(u'second') alternatives = self._get_alternatives(msg) self.assertEqual(alternatives, [ (u'text/plain', u'Second message with only text'), ])
def test_message_with_text_and_html(self): # existing: first_subject.txt, first_message.txt, first_message.html # missing: -- msg = render_mail(u'first') alternatives = self._get_alternatives(msg) self.assertEqual(alternatives, [ (u'text/plain', u'First message text with leading and trailing whitespace'), (u'text/html', u'<p>\nFirst message HTML with leading and trailing whitespace\n</p>'), ])
def _send_notification(self, template, anchor, dictionary): dictionary.update({ u'inforequest': self, u'url': complete_url(self.get_absolute_url(anchor)), }) msg = render_mail(template, from_email=settings.DEFAULT_FROM_EMAIL, to=[self.applicant.email], dictionary=dictionary) msg.send()
def send_by_email(self): msg = render_mail(u'invitations/mails/invitation', from_email=settings.DEFAULT_FROM_EMAIL, to=[self.email], dictionary={ u'invitation': self, }) msg.send() self.message = msg.instance self.save(update_fields=[u'message'])
def test_subject_squeezed(self): u""" Checks that even if the subject template contains leading, trailing or consecutive whitespace, tabs or linebreaks, the rendered subject is normalized. """ # Ensure "first_subject.txt" contains leading/trailing/consecutive whitespace rendered = render_to_string(u'first_subject.txt') self.assertNotRegexpMatches(rendered, r'^(\S+ )*\S+$') msg = render_mail(u'first') self.assertRegexpMatches(msg.subject, r'^(\S+ )*\S+$')
def test_message_with_text_and_html(self): # existing: first_subject.txt, first_message.txt, first_message.html # missing: -- msg = render_mail(u'first') alternatives = self._get_alternatives(msg) self.assertEqual(alternatives, [ (u'text/plain', u'First message text with leading and trailing whitespace'), (u'text/html', u'<p>\nFirst message HTML with leading and trailing whitespace\n</p>' ), ])
def test_render_mail_with_dictionary(self): u""" Passes a dictionary to ``render_mail()`` function and checks if templates using it are rendered corectly. """ msg = render_mail(u'sixth', dictionary={u'variable': 47}) alternatives = self._get_alternatives(msg) self.assertEqual(msg.subject, u'[example.com] Subject with dictionary: 47') self.assertEqual(alternatives, [ (u'text/plain', u'Text message with dictionary: 47'), (u'text/html', u'<p>HTML message with dictionary: 47</p>'), ])
def test_body_stripped(self): u""" Checks that even if the message template contains leading or trailing whitespace, the rendered message is stripped. """ # Ensure both "first_message.txt" and "first_message.html" contain leading/trailing whitespace rendered_txt = render_to_string(u'first_message.txt') rendered_html = render_to_string(u'first_message.html') self.assertNotEqual(rendered_txt, rendered_txt.strip()) self.assertNotEqual(rendered_html, rendered_html.strip()) msg = render_mail(u'first') alternatives = self._get_alternatives(msg) self.assertEqual(alternatives[0][1], alternatives[0][1].strip()) self.assertEqual(alternatives[1][1], alternatives[1][1].strip())
def finish_help(self): msg = render_mail( u'inforequests/mails/obligee_action_help_request', from_email=settings.DEFAULT_FROM_EMAIL, to=[settings.SUPPORT_EMAIL], attachments=[(a.name, a.content, a.content_type) for a in self.values[u'attachments'] or []], dictionary={ u'wizard': self, u'inforequest': self.inforequest, u'email': self.email, }, ) msg.send() if self.email: self.inforequestemail.type = InforequestEmail.TYPES.UNKNOWN self.inforequestemail.save(update_fields=[u'type']) return self.inforequest.get_absolute_url()
def test_message_arguments(self): u""" Tests arguments passed directly to the message constructor. Checks for ``from_email``, ``to``, ``cc``, ``bcc``, ``headers`` and ``attachments`` arguments. Tests them with all three message forms: text only, html only and with both text and html parts. """ kwargs = { u'from_email': u'*****@*****.**', u'to': [u'*****@*****.**', u'*****@*****.**'], u'cc': [u'John Shmith <*****@*****.**>'], u'bcc': [u'*****@*****.**'], u'headers': {'Reply-To': '*****@*****.**'}, u'attachments': [(u'filename', u'content', u'application/octet-stream')], } for template in [u'first', u'second', u'third']: msg = render_mail(template, **kwargs) self.assertEqual(msg.from_email, kwargs[u'from_email']) self.assertEqual(msg.to, kwargs[u'to']) self.assertEqual(msg.cc, kwargs[u'cc']) self.assertEqual(msg.bcc, kwargs[u'bcc']) self.assertEqual(msg.extra_headers, kwargs[u'headers']) self.assertEqual(msg.attachments, kwargs[u'attachments'])
def test_message_with_missing_subject_template(self): # existing: fifth_message.txt # missing: fifth_subject.txt, fifth_message.html with self.assertRaisesMessage(TemplateDoesNotExist, u'fifth_subject.txt'): msg = render_mail(u'fifth')
def test_message_with_missing_templates(self): # existing: fourth_subject.txt # missing: fourth_message.txt, fourth_message.html with self.assertRaisesMessage(TemplateDoesNotExist, u'fourth_message.txt'): msg = render_mail(u'fourth')