"""Unit tests for unsafe methods.""" from oto import response from product_digital.constants.product_statuses import IN_CONTENT from product_digital.logic import unsafe_methods from product_digital.models import release, ows_product def test_UNSAFE_update_display_upc(mocker, product_id, display_upc): """Test that a product dict is returned with correct formatting.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'SMEAnalyticsDummy'})) mocker.patch.object( release, 'UNSAFE_update_display_upc', return_value=response.Response(status=200, message={'display_upc': display_upc}) ) mocker.patch.object( ows_product, 'get_product_by_upc', return_value=response.Response({'product_id': product_id, 'status': 'label_processing', 'deletions': 'N', 'not_for_distribution': 'N' })) unsafe_methods.UNSAFE_update_display_upc(product_id, display_upc) assert release.UNSAFE_update_display_upc.call_count == 1 release.UNSAFE_update_display_upc.assert_called_with(product_id, display_upc) def test_UNSAFE_update_display_upc_update_release_model(mocker, product_id, display_upc): """Test that release was updated.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'SMEAnalyticsDummy'})) mocker.patch.object( release, 'UNSAFE_update_display_upc', return_value=response.Response(status=200, message={'display_upc': display_upc}) ) mocker.patch.object( ows_product, 'get_product_by_upc', return_value=response.Response({'product_id': product_id, 'status': 'label_processing', 'deletions': 'N', 'not_for_distribution': 'N' })) unsafe_methods.UNSAFE_update_display_upc(product_id, display_upc) assert release.UNSAFE_update_display_upc.call_count == 1 release.UNSAFE_update_display_upc.assert_called_with(product_id, display_upc) def test_UNSAFE_update_display_upc_for_non_sme_product(mocker, product_id, display_upc): """Test that update is denied for non-SME products.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'N'})) update_response = unsafe_methods.UNSAFE_update_display_upc(product_id, display_upc) assert update_response.status == 403 def test_UNSAFE_update_display_upc_denied_for_sme_substituted_product(mocker, product_id, display_upc): """Test update denied if substituted product is SME product.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'SMEAnalyticsDummy'})) mocker.patch.object( ows_product, 'get_product_by_upc', return_value=response.Response({'product_id': product_id, 'status': 'label_processing', 'deletions': 'N', 'not_for_distribution': 'SMEAnalyticsDummy' })) update_response = unsafe_methods.UNSAFE_update_display_upc(product_id, display_upc) assert update_response.status == 403 assert update_response.message == (f'Substituted product (the product with upc {display_upc}) ' f'nfd value cannot be \'SMEAnalyticsDummy\'.') def test_UNSAFE_update_display_upc_denied_for_in_content_non_deleted_product(mocker, product_id, display_upc): """Test update denied if substituted product is in_content and deletions = N.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'SMEAnalyticsDummy'})) mocker.patch.object( ows_product, 'get_product_by_upc', return_value=response.Response({'product_id': product_id, 'status': IN_CONTENT, 'deletions': 'N', 'not_for_distribution': 'N' })) update_response = unsafe_methods.UNSAFE_update_display_upc(product_id, display_upc) assert update_response.status == 403 assert update_response.message == (f"Substituted product (the product with upc {display_upc}) " "product has to be either not in_content or marked as deleted.") def test_UNSAFE_update_display_upc_allowed_for_in_content_non_deleted_product(mocker, product_id, display_upc): """Test update allowed if substituted product is in_content and deletions = Y.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'SMEAnalyticsDummy'})) mocker.patch.object( release, 'UNSAFE_update_display_upc', return_value=response.Response(status=200, message={'display_upc': display_upc}) ) mocker.patch.object( ows_product, 'get_product_by_upc', return_value=response.Response({'product_id': product_id, 'status': IN_CONTENT, 'deletions': 'Y', 'not_for_distribution': 'N' })) update_response = unsafe_methods.UNSAFE_update_display_upc(product_id, display_upc) assert release.UNSAFE_update_display_upc.call_count == 1 release.UNSAFE_update_display_upc.assert_called_with(product_id, display_upc) assert update_response.status == 200 def test_UNSAFE_soft_delete_rejected(mocker, product_id): """Test that soft delete rejected for non sme product.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'N', 'status': IN_CONTENT})) soft_delete_response = unsafe_methods.UNSAFE_soft_delete(product_id) assert soft_delete_response.status == 403 assert soft_delete_response.message == ("It is only allowed to soft delete " "products with nfd value 'SMEAnalyticsDummy'.") def test_UNSAFE_soft_delete_success(mocker, product_id): """Test success UNSAFE_soft_delete.""" mocker.patch.object( ows_product, 'get_product', return_value=response.Response({'product_id': product_id, 'not_for_distribution': 'SMEAnalyticsDummy', 'status': IN_CONTENT})) mocker.patch.object( release, 'UNSAFE_soft_delete', return_value=response.Response(status=200, message={'deletions': 'Y'}) ) soft_delete_response = unsafe_methods.UNSAFE_soft_delete(product_id) assert soft_delete_response.status == 200 assert release.UNSAFE_soft_delete.call_count == 1 release.UNSAFE_soft_delete.assert_called_with(product_id)