"""Integration tests for /run-controller(s) endpoints and logic.""" import pytest from core.app_factory import db from royalties.tests.integration.conftest import ( create_accounting_period_if_not_exist, ows_royalties_api_client, ) from royalties.tests.integration.consts import constants from royalties.tests.integration.utils.generic_helper import generate_random_string @pytest.mark.jira('ACC-1838', 'ACC-1839') def test_post_get_runcontrollers(basic_headers): """POST and GET /run-controller endpoints.""" ows_royalties_client = ows_royalties_api_client(basic_headers) params = { 'run_controller_name': 'orcd_autotest_{}'.format(generate_random_string(16)), 'contract_type': 'distribution', } response = ows_royalties_client.post_runcontroller(params) response_body_post = response.json() assert response.status_code == 201 assert response_body_post['run_controller_name'] == params['run_controller_name'] run_controller_id = response_body_post['run_controller_id'] response = ows_royalties_client.get_runcontrollers() assert response.status_code == 200 response_body_get = response.json()['items'] expected_item = { 'contract_type': 'distribution', 'run_controller_id': run_controller_id, 'run_controller_name': params['run_controller_name'], 'contract_count': 0, } assert expected_item in response_body_get @pytest.mark.jira('ACC-1838', 'ACC-1839') def test_runcontroller_added_to_open_period( basic_headers, test_label_id, statement_period_fixtures ): """Runcontroller attached to a new contract gets added to open period.""" ows_royalties_client = ows_royalties_api_client(basic_headers) accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] # create run controller for scenario params = { 'run_controller_name': 'orcd_autotest_{}'.format(generate_random_string(16)), 'contract_type': 'distribution', } response_post_runcontroller = ows_royalties_client.post_runcontroller(params) response_body_post_runcontroller = response_post_runcontroller.json() runcontroller_name = response_body_post_runcontroller['run_controller_name'] runcontroller_id = response_body_post_runcontroller['run_controller_id'] assert response_post_runcontroller.status_code == 201 query = """ INSERT INTO accounting_run (accounting_run_id, accounting_period_id, run_controller_id, run_status, start_date, summary_export_url, created_by, created_at, last_modified_by, last_modified) VALUES (1, 1, {}, 'No Action Taken', null, null, '123', '2020-11-13 05:43:47', '123', '2020-11-13 05:43:50'); """.format(runcontroller_id) db.engine.execute(query) # verify runcontroller is added to period's accounting runs list results_period = ows_royalties_client.get_accounting_runs(accounting_period_id) assert results_period.status_code == 200 results_period_period_body = results_period.json()['items'] accounting_run = next( ( item for item in results_period_period_body if item['run_controller_name'] == runcontroller_name ), None, ) assert accounting_run is not None @pytest.mark.jira('ACC-3011') def test_get_contracts_by_accounting_run_id(basic_headers): """GET /accounting_run_id/run-controller/contracts endpoint.""" accounting_run_id = 1 query = """ INSERT INTO accounting_run (accounting_run_id, accounting_period_id, run_controller_id, run_status, start_date, summary_export_url, created_by, created_at, last_modified_by, last_modified) VALUES ({}, 1, 1, 'No Action Taken', null, null, '123', '2020-11-13 02:54:04', '123', '2020-11-13 02:54:07'); """.format(accounting_run_id) db.engine.execute(query) query = """ INSERT INTO run_controller_contract (run_controller_contract_id, run_controller_id, contract_id) VALUES (1, 1, 1); """ db.engine.execute(query) ows_royalties_client = ows_royalties_api_client(basic_headers) response_get_contracts = ows_royalties_client.get_contract_ids_by_acc_run_id('1') assert response_get_contracts.status_code == 200 assert response_get_contracts.json()[0] == accounting_run_id @pytest.mark.jira('ACC-1999') def test_post_runcontroller_contract(basic_headers, statement_period_fixtures): """POST /run-controller/contract.""" account_id = 123123 query = """ INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({}, 'automation', 'QA', '2021-10-06 03:53:06', 'QA', '2021-10-06 03:53:10') """.format(account_id) db.engine.execute(query) ows_royalties_client = ows_royalties_api_client(basic_headers) create_accounting_period_if_not_exist(basic_headers, statement_period_fixtures) params = { 'run_controller_name': 'orcd_autotest_{}'.format(generate_random_string(16)), 'contract_type': 'distribution', } response = ows_royalties_client.post_runcontroller(params) response_body_post = response.json() assert response.status_code == 201 assert response_body_post['run_controller_name'] == params['run_controller_name'] run_controller_id = response_body_post['run_controller_id'] contract_id = 1 run_controller_contract = { 'run_controller_id': run_controller_id, 'contract_id': contract_id, 'account_id': account_id, 'contract_type': params['contract_type'], } response_post = ows_royalties_client.post_runcontroller_contract( run_controller_contract ) assert response_post.status_code == 201 response_get = ows_royalties_client.get_runcontroller_by_contract(contract_id) assert response_get.status_code == 200 assert response_get.json()['run_controller_name'] == params['run_controller_name'] insert_contract_query = """ INSERT INTO contract ( contract_id, reference_signing_entity_id, contract_name, term_start, term_end, created_by, created_at, last_modified_by, last_modified ) VALUES ( {}, 1, 'test', '2020-01-01', null, 'QA', '2021-10-06 06:25:51', 'QA', '2021-10-06 06:25:54' ) """ @pytest.mark.jira('PLATFORM-4341') def test_post_runcontroller_contracts_dataloader_authorization( auth_headers: dict[str, str], unauthorized_headers: dict[str, str], runcontroller_deleted_fixture: None, reference_payment_entity_fixtures: None, ) -> None: """Test authorization for POST /run-controllers/contracts/dataloader.""" query = """ INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({}, 'automation', 'QA', '2021-10-06 03:53:06', 'QA', '2021-10-06 03:53:10') """.format(constants.TEST_ACCOUNT_ID) db.engine.execute(query) contract_id = 999999 query = insert_contract_query.format(contract_id) db.engine.execute(query) query = """ INSERT INTO account_contract(account_contract_id, account_id, contract_id) VALUES (999999, {}, {}) """.format(constants.TEST_ACCOUNT_ID, contract_id) db.engine.execute(query) query = """ INSERT INTO run_controller_contract (run_controller_contract_id, run_controller_id, contract_id) VALUES (1, 1, 999999); """ db.engine.execute(query) authorized_client = ows_royalties_api_client(auth_headers) res = authorized_client.post_runcontrollers_contracts_dataloader([contract_id]) assert res.status_code == 200 assert len(res.json()) == 1 assert res.json()[0]['data']['contract_id'] == contract_id unauthorized_client = ows_royalties_api_client(unauthorized_headers) res = unauthorized_client.post_runcontrollers_contracts_dataloader([contract_id]) assert res.status_code == 403 assert res.json()['message'] == 'User is forbidden' @pytest.mark.jira('ACC-2546') def test_get_run_controller_contracts_by_account_id( basic_headers, statement_period_fixtures, reference_payment_entity_fixtures, ): """GET against /run-controllers/contracts/account/ .""" create_accounting_period_if_not_exist(basic_headers, statement_period_fixtures) account_id = 123125 contract_id = 1 query = """ INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({}, 'automation', 'QA', '2021-10-06 03:53:06', 'QA', '2021-10-06 03:53:10') """.format(account_id) db.engine.execute(query) query = """ INSERT INTO account_payment_term (account_payment_term_id, payment_entity_id, account_id, currency_code, payment_minimum, payment_schedule, created_by, created_at, last_modified_by, last_modified) VALUES (1, 1, {}, 'USD', 42.00, null, 'QA', '2021-10-06 06:18:45', 'QA', '2021-10-06 06:18:51') """.format(account_id) db.engine.execute(query) query = insert_contract_query.format(contract_id) db.engine.execute(query) query = """ INSERT INTO account_contract(account_contract_id, account_id, contract_id) VALUES (1, {}, {}) """.format(account_id, contract_id) db.engine.execute(query) ows_royalties_client = ows_royalties_api_client(basic_headers) params = { 'run_controller_name': 'orcd_autotest_{}'.format(generate_random_string(16)), 'contract_type': 'distribution', } response = ows_royalties_client.post_runcontroller(params) response_body_post = response.json() assert response.status_code == 201 assert response_body_post['run_controller_name'] == params['run_controller_name'] run_controller_id = response_body_post['run_controller_id'] run_controller_contract = { 'run_controller_id': run_controller_id, 'contract_id': contract_id, 'account_id': account_id, 'contract_type': params['contract_type'], } response_post = ows_royalties_client.post_runcontroller_contract( run_controller_contract ) assert response_post.status_code == 201 response_get = ows_royalties_client.get_run_controller_contracts_by_account_id( account_id ) assert response_get.status_code == 200 assert response_get.json()['items'][0]['contract_type'] == params['contract_type'] assert response_get.json()['items'][0]['run_controller_id'] == run_controller_id @pytest.mark.jira('ACC-6250') def test_get_deleted_non_runcontrollers(basic_headers, runcontroller_deleted_fixture): """GET /run-controllers that were not soft-deleted.""" ows_royalties_client = ows_royalties_api_client(basic_headers) res_get = ows_royalties_client.get_runcontrollers({'active_only': True}) assert res_get.status_code == 200 assert len(res_get.json()['items']) == 1 # defined in fixtures assert res_get.json()['items'][0]['run_controller_name'] == 'integration_test_2' @pytest.mark.jira('PLATFORM-4341') def test_get_runcontroller_by_contract_authorization( auth_headers: dict[str, str], unauthorized_headers: dict[str, str], runcontroller_deleted_fixture: None, ) -> None: """Test authorization for the GET /run-controller-by-contract/:id endpoint.""" query = """ INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({}, 'automation', 'QA', '2021-10-06 03:53:06', 'QA', '2021-10-06 03:53:10') """.format(constants.TEST_ACCOUNT_ID) db.engine.execute(query) contract_id = 999999 query = insert_contract_query.format(contract_id) db.engine.execute(query) query = """ INSERT INTO account_contract(account_contract_id, account_id, contract_id) VALUES (999999, {}, {}) """.format(constants.TEST_ACCOUNT_ID, contract_id) db.engine.execute(query) query = """ INSERT INTO run_controller_contract (run_controller_contract_id, run_controller_id, contract_id) VALUES (1, 1, 999999); """ db.engine.execute(query) authorized_client = ows_royalties_api_client(auth_headers) response = authorized_client.get_runcontroller_by_contract(contract_id) assert response.status_code == 200 assert response.json()['run_controller_id'] == 1 unauthorized_client = ows_royalties_api_client(unauthorized_headers) response = unauthorized_client.get_runcontroller_by_contract(contract_id) assert response.status_code == 403 assert response.json()['message'] == 'User is forbidden'