"""Tests for ows-permissions connector.""" from fastapi import HTTPException import httpx import pytest from moneyhub.connectors import ows_permissions from moneyhub.constants.error import ACCOUNTS_DOES_NOT_EXIST def test_get_resources_for_profile(ows_client_mock): """Test getting the resources for a profile method.""" account_id = 13 profile_id = '98787' profile_type = 'MoneyhubProfile' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'vendorId': account_id}], 'total_count': 1}, ) ) response = ows_permissions.get_resources_for_profile(profile_type, profile_id) assert response == [{'vendorId': account_id}] def test_get_resources_for_profile_empty(ows_client_mock): """Test getting the resources for a profile method if it doesn't have any associated with it.""" profile_id = '98787' profile_type = 'MoneyhubProfile' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [], 'total_count': 1}, ) ) with pytest.raises(HTTPException): response = ows_permissions.get_resources_for_profile(profile_type, profile_id) assert response.json() == {'detail': ACCOUNTS_DOES_NOT_EXIST} def test_get_resources_for_profile_error(ows_client_mock): """Test getting the resources for a profile method with an unexpected response.""" profile_id = '98787' profile_type = 'MoneyhubProfile' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'error': 'yes'}, ) ) expected = 'Unexpected response from ows-permissions: {"error":"yes"}' with pytest.raises(KeyError, match=expected): ows_permissions.get_resources_for_profile(profile_type, profile_id) def test_get_resources_for_profile_with_subaccount(ows_client_mock): """Test getting accounts for a profile which has access to a subaccount.""" account_id = 13 profile_id = '98787' profile_type = 'MoneyhubProfile' response = { 'items': [ { 'id': account_id, 'isDistributor': 'N', 'name': 'Venn', 'roles': ['accounting'], 'type': 'Vendor', 'vendorId': account_id }, { 'id': 24601, 'name': 'Subb', 'roles': ['accounting'], 'type': 'Subaccount' } ], 'total_count': 2 } ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response(200, json=response) ) response = ows_permissions.get_resources_for_profile(profile_type, profile_id) assert response == [ { 'id': account_id, 'isDistributor': 'N', 'name': 'Venn', 'roles': ['accounting'], 'type': 'Vendor', 'vendorId': account_id }, { 'id': 24601, 'name': 'Subb', 'roles': ['accounting'], 'type': 'Subaccount' } ]