"""Functional tests for GET /dp-payments endpoint.""" import http import json from unittest.mock import ANY from collaborator.constants import error from collaborator.constants.features import ABACUS_COLLABORATORS from collaborator.models.rds.dp_payment import DpPayment, PayoneerPaymentStatus from collaborator.schemas.dp_payment import DpPaymentSchema from tests.testutils import db from tests.testutils.seed.dp_payment_seed import dp_payment_seed_data @db.test_schema_default_seed def test_get_dp_payments_success(test_client, mock_features): """Test successfully fetching DP payments filtered by statement period.""" abacus_statement_period_id = 1 mock_features({ABACUS_COLLABORATORS: True}) result = test_client.get( f"/dp-payments?abacus_statement_period_id={abacus_statement_period_id}" ) assert result.status_code == http.HTTPStatus.OK result_data = json.loads(result.data.decode()) period_1_seed_data = [ payment for payment in dp_payment_seed_data if payment["abacus_statement_period_id"] == abacus_statement_period_id ] expected_payments = [] for payment in period_1_seed_data: expected_payment = DpPaymentSchema.parse(DpPayment(**payment)).dump() expected_payment["updated_date"] = ANY expected_payments.append(expected_payment) assert result_data == { "currency_agnostic_total_amount": sum( [payment["amount"] for payment in period_1_seed_data] ), "total_count": len(period_1_seed_data), "payments": expected_payments, } @db.test_schema_default_seed def test_get_dp_payments_feature_disabled(test_client): """Test GET returns 403 when feature flag is disabled.""" abacus_statement_period_id = 1 result = test_client.get( f"/dp-payments?abacus_statement_period_id={abacus_statement_period_id}" ) assert result.status_code == http.HTTPStatus.FORBIDDEN result_data = json.loads(result.data.decode()) assert result_data["code"] == error.ERROR_CODE_AUTHORIZATION assert result_data["message"] == error.ERROR_MESSAGE_FORBIDDEN_USER @db.test_schema_default_seed def test_get_dp_payments_includes_payoneer_payment_status(test_client, mock_features): """Test that payoneer_payment_status is included in the response.""" abacus_statement_period_id = 1 mock_features({ABACUS_COLLABORATORS: True}) result = test_client.get( f"/dp-payments?abacus_statement_period_id={abacus_statement_period_id}" ) assert result.status_code == http.HTTPStatus.OK result_data = json.loads(result.data.decode()) payments = result_data["payments"] assert payments[0]["payoneer_payment_status"] == PayoneerPaymentStatus.COMPLETE assert payments[1]["payoneer_payment_status"] is None assert payments[2]["payoneer_payment_status"] is None @db.test_schema_default_seed def test_get_all_dp_payments_success(test_client, mock_features): """Test GET /dp-payments returns all payments when no filter is applied.""" mock_features({ABACUS_COLLABORATORS: True}) result = test_client.get("/dp-payments") assert result.status_code == http.HTTPStatus.OK result_data = json.loads(result.data.decode()) expected_payments = [] for payment in dp_payment_seed_data: expected_payment = DpPaymentSchema.parse(DpPayment(**payment)).dump() expected_payment["updated_date"] = ANY expected_payments.append(expected_payment) assert result_data == { "currency_agnostic_total_amount": sum( payment["amount"] for payment in dp_payment_seed_data ), "total_count": len(dp_payment_seed_data), "payments": expected_payments, } @db.test_schema_default_seed def test_get_all_dp_payments_feature_disabled(test_client): """Test GET /dp-payments returns 403 when feature flag is disabled.""" result = test_client.get("/dp-payments") assert result.status_code == http.HTTPStatus.FORBIDDEN result_data = json.loads(result.data.decode()) assert result_data["code"] == error.ERROR_CODE_AUTHORIZATION assert result_data["message"] == error.ERROR_MESSAGE_FORBIDDEN_USER