"""Integration tests for /sales-file endpoints and logic.""" import pytest 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 def util_get_sales_file_post_body(accounting_period_id): """Build a mock sales file.""" return { 'accounting_period_id': accounting_period_id, 'file_name': 'orcd_autotest_{}'.format(generate_random_string(16)), } @pytest.mark.jira('ACC-2112') def test_post_put_sales_file(basic_headers, statement_period_fixtures): """POST and PUT /sales-file endpoints.""" accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] post_sales_file_body = util_get_sales_file_post_body(accounting_period_id) 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() assert ( response_post_sales_file_body['file_name'] == post_sales_file_body['file_name'] ) assert 'row_count' in response_post_sales_file_body assert 'main_url' in response_post_sales_file_body assert 'amount_usd' in response_post_sales_file_body assert 'sales_file_id' in response_post_sales_file_body 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 assert response_put_sales_file.json()['main_url'] == put_body['main_url'] assert response_put_sales_file.json()['amount_usd'] == float(put_body['amount_usd']) assert response_put_sales_file.json()['row_count'] == put_body['row_count'] @pytest.mark.jira('ACC-2790') def test_get_accounting_run_sales_files( fresh_db, basic_headers, statement_period_fixtures ): """Test listing an accounting run's sales files.""" client = ows_royalties_api_client(basic_headers) accounting_period_id = create_accounting_period_if_not_exist( basic_headers, statement_period_fixtures )['accounting_period_id'] post_sales_file_body = util_get_sales_file_post_body(accounting_period_id) response_post_sales_file = client.post_sales_file(post_sales_file_body) assert response_post_sales_file.status_code == 201 sales_file_1 = response_post_sales_file.json() get_period_sales_files = client.get_accounting_period_sales_files( accounting_period_id ) assert get_period_sales_files.status_code == 200 body = get_period_sales_files.json() assert len(body) == 1 assert body[0].get('sales_file_id') == sales_file_1.get('sales_file_id')