"""Tests for profile logic.""" import pytest from oto import response from pytest_mock import MockerFixture from product.constants import service_name from product.logic import profile as profile_logic from product.models import ows_permissions, product as product_model @pytest.mark.parametrize( "resources_response, vendor_id, subaccount_id, status", ( ( # returns 500 when the internal call fails response.Response( status=500, message=f"Could not connect to {service_name.OWS_PERMISSIONS}." ), 1, 1, 500 ), ( # checks passes for a vendor response.Response( status=200, message={ "items": [ { "type": "Vendor", "id": 1, }, { "type": "Vendor", "id": 321, } ], "pagination": { "total_records": 2, "type": "none" } } ), 1, None, 200 ), ( # checks passes for a vendor (with subaccount) response.Response( status=200, message={ "items": [ { "type": "Vendor", "id": 1, }, { "type": "Vendor", "id": 321, } ], "pagination": { "total_records": 2, "type": "none" } } ), 1, 1, 200 ), ( # checks passes for all vendors response.Response( status=200, message={ "items": [ { "type": "Vendor", "id": '*', }, ], "pagination": { "total_records": 1, "type": "none" } } ), 1, 1, 200 ), ( # checks passes for a subaccount response.Response( status=200, message={ "items": [ { "type": "Subaccount", "id": 1, }, { "type": "Vendor", "id": 321, } ], "pagination": { "total_records": 2, "type": "none" } } ), 1, 1, 200 ), ( # check fails response.Response( status=200, message={ "items": [ { "type": "Vendor", "id": 321, } ], "pagination": { "total_records": 1, "type": "none" } } ), 1, 1, 403 ), ), ) def test_check_profile_access( resources_response: response.Response, vendor_id: int | None, subaccount_id: int | None, status: int, profile_id: int, profile_type_label: str, mocker: MockerFixture, ): """Check check_profile_access with various parameters.""" get_profile_resources_mock = mocker.patch.object( ows_permissions, 'get_profile_resources', return_value=resources_response) result = profile_logic.check_profile_access( profile_id, profile_type_label, vendor_id=vendor_id, subaccount_id=subaccount_id) assert result.status == status get_profile_resources_mock.assert_called_with( profile_id, profile_type_label, resource_type="label" ) @pytest.mark.parametrize( "product_response, access_response, status", ( ( response.Response(status=404), None, 404 ), ( response.Response( status=200, message={ "product_id": 5, "vendor_id": 1, "subaccount_id": 2 } ), response.Response(status=200), 200 ), ( response.Response( status=200, message={ "product_id": 5, "vendor_id": 1, "subaccount_id": 2 } ), response.Response(status=403), 403 ), ( response.Response( status=200, message={ "product_id": 5, "vendor_id": 1, "subaccount_id": 2 } ), response.Response(status=500), 500 ) ) ) def check_profile_access_to_product( product_response: response.Response, access_response: response.Response, status: int, profile_id: int, profile_type_label: str, mocker: MockerFixture ): get_product_mock = mocker.patch.object( product_model, 'get_product_by_id', return_value=product_response ) access_mock = mocker.patch.object( profile_logic, 'check_profile_access', return_value=access_response ) product_id = 5 result = profile_logic.check_profile_access_to_product( profile_id, profile_type_label, product_id) assert result.status == status get_product_mock.assert_called_with(product_id) if product_response and product_response.status == 200: access_mock.assert_called_with( profile_id, profile_type_label, product_response.message.get("vendor_id"), product_response.message.get("subaccount_id") ) else: access_mock.assert_not_called()