"""Functional tests for updating an product project code via a PUT call.""" import json from owsrequest import request from owsrequest import test_utils import pytest from product_digital.constants import account from product_digital.constants import header as header_constants from product_digital.constants import services from product_digital.models import ows_product_workflow from tests.factories import release as release_factory from tests.factories import release_subgenre as release_subgenre_factory from tests.testutils import db NEW_PROJECT_ID = 4242 NEW_SUBACCOUNT_ID = 2424 NEW_ARTIST_ID = 1212 def test_put_project_id_invalid_format( client, product_id, valid_headers_for_vendor): """Test that a put request fails if project id in invalid format.""" put_body = {'project_id': 'ABC123'} put_response = client.put( '/product/audio/{}'.format(product_id), headers=valid_headers_for_vendor, data=json.dumps(put_body)) assert put_response.status_code == 400 def _mock_services(mocker, account_id, product_id): head_specs = [{ 'service': services.OWS_PROJECT_MANAGER, 'path': '/ownership/{}/{}/project/{}'.format( account.VENDOR_TYPE, account_id, NEW_PROJECT_ID), 'status': 200, 'json': {} }] get_specs = [ { 'service': services.OWS_PROJECT_MANAGER, 'path': '/project/{}'.format(NEW_PROJECT_ID), 'status': 200, 'json': { 'vendor_id': 9999, 'subaccount_id': NEW_SUBACCOUNT_ID, 'artist_id': NEW_ARTIST_ID }, }, { 'service': services.OWS_ACCOUNT, 'path': '/vendor/brand/9999', 'status': 200, 'json': 'brand', }, { 'service': services.OWS_ACCOUNT, 'path': '/vendor/9999', 'status': 200, 'json': {'owner': 'odd'}, }, { 'service': services.OWS_PRODUCT_WORKFLOW, 'path': '/product/{}/release-approval'.format(product_id), 'status': 200, 'json': None }, { 'service': services.OWS_PRODUCT_WORKFLOW, 'path': '/product/{}/correction'.format(product_id), 'status': 404, 'json': None } ] mocker.patch.object( request, 'head', test_utils.mock_ows_requests(head_specs)) mocker.patch.object( request, 'get', test_utils.mock_ows_requests(get_specs)) @db.test_schema @pytest.mark.parametrize( 'login_source,product_status', [ ('alw', 'label_processing'), ('oa', 'in_content'), ] ) def test_put_project_id_edit_enabled( client, product_id, valid_headers, mocker, account_id, login_source, product_status, ): """Test that a put request can change project code.""" _mock_services(mocker, account_id, product_id) existing_release = release_factory.ReleaseFactory.build( release_status=product_status, release_id=product_id) existing_subgenre = release_subgenre_factory.ReleaseSubgenreFactory.build( release_id=product_id) db.seed_models(existing_release) db.seed_models(existing_subgenre) put_body = { 'project_id': NEW_PROJECT_ID, 'account_type': account.VENDOR_TYPE, 'account_id': account_id} valid_headers[header_constants.ORCHARD_USER_ID] = login_source + ':21159' put_response = client.put( '/product/audio/{}'.format(product_id), headers=valid_headers, data=json.dumps(put_body)) assert put_response.status_code == 200 response_body = json.loads(put_response.data.decode()) assert response_body['project_id'] == NEW_PROJECT_ID @db.test_schema def test_put_project_id_rejected_when_no_rights( account_id, client, orchard_user_id, mocker, product_id, valid_headers ): """Test that a put request reject when no rights on new project.""" head_specs = [{ 'service': services.OWS_PROJECT_MANAGER, 'path': '/ownership/{}/{}/project/{}'.format( account.VENDOR_TYPE, account_id, NEW_PROJECT_ID), 'status': 403, 'json': {} }] mocker.patch.object( request, 'head', test_utils.mock_ows_requests(head_specs)) existing_release = release_factory.ReleaseFactory.build( release_id=product_id) existing_subgenre = release_subgenre_factory.ReleaseSubgenreFactory.build( release_id=product_id) db.seed_models(existing_release) db.seed_models(existing_subgenre) put_body = { 'project_id': NEW_PROJECT_ID, 'account_type': account.VENDOR_TYPE, 'account_id': account_id} valid_headers[header_constants.ORCHARD_USER_ID] = orchard_user_id put_response = client.put( '/product/audio/{}'.format(product_id), headers=valid_headers, data=json.dumps(put_body)) assert put_response.status_code == 403 @db.test_schema @pytest.mark.parametrize( 'login_source,product_status,checked_out', [ ('alw', 'label_processing', False), ('alw', 'transfer_to_content', True), ('alw', 'in_content', True), ('alw', 'in_content', False), ('oa', 'in_content', False), ] ) def test_put_project_id_when_move_product_all_status( client, product_id, valid_headers, mocker, account_id, login_source, product_status, checked_out): """Test that a put request can change project id.""" _mock_services(mocker, account_id, product_id) mocker.patch.object( ows_product_workflow, 'is_product_checked_out_by_another_user', return_value=checked_out) existing_release = release_factory.ReleaseFactory.build( release_status=product_status, release_id=product_id) existing_subgenre = release_subgenre_factory.ReleaseSubgenreFactory.build( release_id=product_id) db.seed_models(existing_release) db.seed_models(existing_subgenre) put_body = { 'project_id': NEW_PROJECT_ID, 'account_type': account.VENDOR_TYPE, 'account_id': account_id} valid_headers[header_constants.ORCHARD_USER_ID] = login_source + ':21159' put_response = client.put( '/product/audio/{}'.format(product_id), headers=valid_headers, data=json.dumps(put_body)) assert put_response.status_code == 200 response_body = json.loads(put_response.data.decode()) assert response_body['project_id'] == NEW_PROJECT_ID