"""Base module for unittesting ploneintranet.search."""

from contextlib import contextmanager
from plone import api
from plone.app import testing
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE

import ploneintranet.search
import unittest

TEST_USER_1_NAME = "icarus"

TEST_USER_1_EMAIL = "icarus@ploneintranet.org"


@contextmanager
def login_session(username):
    """Temporarily login as another user.

    Re-logging-in the previous user after exiting the context.
    """
    portal = api.portal.get()
    prev_login = None
    try:
        if not api.user.is_anonymous():
            prev_login = api.user.get_current().getUserName()
        testing.logout()
        testing.login(portal, username)
        yield username
    finally:
        testing.logout()
        if prev_login:
            testing.login(portal, prev_login)


class PloneintranetSearchLayer(testing.PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        self.loadZCML(package=ploneintranet.search)

    def setUpPloneSite(self, portal):
        self.applyProfile(portal, "ploneintranet.search:default")
        with login_session(testing.TEST_USER_NAME):
            api.user.create(username=TEST_USER_1_NAME, email=TEST_USER_1_EMAIL)
        testing.setRoles(portal, testing.TEST_USER_ID, ["Manager"])

    def tearDownPloneSite(self, portal):
        with api.env.adopt_roles(roles=["Manager"]):
            api.user.delete(username=TEST_USER_1_NAME)


FIXTURE = PloneintranetSearchLayer()
INTEGRATION_TESTING = testing.IntegrationTesting(
    bases=(FIXTURE,), name="PloneintranetSearchLayer:Integration"
)
FUNCTIONAL_TESTING = testing.FunctionalTesting(
    bases=(FIXTURE,), name="PloneintranetSearchLayer:Functional"
)


class IntegrationTestCase(unittest.TestCase):
    """Base class for integration tests."""

    layer = INTEGRATION_TESTING


class FunctionalTestCase(unittest.TestCase):
    """Base class for functional tests."""

    layer = FUNCTIONAL_TESTING

    def _create_content(self, **kw):
        obj = api.content.create(**kw)
        obj.reindexObject()
        self._created.append(obj)
        return obj

    def _delete_content(self, obj):
        api.content.delete(obj=obj, check_linkintegrity=False)

    def setUp(self):
        self._created = []
