"""Integration tests for statement periods.""" import pytest from abacus_common_logic.connectors.database import db from royalties.tests.integration.conftest import ows_royalties_api_client @pytest.mark.jira('ACC-4004') def test_get_recent_statement_periods(basic_headers, statement_period_fixtures): """Test recent statement periods endpoints.""" ows_royalties_client = ows_royalties_api_client(basic_headers) response_get = ows_royalties_client.get_recent_statement_periods() assert response_get.status_code == 200 assert len(response_get.json()) > 0 assert response_get.json() == [ { 'statement_period_id': 11, 'statement_period_name': 'Month 10', 'statement_month': 10, 'statement_year': 2024, 'closed_date': None, 'exchange_rates_delivered': False, 'statement_period_status': 'current', }, { 'statement_period_id': 10, 'statement_period_name': 'Month 9', 'statement_month': 9, 'statement_year': 2020, 'closed_date': '2021-01-01', 'exchange_rates_delivered': False, 'statement_period_status': 'closed', }, { 'statement_period_id': 9, 'statement_period_name': 'Month 8', 'statement_month': 8, 'statement_year': 2020, 'closed_date': '2021-01-01', 'exchange_rates_delivered': False, 'statement_period_status': 'closed', }, { 'statement_period_id': 8, 'statement_period_name': 'Month 7', 'statement_month': 7, 'statement_year': 2020, 'closed_date': '2021-01-01', 'exchange_rates_delivered': False, 'statement_period_status': 'closed', }, { 'statement_period_id': 7, 'statement_period_name': 'Month 6', 'statement_month': 6, 'statement_year': 2020, 'closed_date': '2021-01-01', 'exchange_rates_delivered': False, 'statement_period_status': 'closed', }, { 'statement_period_id': 6, 'statement_period_name': 'Month 5', 'statement_month': 5, 'statement_year': 2020, 'closed_date': '2021-01-01', 'exchange_rates_delivered': False, 'statement_period_status': 'closed', }, { 'statement_period_id': 5, 'statement_period_name': 'Month 4', 'statement_month': 4, 'statement_year': 2020, 'closed_date': '2021-01-01', 'exchange_rates_delivered': False, 'statement_period_status': 'closed', }, ] @pytest.mark.jira('PLATFORM-4341') def test_get_recent_statement_periods_authorization( statement_period_fixtures: None, auth_headers: dict[str, str], unauthorized_headers: dict[str, str], ) -> None: """Test authorization of get recent statement periods endpoint.""" authorized_client = ows_royalties_api_client(auth_headers) res = authorized_client.get_recent_statement_periods() assert res.status_code == 200 assert len(res.json()) > 0 unauthorized_client = ows_royalties_api_client(unauthorized_headers) res = unauthorized_client.get_recent_statement_periods() assert res.status_code == 403 assert res.json()['message'] == 'User is forbidden' @pytest.mark.jira('ACC-4004') def test_get_upcoming_statement_periods(basic_headers, statement_period_fixtures): """Test upcoming statement periods endpoints.""" ows_royalties_client = ows_royalties_api_client(basic_headers) response_get = ows_royalties_client.get_upcoming_statement_periods() assert response_get.status_code == 200 assert len(response_get.json()) > 0 assert all( [item['statement_period_status'] == 'open' for item in response_get.json()] ) @pytest.mark.jira('PLATFORM-4341') def test_get_upcoming_statement_periods_authorization( statement_period_fixtures: None, auth_headers: dict[str, str], unauthorized_headers: dict[str, str], ) -> None: """Test authorization of get recent statement periods endpoint.""" authorized_client = ows_royalties_api_client(auth_headers) res = authorized_client.get_upcoming_statement_periods() assert res.status_code == 200 assert len(res.json()) > 0 unauthorized_client = ows_royalties_api_client(unauthorized_headers) res = unauthorized_client.get_upcoming_statement_periods() assert res.status_code == 403 assert res.json()['message'] == 'User is forbidden' @pytest.mark.jira('ACC-4004') def test_put_statement_period(basic_headers, statement_period_fixtures): """Test update statement period.""" ows_royalties_client = ows_royalties_api_client(basic_headers) response_get = ows_royalties_client.get_recent_statement_periods() assert response_get.status_code == 200 assert len(response_get.json()) > 0 current_period = response_get.json()[0] statement_period_id = current_period['statement_period_id'] response_put = ows_royalties_client.close_statement_period(statement_period_id) assert response_put.status_code == 201 new_recent_periods = ows_royalties_client.get_recent_statement_periods() current_period = new_recent_periods.json()[0] updated_period = new_recent_periods.json()[1] assert updated_period['statement_period_status'] == 'closed' assert updated_period['statement_period_id'] == statement_period_id assert current_period['statement_period_status'] == 'current' assert current_period['statement_period_id'] == statement_period_id + 1 @pytest.mark.jira('ACC-4004') def test_get_statement_period(basic_headers, statement_period_fixtures): """Test get statement period by statement_period_id.""" ows_royalties_client = ows_royalties_api_client(basic_headers) response_get = ows_royalties_client.get_statement_period(11) assert response_get.status_code == 200 assert response_get.json() == { 'statement_period_id': 11, 'statement_period_name': 'Month 10', 'statement_month': 10, 'statement_year': 2024, 'exchange_rates_delivered': False, 'closed_date': None, 'statement_period_status': 'current', } @pytest.mark.jira('ACC-4004') def test_get_statement_periods(basic_headers, statement_period_fixtures): """Test get statement periods.""" ows_royalties_client = ows_royalties_api_client(basic_headers) response_get = ows_royalties_client.get_statement_periods() assert response_get.status_code == 200 assert len(response_get.json()['items']) > 0 params = {'limit': 2} response_get = ows_royalties_client.get_statement_periods(params=params) assert response_get.status_code == 200 assert len(response_get.json()['items']) == 2 @pytest.mark.jira('PLATFORM-4341') def test_get_statement_periods_authorization( statement_period_fixtures: None, auth_headers: dict[str, str], unauthorized_headers: dict[str, str], ) -> None: """Test authorization of get statement periods endpoint.""" authorized_client = ows_royalties_api_client(auth_headers) res = authorized_client.get_statement_periods() assert res.status_code == 200 assert len(res.json()['items']) > 0 unauthorized_client = ows_royalties_api_client(unauthorized_headers) res = unauthorized_client.get_statement_periods() assert res.status_code == 403 assert res.json()['message'] == 'User is forbidden' @pytest.mark.jira('ACC-4806') def test_put_payment_entity_visible(basic_headers, statement_period_fixtures): """PUT payment_entity_visible .""" ows_royalties_client = ows_royalties_api_client(basic_headers) # defined in fixtures statement_period_id = 1 payment_entity_id = 1 query = """ INSERT INTO statement_period_payment_entity ( statement_period_id, reference_payment_entity_id, is_visible_to_customer ) VALUES ( {}, {}, 0 ) """.format(statement_period_id, payment_entity_id) db.engine.execute(query) res = ows_royalties_client.get_payment_entities_for_statement_period( statement_period_id ) assert res.status_code == 200 assert res.json()[0]['statement_period_id'] == statement_period_id assert res.json()[0]['reference_payment_entity_id'] == payment_entity_id assert not res.json()[0]['is_visible_to_customer'] body = { 'statement_period_payment_entity_id': 1, 'statement_period_id': statement_period_id, 'reference_payment_entity_id': payment_entity_id, 'is_visible_to_customer': True, } res = ows_royalties_client.put_payment_entity_visible(1, 1, body) assert res.status_code == 200 assert res.json() == body res = ows_royalties_client.get_payment_entities_for_statement_period( statement_period_id ) assert res.status_code == 200 assert res.json()[0]['statement_period_id'] == statement_period_id assert res.json()[0]['reference_payment_entity_id'] == payment_entity_id assert res.json()[0]['is_visible_to_customer'] @pytest.mark.jira('ACC-6309') def test_post_put_get_statement_period_adjustment_file( basic_headers, statement_period_fixtures ): """Test POST PUT and GET /statement-period//adjustment-file .""" # defined in fixtures statement_period_id = 11 ows_royalties_client = ows_royalties_api_client(basic_headers) post_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'error_type': 'content_error', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'file_name': 'vz test file', 'source_file_upload_id': None, 'created_by': 'f6770fff-8ecc-437c-902e-e7e52e66dd2d', } response_post = ows_royalties_client.post_statement_period_adjustment_file( statement_period_id, post_body ) assert response_post.status_code == 201 updated_response = response_post.json() adjustment_file_id = response_post.json()['statement_period_adjustment_file_id'] updated_response.pop('statement_period_adjustment_file_id') updated_response.pop('statement_period_id') expected_response = post_body expected_response.update({'batch_type': 'upload'}) assert updated_response == expected_response response_get = ows_royalties_client.get_statement_period_adjustment_file( statement_period_id, adjustment_file_id ) assert response_get.status_code == 200 updated_response = response_get.json() updated_response.pop('statement_period_adjustment_file_id') updated_response.pop('statement_period_id') assert updated_response == expected_response put_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'error_type': 'content_error', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'file_name': 'test_file_updated', } response_put = ows_royalties_client.put_statement_period_adjustment_file( statement_period_id, adjustment_file_id, put_body ) assert response_put.status_code == 201 response_get = ows_royalties_client.get_statement_period_adjustment_file( statement_period_id, adjustment_file_id ) assert response_get.status_code == 200 assert response_get.json()['file_name'] == put_body['file_name'] @pytest.mark.jira('ACC-6310') def test_get_adjustment_files_by_statement_period_id( basic_headers, statement_period_fixtures ): """GET /statement-period//adjustment-files endpoint.""" # defined in fixtures statement_period_id = 11 ows_royalties_client = ows_royalties_api_client(basic_headers) post_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'error_type': 'content_error', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'file_name': 'vz test file', 'source_file_upload_id': None, 'created_by': 'f6770fff-8ecc-437c-902e-e7e52e66dd2d', } response_post = ows_royalties_client.post_statement_period_adjustment_file( statement_period_id, post_body ) assert response_post.status_code == 201 expected_response = post_body expected_response.update({'batch_type': 'upload'}) response_get = ows_royalties_client.get_adjustment_files_by_statement_period_id( statement_period_id ) assert response_get.status_code == 200 updated_response = response_get.json()['items'][0] updated_response.pop('statement_period_adjustment_file_id') updated_response.pop('statement_period_id') assert updated_response == expected_response @pytest.mark.jira('ACC-6310') def test_get_adjustment_file_by_file_id(basic_headers, statement_period_fixtures): """GET /statement-period-adjustment-file/ .""" # defined in fixtures statement_period_id = 11 ows_royalties_client = ows_royalties_api_client(basic_headers) post_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'error_type': 'content_error', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'file_name': 'vz test file', 'source_file_upload_id': None, 'created_by': 'f6770fff-8ecc-437c-902e-e7e52e66dd2d', } response_post = ows_royalties_client.post_statement_period_adjustment_file( statement_period_id, post_body ) expected_response = post_body expected_response.update({'batch_type': 'upload'}) assert response_post.status_code == 201 adjustment_file_id = response_post.json()['statement_period_adjustment_file_id'] response_get = ows_royalties_client.get_adjustment_file_by_file_id( adjustment_file_id ) assert response_get.status_code == 200 updated_response = response_get.json() updated_response.pop('statement_period_adjustment_file_id') updated_response.pop('statement_period_id') assert updated_response == expected_response @pytest.mark.jira('ACC-6309') def test_delete_adjustment_file(basic_headers, statement_period_fixtures): """Test DELETE /statement-period//adjustment-file/ .""" # defined in fixtures statement_period_id = 11 ows_royalties_client = ows_royalties_api_client(basic_headers) post_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'error_type': 'content_error', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'file_name': 'vz test file', 'source_file_upload_id': None, 'created_by': 'f6770fff-8ecc-437c-902e-e7e52e66dd2d', } response_post = ows_royalties_client.post_statement_period_adjustment_file( statement_period_id, post_body ) expected_response = post_body expected_response.update({'batch_type': 'upload'}) assert response_post.status_code == 201 updated_response = response_post.json() adjustment_file_id = response_post.json()['statement_period_adjustment_file_id'] updated_response.pop('statement_period_adjustment_file_id') updated_response.pop('statement_period_id') assert updated_response == expected_response response_get = ows_royalties_client.get_statement_period_adjustment_file( statement_period_id, adjustment_file_id ) assert response_get.status_code == 200 updated_response = response_get.json() updated_response.pop('statement_period_adjustment_file_id') updated_response.pop('statement_period_id') assert updated_response == expected_response response_delete = ows_royalties_client.delete_adjustment_file( statement_period_id, adjustment_file_id ) assert response_delete.status_code == 204 response_get = ows_royalties_client.get_statement_period_adjustment_file( statement_period_id, adjustment_file_id ) assert response_get.status_code == 404 @pytest.mark.jira('ACC-6730') def test_get_current_statement_period(basic_headers, statement_period_fixtures): """Test GET /statement-period/current .""" ows_royalties_client = ows_royalties_api_client(basic_headers) res_get = ows_royalties_client.get_current_statement_period() assert res_get.status_code == 200 assert res_get.json()['statement_period_status'] == 'current' assert res_get.json()['statement_period_id'] == 11 @pytest.mark.jira('ACC-7004') def test_get_statement_period_payment_entities_states( basic_headers, statement_period_fixtures, reference_payment_entity_fixtures, payment_entities_actions_fixtures, statement_period_payment_entity_fixture, ): """Test GET /statement-period//payment-entities/states/ .""" statement_period_id = 11 ows_royalties_client = ows_royalties_api_client(basic_headers) res_get = ows_royalties_client.get_statement_period_payment_entities_states( statement_period_id ) assert res_get.status_code == 200 assert len(res_get.json()) > 0 assert res_get.json()[0]['statement_period_id'] == statement_period_id @pytest.mark.jira('ACC-7498') def test_get_statement_periods_by_statement_year( basic_headers, statement_period_fixtures ): """Test GET /statement-period by statement_year .""" ows_royalties_client = ows_royalties_api_client(basic_headers) post_body = [2020] res_get = ows_royalties_client.post_statement_periods(post_body) assert res_get.status_code == 200 for year in res_get.json(): assert year['statement_year'] == post_body[0]