"""Tests for the access checks. We're testing every endpoint that's open to non-admin profiles. The LabelProfile used in the tests only has access to `LABEL_ACCOUNT_ID`. The MoneyhubProfile used in the tests only has access to `MONEYHUB_ACCOUNT_ID`. """ import pytest from tests.integration.conftest import ows_abacus_contract_api_client from tests.integration.consts.headers import ADMIN_HEADERS from tests.integration.consts.headers import LABEL_HEADERS from tests.integration.consts.headers import MONEYHUB_HEADERS from tests.integration.utils.generic_helper import create_account_and_contract LABEL_ACCOUNT_ID = 7123 MONEYHUB_ACCOUNT_ID = 35598 def _setup(): """Create common objects for the tests.""" label_client = ows_abacus_contract_api_client(LABEL_HEADERS) moneyhub_client = ows_abacus_contract_api_client(MONEYHUB_HEADERS) label_contract_id, _, _ = create_account_and_contract( account_id=LABEL_ACCOUNT_ID ) moneyhub_contract_id, _, _ = create_account_and_contract( account_id=MONEYHUB_ACCOUNT_ID ) return ( label_client, moneyhub_client, label_contract_id, moneyhub_contract_id ) @pytest.mark.jira('ACC-8345') def test_access_check_get_contract_by_id(): """Test the access check for `GET /contract/`.""" (_, moneyhub_client, label_contract_id, moneyhub_contract_id) = _setup() # The MoneyhubProfile makes a request for an Account they have access to res = moneyhub_client.get_contract(moneyhub_contract_id) assert res.status_code == 200 assert res.json()['contract_id'] == moneyhub_contract_id # The MoneyhubProfile makes a request for an Account they don't have access to res = moneyhub_client.get_contract(label_contract_id) assert res.status_code == 403 @pytest.mark.jira('ACC-8345') def test_access_check_get_contracts_dataloader(): """Test the access check for `POST /contracts/dataloader`.""" (_, moneyhub_client, label_contract_id, moneyhub_contract_id) = _setup() # The MoneyhubProfile makes a request for an Account they have access to res = moneyhub_client.post_contracts_dataloader([moneyhub_contract_id]) assert res.status_code == 200 assert res.json()[0]['data']['contract_id'] == moneyhub_contract_id # The MoneyhubProfile makes a request for an Account they don't have access to res = moneyhub_client.post_contracts_dataloader([label_contract_id]) assert res.status_code == 403 @pytest.mark.jira('ACC-8345') def test_access_check_get_contracts_by_account_id(): """Test the access check for `GET /contracts/account/`.""" (label_client, moneyhub_client, label_contract_id, moneyhub_contract_id) = _setup() # The LabelProfile makes a request for an Account they have access to res = label_client.get_contracts_by_account_id(LABEL_ACCOUNT_ID) assert res.status_code == 200 assert res.json()['items'][0]['contract_id'] == label_contract_id # The LabelProfile makes a request for an Account they don't have access to res = label_client.get_contracts_by_account_id(MONEYHUB_ACCOUNT_ID) assert res.status_code == 403 # The MoneyhubProfile makes a request for an Account they have access to res = moneyhub_client.get_contracts_by_account_id(MONEYHUB_ACCOUNT_ID) assert res.status_code == 200 assert res.json()['items'][0]['contract_id'] == moneyhub_contract_id # The MoneyhubProfile makes a request for an Account they don't have access to res = moneyhub_client.get_contracts_by_account_id(LABEL_ACCOUNT_ID) assert res.status_code == 403 @pytest.mark.jira('ACC-8345') def test_access_check_get_contracts_by_account_dataloader(): """Test the access check for `POST /contracts/account/dataloader`.""" (label_client, moneyhub_client, label_contract_id, moneyhub_contract_id) = _setup() # The LabelProfile makes a request for an Account they have access to res = label_client.get_contracts_by_account_id_dataloaded([LABEL_ACCOUNT_ID]) assert res.status_code == 200 assert res.json()[0]['data'][0]['contract_id'] == label_contract_id # The LabelProfile makes a request for an Account they don't have access to res = label_client.get_contracts_by_account_id_dataloaded([MONEYHUB_ACCOUNT_ID]) assert res.status_code == 403 # The MoneyhubProfile makes a request for an Account they have access to res = moneyhub_client.get_contracts_by_account_id_dataloaded([MONEYHUB_ACCOUNT_ID]) assert res.status_code == 200 assert res.json()[0]['data'][0]['contract_id'] == moneyhub_contract_id # The MoneyhubProfile makes a request for an Account they don't have access to res = moneyhub_client.get_contracts_by_account_id_dataloaded([LABEL_ACCOUNT_ID]) assert res.status_code == 403 @pytest.mark.jira('ACC-8345') def test_access_check_get_mechanical_deductions_by_contract_id(): """Test the access check for `GET /contract//contract-mechanical-deductions`.""" # noqa: E501 (label_client, _, label_contract_id, moneyhub_contract_id) = _setup() admin_client = ows_abacus_contract_api_client(ADMIN_HEADERS) admin_client.post_mechanical_deduction( label_contract_id, { 'admin_fee': '10.00', 'admin_type': 'business', 'mechanical_type': ['digital', 'physical'], 'territory': 'USA' } ) # The LabelProfile makes a request for an Account they have access to res = label_client.get_mechanical_deductions_by_contract_id(label_contract_id) assert res.status_code == 200 assert res.json()[0]['contract_id'] == label_contract_id # The LabelProfile makes a request for an Account they don't have access to res = label_client.get_mechanical_deductions_by_contract_id(moneyhub_contract_id) assert res.status_code == 403