"""Tests for copy product handler.""" import json from oto import response from product.logic import product as product_logic from product.logic import product_copy as product_copy_logic orchard_user_id = 'alw:123456' def test_handler_copy_product_grass_success(client, mocker): """Test copy product success.""" account_id = 1234 account_type = 'vendor' headers = { 'Content-Type': 'application/json', 'Grass-Account-Type': account_type, 'Grass-Account-Id': account_id, 'Orchard-User-Id': orchard_user_id } existing_product_id = 12321 product_data_changes = {'some_key': 'some value'} new_product_id = 98765 expected_response = {'product_id': new_product_id} mocker.patch.object( product_logic, 'check_product_ownership', return_value=response.Response()) mocker.patch.object( product_copy_logic, 'copy_product', return_value=response.Response(message=expected_response)) result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) product_logic.check_product_ownership.assert_called_once_with( existing_product_id, vendor_id='1234') product_copy_logic.copy_product.assert_called_once_with( existing_product_id, product_data_changes, orchard_user_id, account_type, str(account_id)) assert result.status_code == 200 assert json.loads(result.data.decode('utf8')) == expected_response def test_handler_copy_product_grass_forbidden(client, mocker): """Test copy product unauth.""" headers = { 'Content-Type': 'application/json', 'Grass-Account-Type': 'vendor' } existing_product_id = 12321 product_data_changes = {'some_key': 'some value'} new_product_id = 98765 expected_response = {'product_id': new_product_id} mocker.patch.object( product_logic, 'check_product_ownership', return_value=response.Response(status=403)) mocker.patch.object( product_copy_logic, 'copy_product', return_value=response.Response(message=expected_response)) result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) product_logic.check_product_ownership.assert_called_once_with( existing_product_id, vendor_id=None) product_copy_logic.copy_product.assert_not_called() assert result.status_code == 403 def test_handler_copy_product_non_grass_success(client, mocker): """Test copy product success.""" account_id = None account_type = None headers = { 'Content-Type': 'application/json', 'Orchard-User-Id': orchard_user_id} existing_product_id = 12321 product_data_changes = {'some_key': 'some value'} new_product_id = 98765 expected_response = {'product_id': new_product_id} mocker.patch.object(product_logic, 'check_product_ownership') mocker.patch.object( product_copy_logic, 'copy_product', return_value=response.Response(message=expected_response)) result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) product_logic.check_product_ownership.assert_not_called() product_copy_logic.copy_product.assert_called_once_with( existing_product_id, product_data_changes, orchard_user_id, account_type, account_id) assert result.status_code == 200 assert json.loads(result.data.decode('utf8')) == expected_response def test_handler_copy_product_flexible_grass_success(client, mocker): """Test copy product success.""" account_id = 1234 account_type = 'vendor' headers = { 'Content-Type': 'application/json', 'Grass-Account-Type': account_type, 'Grass-Account-Id': account_id, 'Orchard-User-Id': orchard_user_id } existing_product_id = 12321 product_data_changes = {'some_key': 'some value'} new_product_id = 98765 context = 'digital' expected_response = {'product_id': new_product_id} mocker.patch.object( product_logic, 'check_product_ownership', return_value=response.Response()) mocker.patch.object( product_copy_logic, 'copy_product_flexible', return_value=response.Response(message=expected_response)) result = client.post( '/product/{}/copy/{}'.format(existing_product_id, context), headers=headers, data=json.dumps(product_data_changes)) product_logic.check_product_ownership.assert_called_once_with( existing_product_id, vendor_id='1234') product_copy_logic.copy_product_flexible.assert_called_once_with( existing_product_id, 'digital', product_data_changes, orchard_user_id, account_type, str(account_id)) assert result.status_code == 200 assert json.loads(result.data.decode('utf8')) == expected_response def test_handler_copy_product_flexible_grass_forbidden(client, mocker): """Test copy product unauth.""" headers = { 'Content-Type': 'application/json', 'Grass-Account-Type': 'vendor' } existing_product_id = 12321 product_data_changes = {'some_key': 'some value'} new_product_id = 98765 context = 'digital' expected_response = {'product_id': new_product_id} mocker.patch.object( product_logic, 'check_product_ownership', return_value=response.Response(status=403)) mocker.patch.object( product_copy_logic, 'copy_product_flexible', return_value=response.Response(message=expected_response)) result = client.post( '/product/{}/copy/{}'.format(existing_product_id, context), headers=headers, data=json.dumps(product_data_changes)) product_logic.check_product_ownership.assert_called_once_with( existing_product_id, vendor_id=None) product_copy_logic.copy_product_flexible.assert_not_called() assert result.status_code == 403 def test_handler_copy_product_flexible_non_grass_success(client, mocker): """Test copy product success.""" account_id = None account_type = None headers = { 'Content-Type': 'application/json', 'Orchard-User-Id': orchard_user_id} existing_product_id = 12321 context = 'physical' product_data_changes = {'some_key': 'some value'} new_product_id = 98765 expected_response = {'product_id': new_product_id} mocker.patch.object(product_logic, 'check_product_ownership') mocker.patch.object( product_copy_logic, 'copy_product_flexible', return_value=response.Response(message=expected_response)) result = client.post( '/product/{}/copy/{}'.format(existing_product_id, context), headers=headers, data=json.dumps(product_data_changes)) product_logic.check_product_ownership.assert_not_called() product_copy_logic.copy_product_flexible.assert_called_once_with( existing_product_id, context, product_data_changes, orchard_user_id, account_type, account_id) assert result.status_code == 200 assert json.loads(result.data.decode('utf8')) == expected_response