"""Functional tests for product creation.""" from copy import deepcopy import json from oto import response import pytest from ows_product_physical.constant import field from ows_product_physical.constant import header from ows_product_physical.constant import marketing from ows_product_physical.constant import service_name from ows_product_physical.models import persister @pytest.fixture def post_data_with_display_upc( post_data_no_upcish, valid_display_upc, valid_country_of_origin): """Valid post data with display_upc and country_of_origin set.""" data = deepcopy(post_data_no_upcish) data[field.DISPLAY_UPC] = valid_display_upc data[field.COUNTRY_OF_ORIGIN] = valid_country_of_origin return data @pytest.fixture def setup_request_specs(valid_post_header, valid_vendor_id, valid_subaccount_id, valid_project_code, valid_artist_id, valid_placeholder_upc, ows_request_mocker): """Mock out all the server calls.""" def _setup(post_data): request_specs = [ { 'method': 'get', 'service': service_name.OWS_FEATURES, 'path': '/features/user/{}'.format( valid_post_header[header.ORCHARD_USER_ID]), 'status': 200, 'json': {} }, { 'method': 'get', 'service': service_name.OWS_PROJECT, 'path': '/project/{}'.format( post_data[field.PROJECT_ID] ), 'status': 200, 'json': { 'vendor_id': valid_vendor_id, 'subaccount_id': valid_subaccount_id, 'project_code': valid_project_code, 'artist_id': valid_artist_id } }, { 'method': 'get', 'service': service_name.OWS_MARKETING, 'path_regex': ( r'/highlights/release/[0-9]+\?mkt_program_id={}\Z'.format( marketing.PROGRAM_MARKETING_HIGHLIGHTS)), 'status': 200, 'json': { 'items': [{'description': post_data[ field.PRODUCT_HIGHLIGHTS]}] # noqa } }, { 'method': 'head', 'service': service_name.OWS_PROJECT, 'path': '/ownership/{}/{}/project/{}'.format( valid_post_header[header.GRASS_ACCOUNT_TYPE], valid_post_header[header.GRASS_ACCOUNT_ID], post_data[field.PROJECT_ID] ), 'status': 200 }, { 'method': 'post', 'service': service_name.OWS_MARKETING, 'path': '/highlights', 'status': 200, 'json': {} # This seems to be ignored. }, { 'method': 'head', 'service': service_name.OWS_PRODUCT, 'path': '/subaccount/{}/display_upc/{}/available?context_type=physical'.format(# noqa valid_subaccount_id, post_data[field.DISPLAY_UPC]), 'status': 200 }, { 'method': 'post', 'service': service_name.OWS_PRODUCT, 'path': '/upc/placeholder', 'status': 200, 'json': {'placeholder_upc': valid_placeholder_upc} } ] for spec in request_specs: ows_request_mocker.add(spec) ows_request_mocker.apply() return _setup def test_post_product_happy_path_display_upc_on( client, db_fixture, valid_post_header, post_data_with_display_upc, valid_artist_id, setup_request_specs, valid_subaccount_id, valid_placeholder_upc, valid_display_upc): """Test creating a product.""" setup_request_specs(post_data_with_display_upc) expected_response_data = deepcopy( post_data_with_display_upc) expected_response_data.update({ field.ARTIST_ID: valid_artist_id, field.MANUFACTURER_UPC: None, field.MANUFACTURING_OBLIGATION: 'N', field.RELEASE_STATUS: 'label_processing', field.SUBACCOUNT_ID: valid_subaccount_id, field.UPC: valid_placeholder_upc, field.PRODUCTION_NOTES: None }) client_response = client.post( '/product', headers=valid_post_header, data=json.dumps(post_data_with_display_upc)) assert client_response.status_code == 200 actual_response_data = json.loads(client_response.data.decode('utf-8')) product_id = actual_response_data.pop(field.PRODUCT_ID) assert product_id is not None expected_response_data['wholesale_price'] = None assert actual_response_data == expected_response_data def test_post_product_no_upc_with_display_upc_on( client, db_fixture, valid_post_header, post_data_with_display_upc, valid_artist_id, setup_request_specs, valid_subaccount_id, valid_placeholder_upc): """Test creating a product with no upc and flag on.""" setup_request_specs(post_data_with_display_upc) post_data_with_display_upc.pop(field.DISPLAY_UPC) expected_response_data = deepcopy( post_data_with_display_upc) expected_response_data.update({ field.ARTIST_ID: valid_artist_id, field.DISPLAY_UPC: None, field.UPC: valid_placeholder_upc, field.MANUFACTURER_UPC: None, field.MANUFACTURING_OBLIGATION: 'N', field.RELEASE_STATUS: 'label_processing', field.SUBACCOUNT_ID: valid_subaccount_id, field.PRODUCTION_NOTES: None, field.JAPAN_DISTRIBUTION: 'yes_other', field.EDITION: 'normal_edition' }) client_response = client.post( '/product', headers=valid_post_header, data=json.dumps(post_data_with_display_upc)) assert client_response.status_code == 200 actual_response_data = json.loads(client_response.data.decode('utf-8')) product_id = actual_response_data.pop(field.PRODUCT_ID) assert product_id is not None expected_response_data['wholesale_price'] = None assert actual_response_data == expected_response_data @pytest.mark.parametrize( ('expected_display_upc', 'expected_upc', 'assign_display_upc'), [ ('565685854679', 20000000000042, True), (None, 20000000000042, False) ]) def test_post_product_with_upc_assignment_happy_path( client, db_fixture, valid_post_header, post_data_with_display_upc, valid_artist_id, setup_request_specs, valid_subaccount_id, expected_display_upc, expected_upc, assign_display_upc, mocker, ): """Test creating a product with upc assignment.""" setup_request_specs(post_data_with_display_upc) post_data_with_display_upc.pop(field.DISPLAY_UPC) mocker.patch.object( persister, 'get_provisioned_upc', return_value=response.Response(message=expected_display_upc) ) post_data_with_display_upc['assign_display_upc'] = assign_display_upc expected_response_data = deepcopy(post_data_with_display_upc) expected_response_data.update({ field.ARTIST_ID: valid_artist_id, field.MANUFACTURER_UPC: None, field.MANUFACTURING_OBLIGATION: None, field.RELEASE_STATUS: 'label_processing', field.SUBACCOUNT_ID: valid_subaccount_id, field.PRODUCTION_NOTES: None }) client_response = client.post( '/product', headers=valid_post_header, data=json.dumps(post_data_with_display_upc)) assert client_response.status_code == 200 actual_response_data = json.loads(client_response.data.decode('utf-8')) display_upc = actual_response_data.pop(field.DISPLAY_UPC) upc = actual_response_data.pop(field.UPC) assert display_upc == expected_display_upc assert upc == expected_upc