"""Functional tests for creating transactions for collaborators.""" import json from unittest.mock import ANY from oto import status as response_code from collaborator.constants.header import ABACUS_PROFILE from tests.testutils import db @db.test_schema_default_seed def test_post_collaborators_transactions(auth_client, mocker, mock_account): """Test creating collaborators transactions with a CollaboratorsProfile.""" input_data = [ { "collaborator_id": 1, "date": "2012-12-20", "type": "PAYMENT", "original_amount": 12.34, "currency": "USD", }, { "collaborator_id": 2, "date": "2012-12-20", "type": "REVENUE", "original_amount": 100.0, "currency": "USD", "collaborator_share": 0.5, "description": "A test", "report_id": 4, }, { "collaborator_id": 2, "date": "2012-12-20", "type": "WHT_ALLOCATION", "original_amount": 100.0, "currency": "USD", "collaborator_share": 0.5, "description": "A test", "report_id": 4, }, ] result = auth_client.post( "/transactions", data=json.dumps(input_data), content_type="application/json" ) result_data = json.loads(result.data.decode()) assert result.status_code == response_code.CREATED assert result_data == [ { "id": ANY, "collaborator_id": 1, "date": input_data[0]["date"], "type": "PAYMENT", "description": None, "original_amount": -12.34, "collaborator_share": None, "chargeable_amount": -12.34, "transferwise_transaction_id": None, "report_id": None, "created_date": ANY, "voided_transaction_id": None, "credited_payment_id": None, "currency": "USD", "deleted_date": None, "current_balance": 0, "statement_period_id": 2, "creation_batch_uuid": ANY, }, { "id": ANY, "collaborator_id": 2, "date": input_data[1]["date"], "type": "REVENUE", "description": "A test", "original_amount": 100.0, "collaborator_share": 0.5, "chargeable_amount": 50.0, "transferwise_transaction_id": None, "report_id": 4, "created_date": ANY, "voided_transaction_id": None, "credited_payment_id": None, "currency": "USD", "deleted_date": None, "current_balance": 0, "statement_period_id": 2, "creation_batch_uuid": ANY, }, { "id": ANY, "collaborator_id": 2, "date": input_data[1]["date"], "type": "WHT_ALLOCATION", "description": "A test", "original_amount": 100.0, "collaborator_share": 0.5, "chargeable_amount": 50.0, "transferwise_transaction_id": None, "report_id": 4, "created_date": ANY, "voided_transaction_id": None, "credited_payment_id": None, "currency": "USD", "deleted_date": None, "current_balance": 0, "statement_period_id": 2, "creation_batch_uuid": ANY, }, ] @db.test_schema_default_seed def test_post_collaborators_transactions_missing_required_field( auth_client, mocker, mock_account ): """Expect a 400 when a transaction is missing a required field.""" input_data = [ { # missing original_amount "collaborator_id": 1, "date": "2012-12-20", "type": "PAYMENT", "currency": "USD", }, ] result = auth_client.post( "/transactions", data=json.dumps(input_data), content_type="application/json" ) assert result.status_code == response_code.BAD_REQUEST @db.test_schema_default_seed def test_post_collaborators_transactions_not_a_list(auth_client, mocker, mock_account): """Expect a 400 when the body is not a JSON array.""" result = auth_client.post( "/transactions", data=json.dumps({"collaborator_id": 1}), content_type="application/json", ) assert result.status_code == response_code.BAD_REQUEST @db.test_schema_default_seed def test_post_collaborators_transactions_not_all_collaborators_found( auth_client, mocker, mock_account ): """Test creating collaborators transactions with a CollaboratorsProfile.""" input_data = [ { "collaborator_id": 1, "date": "2012-12-20", "type": "PAYMENT", "original_amount": 12.34, "currency": "USD", }, { "collaborator_id": 18, "date": "2012-12-20", "type": "REVENUE", "original_amount": 100.0, "currency": "USD", "collaborator_share": 0.5, "description": "A test", "report_id": 4, }, ] result = auth_client.post( "/transactions", data=json.dumps(input_data), content_type="application/json" ) result_data = json.loads(result.data.decode()) assert result_data == { "message": "Collaborator Not Found", "code": "collaborator_not_found", } assert result.status_code == 404 @db.test_schema_default_seed def test_post_collaborators_transactions_no_access(auth_client, mocker): """Test creating collaborators transactions with no access.""" input_data = [ { # has access "collaborator_id": 1, "date": "2012-12-20", "type": "WHT_ALLOCATION", "original_amount": 12.34, "currency": "USD", }, { # no access "collaborator_id": 3, "date": "2012-12-20", "type": "REVENUE", "original_amount": 100.0, "currency": "USD", "collaborator_share": 0.5, "description": "A test", "report_id": 4, }, ] result = auth_client.post( "/transactions", data=json.dumps(input_data), content_type="application/json" ) assert result.status_code == response_code.FORBIDDEN @db.test_schema_default_seed def test_post_collaborators_transactions_abacus(make_auth_client, mocker, mock_account): """Test creating collaborators transactions with a AbacusProfile.""" auth_client = make_auth_client(ABACUS_PROFILE) input_data = [ { # vendor 24601 "collaborator_id": 1, "date": "2012-12-20", "type": "WHT_ALLOCATION", "original_amount": 12.34, "currency": "USD", }, { # vendor 90210 "collaborator_id": 3, "date": "2012-12-20", "type": "REVENUE", "original_amount": 100.0, "currency": "USD", "collaborator_share": 0.5, "description": "A test", "report_id": 4, }, ] result = auth_client.post( "/transactions", data=json.dumps(input_data), content_type="application/json" ) result_data = json.loads(result.data.decode()) assert result.status_code == response_code.CREATED assert result_data == [ { "id": ANY, "collaborator_id": 1, "date": input_data[0]["date"], "type": "WHT_ALLOCATION", "description": None, "original_amount": 12.34, "collaborator_share": None, "chargeable_amount": 12.34, "transferwise_transaction_id": None, "report_id": None, "created_date": ANY, "voided_transaction_id": None, "credited_payment_id": None, "currency": "USD", "deleted_date": None, "current_balance": 0, "statement_period_id": 2, "creation_batch_uuid": ANY, }, { "id": ANY, "collaborator_id": 3, "date": input_data[1]["date"], "type": "REVENUE", "description": "A test", "original_amount": 100.0, "collaborator_share": 0.5, "chargeable_amount": 50.0, "transferwise_transaction_id": None, "report_id": 4, "created_date": ANY, "voided_transaction_id": None, "credited_payment_id": None, "currency": "USD", "deleted_date": None, "current_balance": 0, "statement_period_id": 90210, "creation_batch_uuid": ANY, }, ]