"""Test UpdateSchema schema.""" import marshmallow import pytest from permissions.validations.schemas import identity @pytest.mark.parametrize( ('schema', 'expected_err'), [ pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': ['BANKING_TAX_BASE_ROLE'], 'tenant': { 'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, }, {'roles_to_attach': ['Missing data for required field.']}, id='Missing roles to attach', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_attach': ['BANKING_TAX_BASE_ROLE'], 'tenant': { 'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, }, {'roles_to_detach': ['Missing data for required field.']}, id='Missing roles to detach', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': ['INSIGHTS_BASE_ROLE'], 'roles_to_attach': ['BANKING_TAX_BASE_ROLE'], 'tenant': {'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6'}, }, {'tenant': {'tenant_type': ['Missing data for required field.']}}, id='Missing tenant type', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': ['INSIGHTS_BASE_ROLE'], 'roles_to_attach': ['BANKING_TAX_BASE_ROLE'], 'tenant': {'tenant_type': 'account'}, }, {'tenant': {'tenant_uuid': ['Missing data for required field.']}}, id='Missing tenant uuid', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': [], 'roles_to_attach': [], 'tenant': { 'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, }, {'_schema': ['Must provide at least one role to attach or detach.']}, id='Roles to detach and roles to attach both empty', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': ['BANKING_TAX_BASE_ROLE', 'INSIGHTS_BASE_ROLE'], 'roles_to_attach': ['BANKING_TAX_BASE_ROLE'], 'tenant': { 'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, }, { '_schema': [ "Role(s) present in both attach and detach lists: {'BANKING_TAX_BASE_ROLE'}" ] }, id='Roles to detach and roles to attach have an item in common', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': [], 'roles_to_attach': ['BANKING_TAX_BASE_ROLE'], 'tenant': { 'tenant_type': 'label_participant', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, }, { '_schema': [ 'Role BANKING_TAX_BASE_ROLE is not supported by the label_participant tenant type.' ] }, id='Invalid role for tenant type', ), pytest.param( { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': [], 'roles_to_attach': ['WORKSTATION_ADMIN_ROLE'], 'tenant': { 'tenant_type': 'label_participant', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, }, { '_schema': [ 'Role WORKSTATION_ADMIN_ROLE is not supported by the label_participant tenant type.' ] }, id='Invalid workstation admin role for label participant tenant type', ), ], ) def test_validates_schema(schema, expected_err): """Test validates_schema for CreateIdentitySchema.""" with pytest.raises(marshmallow.ValidationError) as err: identity.UpdateIdentity().load(schema) assert err.value.messages == expected_err def test_schema_doesnt_validate_roles_to_detach(): """Test validates_schema for CreateIdentitySchema.""" schema = { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', # Not a valid role for label participant tenant type 'roles_to_detach': ['WORKSTATION_CATALOG_ROLE'], 'roles_to_attach': [], 'tenant': { 'tenant_type': 'label_participant', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6', }, } # No error should be raised assert identity.UpdateIdentity().load(schema) def test_workstation_admin_role_valid_for_account(): """Test WORKSTATION_ADMIN_ROLE is valid for account tenant type.""" schema = { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': [], 'roles_to_attach': ['WORKSTATION_ADMIN_ROLE'], 'tenant': {'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6'}, } # No error should be raised assert identity.UpdateIdentity().load(schema) def test_workstation_admin_role_valid_for_subaccount(): """Test WORKSTATION_ADMIN_ROLE is valid for subaccount tenant type.""" schema = { 'identity_uuid': '555df6b6-661b-4d5b-bc0f-df83cf46eb75', 'roles_to_detach': [], 'roles_to_attach': ['WORKSTATION_ADMIN_ROLE'], 'tenant': { 'tenant_type': 'subaccount', 'tenant_uuid': '1fab3fe8-c9a6-488a-9a96-959734a93997', }, } # No error should be raised assert identity.UpdateIdentity().load(schema)