| Server IP : 85.214.239.14 / Your IP : 216.73.216.212 Web Server : Apache/2.4.65 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Mon Sep 30 15:36:27 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /srv/modoboa/env/lib64/python3.5/site-packages/modoboa_webmail/tests/ |
Upload File : |
# coding: utf-8
"""FETCH parser tests."""
from __future__ import print_function
import unittest
import six
from modoboa_webmail.lib.fetch_parser import FetchResponseParser
from . import data
def dump_bodystructure(fp, bs, depth=0):
"""Dump a parsed BODYSTRUCTURE."""
indentation = " " * (depth * 4)
for mp in bs:
if isinstance(mp, list):
if isinstance(mp[0], list):
print("{}multipart/{}".format(indentation, mp[1]), file=fp)
dump_bodystructure(fp, mp, depth + 1)
else:
dump_bodystructure(fp, mp, depth)
elif isinstance(mp, dict):
if isinstance(mp["struct"][0], list):
print("{}multipart/{}".format(
indentation, mp["struct"][1]), file=fp)
dump_bodystructure(fp, mp["struct"][0], depth + 1)
else:
print("{}{}/{}".format(
indentation, *mp["struct"][:2]), file=fp)
fp.seek(0)
result = fp.read()
return result
class FetchParserTestCase(unittest.TestCase):
"""Test FETCH parser."""
def setUp(self):
"""Setup test env."""
self.parser = FetchResponseParser()
def _test_bodystructure_output(self, bs, expected):
"""."""
r = self.parser.parse(bs)
fp = six.StringIO()
output = dump_bodystructure(fp, r[list(r.keys())[0]]["BODYSTRUCTURE"])
fp.close()
self.assertEqual(output, expected)
return r
def test_parse_bodystructure(self):
"""Test the parsing of several responses containing BS."""
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_1, """multipart/alternative
text/plain
text/html
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_2, """multipart/mixed
text/plain
message/rfc822
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_3, """multipart/mixed
multipart/alternative
text/plain
text/html
application/pdf
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_4, """multipart/mixed
multipart/alternative
text/plain
text/html
application/octet-stream
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_5, """multipart/alternative
text/plain
text/html
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_6, """multipart/mixed
multipart/related
multipart/alternative
text/plain
text/html
image/png
image/jpeg
application/pdf
multipart/alternative
text/plain
text/html
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_7, """multipart/mixed
multipart/mixed
text/plain
application/octet-stream
""")
self._test_bodystructure_output(
data.BODYSTRUCTURE_SAMPLE_8, "text/html\n")