"""Tests for get product-distribution logic.""" from unittest.mock import MagicMock from oto import response from ows_product_physical.constant import error from ows_product_physical.constant import header from ows_product_physical.constant import success 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_get_product_distribution_success(monkeypatch, valid_get_header, get_product_distribution_response): """Test get 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)) account_type = valid_get_header['Grass-Account-Type'] account_id = valid_get_header['Grass-Account-Id'] result = product_distribution.fetch_product_distribution( 1234, account_type, account_id) assert result.status == 200 assert product_distribution_model.get_product_distribution.called assert ows_product.check_product_ownership.called is True def test_get_product_distribution_fail(monkeypatch, valid_get_header): """Test get 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=response.create_error_response( code=error.INTERNAL_ERROR, message='mysql error', status=500))) account_type = valid_get_header['Grass-Account-Type'] account_id = valid_get_header['Grass-Account-Id'] result = product_distribution.fetch_product_distribution( 1234, account_type, account_id) assert result.status == 500 assert product_distribution_model.get_product_distribution.called def test_get_product_distribution_not_found(monkeypatch, valid_get_header): """Test get product distribution fails when it 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.create_not_found_response())) account_type = valid_get_header['Grass-Account-Type'] account_id = valid_get_header['Grass-Account-Id'] result = product_distribution.fetch_product_distribution( 1234, account_type, account_id) assert result.status == 404 assert product_distribution_model.get_product_distribution.called def test_get_product_distribution_invalid_ownership( monkeypatch, valid_get_header): """Test get 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, 'get_product_distribution', value=MagicMock(return_value=response.Response())) account_type = valid_get_header['Grass-Account-Type'] account_id = 'invalid_id' result = product_distribution.fetch_product_distribution( 1234, account_type, account_id) assert result.status == 403 assert not product_distribution_model.get_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_get_header['Grass-Account-Type'], 'invalid_id') def test_get_product_distribution_without_grass_header(monkeypatch): """Test get 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=response.Response())) result = product_distribution.fetch_product_distribution(1234) assert result.status == 200 assert product_distribution_model.get_product_distribution.called def test_save_product_distribution_success(monkeypatch, valid_post_header): """Test save_product_distribution method in success.""" save_product_distribution_response = {'status': 'success'} request_data = {'distribute_to': 'AB'} monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( ows_product, 'check_product_ownership', value=MagicMock( return_value=response.Response(message=True))) monkeypatch.setattr( product_distribution_model, 'set_product_distribution', value=MagicMock( return_value=response.Response( message=save_product_distribution_response, status=success.SUCCESS_CODE))) account_type = valid_post_header[header.GRASS_ACCOUNT_TYPE] account_id = valid_post_header[header.GRASS_ACCOUNT_ID] user = valid_post_header[header.ORCHARD_USER_ID] result = product_distribution.save_product_distribution( 1234, request_data, user, account_type, account_id) assert result.status == 200 assert product_distribution_model.set_product_distribution.called is True assert ows_product.check_product_ownership.called is True assert result.message == save_product_distribution_response def test_save_product_distribution_for_existing_data(monkeypatch, valid_post_header): """Test when existing record found in product_distribution.""" save_product_distribution_response = {'status': 'success'} request_data = {'distribute_to': 'AB'} monkeypatch.setattr( persister, 'get_product_by_id', value=MagicMock( return_value=response.Response(status=200))) monkeypatch.setattr( ows_product, 'check_product_ownership', value=MagicMock( return_value=response.Response(message=True))) monkeypatch.setattr( product_distribution_model, 'set_product_distribution', value=MagicMock( return_value=response.Response( message=save_product_distribution_response, status=success.SUCCESS_CODE))) account_type = valid_post_header[header.GRASS_ACCOUNT_TYPE] account_id = valid_post_header[header.GRASS_ACCOUNT_ID] user = valid_post_header[header.ORCHARD_USER_ID] result = product_distribution.save_product_distribution( 1234, request_data, user, account_type, account_id) assert result.status == 200 assert ows_product.check_product_ownership.called is True assert result.message == save_product_distribution_response def test_save_product_distribution_invalid_ownership( monkeypatch, valid_post_header): """Test save product distribution fails when ownership is false.""" request_data = {'distribute_to': 'AB'} 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))) account_type = valid_post_header[header.GRASS_ACCOUNT_TYPE] user = valid_post_header[header.ORCHARD_USER_ID] account_id = 'invalid_id' result = product_distribution.save_product_distribution( 12345, request_data, user, account_type, account_id) assert result.status == 403 assert result.errors['message'] == error.OWNERSHIP_ERROR_MESSAGE.format( account_type) args, kwargs = ows_product.check_product_ownership.call_args assert args == ( 12345, valid_post_header[header.GRASS_ACCOUNT_TYPE], 'invalid_id')