from plone import api
from plone.app.testing import applyProfile
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from ploneintranet import api as pi_api
from ploneintranet.userprofile.testing import PLONEINTRANET_USERPROFILE_FIXTURE
from ploneintranet.userprofile.tests.base import BaseTestCase
from Products.CMFPlone.utils import get_installer

TEST_AVATAR_FILENAME = "test_avatar.jpg"


class Layer(PloneSandboxLayer):

    defaultBases = (PLONEINTRANET_USERPROFILE_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        import ploneintranet.todo

        self.loadZCML(package=ploneintranet.todo)

    def setUpPloneSite(self, portal):
        applyProfile(portal, "ploneintranet.todo:default")


FIXTURE = Layer()

INTEGRATION_TESTING = IntegrationTesting(
    bases=(FIXTURE,),
    name="PloneintranetuserprofileWithTodoLayer:Integration",
)


class TestPersonalTasks(BaseTestCase):

    layer = INTEGRATION_TESTING

    def setUp(self):
        super().setUp()
        username1 = "johndoe"
        self.profile1 = pi_api.userprofile.create(username=username1)
        self.installer = get_installer(self.portal, self.portal.REQUEST)
        self.installer.install_product("ploneintranet.todo")
        with api.env.adopt_user(username=username1):
            self.todo = api.content.create(
                title="Personal task", container=self.profile1, type="todo"
            )

    def test_personal_task_allowed(self):
        """Check that we can create personal tasks"""
        view = api.content.get_view("view", self.todo, self.request)
        self.assertIn("todo_view", repr(view))

    def test_personal_task_workflow(self):
        pw = api.portal.get_tool("portal_workflow")
        self.assertTupleEqual(
            pw.getChainFor(self.todo), ("userprofile_content_workflow", "todo_workflow")
        )

    def test_personal_task_visibility(self):
        """Check that we can view personla tasks if we are assignee"""
        with api.env.adopt_user("admin"):
            api.content.create(
                container=self.profiles,
                type="ploneintranet.userprofile.userprofile",
                id="janedoe",
                username="janedoe",
                first_name="Jane",
                last_name="Doe",
            )

        def _can_view(username):
            # clear borg local role cache
            getattr(self.todo.REQUEST, "__annotations__", {}).clear()
            return api.user.has_permission("View", username=username, obj=self.todo)

        self.assertFalse(_can_view("janedoe"))
        self.assertTrue(_can_view("johndoe"))
        self.todo.assignee = "janedoe"
        self.assertTrue(_can_view("janedoe"))
        self.assertTrue(_can_view("johndoe"))
        self.todo.assignee = "johndoe"
        self.assertFalse(_can_view("janedoe"))
        self.assertTrue(_can_view("johndoe"))
        self.todo.initiator = "janedoe"
        self.assertTrue(_can_view("janedoe"))
        self.assertTrue(_can_view("johndoe"))
