"""Test logic for identity.""" import uuid from unittest.mock import MagicMock import pytest import requests from owsresponse import response from account.constants import header from account.logic import identity as identity_logic from account.models import ows_users, subaccount as subaccount_model, vendor as vendor_model from account.models.types import ProfileInfo from tests.unit.helpers import subaccount as subaccount_test_helper, vendor as vendor_test_helper def fast_patch(monkeypatch, options): """Helper for monkeypatching. Args: monkeypatch (pytest.monkeypatch.monkeypatch): monkeypatch object options (dict): describing the module, attributes, return values Usage: fast_patch(monkeypatch, { module1: dict(attribute1=value1, attribute2=value2), module2: dict(attribute3=value3, attribute4=value4), }) """ for module, attributes in options.items(): for attribute, fixture in attributes.items(): monkeypatch.setattr(module, attribute, MagicMock(return_value=fixture)) @pytest.fixture def fixture_identity_vendor(): """Fixture for vendor identity.""" return response.Response( { 'account_id': 2, 'account_type': 'vendor', 'account_name': 'FatCat Records', 'country_id': 1, } ) @pytest.fixture def fixture_identity_subaccount(): """Fixture for subaccount identity.""" return response.Response( { 'account_id': 1, 'account_type': 'subaccount', 'account_name': 'FatCat Records, Subaccount', 'country_id': 1, } ) @pytest.fixture def fixture_subaccount(): """Create a response.Response fixture for subaccount data.""" return response.Response( { 'subaccount_id': 1, 'vendor_id': 1, 'subaccount_name': 'FatCat Records, Subaccount', 'description': 'Subaccount Description 1', 'date_deleted': None, 'country_id': 1, } ) @pytest.fixture def fixture_full_vendor(): """Fixture for vendor.""" return response.Response({'vendor_id': 2, 'name': 'FatCat Records', 'country_id': 1}) @pytest.fixture def fixture_user_details(): """User Details response.""" return { 'first_name': 'Adam', 'email': 'adameve@email.com', 'account': {'vendor_id': 0, 'subaccount_id': 0}, 'type': 'oa', 'last_name': 'Eve', 'user_id': 'oa:100', } @pytest.fixture def userdetails_success_response(fixture_user_details): """Mock successful response from user_details.""" return MagicMock( json=lambda: fixture_user_details, spec=requests.models.Response, status_code=200, ) @pytest.fixture def userdetails_not_found(): """Mock not found response from user_details.""" return MagicMock(json=lambda: '', spec=requests.models.Response, status_code=404) @pytest.mark.parametrize( ['account_type', 'account_id', 'identity_result'], [ ('subaccount', 1, subaccount_test_helper.fixture_identity_subaccount()), ('vendor', 2, vendor_test_helper.fixture_identity_vendor()), ], ) def test_get_identity_success( monkeypatch, fixture_subaccount, fixture_full_vendor, account_type, account_id, identity_result, ): """Test get identity.""" if account_type == header.GRASS_ACCOUNT_TYPE_SUBACCOUNT: fast_patch(monkeypatch, {subaccount_model: dict(get_subaccount=fixture_subaccount)}) elif account_type == header.GRASS_ACCOUNT_TYPE_VENDOR: fast_patch(monkeypatch, {vendor_model: dict(get_vendor=fixture_full_vendor)}) result = identity_logic.get_identity(account_type, account_id) assert result.status == 200 assert result.message == identity_result.message @pytest.mark.parametrize( ['account_type', 'account_id'], [('subaccount', None), ('vendor', None), (None, 111)], ) def test_get_identity_failure(monkeypatch, account_type, account_id): """Test get identity.""" if account_type == header.GRASS_ACCOUNT_TYPE_SUBACCOUNT: fast_patch( monkeypatch, {subaccount_model: dict(get_subaccount=response.create_not_found_response())}, ) elif account_type == header.GRASS_ACCOUNT_TYPE_VENDOR: fast_patch( monkeypatch, {vendor_model: dict(get_vendor=response.create_not_found_response())}, ) result = identity_logic.get_identity(account_type, account_id) assert result.status == 404 def test_get_user_details(monkeypatch, userdetails_success_response): """Test get user details success.""" orchard_user_id = 'oa:123' fast_patch(monkeypatch, {ows_users: dict(get_user_details=userdetails_success_response)}) result = identity_logic.get_user_details(orchard_user_id) assert result.status_code == 200 def test_get_user_details_not_found(monkeypatch, userdetails_not_found): """Test get user details not found.""" orchard_user_id = 'oa:12334444444' fast_patch(monkeypatch, {ows_users: dict(get_user_details=userdetails_not_found)}) result = identity_logic.get_user_details(orchard_user_id) assert result.status_code == 404 def test_get_oa_user_id_returns_correct_id(monkeypatch): # API response: one profile with 'OrchAdminProfile' mock_profiles = [ ProfileInfo( profile_type='OrchAdminProfile', profile_id=100075, roles=[], uuid='00cfbaec-362c-4452-89b2-f47fafe86b77', ) ] monkeypatch.setattr( ows_users, 'get_user_profiles_by_identity_id', lambda identity_id: mock_profiles ) identity_id = uuid.uuid4() user_id = identity_logic.get_oa_user_id(identity_id) assert user_id == 100075 def test_get_oa_user_id_returns_none_when_no_matching_profile(monkeypatch): # profiles that do not include 'OrchAdminProfile' mock_profiles = [ ProfileInfo(profile_type='OtherProfile', profile_id=123456, roles=[], uuid='dummy-uuid') ] monkeypatch.setattr( ows_users, 'get_user_profiles_by_identity_id', lambda identity_id: mock_profiles ) identity_id = uuid.uuid4() user_id = identity_logic.get_oa_user_id(identity_id) assert user_id is None