from collective.workspace.interfaces import IWorkspace
from plone import api
from plone.app.testing import MOCK_MAILHOST_FIXTURE
from ploneintranet import api as pi_api
from ploneintranet.search.solr.testing import SolrIntegrationTesting
from ploneintranet.workspace.testing import PloneintranetworkspaceLayer
from ploneintranet.workspace.tests.base import BaseTestCase


class Layer(PloneintranetworkspaceLayer):
    def createPloneContents(self, portal):
        super().createPloneContents(portal)
        pi_api.userprofile.create(
            "demo",
            "demo@example.com",
            approve=True,
            properties={"first_name": "Demo", "last_name": "Example"},
        )
        pi_api.userprofile.create(
            "test_user_1_",
            "test_user_1_@example.com",
            properties={"first_name": "Admin", "last_name": "Example"},
        )
        ws1 = api.content.create(
            container=portal.workspaces,
            title="Workspace private with access",
            type="ploneintranet.workspace.workspacefolder",
        )
        IWorkspace(ws1).add_to_team("demo")
        api.content.create(
            container=portal.workspaces,
            title="Workspace private but visible",
            type="ploneintranet.workspace.workspacefolder",
            visible_to_intranet_users=True,
        )


FIXTURE = Layer()

bases = (FIXTURE, MOCK_MAILHOST_FIXTURE)
LAYER = SolrIntegrationTesting(
    bases=bases, name="PloneintranetworkspaceLayer:Integration"
)


class TestWorkspaceRequestAccess(BaseTestCase):

    layer = LAYER

    def setUp(self):
        super().setUp()
        self.mailhost = self.portal.MailHost

    def test_workspace_request_access_url(self):
        with api.env.adopt_user("demo"):
            view = api.content.get_view(
                name="workspaces.html",
                context=self.portal.workspaces,
                request=self.request.clone(),
            )

            self.assertListEqual(
                [x["url"].partition("/workspaces/")[-1] for x in view.get_workspaces()],
                [
                    "workspace-private-but-visible/@@panel-request-access",
                    "workspace-private-with-access",
                ],
            )

    def test_request_access_panel_request(self):
        ws = self.portal.workspaces["workspace-private-but-visible"]
        with api.env.adopt_user("demo"):
            view = api.content.get_view(
                name="panel-request-access", context=ws, request=self.request.clone()
            )
            view.request.method = "POST"
            view()
            self.assertEqual(
                view.request.response.headers["location"],
                "http://nohost/plone/workspaces",
            )
            self.assertEqual(len(self.mailhost.messages), 2)

    def test_request_access_panel_manage(self):
        ws = self.portal.workspaces["workspace-private-but-visible"]
        with api.env.adopt_user("admin"):
            view = api.content.get_view(
                name="panel-request-access-manage",
                context=ws,
                request=self.request.clone(),
            )
            view.request.method = "POST"
            view.request.form["form.buttons.accept"] = ""
            view.userid = "demo"
            view()
            self.assertEqual(
                view.request.response.headers["location"], ws.absolute_url()
            )
            self.assertEqual(len(self.mailhost.messages), 1)
            view.request.form["form.buttons.decline"] = ""
            view.request.__annotations__.clear()
            view()
            self.assertEqual(
                view.request.response.headers["location"], ws.absolute_url()
            )
            self.assertEqual(len(self.mailhost.messages), 2)
