"""Tests for copy product.""" import json from product.constants import header from product.constants import service_name from tests.testutils import db @db.test_schema def test_handler_copy_physical_product_success(client, ows_request_mocker): """Test copy product success.""" orchard_user_id = 'alw:12345' headers = { 'Content-Type': 'application/json', header.ORCHARD_USER_ID: orchard_user_id} existing_product_id = 93 product_data_changes = {'some_key': 'some value'} new_product_id = 98765 expected_response = {'product_id': new_product_id} db.insert_vw_product() ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_PRODUCT_PHYSICAL, 'path': '/product/{}/copy'.format(existing_product_id), 'status': 200, 'json': expected_response }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_PRODUCT_PHYSICAL, 'path': '/product/{}/tracks/copy/{}'.format( existing_product_id, new_product_id), 'status': 201, 'json': expected_response }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_CARVEOUTS, 'path': '/carveout/product/{}/copy/{}'.format( existing_product_id, new_product_id), 'status': 201 }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_MARKETING, 'path': '/highlights/product/{}/copy/{}'.format( existing_product_id, new_product_id), 'status': 201 }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_ASSETS, 'path': '/image/{}/copy/{}'.format( existing_product_id, new_product_id ), 'status': 200, 'json': {header.ORCHARD_USER_ID: orchard_user_id} }) ows_request_mocker.apply() result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) assert result.status_code == 200 assert json.loads(result.data.decode('utf8')) == expected_response @db.test_schema def test_handler_copy_product_grass_forbidden(client): """Test copy product grass forbidden.""" headers = { 'Content-Type': 'application/json', 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': 1234 } existing_product_id = 91 product_data_changes = {'some_key': 'some value'} db.insert_vw_product() result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) assert result.status_code == 403 @db.test_schema def test_handler_copy_product_grass_not_found(client): """Test copy product grass not found.""" headers = {'Content-Type': 'application/json'} existing_product_id = 12321 product_data_changes = {'some_key': 'some value'} db.insert_vw_product() result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) assert result.status_code == 404 @db.test_schema def test_handler_copy_digital_product_success(client, ows_request_mocker): """Test copy digital product success.""" orchard_user_id = 'alw:12345' headers = { 'Content-Type': 'application/json', header.ORCHARD_USER_ID: orchard_user_id} existing_product_id = 90 product_data_changes = {'some_key': 'some value'} new_product_id = 98766 expected_response = {'product_id': new_product_id} db.insert_vw_product() ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_PRODUCT_DIGITAL, 'path': '/product/{}/copy'.format(existing_product_id), 'status': 200, 'json': expected_response }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_TRACK, 'path': '/product/{}/tracks/copy/{}'.format( existing_product_id, new_product_id), 'status': 201, 'json': expected_response }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_CARVEOUTS, 'path': '/carveout/product/{}/copy/{}'.format( existing_product_id, new_product_id), 'status': 201 }) ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_ASSETS, 'path': '/product/{}/copy/{}'.format( existing_product_id, new_product_id ), 'status': 200, 'json': {header.ORCHARD_USER_ID: orchard_user_id} }) ows_request_mocker.apply() result = client.post( '/product/{}/copy'.format(existing_product_id), headers=headers, data=json.dumps(product_data_changes)) assert result.status_code == 200 assert json.loads(result.data.decode('utf8')) == expected_response