from borg.localrole.interfaces import ILocalRoleProvider
from ploneintranet.userprofile.testing import (
    PLONEINTRANET_USERPROFILE_INTEGRATION_TESTING,
)
from ploneintranet.workspace.config import INTRANET_USERS_GROUP_ID
from zope.component import getAdapter
from zope.component import getAdapters

import unittest


class TestBehaviorTestCase(unittest.TestCase):
    layer = PLONEINTRANET_USERPROFILE_INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer["portal"]

    def test_maintainer_group_list_local_roles(self):
        """Check that the maintainer group list local role if an object has
        the related behavior
        """
        # Add the behavior to the fti of an existent content
        fti_id = "ploneintranet.layout.appscontainer"
        behavior_name = "quaive.maintainer_group_list"
        self.portal.portal_types[fti_id].behaviors += (behavior_name,)

        adapters = getAdapters((self.portal.apps,), ILocalRoleProvider)
        self.assertSetEqual(
            {name for name, adapter in adapters},
            {"default", "maintainer_group_list_role_provider"},
        )

        # By default no special permission is granted for the group
        adapter = getAdapter(
            self.portal.apps, ILocalRoleProvider, "maintainer_group_list_role_provider"
        )
        self.assertListEqual(adapter.getRoles(INTRANET_USERS_GROUP_ID), [])

        # but we can change that
        self.portal.apps.maintainer_group_list = {INTRANET_USERS_GROUP_ID}
        self.assertSetEqual(
            set(adapter.getRoles(INTRANET_USERS_GROUP_ID)),
            {"Contributor", "Editor", "Reviewer", "Reader"},
        )

    def test_reader_group_list_local_roles(self):
        """Check that the reader group list local role if an object has
        the related behavior
        """
        # Add the behavior to the fti of an existent content
        fti_id = "ploneintranet.layout.appscontainer"
        behavior_name = "quaive.reader_group_list"
        self.portal.portal_types[fti_id].behaviors += (behavior_name,)

        adapters = getAdapters((self.portal.apps,), ILocalRoleProvider)
        self.assertSetEqual(
            {name for name, adapter in adapters},
            {"default", "reader_group_list_role_provider"},
        )

        # By default no special permission is granted for the group
        adapter = getAdapter(
            self.portal.apps, ILocalRoleProvider, "reader_group_list_role_provider"
        )
        self.assertListEqual(adapter.getRoles(INTRANET_USERS_GROUP_ID), [])

        # but we can change that
        self.portal.apps.reader_group_list = {INTRANET_USERS_GROUP_ID}
        self.assertSetEqual(set(adapter.getRoles(INTRANET_USERS_GROUP_ID)), {"Reader"})

    def test_contributor_group_list_local_roles(self):
        """Check that the contributor group list local role if an object has
        the related behavior
        """
        # Add the behavior to the fti of an existent content
        fti_id = "ploneintranet.layout.appscontainer"
        behavior_name = "quaive.contributor_group_list"
        self.portal.portal_types[fti_id].behaviors += (behavior_name,)

        adapters = getAdapters((self.portal.apps,), ILocalRoleProvider)
        self.assertSetEqual(
            {name for name, adapter in adapters},
            {"default", "contributor_group_list_role_provider"},
        )

        # By default no special permission is granted for the group
        adapter = getAdapter(
            self.portal.apps, ILocalRoleProvider, "contributor_group_list_role_provider"
        )
        self.assertListEqual(adapter.getRoles(INTRANET_USERS_GROUP_ID), [])

        # but we can change that
        self.portal.apps.contributor_group_list = {INTRANET_USERS_GROUP_ID}
        self.assertSetEqual(
            set(adapter.getRoles(INTRANET_USERS_GROUP_ID)), {"Contributor"}
        )
