"""Tests for ows-product-digital model.""" import json from contextlib import nullcontext as does_not_raise import pytest from owsrequest import request from owsrequest.test_utils import MockOwsResponse from product_review.constants import services from product_review.models import ows_product_digital def test_put_rejection_ok(mocker): """Test correct call made to ows-product-digital.""" product_id = 1001 mock_response = MockOwsResponse(200, "product rejected") mocker.patch.object(request, "put", return_value=mock_response) result = ows_product_digital.reject_product(product_id) request.put.assert_called_once_with( services.OWS_PRODUCT_DIGITAL, f"/product/audio/{product_id}/reject" ) assert result.status_code == 200, result.json() assert result.json() == "product rejected" def test_put_rejection_error(mocker): """Test rejecting nonexistent product in ows-product-digital.""" product_id = 9999 mock_response = MockOwsResponse(404, {}) mocker.patch.object(request, "put", return_value=mock_response) with pytest.raises(Exception): ows_product_digital.reject_product(product_id) @pytest.mark.parametrize( ("test_description", "response_status", "expected_raise", "expected_result"), [ ("success", 200, does_not_raise(), "validation result"), ( "error", 500, pytest.raises( Exception, match=json.dumps( { "status": 500, "code": "ows_product_digital_error", "message": {"validations": "validation result"}, } ), ), None, ), ], ) def test_validate_product( request_engine, app_context, test_description, response_status, expected_raise, expected_result, ): """Test validate_product.""" validate_product_mock = request_engine[services.OWS_PRODUCT_DIGITAL].add_spec( "GET", "/product/audio/123/validate", response={"validations": "validation result"}, status=response_status, ) app_context.g.request_context.profile_type = "ProfileType" app_context.g.request_context.profile_id = "profile-id" result = None with expected_raise: result = ows_product_digital.validate_product(123) (validate_product_mock_call,) = validate_product_mock.calls assert validate_product_mock_call["headers"] == { "Content-Type": "application/json", "Orchard-Profile-Type": "ProfileType", "Orchard-Profile-Id": "profile-id", } assert validate_product_mock_call["params"] == { "validation_context": "post_submission", "format": "list", } assert result == expected_result @pytest.mark.parametrize( ( "test_description", "product_validation_result", "expected_result", ), [ ( "product_validation_result without POTENTIAL_AUDIO_INFRINGEMENT warnings", { "is_valid": True, "errors": [], "warnings": [], "tracks": { "123": { "is_valid": True, "tuid": 123, "errors": [], "warnings": [ {"code": "CODE_A", "reason": "reason_a"}, {"code": "CODE_B", "reason": "reason_b"}, ], } }, }, [], ), ( "product_validation_result with POTENTIAL_AUDIO_INFRINGEMENT warnings", { "is_valid": True, "errors": [], "warnings": [], "tracks": { "123": { "is_valid": True, "tuid": 123, "errors": [], "warnings": [ {"code": "CODE_A", "reason": "reason_a"}, { "code": "POTENTIAL_AUDIO_INFRINGEMENT", "reason": "reason_123", }, ], }, "124": { "is_valid": True, "tuid": 124, "errors": [], "warnings": [ { "code": "POTENTIAL_AUDIO_INFRINGEMENT", "reason": "reason_124", }, {"code": "CODE_B", "reason": "reason_b"}, ], }, "125": { "is_valid": True, "tuid": 125, "errors": [], "warnings": [ { "code": "CROSS_ACCOUNT_OSR_CONFLICT", "reason": "reason_125", }, {"code": "CODE_C", "reason": "reason_c"}, ], }, }, }, [ { "track_id": 123, "warning": { "code": "POTENTIAL_AUDIO_INFRINGEMENT", "reason": "reason_123" } }, { "track_id": 124, "warning": { "code": "POTENTIAL_AUDIO_INFRINGEMENT", "reason": "reason_124" } }, { "track_id": 125, "warning": { "code": "CROSS_ACCOUNT_OSR_CONFLICT", "reason": "reason_125" } }, ], ), ], ) def test_select_potential_audio_infringement_track_warnings( test_description, product_validation_result, expected_result ): """Test select_potential_audio_infringement_track_warnings.""" assert ( ows_product_digital.select_potential_audio_infringement_track_warnings( product_validation_result ) == expected_result ) @pytest.mark.parametrize( ( "test_description", "product_validation_result", "expected_result", ), [ ( "Valid with valid product_blocklist", {"is_valid": True, "errors": [], "warnings": [], "tracks": {}}, True, ), ( "Valid with invalid product_blocklist", { "is_valid": True, "errors": [], "warnings": [{"code": "BLOCKLIST", "reason": "{info}"}], "tracks": {}, }, True, ), ( "Valid with invalid product_blocklist", { "is_valid": False, "errors": [{"code": "CODE_A", "reason": "reason_a"}], "warnings": [], "tracks": {}, }, False, ), ], ) def test_is_product_valid(test_description, product_validation_result, expected_result): """Test is_product_valid.""" assert ( ows_product_digital.is_product_valid(product_validation_result) == expected_result ) def test_put_approval_ok(mocker): """Test correct call made to ows-product-digital.""" product_id = 1001 mock_response = MockOwsResponse(200, "product approved") mocker.patch.object(request, "put", return_value=mock_response) result = ows_product_digital.approve_product(product_id) request.put.assert_called_once_with( services.OWS_PRODUCT_DIGITAL, f"/product/audio/{product_id}/approve" ) assert result.status_code == 200 assert result.json() == "product approved" def test_put_approval_error(mocker): """Test approving nonexistent product in ows-product-digital.""" product_id = 9999 mocker.patch.object(request, "put", return_value=MockOwsResponse(404, "foo")) with pytest.raises(Exception): ows_product_digital.approve_product(product_id) @pytest.mark.parametrize( ( "test_description", "response_status", "expected_result", "expected_raise", "expected_raise_message", ), [ ( "Success", 200, {"applesauce": "bananas"}, does_not_raise(), "None", ), ( "Error", 808, None, pytest.raises(Exception), '{"status": 808, "code": "ows_product_digital_error", ' '"message": "{\\"applesauce\\": \\"bananas\\"}"}', ), ], ) def test_get_product( test_description, response_status, expected_result, expected_raise, expected_raise_message, request_engine, app_context, ): """Test get_product.""" request_engine[services.OWS_PRODUCT_DIGITAL].add_spec( "GET", "/product/audio/12321", {"applesauce": "bananas"}, status=response_status ) result = None with expected_raise as er: result = ows_product_digital.get_product(12321) assert str(getattr(er, "value", None)) == expected_raise_message assert result == expected_result @pytest.mark.parametrize( ( "test_description", "response_status", "expected_result", "expected_raise", "expected_raise_message", ), [ ( "Success", 200, {"foo": "bar"}, does_not_raise(), "None", ), ( "Error", 808, None, pytest.raises(Exception), '{"status": 808, "code": "ows_product_digital_error", ' '"message": "{\\"foo\\": \\"bar\\"}"}', ), ], ) def test_update_not_for_distribution( test_description, response_status, expected_result, expected_raise, expected_raise_message, request_engine, app_context, ): """Test update_not_for_distribution.""" request_engine[services.OWS_PRODUCT_DIGITAL].add_spec( "PUT", "/product/12321/not_for_distribution", {"foo": "bar"}, status=response_status, ) result = None with expected_raise as er: result = ows_product_digital.update_not_for_distribution( 12321, "ReviewedWontDeliver" ) assert str(getattr(er, "value", None)) == expected_raise_message assert result == expected_result