| 1 | """ |
| 2 | Unit tests for all of send_templated_mail's helper functions. |
| 3 | """ |
| 4 | |
| 5 | from django.test import TestCase |
| 6 | from django.template import Context, loader |
| 7 | from django.utils.html import strip_tags |
| 8 | from django.template.loader_tags import BlockNode |
| 9 | from django import shortcuts |
| 10 | from django.template import Context, loader |
| 11 | |
| 12 | |
| 13 | class SendTemplatedMailTest(TestCase): |
| 14 | def test_render_mail_plaintext_only(self): |
| 15 | """ |
| 16 | Test that an email created by _render_node with only a "plain" block is |
| 17 | plaintext only. |
| 18 | """ |
| 19 | |
| 20 | email_msg = shortcuts._render_mail("shortcuts/plain.tpl", |
| 21 | "person@example.com", |
| 22 | ["people@example.com"]) |
| 23 | self.assertEqual(email_msg.body, |
| 24 | "This message should only have plain text") |
| 25 | self.assertEqual(email_msg.attachments, []) |
| 26 | |
| 27 | |
| 28 | def test_render_mail_html_only(self): |
| 29 | """ |
| 30 | Test that an email created by _render_node with only an html block |
| 31 | resolves to a multipart message with an auto generated plaintext |
| 32 | component. |
| 33 | """ |
| 34 | |
| 35 | email_msg = shortcuts._render_mail("shortcuts/html.tpl", |
| 36 | "person@example.com", |
| 37 | ["people@example.com"]) |
| 38 | self.assertEqual(email_msg.body, |
| 39 | u"This message should be multipart and have an auto generated "+\ |
| 40 | "plaintext component") |
| 41 | self.assertEqual(email_msg.alternatives[0], |
| 42 | (u"<span>This message should be multipart and have " +\ |
| 43 | "an auto generated plaintext component</span>", 'text/html')) |
| 44 | |
| 45 | |
| 46 | def test_render_block_node(self): |
| 47 | """ |
| 48 | Test that _render_block_node renders the named node if it exists, and |
| 49 | returns None otherwise. |
| 50 | """ |
| 51 | |
| 52 | template = loader.get_template("shortcuts/plain_with_context.tpl") |
| 53 | context = Context({"word" : " hello "}) |
| 54 | rendered_block = shortcuts._render_block_node(template, "plain", |
| 55 | context) |
| 56 | self.assertEqual(rendered_block, |
| 57 | "hello") |
| 58 | |
| 59 | empty_block = shortcuts._render_block_node(template, "nonexistent", |
| 60 | context) |
| 61 | self.assertEqual(empty_block, None) |
| 62 | |
| 63 | |
| 64 | def test_get_block_node(self): |
| 65 | """ |
| 66 | Test that _get_block_node gets a named blocknode if it exists and returns |
| 67 | None otherwise. |
| 68 | """ |
| 69 | |
| 70 | template = loader.get_template("shortcuts/plain_and_html.tpl") |
| 71 | block = shortcuts._get_block_node(template, "plain") |
| 72 | self.assertEqual(block.name, "plain") |
| 73 | self.assertEqual(type(block), BlockNode) |
| 74 | |
| 75 | |
| 76 | def test_plaintext_with_files(self): |
| 77 | """ |
| 78 | Test that emails with only a "plain" block but with attached files |
| 79 | are plaintext only but still contain the files within their list of |
| 80 | attachments |
| 81 | """ |
| 82 | |
| 83 | email_msg = shortcuts._render_mail("shortcuts/plain.tpl", |
| 84 | "person@example.com", ["people@example.com"], files=[__file__]) |
| 85 | self.assertEqual(email_msg.body, |
| 86 | "This message should only have plain text") |
| 87 | self.assertEqual(email_msg.attachments, ["tests.py"]) |
| 88 | |
| 89 | def test__create_message_plaintext(self): |
| 90 | """ |
| 91 | Tests that messages created with only plaintext will have all of the |
| 92 | proper fields. |
| 93 | """ |
| 94 | |
| 95 | message = shortcuts._create_message("This is a subject", |
| 96 | "This is a plaintext message", None, "person@example.com", |
| 97 | ["people@example.com"], ["peoplecc@example.com"], |
| 98 | ["peoplebcc@example.com"]) |
| 99 | self.assertEqual(message.subject, "This is a subject") |
| 100 | self.assertEqual(message.body, "This is a plaintext message") |
| 101 | self.assertEqual(message.from_email, "person@example.com") |
| 102 | self.assertEqual(message.to, ["people@example.com"]) |
| 103 | self.assertEqual(message.cc, ["peoplecc@example.com"]) |
| 104 | self.assertEqual(message.bcc, ["peoplebcc@example.com"]) |
| 105 | self.assertEqual(message.attachments, []) |
| 106 | |
| 107 | def test__create_message_html(self): |
| 108 | """ |
| 109 | Tests that messages created with html will have all of the proper fields |
| 110 | and the body should be empty because _create_message is not responsible |
| 111 | for creating a plaintext body from an html message. |
| 112 | """ |
| 113 | |
| 114 | message = shortcuts._create_message("This is a subject", |
| 115 | None, u"<span>This is an html message</span>", "person@example.com", |
| 116 | ["people@example.com"], ["peoplecc@example.com"], |
| 117 | ["peoplebcc@example.com"]) |
| 118 | self.assertEqual(message.subject, "This is a subject") |
| 119 | self.assertEqual(message.body, '') |
| 120 | self.assertEqual(message.from_email, "person@example.com") |
| 121 | self.assertEqual(message.to, ["people@example.com"]) |
| 122 | self.assertEqual(message.cc, ["peoplecc@example.com"]) |
| 123 | self.assertEqual(message.bcc, ["peoplebcc@example.com"]) |
| 124 | self.assertEqual(message.attachments, []) |
| 125 | self.assertEqual(message.alternatives[0], |
| 126 | (u"<span>This is an html message</span>", "text/html")) |
| 127 | |
| 128 | def test_plain_and_html(self): |
| 129 | """ |
| 130 | Tests an email with both html and plain |
| 131 | """ |
| 132 | |
| 133 | email_msg = shortcuts._render_mail("shortcuts/plain_and_html.tpl","person@example.com", |
| 134 | ["people@example.com"]) |
| 135 | self.assertEqual(email_msg.body, |
| 136 | u"This is the plaintext block") |
| 137 | self.assertEqual(email_msg.alternatives, |
| 138 | [(u"<span> This is the html block </span>",'text/html')]) |
| 139 | |
| 140 | def test_create_message_plaintext(self): |
| 141 | """ |
| 142 | Tests if an email is created with plaintext only |
| 143 | """ |
| 144 | |
| 145 | email_msg = shortcuts._create_message('test_subject', 'test_plain', None, |
| 146 | "person@example.com", ["people@example.com"], None) |
| 147 | |
| 148 | self.assertEqual(email_msg.body, 'test_plain') |
| 149 | self.assertEqual(email_msg.attachments, []) |
| 150 | |
| 151 | |
| 152 | def test_create_message_html(self): |
| 153 | """ |
| 154 | Tests if an email is created in html with specified fields |
| 155 | """ |
| 156 | email_msg = shortcuts._create_message('test_subject', None, '<span>test_html</span>', |
| 157 | "person@example.com", ["people@example.com"], None) |
| 158 | |
| 159 | self.assertEqual(email_msg.body, '') |
| 160 | self.assertEqual(email_msg.alternatives, [('<span>test_html</span>', 'text/html')]) |
| 161 | |
| 162 | def test_create_message_html_and_plain_text(self): |
| 163 | |
| 164 | """ |
| 165 | Tests if an email is created in html and plaintext with specified fields |
| 166 | """ |
| 167 | email_msg = shortcuts._create_message('test_subject', 'test_plain', 'test_html', |
| 168 | "person@example.com", ["people@example.com"], None) |
| 169 | |
| 170 | self.assertEqual(email_msg.body, 'test_plain') |
| 171 | self.assertEqual(email_msg.alternatives, [('test_html', 'text/html')]) |
| 172 | No newline at end of file |