"""Tests for the submit DP payments handler.""" from http import HTTPStatus from unittest.mock import ANY, patch from collaborator.constants import error from collaborator.constants.features import ( ABACUS_COLLABORATORS_PAYONEER_API_INTEGRATION, ) from collaborator.utils.error import OwsError from tests.testutils import mock_auth @patch("collaborator.logic.abacus_statement_period.submit_dp_payments") def test_submit_dp_payments_success( mock_submit, auth_client, mock_account, mocker, mock_features, ): """POST .../dp-payments/submit-to-payoneer returns 200.""" mock_auth(mocker, mock_account.id) mock_features({ABACUS_COLLABORATORS_PAYONEER_API_INTEGRATION: True}) mock_submit.return_value = {"submitted_count": 2, "program_ids": [100000, 200000]} abacus_statement_period_id = 1 result = auth_client.post( f"/abacus-statement-period/{abacus_statement_period_id}/dp-payments/submit-to-payoneer" ) mock_submit.assert_called_once_with(abacus_statement_period_id, ANY) assert result.status_code == 200 assert result.json == {"submitted_count": 2, "program_ids": [100000, 200000]} def test_submit_dp_payments_feature_disabled( auth_client, mock_account, mocker, mock_features, ): """POST .../dp-payments/submit-to-payoneer returns 403 when FF is off.""" mock_auth(mocker, mock_account.id) # Feature flag NOT enabled (mock_features default is False) abacus_statement_period_id = 1 result = auth_client.post( f"/abacus-statement-period/{abacus_statement_period_id}/dp-payments/submit-to-payoneer" ) assert result.status_code == 403 assert result.json["code"] == error.ERROR_CODE_AUTHORIZATION assert result.json["message"] == error.ERROR_MESSAGE_FORBIDDEN_USER @patch("collaborator.logic.abacus_statement_period.submit_dp_payments") def test_submit_dp_payments_no_approved_raises_422( mock_submit, auth_client, mock_account, mocker, mock_features, ): """POST returns 422 when logic raises OwsError for no approved payments.""" mock_auth(mocker, mock_account.id) mock_features({ABACUS_COLLABORATORS_PAYONEER_API_INTEGRATION: True}) mock_submit.side_effect = OwsError( code=error.ERROR_CODE_DP_PAYMENT_NOT_APPROVED, message=error.ERROR_MESSAGE_DP_PAYMENT_NOT_APPROVED, status=HTTPStatus.UNPROCESSABLE_ENTITY, ) abacus_statement_period_id = 1 result = auth_client.post( f"/abacus-statement-period/{abacus_statement_period_id}/dp-payments/submit-to-payoneer" ) assert result.status_code == 422 assert result.json["code"] == error.ERROR_CODE_DP_PAYMENT_NOT_APPROVED