from plone import api
from ploneintranet.workspace.tests.base import WorkflowBaseTestCase

VIEW = "View"
ACCESS = "Access contents information"
ADD_CONTENT = "Add portal content"


class TestWorkSpaceContainerWorkflow(WorkflowBaseTestCase):
    """
    This tests the workflow of the workspaces container.
    """

    def setUp(self):
        super().setUp()
        self.login_as_portal_owner()
        api.user.create(username="normal_user", email="user@test.com")
        api.user.create(
            username="contributor",
            email="contributor@test.com",
            roles=("Member", "Contributor"),
        )

    def test_open_workspacecontainer(self):
        """
        Check that WorkspaceContainer is by default open: all authenticated
        members can add content
        """
        contributor_permissions = api.user.get_permissions(
            username="contributor", obj=self.workspace_container
        )
        intranet_permissions = api.user.get_permissions(
            username="intranet", obj=self.workspace_container
        )
        extranet_permissions = api.user.get_permissions(
            username="extranet", obj=self.workspace_container
        )

        self.assertTrue(
            contributor_permissions[ADD_CONTENT],
            "Contributor can't add content to an open workspace container",
        )

        self.assertTrue(
            intranet_permissions[ADD_CONTENT],
            "Member can't add content to an open workspace container",
        )
        self.assertTrue(
            intranet_permissions[VIEW], "Members can't view an open workspace container"
        )
        self.assertTrue(
            intranet_permissions[ACCESS],
            "Members can't access an open workspace container",
        )

        self.assertFalse(
            extranet_permissions[ADD_CONTENT],
            "Extranet users shouldn't be able to add content to "
            "an open workspace container",
        )
        self.assertTrue(
            extranet_permissions[VIEW],
            "Extranet users should be able view an open workspace container",
        )
        self.assertTrue(
            extranet_permissions[ACCESS],
            "Extranet users should be able to access an open workspace " "container",
        )

    def test_restricted_workspacecontainer(self):
        """
        Check that a restricted WorkspaceContainer prevents authenticated from
        adding content.
        """
        api.content.transition(obj=self.workspace_container, transition="restrict")
        contributor_permissions = api.user.get_permissions(
            username="contributor", obj=self.workspace_container
        )
        intranet_permissions = api.user.get_permissions(
            username="intranet", obj=self.workspace_container
        )
        extranet_permissions = api.user.get_permissions(
            username="extranet", obj=self.workspace_container
        )

        self.assertTrue(
            contributor_permissions[ADD_CONTENT],
            "Contributor can't add content to a restricted workspace container",
        )

        self.assertFalse(
            intranet_permissions[ADD_CONTENT],
            "Intranet users can add content to a " "restricted workspace container",
        )
        self.assertTrue(
            intranet_permissions[VIEW],
            "Intranet users can't view a restricted workspace container",
        )
        self.assertTrue(
            intranet_permissions[ACCESS],
            "Intranet users can't access a restricted workspace container",
        )

        self.assertFalse(
            extranet_permissions[ADD_CONTENT],
            "Extranet users should not add content to a restricted workspace "
            "container",
        )
        self.assertTrue(
            extranet_permissions[VIEW],
            "Extranet users should be allowed to view a restricted "
            "workspace container",
        )
        self.assertTrue(
            extranet_permissions[ACCESS],
            "Extranet users should be allowed to access a restricted "
            "workspace container",
        )
