"""Test RolesListHelper.""" from typing import List import pytest from pdp.fastapi.schemas.identity import Role from pdp.utils.roles import RolesListHelper @pytest.fixture() def existing_roles() -> List[Role]: """Return a list of roles.""" return [ Role(role="beekeeper"), Role(role="dog walker"), Role(role="software engineer"), ] def test_attach(existing_roles: List[Role]) -> None: """Test attach.""" roles_after_attach = ( RolesListHelper(existing_roles) .attach( [ Role(role="beekeeper", **{"breeds": ["honey", "bumble"]}), Role(role="cat sitter"), ] ) .get_roles_list() ) assert roles_after_attach == [ Role(role="beekeeper", **{"breeds": ["honey", "bumble"]}), Role(role="dog walker"), Role(role="software engineer"), Role(role="cat sitter"), ] def test_detach(existing_roles: List[Role]) -> None: """Test detach.""" roles_after_detach = ( RolesListHelper(existing_roles) .detach( [ Role(role="beekeeper", **{"breeds": ["honey", "bumble"]}), Role(role="cat sitter"), ] ) .get_roles_list() ) assert roles_after_detach == [ Role(role="dog walker"), Role(role="software engineer"), ] def test_attach_and_then_detach_chain(existing_roles: List[Role]) -> None: """Test attach and detach operations can be chained. Note that this test is very similar to test_detach_and_then_attach_chain, but the order of operations is different, and thus the output is different. """ roles_after_chained_ops = ( RolesListHelper(existing_roles) .attach( [ Role(role="beekeeper", **{"other": "attribute"}), Role(role="zookeeper"), ] ) .detach( [ Role(role="beekeeper", **{"breeds": ["honey", "bumble"]}), Role(role="cat sitter"), ] ) .get_roles_list() ) assert roles_after_chained_ops == [ Role(role="dog walker"), Role(role="software engineer"), Role(role="zookeeper"), ] def test_detach_and_then_attach_chain(existing_roles: List[Role]) -> None: """Test detach and attach operations can be chained. Note that this test is very similar to test_attach_and_then_detach_chain, but the order of operations is different, and thus the output is different. """ roles_after_chained_ops = ( RolesListHelper(existing_roles) .detach( [ Role(role="beekeeper", **{"breeds": ["honey", "bumble"]}), Role(role="cat sitter"), ] ) .attach( [ Role(role="beekeeper", **{"other": "attribute"}), Role(role="zookeeper"), ] ) .get_roles_list() ) assert roles_after_chained_ops == [ Role(role="dog walker"), Role(role="software engineer"), Role(role="beekeeper", **{"other": "attribute"}), Role(role="zookeeper"), ] def test_fully_detach(existing_roles: List[Role]) -> None: """Test sending all the existing roles will detach all roles.""" roles_after_chained_ops = ( RolesListHelper(existing_roles).detach(existing_roles).get_roles_list() ) assert roles_after_chained_ops == [] def test_detach_something_that_is_not_there(existing_roles: List[Role]) -> None: """Test detaching a role that is not present has no impact.""" roles_after_chained_ops = ( RolesListHelper(existing_roles) .detach([Role(role="not an existing role")]) .get_roles_list() ) assert roles_after_chained_ops == existing_roles