2012-08-13 14:22:32 +00:00
|
|
|
#!/usr/bin/env python
|
2012-08-14 12:14:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-11-06 12:18:24 +00:00
|
|
|
# This test can be run stand-alone with something like:
|
|
|
|
# > PYTHONPATH=. python2 openerp/tests/test_misc.py
|
2012-11-06 11:54:20 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Business Applications
|
|
|
|
# Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-11-06 12:18:24 +00:00
|
|
|
import unittest2
|
|
|
|
from openerp.tools.mail import html_sanitize, html_email_clean, append_content_to_html
|
2012-08-13 14:22:32 +00:00
|
|
|
|
|
|
|
test_case = """
|
|
|
|
<font size="2" style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; ">test1</font>
|
|
|
|
<div style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; font-size: 12px; font-style: normal; ">
|
|
|
|
<b>test2</b></div><div style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; font-size: 12px; ">
|
|
|
|
<i>test3</i></div><div style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; font-size: 12px; ">
|
|
|
|
<u>test4</u></div><div style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; font-size: 12px; ">
|
|
|
|
<strike>test5</strike></div><div style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; ">
|
|
|
|
<font size="5">test6</font></div><div><ul><li><font color="#1f1f1f" face="monospace" size="2">test7</font></li><li>
|
|
|
|
<font color="#1f1f1f" face="monospace" size="2">test8</font></li></ul><div><ol><li><font color="#1f1f1f" face="monospace" size="2">test9</font>
|
|
|
|
</li><li><font color="#1f1f1f" face="monospace" size="2">test10</font></li></ol></div></div>
|
|
|
|
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div><font color="#1f1f1f" face="monospace" size="2">
|
|
|
|
test11</font></div></div></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;">
|
|
|
|
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font color="#1f1f1f" face="monospace" size="2">
|
|
|
|
test12</font></div><div><font color="#1f1f1f" face="monospace" size="2"><br></font></div></blockquote></blockquote>
|
|
|
|
<font color="#1f1f1f" face="monospace" size="2"><a href="http://google.com">google</a></font>
|
2012-08-13 15:52:05 +00:00
|
|
|
<a href="javascript:alert('malicious code')">test link</a>
|
2012-08-13 14:22:32 +00:00
|
|
|
"""
|
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
GMAIL_REPLY_SAN = """<div>Réponse via thunderbird, classique.<br><br>
|
|
|
|
On 11/05/2012 10:51 AM, Raoul Tartopoils wrote:<br></div>
|
2012-11-06 12:18:24 +00:00
|
|
|
<blockquote>
|
|
|
|
<div>Plop !</div>
|
|
|
|
<ul><li>Vive les lapins rapides !<br></li>
|
|
|
|
<li>Nouille</li>
|
|
|
|
<li>Frites</li>
|
|
|
|
</ul><div><br></div>
|
|
|
|
<div>Clairement, hein ?</div>
|
|
|
|
-- <br>
|
2012-11-07 09:32:01 +00:00
|
|
|
Raoul Tartopoils<br></blockquote>
|
2012-11-06 12:18:24 +00:00
|
|
|
<br><br><pre>--
|
2012-11-07 09:32:01 +00:00
|
|
|
Raoul Tartopoils
|
2012-11-06 12:18:24 +00:00
|
|
|
</pre>"""
|
|
|
|
|
|
|
|
GMAIL_REPLY2_SAN = """<div>Je réponds, hop, via thunderbird. Mais
|
|
|
|
je vais répodnre aussi au milieu du thread.<br><br>
|
2012-11-07 09:32:01 +00:00
|
|
|
On 11/05/2012 10:53 AM, Raoul Tartopoils wrote:<br></div>
|
2012-11-06 12:18:24 +00:00
|
|
|
<blockquote>Reply rapide de gmail.</blockquote>
|
|
|
|
<br>
|
|
|
|
Jamais.<br><br><blockquote>
|
|
|
|
<div><br><br><div>2012/11/5 Thibault Delavallée <span><<a href="mailto:tde@openerp.com">tde@openerp.com</a>></span><br><blockquote>
|
|
|
|
<div>
|
|
|
|
<div>Réponse via thunderbird, classique.
|
|
|
|
<div>
|
|
|
|
<div><br><br>
|
2012-11-07 09:32:01 +00:00
|
|
|
On 11/05/2012 10:51 AM, Raoul Tartopoils wrote:<br></div>
|
2012-11-06 12:18:24 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<blockquote>
|
|
|
|
<div>Plop !</div>
|
|
|
|
<ul><li>Vive les lapins rapides !<br></li>
|
|
|
|
<li>Nouille</li>
|
|
|
|
</ul></blockquote>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</blockquote>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</blockquote>
|
|
|
|
je rajotuerais bien pommes de terre dans la liste.<br><blockquote>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<blockquote>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<blockquote>
|
|
|
|
<ul><li>Frites</li>
|
|
|
|
</ul><div><br></div>
|
|
|
|
<div>Clairement, hein ?</div>
|
|
|
|
-- <br>
|
2012-11-07 09:32:01 +00:00
|
|
|
Raoul Tartopoils<br></blockquote>
|
2012-11-06 12:18:24 +00:00
|
|
|
<br><br></div>
|
|
|
|
</div>
|
|
|
|
<span><font>
|
|
|
|
<pre>--
|
2012-11-07 09:32:01 +00:00
|
|
|
Raoul Tartopoils
|
2012-11-06 12:18:24 +00:00
|
|
|
</pre>
|
|
|
|
</font></span></div>
|
|
|
|
</blockquote>
|
|
|
|
</div>
|
|
|
|
<br><br><div><br></div>
|
|
|
|
-- <br>
|
2012-11-07 09:32:01 +00:00
|
|
|
Raoul Tartopoils<br></div>
|
2012-11-06 12:18:24 +00:00
|
|
|
</blockquote>
|
|
|
|
<br><br><pre>--
|
2012-11-07 09:32:01 +00:00
|
|
|
Raoul Tartopoils
|
2012-11-06 12:18:24 +00:00
|
|
|
</pre>"""
|
|
|
|
|
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
TEXT_TPL = """Salut Raoul!
|
|
|
|
Le 28 oct. 2012 à 00:02, Raoul Grosbedon a écrit :
|
2012-11-06 12:18:24 +00:00
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
> C'est sûr que je suis intéressé (quote)!
|
2012-11-06 12:18:24 +00:00
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
Trouloulou pouet pouet.
|
2012-11-06 12:18:24 +00:00
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
Je ne vais quand même pas écrire de vrais mails, non mais ho.
|
2012-11-06 12:18:24 +00:00
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
> 2012/10/27 Bert Tartopoils :
|
|
|
|
>> Diantre, me disè-je en envoyant un message similaire à Martine, mais comment vas-tu (quote)?
|
2012-11-06 12:18:24 +00:00
|
|
|
>>
|
2012-11-07 09:32:01 +00:00
|
|
|
>> A la base le contenu était un vrai mail, mais je l'ai quand même réécrit pour ce test, histoire de dire que, quand même, on ne met pas n'importe quoi ici. (quote)
|
2012-11-06 12:18:24 +00:00
|
|
|
>>
|
2012-11-07 09:32:01 +00:00
|
|
|
>> Et sinon bon courage pour trouver tes clefs (quote).
|
2012-11-06 12:18:24 +00:00
|
|
|
>>
|
2012-11-07 09:32:01 +00:00
|
|
|
>> Bert TARTOPOILS
|
|
|
|
>> bert.tartopoils@miam.miam
|
2012-11-06 12:18:24 +00:00
|
|
|
>>
|
|
|
|
>
|
|
|
|
>
|
|
|
|
>
|
|
|
|
> --
|
2012-11-07 09:32:01 +00:00
|
|
|
> Raoul Grosbedon
|
2012-11-06 12:18:24 +00:00
|
|
|
|
2012-11-07 09:32:01 +00:00
|
|
|
Bert TARTOPOILS
|
|
|
|
bert.tartopoils@miam.miam
|
2012-11-06 12:18:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class TestAppendContentToHtml(unittest2.TestCase):
|
|
|
|
""" Test some of our generic utility functions """
|
|
|
|
|
|
|
|
def test_append_to_html(self):
|
|
|
|
test_samples = [
|
|
|
|
('<!DOCTYPE...><HTML encoding="blah">some <b>content</b></HtMl>', '--\nYours truly', True,
|
|
|
|
'<!DOCTYPE...><html encoding="blah">some <b>content</b>\n<pre>--\nYours truly</pre>\n</html>'),
|
|
|
|
('<html><body>some <b>content</b></body></html>', '<!DOCTYPE...>\n<html><body>\n<p>--</p>\n<p>Yours truly</p>\n</body>\n</html>', False,
|
|
|
|
'<html><body>some <b>content</b>\n\n\n<p>--</p>\n<p>Yours truly</p>\n\n\n</body></html>'),
|
|
|
|
]
|
|
|
|
for html, content, flag, expected in test_samples:
|
|
|
|
self.assertEqual(append_content_to_html(html, content, flag), expected, 'append_content_to_html is broken')
|
|
|
|
|
|
|
|
|
|
|
|
class TestSanitizer(unittest2.TestCase):
|
2012-11-06 11:54:20 +00:00
|
|
|
# TDE note: could be improved by actually checking the output
|
2012-08-13 14:22:32 +00:00
|
|
|
|
|
|
|
def test_simple(self):
|
|
|
|
x = "yop"
|
|
|
|
self.assertEqual(x, html_sanitize(x))
|
2012-11-06 11:54:20 +00:00
|
|
|
|
2012-09-05 15:32:12 +00:00
|
|
|
def test_trailing_text(self):
|
|
|
|
x = 'lala<p>yop</p>xxx'
|
|
|
|
self.assertEqual(x, html_sanitize(x))
|
2012-11-06 11:54:20 +00:00
|
|
|
|
2012-09-05 15:32:12 +00:00
|
|
|
def test_no_exception(self):
|
2012-08-14 07:14:57 +00:00
|
|
|
html_sanitize(test_case)
|
2012-11-06 11:54:20 +00:00
|
|
|
|
2012-09-05 15:32:12 +00:00
|
|
|
def test_unicode(self):
|
2012-08-14 12:14:25 +00:00
|
|
|
html_sanitize("Merci à l'intérêt pour notre produit.nous vous contacterons bientôt. Merci")
|
2012-08-13 14:22:32 +00:00
|
|
|
|
2012-11-06 11:54:20 +00:00
|
|
|
|
2012-11-06 12:18:24 +00:00
|
|
|
class TestCleaner(unittest2.TestCase):
|
|
|
|
|
|
|
|
def test_gmail(self):
|
2012-11-07 09:32:01 +00:00
|
|
|
# Test1: blahblah
|
|
|
|
new_html = html_email_clean(GMAIL_REPLY_SAN)
|
|
|
|
self.assertNotIn(new_html, 'blockquote')
|
|
|
|
self.assertNotIn(new_html, 'Vive les lapins rapides !')
|
|
|
|
self.assertNotIn(new_html, 'Bert Tartopoils')
|
2012-11-06 11:54:20 +00:00
|
|
|
|
|
|
|
|
2012-08-13 14:22:32 +00:00
|
|
|
if __name__ == '__main__':
|
2012-11-06 12:18:24 +00:00
|
|
|
unittest2.main()
|