"""Tests for delete product-distribution logic.""" from unittest.mock import MagicMock from oto import response from ows_product_physical.constant import error from ows_product_physical.logic import product_distribution from ows_product_physical.models import ows_product from ows_product_physical.models import persister from ows_product_physical.models import product_distribution \ as product_distribution_model def test_delete_product_distribution_success( monkeypatch, valid_delete_header, get_product_distribution_response ): """Test delete product distribution for product success.""" monkeypatch.setattr( ows_product, 'check_product_ownership', value=MagicMock( return_value=response.Response(message=True))) monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( product_distribution_model, 'get_product_distribution', value=MagicMock(return_value=get_product_distribution_response)) monkeypatch.setattr( product_distribution_model, 'delete_product_distribution', value=MagicMock(return_value=response.Response( message={'status': 'ok'}, status=200))) account_type = valid_delete_header['Grass-Account-Type'] account_id = valid_delete_header['Grass-Account-Id'] result = product_distribution.delete_product_distribution( 1234, account_type, account_id) assert product_distribution_model.delete_product_distribution.called assert result.status == 200 assert result.message.get('status') == 'ok' def test_delete_product_distribution_fail( monkeypatch, valid_delete_header, get_product_distribution_response ): """Test delete product distribution fails when mysql error occurs.""" monkeypatch.setattr( ows_product, 'check_product_ownership', value=MagicMock( return_value=response.Response(message=True))) monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( product_distribution_model, 'get_product_distribution', value=MagicMock(return_value=get_product_distribution_response)) monkeypatch.setattr( product_distribution_model, 'delete_product_distribution', value=MagicMock(return_value=response.create_error_response( code=error.INTERNAL_ERROR, message='mysql error', status=500))) account_type = valid_delete_header['Grass-Account-Type'] account_id = valid_delete_header['Grass-Account-Id'] result = product_distribution.delete_product_distribution( 1234, account_type, account_id) assert result.status == 500 assert product_distribution_model.delete_product_distribution.called def test_delete_product_distribution_not_found( monkeypatch, valid_delete_header): """Test delete product distribution fails when product is not found.""" monkeypatch.setattr( ows_product, 'check_product_ownership', value=MagicMock( return_value=response.Response(message=True))) monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( product_distribution_model, 'get_product_distribution', value=MagicMock(return_value=response.Response( message={'items': []}, status=200))) monkeypatch.setattr( product_distribution_model, 'delete_product_distribution', value=MagicMock(return_value=response.Response())) account_type = valid_delete_header['Grass-Account-Type'] account_id = valid_delete_header['Grass-Account-Id'] result = product_distribution.delete_product_distribution( 1234, account_type, account_id) assert result.status == 404 assert result.errors['message'] == \ error.PRODUCT_DISTRIBUTION_NOT_FOUND_MESSAGE assert product_distribution_model.get_product_distribution.called def test_delete_product_distribution_invalid_ownership( monkeypatch, valid_delete_header): """Test delete product distribution fails when ownership is false.""" monkeypatch.setattr( ows_product, 'check_product_ownership', value=MagicMock( return_value=response.Response(message=False))) monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( product_distribution_model, 'delete_product_distribution', value=MagicMock(return_value=response.Response())) account_type = valid_delete_header['Grass-Account-Type'] account_id = 'invalid_id' result = product_distribution.delete_product_distribution( 1234, account_type, account_id) assert result.status == 403 assert not product_distribution_model.delete_product_distribution.called assert result.errors['message'] == error.OWNERSHIP_ERROR_MESSAGE.format( account_type) args, kwargs = ows_product.check_product_ownership.call_args assert args == ( 1234, valid_delete_header['Grass-Account-Type'], 'invalid_id') def test_delete_product_distribution_without_grass_header( monkeypatch, get_product_distribution_response ): """Test delete product distribution call without grass-headers succeeds.""" monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( product_distribution_model, 'get_product_distribution', value=MagicMock(return_value=get_product_distribution_response)) monkeypatch.setattr( product_distribution_model, 'delete_product_distribution', value=MagicMock(return_value=response.Response())) result = product_distribution.delete_product_distribution(1234) assert result.status == 200 assert product_distribution_model.delete_product_distribution.called