# Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe, unittest, os, base64
from frappe.email.email_body import (replace_filename_with_cid,
get_email, inline_style_in_html, get_header)
class TestEmailBody(unittest.TestCase):
def setUp(self):
email_html = '''
Hey John Doe!
This is embedded image you asked for
'''
email_text = '''
Hey John Doe!
This is the text version of this email
'''
img_path = os.path.abspath('assets/frappe/images/favicon.png')
with open(img_path, 'rb') as f:
img_content = f.read()
img_base64 = base64.b64encode(img_content).decode()
# email body keeps 76 characters on one line
self.img_base64 = fixed_column_width(img_base64, 76)
self.email_string = get_email(
recipients=['test@example.com'],
sender='me@example.com',
subject='Test Subject',
content=email_html,
text_content=email_text
).as_string()
def test_image(self):
img_signature = '''
Content-Type: image/png
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="favicon.png"
'''
self.assertTrue(img_signature in self.email_string)
self.assertTrue(self.img_base64 in self.email_string)
def test_text_content(self):
text_content = '''
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Hey John Doe!
This is the text version of this email
'''
self.assertTrue(text_content in self.email_string)
def test_email_content(self):
html_head = '''
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
'''
html = '''Hey John Doe!
'''
self.assertTrue(html_head in self.email_string)
self.assertTrue(html in self.email_string)
def test_replace_filename_with_cid(self):
original_message = '''
'''
message, inline_images = replace_filename_with_cid(original_message)
processed_message = '''
'''.format(inline_images[0].get('content_id'))
self.assertEquals(message, processed_message)
def test_inline_styling(self):
html = '''
Hi John
This is a test email
'''
transformed_html = '''
Hi John
This is a test email
'''
self.assertTrue(transformed_html in inline_style_in_html(html))
def test_email_header(self):
email_html = '''
Hey John Doe!
This is embedded image you asked for
'''
email_string = get_email(
recipients=['test@example.com'],
sender='me@example.com',
subject='Test Subject',
content=email_html,
header=['Email Title', 'green']
).as_string()
self.assertTrue('''''' in email_string)
self.assertTrue('Email Title' in email_string)
def test_get_email_header(self):
html = get_header(['This is test', 'orange'])
self.assertTrue('' in html)
self.assertTrue('This is test' in html)
html = get_header(['This is another test'])
self.assertTrue('This is another test' in html)
html = get_header('This is string')
self.assertTrue('This is string' in html)
def fixed_column_width(string, chunk_size):
parts = [string[0+i:chunk_size+i] for i in range(0, len(string), chunk_size)]
return '\n'.join(parts)