"""Tests for create internal identity schema validation.""" import pytest from marshmallow import ValidationError from permissions.constants import application, constants, parent_companies from permissions.validations.schemas import identity as identity_schemas VALID_CREATE_BODY = { 'first_name': '🐶', 'last_name': '🦴', 'email': 'nugget@theorchard.com', 'brand': constants.THEORCHARD_BRAND, 'tenant': { 'tenant_type': constants.PARENT_COMPANY_TENANT_TYPE, 'tenant_uuid': parent_companies.ORCHARD_PARENT_COMPANY_UUID, }, 'roles_to_attach': [application.SETTINGS_BASE_ROLE, application.INSIGHTS_BASE_ROLE], } @pytest.mark.parametrize( ['body', 'expected_error'], [ pytest.param( {**VALID_CREATE_BODY, 'first_name': None}, {'first_name': ['Field may not be null.']}, id='Missing first name', ), pytest.param( {**VALID_CREATE_BODY, 'last_name': None}, {'last_name': ['Field may not be null.']}, id='Missing last name', ), pytest.param( {**VALID_CREATE_BODY, 'email': None}, {'email': ['Field may not be null.']}, id='Missing email', ), pytest.param( {**VALID_CREATE_BODY, 'email': '🌭'}, {'email': ['Not a valid email address.']}, id='Invalid email', ), pytest.param( {**VALID_CREATE_BODY, 'email': 'nugget@gmail.dog'}, {'_schema': ['Email domain gmail.dog is not valid for employee identities.']}, id='Disallowed email domain', ), pytest.param( {**VALID_CREATE_BODY, 'brand': '🐻‍❄️'}, {'brand': ['Must be one of: theorchard, sme, awal, knr.']}, id='Invalid brand', ), pytest.param( { **VALID_CREATE_BODY, 'tenant': { 'tenant_type': 'subaccount', 'tenant_uuid': parent_companies.ORCHARD_PARENT_COMPANY_UUID, }, }, {'tenant': {'tenant_type': ['Must be one of: parent_company, account.']}}, id='Unsupported tenant type', ), pytest.param( { **VALID_CREATE_BODY, 'tenant': { 'tenant_type': constants.PARENT_COMPANY_TENANT_TYPE, 'tenant_uuid': '1c1d256d-fedb-4a71-a96d-8373b8e20db7', }, }, { 'tenant': { '_schema': [ 'Tenant uuid 1c1d256d-fedb-4a71-a96d-8373b8e20db7 ' 'is not a valid parent company uuid.' ] } }, id='Invalid parent company tenant uuid', ), pytest.param( {**VALID_CREATE_BODY, 'roles_to_attach': []}, {'roles_to_attach': ['Shorter than minimum length 1.']}, id='Empty roles list', ), pytest.param( {**VALID_CREATE_BODY, 'roles_to_attach': [application.BANKING_TAX_BASE_ROLE]}, { 'roles_to_attach': { 0: [ 'Must be one of: INSIGHTS_BASE_ROLE, SETTINGS_BASE_ROLE, fansifter_can_view_fan_data, songwhip_read.' ] } }, id='Unsupported role', ), pytest.param( { **VALID_CREATE_BODY, 'brand': constants.THEORCHARD_BRAND, 'roles_to_attach': [ application.FANSIFTER_BASE_ROLE, application.SONGWHIP_READ_ROLE, ], }, { '_schema': [ 'Roles not supported by the parent_company tenant type: fansifter_can_view_fan_data, songwhip_read' ] }, id='Parent company tenant with account roles', ), pytest.param( {key: value for key, value in VALID_CREATE_BODY.items() if key != 'brand'}, {'_schema': ['Brand is required for parent company tenant type.']}, id='Parent company tenant without brand', ), pytest.param( { 'first_name': '🐶', 'last_name': '🦴', 'email': 'nugget@theorchard.com', 'tenant': { 'tenant_type': constants.ACCOUNT_TENANT_TYPE, 'tenant_uuid': 'some-account-uuid', }, 'roles_to_attach': [application.FANSIFTER_BASE_ROLE], }, None, id='Valid account tenant without brand', ), pytest.param( { 'first_name': '🐶', 'last_name': '🦴', 'email': 'nugget@theorchard.com', 'tenant': { 'tenant_type': constants.ACCOUNT_TENANT_TYPE, 'tenant_uuid': 'some-account-uuid', }, 'roles_to_attach': [application.INSIGHTS_BASE_ROLE], }, {'_schema': ['Roles not supported by the account tenant type: INSIGHTS_BASE_ROLE']}, id='Account tenant with parent company role', ), pytest.param(VALID_CREATE_BODY, None, id='Valid data'), ], ) def test_create_internal_identity_validation( body: dict[str, str | dict[str, str]], expected_error: dict | None, ) -> None: """Test validation for creating an internal identity.""" try: identity_schemas.CreateInternalIdentity().load(body) assert expected_error is None except ValidationError as err: assert err.messages == expected_error def test_lowercase_email_and_strip_spaces() -> None: """Test that email is lowercased and spaces are stripped from names.""" input_data = { **VALID_CREATE_BODY, 'first_name': ' 🐶 ', 'last_name': ' 🦴 ', 'email': 'NUGGET@THEORCHARD.COM', } result = identity_schemas.CreateInternalIdentity().load(input_data) assert result['first_name'] == '🐶' assert result['last_name'] == '🦴' assert result['email'] == 'nugget@theorchard.com'