"""Integration tests for /accounting-period(s) endpoints and logic.""" import random 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.utils.generic_helper import generate_random_string @pytest.mark.jira('ACC-2070') def test_post_accounting_period(basic_headers, statement_period_fixtures): """Test POST /accounting-period endpoint.""" ows_royalties_client = ows_royalties_api_client(basic_headers) accounting_period_name = 'March 2020' params = { 'accounting_period_name': accounting_period_name, 'statement_period_id': 11, 'contract_type': 'distribution', } response_post_period = ows_royalties_client.post_accounting_period(params) assert response_post_period.status_code == 201 response = ows_royalties_client.get_accounting_periods() assert response.status_code == 200 response_body = response.json() items = response_body['items'] assert items[0]['accounting_period_name'] == accounting_period_name @pytest.mark.jira('ACC-2069') def test_get_accounting_periods(basic_headers, statement_period_fixtures): """GET /accounting_period/ and GET /accounting-periods.""" create_accounting_period_if_not_exist(basic_headers, statement_period_fixtures) ows_royalties_client = ows_royalties_api_client(basic_headers) response = ows_royalties_client.get_accounting_periods() assert response.status_code == 200 response_body = response.json() items = response_body['items'] open_period = next((item for item in items if item['closed_date'] is None), None) if open_period: assert 'accounting_period_id' in open_period assert 'closed_date' in open_period assert 'accounting_period_status' in open_period assert 'accounting_period_name' in open_period response_open_period = ows_royalties_client.get_accounting_period( open_period['accounting_period_id'] ) assert response_open_period.status_code == 200 assert 'accounting_period_id' in response_open_period.json() assert 'accounting_period_name' in response_open_period.json() assert 'accounting_period_status' in response_open_period.json() assert 'closed_date' in response_open_period.json() closed_periods = [item for item in items if item['closed_date']] if closed_periods: rnd_closed_period = random.choice(closed_periods) assert 'accounting_period_id' in rnd_closed_period assert 'accounting_period_name' in rnd_closed_period assert 'accounting_period_status' in rnd_closed_period assert 'closed_date' in rnd_closed_period @pytest.mark.jira('ACC-1880') def test_period_status(basic_headers, statement_period_fixtures): """Period status should be Sales Available when there are sales.""" accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] post_sales_file_body = { 'accounting_period_id': accounting_period_id, 'file_name': 'orcd_autotest_{}'.format(generate_random_string(16)), } ows_royalties_client = ows_royalties_api_client(basic_headers) response_post_sales_file = ows_royalties_client.post_sales_file( post_sales_file_body ) assert response_post_sales_file.status_code == 201 response_post_sales_file_body = response_post_sales_file.json() file_id = response_post_sales_file_body['sales_file_id'] put_body = { 'main_url': 'https://orcd_testing.com/{}'.format(generate_random_string(16)), 'amount_usd': '42000.0', 'row_count': 180, } response_put_sales_file = ows_royalties_client.put_sales_file(file_id, put_body) assert response_put_sales_file.status_code == 200 response_get_periods = ows_royalties_client.get_accounting_periods() assert response_get_periods.status_code == 200 response_get_periods_body = response_get_periods.json() assert ( len( list( filter( lambda item: item['accounting_period_status'] == 'open', response_get_periods_body['items'], ) ) ) > 0 ) @pytest.mark.jira('ACC-3970') def test_get_acc_period_reports(basic_headers, clear_db, statement_period_fixtures): """GET accounting period reports.""" accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] ows_royalties_client = ows_royalties_api_client(basic_headers) report_type = 'vat_exempt' filename = 'test.tsv' bucket = 'qa-royalties-sales-files' report_export_url = 's3://{}/{}'.format(bucket, filename) query = """ INSERT INTO accounting_period_report (accounting_period_id, report_type, report_export_url, created_by, created_at, last_modified_by, last_modified) VALUES (1, '{}', '{}', 'QA', '2021-10-15 03:29:45', 'QA', '2021-10-15 03:29:48') """.format(report_type, report_export_url) db.engine.execute(query) response_get = ows_royalties_client.get_period_reports_by_period_id( accounting_period_id ) assert response_get.status_code == 200 assert response_get.json()[0]['report_type'] == report_type assert response_get.json()[0]['report_export_url'] == report_export_url response_get = ows_royalties_client.get_period_reports_by_period_id_and_report_type( accounting_period_id, report_type ) assert response_get.status_code == 200 assert response_get.json()['report_type'] == report_type assert bucket in response_get.json()['report_export_url'] assert filename in response_get.json()['report_export_url'] @pytest.mark.jira('ACC-3970') def test_create_update_acc_period_report(basic_headers, statement_period_fixtures): """POST /accounting-period//accounting-period-report .""" accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] ows_royalties_client = ows_royalties_api_client(basic_headers) first_post_body = { 'report_type': 'vat_applied_gbr', 'report_export_url': 's3://awesome/vat/report', } response_post = ows_royalties_client.post_accounting_period_report( accounting_period_id, first_post_body ) assert response_post.status_code == 201 assert response_post.json() == { 'report_export_url': first_post_body['report_export_url'], 'report_type': first_post_body['report_type'], 'accounting_period_report_id': 1, 'accounting_period_id': 1, } second_post_body = { 'report_type': 'vat_exempt', 'report_export_url': 's3://not/so/awesome/vat/report', } response_post = ows_royalties_client.post_accounting_period_report( accounting_period_id, second_post_body ) assert response_post.status_code == 201 assert response_post.json() == { 'report_export_url': second_post_body['report_export_url'], 'report_type': second_post_body['report_type'], 'accounting_period_report_id': 2, 'accounting_period_id': 1, } @pytest.mark.jira('ACC-4882') def test_get_accounting_period_by_accounting_run_id( basic_headers, statement_period_fixtures ): """GET /accounting-run//accounitng-period .""" accounting_run_id = 1 ows_royalties_client = ows_royalties_api_client(basic_headers) accounting_period = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period'] 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) res = ows_royalties_client.get_accounting_period_by_accounting_run_id( accounting_run_id ) assert res.status_code == 200 assert res.json() == accounting_period @pytest.mark.jira('PLATFORM-4341') def test_get_accounting_period_by_id_authorization( basic_headers: dict[str, str], statement_period_fixtures: None, auth_headers: dict[str, str], unauthorized_headers: dict[str, str], ): """Test authorization for endpoint to get accounting run by id.""" accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] authorized_client = ows_royalties_api_client(auth_headers) res = authorized_client.get_accounting_period(accounting_period_id) assert res.status_code == 200 assert res.json()['accounting_period_id'] == 1 unauthorized_client = ows_royalties_api_client(unauthorized_headers) res = unauthorized_client.get_accounting_period(accounting_period_id) assert res.status_code == 403 assert res.json()['message'] == 'User is forbidden'