"""Tests for the ows-permissions connector.""" from json.decoder import JSONDecodeError from unittest.mock import MagicMock, patch import pytest from werkzeug.exceptions import HTTPException from abacus_common_logic.connectors.ows_permissions import get_accounts_for_profile def test_get_accounts_for_profile(): """Test gettings the accounts the profile has access to.""" profile_type = 'LabelProfile' profile_id = 123 permissions_result = {'items': [{'vendorId': 1}, {'vendorId': 2}, {'vendorId': 3}]} mock_permissions_response = MagicMock() mock_permissions_response.status_code = 200 mock_permissions_response.json.return_value = permissions_result mock_ows_client = MagicMock() mock_ows_client.get.return_value = mock_permissions_response result = get_accounts_for_profile(mock_ows_client, profile_type, profile_id) mock_ows_client.get.assert_called_once_with( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ) assert result == [1, 2, 3] @patch('abacus_common_logic.connectors.ows_permissions.log') def test_get_accounts_for_profile_error_json(mock_log): """Test gettings the accounts the profile has access to with a JSON error.""" profile_type = 'LabelProfile' profile_id = 123 permissions_error = {'code': 'internal_error', 'message': 'Internal Error'} mock_permissions_response = MagicMock() mock_permissions_response.status_code = 500 mock_permissions_response.json.return_value = permissions_error mock_ows_client = MagicMock() mock_ows_client.get.return_value = mock_permissions_response with pytest.raises(HTTPException) as excinfo: get_accounts_for_profile(mock_ows_client, profile_type, profile_id) mock_ows_client.get.assert_called_once_with( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ) mock_log.assert_called_once() assert excinfo.value.code == 500 assert excinfo.value.description == permissions_error @patch('abacus_common_logic.connectors.ows_permissions.log') def test_get_accounts_for_profile_error_text(mock_log): """Test gettings the accounts the profile has access to with a text error.""" profile_type = 'LabelProfile' profile_id = 123 permissions_error = 'Not Found' mock_permissions_response = MagicMock() mock_permissions_response.status_code = 404 mock_permissions_response.text = permissions_error mock_permissions_response.json.side_effect = JSONDecodeError( 'JSON Error', permissions_error, 0 ) mock_ows_client = MagicMock() mock_ows_client.get.return_value = mock_permissions_response with pytest.raises(HTTPException) as excinfo: get_accounts_for_profile(mock_ows_client, profile_type, profile_id) mock_ows_client.get.assert_called_once_with( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ) mock_log.assert_called_once() assert mock_log.call_args.args[0] == 'error' assert excinfo.value.code == 404 assert excinfo.value.description == permissions_error