<commit_before>"""Export related test cases."""

from django.core.urlresolvers import reverse

from modoboa.lib.tests import ModoTestCase

from .. import factories


class ExportTestCase(ModoTestCase):

    """Test case for export operations."""

    @classmethod
    def setUpTestData(cls):
        """Create test data."""
        super(ExportTestCase, cls).setUpTestData()
        factories.populate_database()

    def __export_identities(self, idtfilter="", grpfilter=""):
        self.clt.get(
            reverse("admin:_identity_list") +
            "?grpfilter=%s&idtfilter=%s" % (grpfilter, idtfilter)
        )
        return self.clt.post(
            reverse("admin:identity_export"),
            {"filename": "test.csv"}
        )

    def assertListEqual(self, list1, list2):
        list2 = list2.split('\r\n')
        for entry in list1.split('\r\n'):
            if not entry:
                continue
            self.assertIn(entry, list2)

    def test_export_identities(self):
        response = self.__export_identities()
        self.assertListEqual(
            "account;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test.com\r\naccount;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test2.com\r\naccount;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10\r\naccount;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10\r\nalias;<EMAIL>;True;<EMAIL>\r\nforward;<EMAIL>;True;user@external.com\r\ndlist;<EMAIL>;True;t<EMAIL>;test@truc.fr\r\n",
            response.content.strip()
        )

    def test_export_simpleusers(self):
        response = self.__export_identities(
            idtfilter="account", grpfilter="SimpleUsers"
        )
        self.assertListEqual(
            "account;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10\r\naccount;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10",
            response.content.strip()
        )

    def test_export_superadmins(self):
        """A test to validate we only export 1 super admin.

        The password is removed since it is hashed using SHA512-CRYPT.
        """
        response = self.__export_identities(
            idtfilter="account", grpfilter="SuperAdmins"
        )
        elements = response.content.strip().split(";")
        self.assertEqual(len(elements), 9)
        elements[2] = ""
        self.assertEqual(
            ";".join(elements), "account;admin;;;;True;SuperAdmins;;"
        )

    def test_export_domainadmins(self):
        response = self.__export_identities(
            idtfilter="account", grpfilter="DomainAdmins"
        )
        self.assertListEqual(
            "account;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test.com\r\naccount;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test2.com",
            response.content.strip()
        )

    def test_export_aliases(self):
        response = self.__export_identities(idtfilter="alias")
        self.assertEqual(
            response.content.strip(),
            "alias;<EMAIL>;True;<EMAIL>"
        )

    def test_export_forwards(self):
        response = self.__export_identities(idtfilter="forward")
        self.assertEqual(
            response.content.strip(),
            "forward;<EMAIL>;True;<EMAIL>"
        )

    def test_export_dlists(self):
        response = self.__export_identities(idtfilter="dlist")
        self.assertEqual(
            response.content.strip(),
            "dlist;<EMAIL>;True;toto<EMAIL>;<EMAIL>"
        )<commit_msg>Fix unit tests.<commit_after>"""Export related test cases."""

from django.core.urlresolvers import reverse

from modoboa.lib.tests import ModoTestCase

from .. import factories


class ExportTestCase(ModoTestCase):

    """Test case for export operations."""

    @classmethod
    def setUpTestData(cls):
        """Create test data."""
        super(ExportTestCase, cls).setUpTestData()
        factories.populate_database()

    def __export_identities(self, idtfilter="", grpfilter=""):
        self.clt.get(
            reverse("admin:_identity_list") +
            "?grpfilter=%s&idtfilter=%s" % (grpfilter, idtfilter)
        )
        return self.clt.post(
            reverse("admin:identity_export"),
            {"filename": "test.csv"}
        )

    def assertListEqual(self, list1, list2):
        list2 = list2.split('\r\n')
        for entry in list1.split('\r\n'):
            if not entry:
                continue
            self.assertIn(entry, list2)

    def test_export_identities(self):
        response = self.__export_identities()
        self.assertListEqual(
            "account;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test.com\r\naccount;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test2.com\r\naccount;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10\r\naccount;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10\r\nalias;<EMAIL>;True;<EMAIL>@<EMAIL>\r\nforward;<EMAIL>;True;user@external.com\r\ndlist;<EMAIL>;True;<EMAIL>;<EMAIL>\r\n",
            response.content.strip()
        )

    def test_export_simpleusers(self):
        response = self.__export_identities(
            idtfilter="account", grpfilter="SimpleUsers"
        )
        self.assertListEqual(
            "account;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10\r\naccount;<EMAIL>;{PLAIN}toto;;;True;SimpleUsers;<EMAIL>;10",
            response.content.strip()
        )

    def test_export_superadmins(self):
        """A test to validate we only export 1 super admin.

        The password is removed since it is hashed using SHA512-CRYPT.
        """
        response = self.__export_identities(
            idtfilter="account", grpfilter="SuperAdmins"
        )
        elements = response.content.strip().split(";")
        self.assertEqual(len(elements), 9)
        elements[2] = ""
        self.assertEqual(
            ";".join(elements), "account;admin;;;;True;SuperAdmins;;"
        )

    def test_export_domainadmins(self):
        response = self.__export_identities(
            idtfilter="account", grpfilter="DomainAdmins"
        )
        self.assertListEqual(
            "account;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test.com\r\naccount;<EMAIL>;{PLAIN}toto;;;True;DomainAdmins;<EMAIL>;10;test<EMAIL>",
            response.content.strip()
        )

    def test_export_aliases(self):
        response = self.__export_identities(idtfilter="alias")
        self.assertEqual(
            response.content.strip(),
            "alias;<EMAIL>;True;<EMAIL>"
        )

    def test_export_forwards(self):
        response = self.__export_identities(idtfilter="forward")
        self.assertEqual(
            response.content.strip(),
            "forward;<EMAIL>;True;<EMAIL>"
        )

    def test_export_dlists(self):
        response = self.__export_identities(idtfilter="dlist")
        self.assertEqual(
            response.content.strip(),
            "dlist;<EMAIL>;True;<EMAIL>;<EMAIL>"
        )
{'fraction_non_alphanumeric': 0.1448584633160023, 'fraction_numerical': 0.0085210860774119, 'mean_word_length': 2.8217439293598234, 'pattern_counts': {'":': 2, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, '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': '1438151', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2125, 'n_words': 284}