"""Functional tests for updating an product project code via a PUT call.""" import json from owsrequest import request from owsrequest import test_utils new_project_code = 'new_project_code' update_request_project_code = { 'project_code': new_project_code, 'artist_id': 123} def test_put_project_code( db_fixture, project_highlight_ok, monkeypatch, project, client, header_user, get_artist_info_ok, project_id, get_company_brand_ows_response, get_vendor_ows_response): """Assert valid response from PUT /project when updating project code. It checks to see updated json is returned when updating a project code for a project that has products. """ get_specs = [ project_highlight_ok, get_artist_info_ok, get_company_brand_ows_response, get_vendor_ows_response, { 'service': 'ows-account', 'path': '/subaccount/1', 'status': 200, 'json': {'vendor_id': 7123} } ] put_specs = [ { 'service': 'ows-product-digital', 'path': '/product/audio/{}'.format(1001), 'status': 200, 'json': {} }, { 'service': 'ows-product-physical', 'path': '/product/{}'.format(1002), 'status': 200, 'json': {} }, { 'service': 'ows-product-digital', 'path': '/product/audio/{}'.format(1003), 'status': 200, 'json': {} }, { 'service': 'ows-product-digital', 'path': '/product/audio/{}'.format(1014), 'status': 200, 'json': {} }, ] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) monkeypatch.setattr( request, 'put', test_utils.mock_ows_requests(put_specs)) res = client.put( '/project/{}'.format(project_id), data=json.dumps(update_request_project_code), headers=header_user) assert res.status_code == 200 response_body = json.loads(res.data.decode()) assert response_body['project_code'] == new_project_code def test_put_project_code_failed( db_fixture, project_highlight_ok, monkeypatch, project, client, header_user, get_artist_info_ok, project_id, get_company_brand_ows_response, get_vendor_ows_response): """Assert valid response when code not changed during error.""" get_specs = [ project_highlight_ok, get_artist_info_ok, get_company_brand_ows_response, get_vendor_ows_response, { 'service': 'ows-account', 'path': '/subaccount/1', 'status': 200, 'json': {'vendor_id': 7123} } ] put_specs = [ { 'service': 'ows-product-digital', 'path': '/product/audio/{}'.format(1001), 'status': 200, 'json': {} }, { 'service': 'ows-product-physical', 'path': '/product/{}'.format(1002), 'status': 418, 'json': {'message': 'mad tea party'} }, { 'service': 'ows-product-digital', 'path': '/product/audio/{}'.format(1003), 'status': 200, 'json': {} }, { 'service': 'ows-product-digital', 'path': '/product/audio/{}'.format(1014), 'status': 200, 'json': {} }, ] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) monkeypatch.setattr( request, 'put', test_utils.mock_ows_requests(put_specs)) res = client.put( '/project/{}'.format(project_id), data=json.dumps(update_request_project_code), headers=header_user) assert res.status_code == 418 response_body = json.loads(res.data.decode()) assert response_body['message'] == 'mad tea party' def test_put_project_code_exists( db_fixture, project_highlight_ok, monkeypatch, projects, client, header_user, get_artist_info_ok, project_id, orchard_user_id, get_company_brand_ows_response, get_vendor_ows_response): """Assert valid response from PUT /project when updating project code. It checks to see updated json is returned when updating a project code for a project that has products. """ get_specs = [ project_highlight_ok, get_company_brand_ows_response, get_vendor_ows_response, get_artist_info_ok, { 'service': 'ows-account', 'path': '/subaccount/1', 'status': 200, 'json': {'vendor_id': 7123} } ] put_specs = [ { 'service': 'ows-product-digital', 'path': r'/product/audio/\w+', 'status': 200, 'json': {} }, { 'service': 'ows-product-physical', 'path': r'/product/\w+', 'status': 200, 'json': {} }, ] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) monkeypatch.setattr( request, 'put', test_utils.mock_ows_requests(put_specs)) res = client.put( '/project/{}'.format(project_id), data=json.dumps({ 'project_code': 'DEF', 'artist_id': 123}), headers=header_user) assert res.status_code == 400 response_body = json.loads(res.data.decode()) assert response_body['code'] == 'validation_error' assert 'project_code' in response_body['message']