from borg.localrole.interfaces import ILocalRoleProvider
from plone import api
from ploneintranet import api as pi_api
from ploneintranet.todo.testing import IntegrationTestCase
from zope.component import getAdapter


class TestTodoLocalRoles(IntegrationTestCase):
    def setUp(self):
        self.portal = self.layer["portal"]
        folder = pi_api.userprofile.create("folder")
        self.todo = api.content.create(title="A todo", type="todo", container=folder)
        self.adapter = getAdapter(
            self.todo, ILocalRoleProvider, "todo_assignee_role_provider"
        )

    def test_roles_no_initiator_no_assignee(self):
        self.assertListEqual(self.adapter.getRoles("foo"), [])
        self.assertListEqual(self.adapter.getRoles("bar"), [])
        self.assertListEqual(self.adapter.getAllRoles(), [])

    def test_roles_initiator(self):
        self.todo.initiator = "foo"
        self.assertListEqual(self.adapter.getRoles("foo"), ["Reader"])
        self.assertListEqual(self.adapter.getRoles("bar"), [])
        self.assertListEqual(self.adapter.getAllRoles(), [("foo", ["Reader"])])

    def test_roles_assignee(self):
        self.todo.assignee = "bar"
        self.assertListEqual(self.adapter.getRoles("bar"), ["Reader", "Assignee"])
        self.assertListEqual(self.adapter.getRoles("foo"), [])
        self.assertDictEqual(
            dict(self.adapter.getAllRoles()),
            {"bar": ["Reader", "Assignee"]},
        )

    def test_roles_initiator_and_assignee(self):
        self.todo.initiator = "foo"
        self.todo.assignee = "bar"
        self.assertListEqual(self.adapter.getRoles("bar"), ["Reader", "Assignee"])
        self.assertListEqual(self.adapter.getRoles("foo"), ["Reader"])
        self.assertDictEqual(
            dict(self.adapter.getAllRoles()),
            {"bar": ["Reader", "Assignee"], "foo": ["Reader"]},
        )

    def test_roles_initiator_is_assignee(self):
        self.todo.initiator = "foo"
        self.todo.assignee = "foo"
        self.assertListEqual(self.adapter.getRoles("foo"), ["Reader", "Assignee"])
        self.assertListEqual(self.adapter.getRoles("bar"), [])
        self.assertListEqual(
            self.adapter.getAllRoles(), [("foo", ["Reader", "Assignee"])]
        )
