<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#   Debian Changes Bot
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#
#   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/>.

import unittest

from io import BytesIO as StringIO
from DebianDevelChangesBot.utils import parse_mail


class TestUtilsParseMail(unittest.TestCase):

    def testSimple(self):
        f = StringIO(b"""\
From: <NAME> <<EMAIL>>
Subject: This is the subject

Simple message body""")

        headers, body = parse_mail(f)

        self.assertEqual(headers['From'], "Chris Lamb <<EMAIL>>")
        self.assertEqual(headers['Subject'], "This is the subject")
        self.assertEqual(len(body), 1)
        self.assertEqual(body[0], "Simple message body")

        self.assertEqual(type(headers['From']), str)
        self.assertEqual(type(headers['Subject']), str)
        self.assertEqual(type(body[0]), str)


    def testLongSubject(self):
        f = StringIO(b"""\
From: <NAME> <<EMAIL>>
Subject: Bug#123456: marked as done (pinafore: Inertial couplings may
 exceed tolerance when docking)

Simple message body""")

        headers, body = parse_mail(f)

        self.assertEqual(headers['Subject'], "Bug#123456: marked as done " \
            "(pinafore: Inertial couplings may exceed tolerance when docking)")

    def testLongLine(self):
        f = StringIO(b"""\
Subject: Subject

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
""")

        headers, body = parse_mail(f)
        self.assertEqual(body[0], ('A' * 73) + ('B' * 73) + ('C' * 73))

    def testNotLongLine(self):
        f = StringIO(b"""\
Subject: Subject

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
""")

        headers, body = parse_mail(f)
        self.assertNotEqual(body, [('A' * 73) + ('B' * 73) + ('C' * 73)])

    def testSpaceAtEndOfLine(self):
        f = StringIO(b"""\
Subject: Subject

Description:=20
""")

        headers, body = parse_mail(f)
        self.assertEqual(body, ['Description: '])

    def testUnicodeHeader(self):
        f = StringIO(b"""\
From: Gon=C3=A9ri Le Bouder

Message body
""")

        headers, body = parse_mail(f)
        self.assertEqual(headers['From'], "Gonéri Le Bouder")
        self.assertEqual(body, ['Message body'])

    def testUnicodeBody(self):
        f = StringIO(b"""\
Subject: Subject line

Gon=C3=A9ri Le Bouder
""")
        headers, body = parse_mail(f)
        self.assertEqual(headers['Subject'], 'Subject line')
        self.assertEqual(body, ["Gon=C3=A9ri Le Bouder"])

    def testUtf8Header(self):
        f = StringIO(b"""\
From: Sebastian =?UTF-8?Q?Dr=C3=B6ge?=

Message body""")

        headers, body = parse_mail(f)
        self.assertEqual(headers['From'], "Sebastian Dröge")
        self.assertEqual(body, ['Message body'])

    def testUtf8Header2(self):
        f = StringIO(b"""\
From: <EMAIL> (Marc =?ISO-8859-1?Q?Poulhi=E8s?=)

Message body""")
        headers, body = parse_mail(f)
        self.assertEqual(headers['From'], "<EMAIL> (Marc Poulhiès)")
        self.assertEqual(body, ['Message body'])

    def testMultipart(self):
        f = StringIO(b"""\
From: <NAME> <<EMAIL>>
To: <NAME> <<EMAIL>>
Cc: <EMAIL>
Content-Type: multipart/signed; micalg=pgp-sha1;
	protocol="application/pgp-signature"; boundary="JlJsEFsx9RQyiX4C"
Content-Disposition: inline<commit_msg>Remove alias of BytesIO as StringIO in tests<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#   Debian Changes Bot
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#
#   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/>.

import unittest

from io import BytesIO
from DebianDevelChangesBot.utils import parse_mail


class TestUtilsParseMail(unittest.TestCase):

    def testSimple(self):
        f = BytesIO(b"""\
From: <NAME> <<EMAIL>>
Subject: This is the subject

Simple message body""")

        headers, body = parse_mail(f)

        self.assertEqual(headers['From'], "<NAME> <<EMAIL>>")
        self.assertEqual(headers['Subject'], "This is the subject")
        self.assertEqual(len(body), 1)
        self.assertEqual(body[0], "Simple message body")

        self.assertEqual(type(headers['From']), str)
        self.assertEqual(type(headers['Subject']), str)
        self.assertEqual(type(body[0]), str)


    def testLongSubject(self):
        f = BytesIO(b"""\
From: <NAME> <<EMAIL>>
Subject: Bug#123456: marked as done (pinafore: Inertial couplings may
 exceed tolerance when docking)

Simple message body""")

        headers, body = parse_mail(f)

        self.assertEqual(headers['Subject'], "Bug#123456: marked as done " \
            "(pinafore: Inertial couplings may exceed tolerance when docking)")

    def testLongLine(self):
        f = BytesIO(b"""\
Subject: Subject

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
""")

        headers, body = parse_mail(f)
        self.assertEqual(body[0], ('A' * 73) + ('B' * 73) + ('C' * 73))

    def testNotLongLine(self):
        f = BytesIO(b"""\
Subject: Subject

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
""")

        headers, body = parse_mail(f)
        self.assertNotEqual(body, [('A' * 73) + ('B' * 73) + ('C' * 73)])

    def testSpaceAtEndOfLine(self):
        f = BytesIO(b"""\
Subject: Subject

Description:=20
""")

        headers, body = parse_mail(f)
        self.assertEqual(body, ['Description: '])

    def testUnicodeHeader(self):
        f = BytesIO(b"""\
From: Gon=C3=A9ri Le Bouder

Message body
""")

        headers, body = parse_mail(f)
        self.assertEqual(headers['From'], "Gonéri Le Bouder")
        self.assertEqual(body, ['Message body'])

    def testUnicodeBody(self):
        f = BytesIO(b"""\
Subject: Subject line

Gon=C3=A9ri Le Bouder
""")
        headers, body = parse_mail(f)
        self.assertEqual(headers['Subject'], 'Subject line')
        self.assertEqual(body, ["Gon=C3=A9ri Le Bouder"])

    def testUtf8Header(self):
        f = BytesIO(b"""\
From: Sebastian =?UTF-8?Q?Dr=C3=B6ge?=

Message body""")

        headers, body = parse_mail(f)
        self.assertEqual(headers['From'], "<NAME>")
        self.assertEqual(body, ['Message body'])

    def testUtf8Header2(self):
        f = BytesIO(b"""\
From: <EMAIL> (Marc =?ISO-8859-1?Q?Poulhi=E8s?=)

Message body""")
        headers, body = parse_mail(f)
        self.assertEqual(headers['From'], "<EMAIL> (<NAME>)")
        self.assertEqual(body, ['Message body'])

    def testMultipart(self):
        f = BytesIO(b"""\
From: <NAME> <<EMAIL>>
To: <NAME> <<EMAIL>>
Cc: <EMAIL>
Content-Type: multipart/signed; micalg=pgp-sha1;
	protocol="application/pgp-signature"; boundary="JlJsEFsx9RQyiX4C"
Content-Disposition: inline
{'fraction_non_alphanumeric': 0.12782394943228373, 'fraction_numerical': 0.013344258457216434, 'mean_word_length': 3.808103545301069, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6320610', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2672, 'n_words': 707}