"""Unit tests for reference_transaction_type endpoints.""" from unittest.mock import MagicMock, patch from flask.testing import FlaskClient import pytest from abacus_contract.models import ReferenceTransactionType @pytest.mark.parametrize( ( 'profile_type', 'profile_role', 'authorize_return', 'expected_status' ), [ pytest.param( 'AbacusProfile', 'administrator', None, 200, id='standalone check' ), pytest.param( 'Account360Profile', 'account360', True, 200, id='pdp check, authorized' ), pytest.param( 'Account360Profile', 'account360', False, 403, id='pdp check, unauthorized' ) ] ) @patch('abacus_contract.blueprints.reference_transaction_type.pdp_authorize_resource') # noqa: E501 @patch('abacus_contract.blueprints.reference_transaction_type.ReferenceTransactionType.get_all') # noqa: E501 def test_get_reference_transaction_types( mock_model_get_all: MagicMock, pdp_authorize_resource: MagicMock, fixture_client: FlaskClient, profile_type: str, profile_role: str, authorize_return: bool, expected_status: int ): """Tests for `GET /reference-transaction-types`.""" mock_model_get_all.return_value = [ ReferenceTransactionType.build( reference_transaction_type_id=1, transaction_type_code='AAA', transaction_type_name='Triple A' ), ReferenceTransactionType.build( reference_transaction_type_id=2, transaction_type_code='ZZZ', transaction_type_name='Rip Van Winkle' ) ] pdp_authorize_resource.return_value = authorize_return res = fixture_client.get( '/reference-transaction-types', headers={ 'Orchard-Requestor-Service': 'graphql-abacus', 'Orchard-Profile-Type': profile_type, 'Orchard-Profile-Id': '1234', 'Orchard-Roles': profile_role, 'Orchard-Identity-Id': '1234' }) assert res.status_code == expected_status if res.status_code == 200: assert res.json == [ { 'txn_type_id': 1, 'txn_type_code': 'AAA', 'txn_type_name': 'Triple A' }, { 'txn_type_id': 2, 'txn_type_code': 'ZZZ', 'txn_type_name': 'Rip Van Winkle' } ] else: assert res.json == {'code': 'forbidden', 'message': 'User is forbidden'} # pdp authorization check called when profile_type is Account360Profile. if profile_type == 'AbacusProfile': pdp_authorize_resource.assert_not_called() else: pdp_authorize_resource.assert_called_once_with( resource_id=0, resource_type='reference_transaction_type', action='view' ) # ReferenceTransactionType.get_all called when authorization succeeds. if authorize_return is False: mock_model_get_all.assert_not_called() else: mock_model_get_all.assert_called_once()