"""Tests for get_physical_product_change_history.""" from unittest.mock import patch from oto import response from owsrequest.constants import headers import pytest from ows_product_physical.logic import product_delivery @pytest.mark.parametrize( 'data,logic_params', [ ( {'product_ids': '2136606'}, {'product_ids': [2136606], 'store_id': 1705}, ), ( {'product_ids': '2136606,3706158'}, {'product_ids': [2136606, 3706158], 'store_id': 1705}, ), ( {'product_ids': ' 2136606 , 3706158 '}, {'product_ids': [2136606, 3706158], 'store_id': 1705}, ), ], ) def test_get_physical_product_change_history( data, logic_params, client, distribution_profile_header ): """Test `GET products//change-history` success.""" with patch.object( product_delivery, 'get_physical_product_change_history', return_value=response.Response({'foo': 'bar'}) ) as mock_logic: result = client.get( '/products/1705/change-history', query_string=data, headers=distribution_profile_header) assert result.status_code == 200 mock_logic.assert_called_once_with(**logic_params) def test_change_history_no_headers(client, distribution_profile_header): """Test invalid `GET products//change-history`.""" data = {'product_ids': '2136606'} with patch.object( product_delivery, 'get_physical_product_change_history', return_value=response.Response({'foo': 'bar'}) ) as mock_logic: result = client.get( '/products/1705/change-history', query_string=data, ) assert result.status_code == 403 mock_logic.assert_not_called() def test_change_history_no_roles(client, distribution_profile_header): """Test invalid `GET products//change-history`.""" data = {'product_ids': '2136606'} headers1 = distribution_profile_header.copy() del headers1[headers.ORCHARD_ROLES] with patch.object( product_delivery, 'get_physical_product_change_history', return_value=response.Response({'foo': 'bar'}) ) as mock_logic: result = client.get( '/products/1705/change-history', query_string=data, headers=headers1) assert result.status_code == 403 mock_logic.assert_not_called() def test_change_history_missing_roles(client, distribution_profile_header): """Test invalid `GET products//change-history`.""" data = {'product_ids': '2136606'} headers1 = distribution_profile_header.copy() headers1[headers.ORCHARD_ROLES] = 'manage_nr_deliveries' with patch.object( product_delivery, 'get_physical_product_change_history', return_value=response.Response({'foo': 'bar'}) ) as mock_logic: result = client.get( '/products/1705/change-history', query_string=data, headers=headers1) assert result.status_code == 403 mock_logic.assert_not_called() def test_change_history_grass_headers(client, valid_get_header): """Test invalid `GET products//change-history`.""" data = {'product_ids': '2136606'} with patch.object( product_delivery, 'get_physical_product_change_history', return_value=response.Response({'foo': 'bar'}) ) as mock_logic: result = client.get( '/products/1705/change-history', query_string=data, headers=valid_get_header) assert result.status_code == 403 mock_logic.assert_not_called() @pytest.mark.parametrize( 'data,error_message', [ ( {}, {'product_ids': ['Missing data for required field.']} ), ( {'product_ids': ''}, {'product_ids': {'0': ['Not a valid integer.']}} ), ( {'product_ids': 'abcd'}, {'product_ids': {'0': ['Not a valid integer.']}} ), ( {'product_ids': '2136606,foo'}, {'product_ids': {'1': ['Not a valid integer.']}} ), ], ) def test_change_history_validations( data, error_message, client, distribution_profile_header ): """Test invalid `GET products//change-history`.""" with patch.object( product_delivery, 'get_physical_product_change_history', return_value=response.Response({'foo': 'bar'}) ) as mock_logic: result = client.get( '/products/1705/change-history', query_string=data, headers=distribution_profile_header) assert result.status_code == 400 mock_logic.assert_not_called() assert result.json['message'] == error_message