"""Tests for private handlers.""" import uuid import pytest from owsresponse import response from notifications import ( private_handlers, # noqa types, ) from notifications.constants import brands as brand_constants, tenant as tenant_constants @pytest.mark.parametrize( ('payload', 'has_jwt_auth', 'expected_status', 'expected_call'), ( pytest.param( { 'identity_id': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'tenant_type': tenant_constants.ACCOUNT_TENANT_TYPE, 'tenant_uuid': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'brand': brand_constants.BRAND_ORCHARD, }, True, 204, { 'identity_id': uuid.UUID('113773f6-853c-4c83-a24b-b2a5a48fa194'), 'tenant_type': types.TenantType.ACCOUNT, 'tenant_uuid': uuid.UUID('113773f6-853c-4c83-a24b-b2a5a48fa194'), 'brand': brand_constants.BRAND_ORCHARD, 'admin_identity_id': 'test-identity-id', }, id='valid case', ), pytest.param( { 'identity_id': 'INVALID', 'tenant_type': tenant_constants.ACCOUNT_TENANT_TYPE, 'tenant_uuid': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'brand': brand_constants.BRAND_ORCHARD, }, True, 400, None, id='invalid identity id', ), pytest.param( { 'identity_id': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'tenant_type': 'INVALID', 'tenant_uuid': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'brand': brand_constants.BRAND_ORCHARD, }, True, 400, None, id='invalid tenant type', ), pytest.param( { 'identity_id': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'tenant_type': tenant_constants.ACCOUNT_TENANT_TYPE, 'tenant_uuid': 'INVALID', 'brand': brand_constants.BRAND_ORCHARD, }, True, 400, None, id='invalid tenant uuid', ), pytest.param( { 'identity_id': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'tenant_type': tenant_constants.ACCOUNT_TENANT_TYPE, 'tenant_uuid': '113773f6-853c-4c83-a24b-b2a5a48fa194', 'brand': brand_constants.BRAND_ORCHARD, }, False, 401, None, id='no jwt auth', ), ), ) def test_permissions_updated( payload, has_jwt_auth, expected_status, expected_call, fixture_client, app_context, mocker ): """Test POST /private/permissions/updated endpoint.""" mocker.patch('notifications.private_handlers.Neo4jSession.__enter__') mocker.patch('notifications.private_handlers.Neo4jSession.__exit__') logic_mock = mocker.patch( 'notifications.logic.permissions.permissions_updated', return_value=response.Response(status=204), ) if has_jwt_auth: mock = mocker.Mock(jwt_identity_id='test-identity-id') mock_g = mocker.patch('notifications.utils.api_utils.g') mock_g2 = mocker.patch('notifications.private_handlers.g') mock_g.request_context = mock mock_g2.request_context = mock result = fixture_client.post('/private/permissions/updated', json=payload) assert result.status_code == expected_status if expected_call is None: logic_mock.assert_not_called() else: logic_mock.assert_called_once_with(**expected_call)