"""Unit tests for permissions logic.""" import uuid import pytest from owsresponse import response from notifications import types from notifications.constants import email as email_constants from notifications.logic import permissions VALID_CASE = { 'type': email_constants.NOTIFICATIONS_DELIVERY_MESSAGE_TYPE_GENERAL, 'user_id': '123e4567-e89b-12d3-a456-426614174000', 'subject': 'Update: Your access for {tenant_name}', 'subject_variables': { 'tenant_name': 'TENANT_NAME', }, 'to': ['1'], 'template': 'branded_user_permissions_updated', 'lang': 'en', 'sender': '"The Orchard Notifications" ', 'variables': { 'admin_identity': { 'first_name': 'Foo', 'last_name': 'Bar', }, 'default_brand': 'theorchard', 'applications': [{'id': 'documents'}], 'tenant_name': 'TENANT_NAME', 'brand_info': {'name': 'orchard', 'logo_url': 'https://logo.url'}, }, } @pytest.mark.parametrize( ('applications_resp', 'identity_resp', 'resource_name_resp', 'org_info_resp', 'expected_resp'), ( pytest.param( response.create_not_found_response('No applications found.'), (None, None), None, None, response.create_not_found_response('No applications found.'), id='no applications found', ), pytest.param( response.Response([{'id': 'settings'}]), (None, None), None, None, response.create_not_found_response('No applications found.'), id='settings application is ignored', ), pytest.param( response.Response([{'id': 'help-center'}]), (None, None), None, None, response.create_not_found_response('No applications found.'), id='help-center application is ignored', ), pytest.param( response.Response([{'id': 'documents'}]), (response.create_not_found_response('No admin identity found.'),), None, None, response.create_not_found_response('No admin identity found.'), id='no admin identity found', ), pytest.param( response.Response([{'id': 'documents'}]), ( response.Response({'first_name': 'Foo', 'last_name': 'Bar'}), response.create_not_found_response('No identity found.'), ), None, None, response.create_not_found_response('No identity found.'), id='no identity found', ), pytest.param( response.Response([{'id': 'documents'}]), ( response.Response({'first_name': 'Foo', 'last_name': 'Bar'}), response.Response( {'default_brand': 'theorchard', 'email': '1', 'localization': 'en'} ), ), response.create_not_found_response('No tenant found.'), None, response.create_not_found_response('No tenant found.'), id='no tenant found', ), pytest.param( response.Response([{'id': 'documents'}]), ( response.Response({'first_name': 'Foo', 'last_name': 'Bar'}), response.Response( {'default_brand': 'theorchard', 'email': '1', 'localization': 'en'} ), ), response.Response('TENANT_NAME'), response.create_not_found_response('No organization info found.'), response.create_not_found_response('No organization info found.'), id='no organization info found', ), pytest.param( response.Response([{'id': 'documents'}]), ( response.Response({'first_name': 'Foo', 'last_name': 'Bar'}), response.Response( {'default_brand': 'theorchard', 'email': '1', 'localization': 'en'} ), ), response.Response('TENANT_NAME'), response.Response({'name': 'orchard', 'logo_url': 'https://logo.url'}), response.Response(status=204), id='all good', ), pytest.param( response.Response([{'id': 'documents'}, {'id': 'settings'}, {'id': 'help-center'}]), ( response.Response({'first_name': 'Foo', 'last_name': 'Bar'}), response.Response( {'default_brand': 'theorchard', 'email': '1', 'localization': 'en'} ), ), response.Response('TENANT_NAME'), response.Response({'name': 'orchard', 'logo_url': 'https://logo.url'}), response.Response(status=204), id='all good with applications to ignore', ), ), ) def test_permissions_updated( applications_resp, identity_resp, resource_name_resp, org_info_resp, expected_resp, mocker ): """Test permissions_updated.""" mocker.patch('notifications.models.ows_users.get_applications', return_value=applications_resp) mocker.patch('notifications.models.ows_users.get_identity', side_effect=identity_resp) mocker.patch('notifications.models.ows_users.get_organization_info', return_value=org_info_resp) mocker.patch('notifications.models.resource.get_resource_name', return_value=resource_name_resp) mocker.patch('notifications.config.NOTIFICATIONS_DELIVERY_URL', 'SQS_URL') send_messages_mock = mocker.patch('notifications.connectors.sqs.send_messages') identity_id = uuid.UUID('123e4567-e89b-12d3-a456-426614174000') tenant_type = types.TenantType.ACCOUNT tenant_uuid = uuid.UUID('123e4567-e89b-12d3-a456-426614174001') admin_identity_id = '123e4567-e89b-12d3-a456-426614174002' result = permissions.permissions_updated( identity_id=identity_id, tenant_type=tenant_type, tenant_uuid=tenant_uuid, brand='theorchard', admin_identity_id=admin_identity_id, ) assert result.status == expected_resp.status if expected_resp.status != 204: assert result.errors == expected_resp.errors send_messages_mock.assert_not_called() else: send_messages_mock.assert_called_once_with('SQS_URL', [VALID_CASE])