# Path: modoboa_amavis/lib.py
# def cleanup_email_address(address):
#     address = parseaddr(address)
#     if address[0]:
#         return "%s <%s>" % address
#     return address[1]
# 
# def make_query_args(address, exact_extension=True, wildcard=None,
#                     domain_search=False):
#     assert isinstance(address, six.text_type),\
#         "address should be of type %s" % six.text_type.__name__
#     conf = dict(param_tools.get_global_parameters("modoboa_amavis"))
#     local_part, domain = split_address(address)
#     if not conf["localpart_is_case_sensitive"]:
#         local_part = local_part.lower()
#     if domain:
#         domain = domain.lstrip("@").rstrip(".")
#         domain = domain.lower()
#         orig_domain = domain
#         domain = idna.encode(domain, uts46=True).decode("ascii")
#     delimiter = conf["recipient_delimiter"]
#     local_part, extension = split_local_part(local_part, delimiter=delimiter)
#     query_args = []
#     if (
#         conf["localpart_is_case_sensitive"] or
#         (domain and domain != orig_domain)
#     ):
#         query_args.append(address)
#     if extension:
#         query_args.append("%s%s%s@%s" % (
#             local_part, delimiter, extension, domain))
#     if delimiter and not exact_extension and wildcard:
#         query_args.append("%s%s%s@%s" % (
#             local_part, delimiter, wildcard, domain))
#     query_args.append("%s@%s" % (local_part, domain))
#     if domain_search:
#         query_args.append("@%s" % domain)
#         query_args.append("@.")
# 
#     return query_args


from django.test import SimpleTestCase
from modoboa.lib.tests import ModoTestCase
from modoboa_amavis.lib import cleanup_email_address, make_query_args
from __future__ import unicode_literals




class MakeQueryArgsTests(ModoTestCase):

    """Tests for modoboa_amavis.lib.make_query_args()."""

    def test_simple_email_address_001(self):
        """Check case insensitive email address without recipient delimiter."""
        self.set_global_parameter("localpart_is_case_sensitive", False)
        self.set_global_parameter("recipient_delimiter", "")
        address = "User+Foo@sub.exAMPLE.COM"
        expected_output = [
            "user+foo@sub.example.com",
        ]
        output = make_query_args(address)
        self.assertEqual(output, expected_output)

    def test_simple_email_address_002(self):
        """Check case sensitive email address with recipient delimiter."""
        self.set_global_parameter("localpart_is_case_sensitive", True)
        self.set_global_parameter("recipient_delimiter", "+")
        address = "User+Foo@sub.exAMPLE.COM"
        expected_output = [
            "User+Foo@sub.exAMPLE.COM",
            "User+Foo@sub.example.com",
            "User@sub.example.com",
        ]

# Path: modoboa_amavis/lib.py
# def cleanup_email_address(address):
#     address = parseaddr(address)
#     if address[0]:
#         return "%s <%s>" % address
#     return address[1]
# 
# def make_query_args(address, exact_extension=True, wildcard=None,
#                     domain_search=False):
#     assert isinstance(address, six.text_type),\
#         "address should be of type %s" % six.text_type.__name__
#     conf = dict(param_tools.get_global_parameters("modoboa_amavis"))
#     local_part, domain = split_address(address)
#     if not conf["localpart_is_case_sensitive"]:
#         local_part = local_part.lower()
#     if domain:
#         domain = domain.lstrip("@").rstrip(".")
#         domain = domain.lower()
#         orig_domain = domain
#         domain = idna.encode(domain, uts46=True).decode("ascii")
#     delimiter = conf["recipient_delimiter"]
#     local_part, extension = split_local_part(local_part, delimiter=delimiter)
#     query_args = []
#     if (
#         conf["localpart_is_case_sensitive"] or
#         (domain and domain != orig_domain)
#     ):
#         query_args.append(address)
#     if extension:
#         query_args.append("%s%s%s@%s" % (
#             local_part, delimiter, extension, domain))
#     if delimiter and not exact_extension and wildcard:
#         query_args.append("%s%s%s@%s" % (
#             local_part, delimiter, wildcard, domain))
#     query_args.append("%s@%s" % (local_part, domain))
#     if domain_search:
#         query_args.append("@%s" % domain)
#         query_args.append("@.")
# 
#     return query_args


# Path: modoboa_amavis/tests/test_lib.py
from django.test import SimpleTestCase
from modoboa.lib.tests import ModoTestCase
from modoboa_amavis.lib import cleanup_email_address, make_query_args

from __future__ import unicode_literals




class MakeQueryArgsTests(ModoTestCase):

    """Tests for modoboa_amavis.lib.make_query_args()."""

    def test_simple_email_address_001(self):
        """Check case insensitive email address without recipient delimiter."""
        self.set_global_parameter("localpart_is_case_sensitive", False)
        self.set_global_parameter("recipient_delimiter", "")
        address = "User+Foo@sub.exAMPLE.COM"
        expected_output = [
            "user+foo@sub.example.com",
        ]
        output = make_query_args(address)
        self.assertEqual(output, expected_output)

    def test_simple_email_address_002(self):
        """Check case sensitive email address with recipient delimiter."""
        self.set_global_parameter("localpart_is_case_sensitive", True)
        self.set_global_parameter("recipient_delimiter", "+")
        address = "User+Foo@sub.exAMPLE.COM"
        expected_output = [
            "User+Foo@sub.exAMPLE.COM",
            "User+Foo@sub.example.com",
            "User@sub.example.com",
        ]

        output = make_query_args(address)