diff --git a/tests/unit/test-012-wallace_footer.py b/tests/unit/test-012-wallace_footer.py new file mode 100644 --- /dev/null +++ b/tests/unit/test-012-wallace_footer.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +import os +import pykolab +import unittest + +from wallace import module_footer as Footer + +conf = pykolab.getConf() + +if not hasattr(conf, 'defaults'): + conf.finalize_conf() + + +class TestWallaceFooter(unittest.TestCase): + + def test_001_append_footer_plain(self): + # bottom + content = Footer.append_footer('test', 'footer') + self.assertEqual('test\n\n-- \nfooter', content) + + # top + content = Footer.append_footer('test', 'footer', 'top') + self.assertEqual('footer\n\ntest', content) + + def test_001_append_footer_html(self): + foot = "\n\nfooter\n\n" + + # bottom + content = Footer.append_footer('

test

', 'footer', None, True) + self.assertEqual('

test

' + foot + '', content) + + content = Footer.append_footer('

test

', 'footer', None, True) + self.assertEqual('

test

' + foot + '', content) + + content = Footer.append_footer('

test

', 'footer', None, True) + self.assertEqual('

test

' + foot + '', content) + + # top + content = Footer.append_footer('

test

', 'footer', 'top', True) + self.assertEqual('' + foot + '

test

', content) + + content = Footer.append_footer('

test

', 'footer', 'top', True) + self.assertEqual('' + foot + '

test

', content) + + content = Footer.append_footer('

test

', 'footer', 'top', True) + self.assertEqual('' + foot + '

test

', content) diff --git a/wallace/module_footer.py b/wallace/module_footer.py --- a/wallace/module_footer.py +++ b/wallace/module_footer.py @@ -18,6 +18,7 @@ # import os +import re import tempfile import time @@ -42,6 +43,29 @@ def description(): return """Append a footer to messages.""" +def append_footer(content, footer, position=None, isHtml=False): + if (isHtml): + append = "\n\n" + footer + "\n\n" + if position == 'top': + match = re.search('(]*>)', content, re.IGNORECASE | re.DOTALL) + if match: + content = content.replace(match.group(0), match.group(0) + append) + else: + content = "" + append + content + "" + else: + match = re.search('()', content, re.IGNORECASE | re.DOTALL) + if match: + content = content.replace(match.group(0), append + match.group(0)) + else: + content = "" + content + append + "" + else: + if position == 'top': + content = footer + "\n\n" + content + else: + content += "\n\n-- \n" + footer + + return content + def set_part_content(part, content): # Reset old encoding and use quoted-printable (#5414) del part['Content-Transfer-Encoding'] @@ -87,6 +111,7 @@ footer = {} + footer_position = conf.get('wallace', 'footer_position') footer_html_file = conf.get('wallace', 'footer_html') footer_text_file = conf.get('wallace', 'footer_text') @@ -140,16 +165,12 @@ if content_type == "text/plain": content = part.get_payload(decode=True) - content += "\n\n-- \n%s" % (footer['plain']) + content = append_footer(content, footer['plain'], footer_position, false) footer_added = set_part_content(part, content) elif content_type == "text/html": content = part.get_payload(decode=True) - append = "\n\n" + footer['html'] - if "" in content: - content = content.replace("", append + "") - else: - content = "" + content + append + "" + content = append_footer(content, footer['html'], footer_position, true) footer_added = set_part_content(part, content) if footer_added: