"""Test AddIdentityProfileResourcesSchema.""" import pytest from marshmallow import ValidationError from permissions.constants import constants from permissions.validations.schemas.add_identity_profile_resources import ( AddIdentityProfileResourcesSchema, ) @pytest.mark.parametrize( ('schema', 'expected_err'), [ # valid data [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-7123'} ], }, False, ], [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'Vendor', 'roles': ['accounting', 'analytics'], 'uuid': '7123', }, {'resource_type': 'Subaccount', 'roles': ['catalog'], 'uuid': 'uuid-8869'}, { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], }, False, ], [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], 'brand': 'awal', }, False, ], # invalid data [ { 'identity': {'name': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'Vendor', 'roles': ['accounting', 'analytics'], 'uuid': '7123', }, {'resource_type': 'Subaccount', 'roles': ['catalog'], 'uuid': 'uuid-8869'}, { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], }, {'identity': {'email': ['Missing data for required field.']}}, ], [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [], }, {'resource_access': ['Shorter than minimum length 1.']}, ], [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'Foo', 'roles': ['accounting', 'analytics'], 'uuid': 'uuid-7123', }, {'resource_type': 'Subaccount', 'roles': ['catalog'], 'uuid': 'uuid-8869'}, { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], }, { 'resource_access': { 0: { 'resource_type': [ f"Must be one of: {', '.join(constants.SETTINGS_SUPPORT_MAPPING['resourceTypes'])}." ] } } }, ], [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': [], 'uuid': 'uuid-7123'}, {'resource_type': 'Subaccount', 'roles': ['catalog'], 'uuid': 'uuid-8869'}, { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], }, {'resource_access': {0: {'roles': ['Shorter than minimum length 1.']}}}, ], [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], 'brand': 'foo', }, {'brand': [f"Must be one of: {', '.join(constants.PROFILE_BRANDS)}."]}, ], ], ) def test_pre_load(schema, expected_err): """Test pre_load for AddIdentityProfileResourcesSchema.""" try: AddIdentityProfileResourcesSchema().load(schema) assert not expected_err except ValidationError as err: assert err.messages == expected_err @pytest.mark.parametrize( ('schema', 'loaded_data'), [ # only mandatory param [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'Vendor', 'roles': ['accounting', 'analytics'], 'uuid': '7123', }, {'resource_type': 'Subaccount', 'roles': ['catalog'], 'uuid': 'uuid-8869'}, { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], }, { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'Vendor', 'roles': ['accounting', 'analytics'], 'uuid': '7123', }, {'resource_type': 'Subaccount', 'roles': ['catalog'], 'uuid': 'uuid-8869'}, { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], 'create_auth0_user': True, 'send_password_reset': True, 'set_email_verified': True, 'overwrite_existing_access': True, 'master_contact': False, 'user_metadata': {}, }, ], # with brand [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], 'brand': 'awal', }, { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ { 'resource_type': 'LabelParticipant', 'roles': ['analytics'], 'uuid': 'uuid-8869', }, ], 'brand': 'awal', 'create_auth0_user': True, 'send_password_reset': True, 'set_email_verified': True, 'overwrite_existing_access': True, 'master_contact': False, 'user_metadata': {}, }, ], # with create auth0 True [ { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-7123'} ], 'create_auth0_user': True, }, { 'identity': {'name': 'foo@bar.com', 'email': 'foo@bar.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-7123'} ], 'create_auth0_user': True, 'send_password_reset': True, 'set_email_verified': True, 'overwrite_existing_access': True, 'master_contact': False, 'user_metadata': {}, }, ], # with create auth0 False [ { 'identity': {'name': 'foo@awal.com', 'email': 'foo@awal.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-new'} ], 'create_auth0_user': False, }, { 'identity': {'name': 'foo@awal.com', 'email': 'foo@awal.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-new'} ], 'create_auth0_user': False, 'set_email_verified': False, 'send_password_reset': False, 'overwrite_existing_access': True, 'master_contact': False, 'user_metadata': {}, }, ], # with user_metadata [ { 'identity': {'name': 'foo@awal.com', 'email': 'foo@awal.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-new'} ], 'create_auth0_user': False, 'user_metadata': {'foo': 'bar'}, }, { 'identity': {'name': 'foo@awal.com', 'email': 'foo@awal.com'}, 'resource_access': [ {'resource_type': 'Vendor', 'roles': ['analytics'], 'uuid': 'uuid-new'} ], 'create_auth0_user': False, 'set_email_verified': False, 'send_password_reset': False, 'user_metadata': {'foo': 'bar'}, 'master_contact': False, 'overwrite_existing_access': True, }, ], ], ) def test_pre_load_valid_data(schema, loaded_data): """Test pre_load for AddIdentityProfileResourcesSchema.""" actual = AddIdentityProfileResourcesSchema().load(schema) assert actual == loaded_data