"""Lambda test module.""" import dataclasses import textwrap from unittest import mock import pytest from src import app as index, constants, types VALID_EVENT = { 'email': 'kat@dachshund.long', 'first_name': 'Kat', 'last_name': 'Dog', 'tenant_uuid': '2e79b9b8-29ed-44e1-832f-2aa3b47f13c8', 'tenant_type': 'vendor', 'is_admin': 'Y', 'catalog': 'Y', 'marketing': 'Y', 'analytics': 'Y', 'accounting': 'Y', 'collaborators': 'Y', 'banking': 'Y', 'is_master_contact': 'N', 'send_email': 'N', } @mock.patch('src.app.create_identity_mutation_input') @mock.patch('src.app.config.ows_client.post') def test_handler_success(post_mock: mock.MagicMock, create_input_mock: mock.MagicMock) -> None: """Test handler function.""" create_input_result = types.CreateIdentityMutationInput( identity=types.IdentityInput( email=VALID_EVENT['email'], firstName=VALID_EVENT['first_name'], lastName=VALID_EVENT['last_name'], ), tenantProfileRoles=types.TenantProfileRolesInput( tenantUuid=VALID_EVENT['tenant_uuid'], tenantType='ACCOUNT', rolesToAttach=[ 'WORKSTATION_CATALOG_ROLE', 'WORKSTATION_MANAGE_RIGHTS_ROLE', 'WORKSTATION_MARKETING_ROLE', 'INSIGHTS_BASE_ROLE', 'CUSTOMER_ACCOUNTING_BASE_ROLE', 'COLLABORATORS_BASE_ROLE', 'BANKING_TAX_BASE_ROLE', 'SETTINGS_BASE_ROLE', ], ), masterContact=False, sendInvite=False, ) create_input_mock.return_value = create_input_result identity_id = 'im-a-uuid-hi' post_mock.return_value.json.return_value = {'data': {'createIdentity': {'id': identity_id}}} result = index.handler(event=VALID_EVENT, context=None) assert result['email'] == VALID_EVENT['email'] assert result['send_email'] == VALID_EVENT['send_email'] assert result['identity_id'] == identity_id create_input_mock.assert_called_with(VALID_EVENT) post_mock.assert_called_with( 'graphql-router', '/graphql', json={ 'operationName': 'createIdentityV2', 'query': textwrap.dedent(""" mutation createIdentityV2( $identity: IdentityInput!, $tenantProfileRoles: TenantProfileRolesCreateInput!, $masterContact: Boolean, $sendInvite: Boolean ) { createIdentity( identity: $identity, tenantProfileRoles: $tenantProfileRoles masterContact: $masterContact, sendInvite: $sendInvite ) { id } } """), 'variables': dataclasses.asdict(create_input_result), }, headers=constants.HEADERS, ) @mock.patch('src.app.config.logger') @mock.patch('src.app.create_identity_mutation_input') def test_handler_error(create_input_mock: mock.MagicMock, logger_mock: mock.MagicMock) -> None: error_message = '🤬' create_input_mock.side_effect = Exception(error_message) with pytest.raises(Exception) as e: index.handler(event=VALID_EVENT, context=None) assert str(e.value) == error_message logger_mock.exception.assert_called_with('🤬') @pytest.mark.parametrize( ['event', 'resulting_tenant_type', 'resulting_roles'], [ pytest.param( VALID_EVENT, 'ACCOUNT', [ 'WORKSTATION_CATALOG_ROLE', 'WORKSTATION_MANAGE_RIGHTS_ROLE', 'WORKSTATION_MARKETING_ROLE', 'INSIGHTS_BASE_ROLE', 'CUSTOMER_ACCOUNTING_BASE_ROLE', 'COLLABORATORS_BASE_ROLE', 'BANKING_TAX_BASE_ROLE', 'SETTINGS_BASE_ROLE', ], id='valid vendor user all Ys', ), pytest.param( {**VALID_EVENT, 'is_admin': 'N', 'catalog': 'N', 'accounting': 'N'}, 'ACCOUNT', [ 'WORKSTATION_MARKETING_ROLE', 'INSIGHTS_BASE_ROLE', 'BANKING_TAX_BASE_ROLE', 'COLLABORATORS_BASE_ROLE', ], id='valid vendor user some Ys', ), pytest.param( { **VALID_EVENT, 'tenant_type': 'collaborator', 'catalog': 'N', 'marketing': 'N', 'analytics': 'N', }, 'COLLABORATOR', [ 'CUSTOMER_ACCOUNTING_BASE_ROLE', 'COLLABORATORS_BASE_ROLE', 'BANKING_TAX_BASE_ROLE', 'SETTINGS_BASE_ROLE', ], id='valid collaborator user', ), ], ) def test_create_identity_mutation_input( event: dict[str, str], resulting_tenant_type: str, resulting_roles: list[str] ) -> None: result = index.create_identity_mutation_input(event) assert result.identity.email == VALID_EVENT['email'] assert result.identity.firstName == VALID_EVENT['first_name'] assert result.identity.lastName == VALID_EVENT['last_name'] assert result.tenantProfileRoles.tenantUuid == VALID_EVENT['tenant_uuid'] assert result.tenantProfileRoles.tenantType == resulting_tenant_type assert sorted(result.tenantProfileRoles.rolesToAttach) == sorted(resulting_roles) @pytest.mark.parametrize( ['event', 'sendInvite', 'masterContact'], [ pytest.param( {**VALID_EVENT, 'is_master_contact': 'Y', 'send_email': 'Y'}, True, True, id='master contact and send email', ), pytest.param( {**VALID_EVENT, 'is_master_contact': 'N', 'send_email': 'N'}, False, False, id='not master contact and not send email', ), ], ) def test_create_identity_mutation_input_master_contact_send_invite( event: dict[str, str], sendInvite: bool, masterContact: bool ) -> None: result = index.create_identity_mutation_input(event) assert result.masterContact == masterContact assert result.sendInvite == sendInvite @pytest.mark.parametrize( ['bad_event', 'error_message'], [ pytest.param( {**VALID_EVENT, 'tenant_type': 'company_brand'}, 'Invalid tenant type: company_brand', id='Invalid tenant type', ), pytest.param( {**VALID_EVENT, 'tenant_uuid': 'Im-not-a-uuid'}, 'Invalid uuid: im-not-a-uuid', id='Invalid uuid', ), ], ) def test_create_identity_mutation_input_error( bad_event: dict[str, str], error_message: str ) -> None: with pytest.raises(RuntimeError) as e: index.create_identity_mutation_input(bad_event) assert str(e.value) == error_message