"""Test approval logic.""" from datetime import datetime, timezone from unittest.mock import NonCallableMagicMock, call, patch import pytest from product_review.logic import approval as approval_logic from product_review.models import approval as approval_model from product_review.models import canned_response_history as canned_response_history_model # noqa: E501 from product_review.models import ows_product_digital as ows_product_digital_model from product_review.models import review_queue as review_queue_model from product_review.models import splitio as splitio_model from product_review.util import sfn some_exception = Exception("some exception") @pytest.mark.parametrize( ( "test_description", "note", "expected_approve_mock_calls", ), [ ( "test approve with note", "test note", [call(12321, note="test note")], ), ( "test approve with canned_response_id", None, [call(12321, note=None)], ), ], ) def test_save_approval(test_description, note, expected_approve_mock_calls, mocker): """Test save_approval.""" mocker.patch.object(review_queue_model, "approve") mocker.patch.object(approval_model, "approve") mocker.patch.object(approval_logic, "vapi_apply_revisions_if_applicable") mocked_item = NonCallableMagicMock( review_queue_id=12321, product_id=1001, status="new", locked_by_user_id="5df21529-2026-4ee9-b823-99b69ade48fd", locked_until_datetime=datetime( 2022, 10, 28, 15, 7, 52, 444714, tzinfo=timezone.utc ), ) approval_logic.save_approval(mocked_item, note) assert review_queue_model.approve.mock_calls == [call(mocked_item)] assert approval_model.approve.mock_calls == [call(12321, note=note)] assert approval_logic.vapi_apply_revisions_if_applicable.mock_calls == [call(12321)] @patch("product_review.logic.approval.ows_product_digital") @patch("product_review.logic.approval.sfn") @patch("product_review.logic.approval.review_queue_model") @patch("product_review.logic.approval.get_review_queue_item_for_update") def test_approve_via_workflow(mock_get_item, mock_model, mock_sfn, mock_ows, mocker): """Test approve via workflow.""" mock_item = NonCallableMagicMock(review_queue_id=1, product_id=2) mock_get_item.return_value = mock_item mock_model.STATUS = NonCallableMagicMock(APPLYING="applying") mocker.patch.object( splitio_model, "is_enabled_pdp_auth_check", return_value=False, ) mock_ows.validate_product.return_value = { "errors": [], "warnings": [], "tracks": {}, "is_valid": True, } approval_logic.approve_via_workflow(1, "abc", None, None, None, []) mock_model.change_status.assert_called_once_with(mock_item, "applying") mock_sfn.create_complete_review_sfn_execution.assert_called_once_with( mock_item, "approve", "abc", None ) @pytest.mark.parametrize( ( "test_description", "note", "canned_response_id", "tracks", "product_validation_result", "create_canned_response_history_args", "canned_response_ids", "is_enabled_pdp_auth_check" ), [ ( "test approve with canned_response_id", "test note", 12, None, { "errors": [], "warnings": [], "tracks": {}, "is_valid": True, }, (1, 12), [], False ), ( "test approve with track approval reasons", "test note", None, [{"track_id": 125, "canned_response_id": 12}], { "errors": [], "warnings": [], "tracks": { "125": { "is_valid": True, "tuid": 125, "errors": [], "warnings": [ { "code": "POTENTIAL_AUDIO_INFRINGEMENT", "reason": '[{"key1":"anything2"}]', } ], }, }, "is_valid": True, }, (1, 12, 125), [], False ), ( "test approve with canned_response_ids", "test note", None, None, { "errors": [], "warnings": [], "tracks": {}, "is_valid": True, }, [(1, 12), (1, 13)], [12, 13], True ), ], ) def test_approve_via_workflow_with_canned_response( test_description, note, canned_response_id, tracks, product_validation_result, create_canned_response_history_args, canned_response_ids, is_enabled_pdp_auth_check, mocker ): """Test approve via workflow.""" mock_review_queue_item = NonCallableMagicMock(review_queue_id=1, product_id=2) mocker.patch.object( review_queue_model, "change_status", return_value=NonCallableMagicMock(APPLYING="applying") ) mocker.patch.object( approval_logic, "get_review_queue_item_for_update", return_value=mock_review_queue_item ) mocker.patch.object( ows_product_digital_model, "validate_product", return_value=product_validation_result ) mocker.patch.object( canned_response_history_model, "create_canned_response_history" ) mocker.patch.object( sfn, "create_complete_review_sfn_execution" ) mocker.patch.object( splitio_model, "is_enabled_pdp_auth_check", return_value=is_enabled_pdp_auth_check, ) approval_logic.approve_via_workflow(1, "abc", note, canned_response_id, tracks, canned_response_ids) if is_enabled_pdp_auth_check: assert canned_response_history_model.create_canned_response_history.call_count == len(canned_response_ids) canned_response_history_model.create_canned_response_history.assert_has_calls( [call(*args) for args in create_canned_response_history_args], any_order=True ) else: canned_response_history_model.create_canned_response_history.assert_called_once_with( *create_canned_response_history_args ) review_queue_model.change_status.assert_called_once_with( mock_review_queue_item, "applying" ) sfn.create_complete_review_sfn_execution.assert_called_once_with( mock_review_queue_item, "approve", "abc", note )