"""Functional tests for /correction/{release_correction_id}/details.""" import json from unittest.mock import MagicMock from flexmock import flexmock from owsrequest import request from product_digital.constants import header as header_constants from product_digital.constants import services from tests.factories import release as release_factory from tests.testutils import db @db.test_schema def test_post_correction_details_success( client, correction_details_data, get_correction_response, post_correction_details_ok_response, release_correction_id, valid_headers, product_id): """Test status code upon successfully creating correction details.""" existing_release = release_factory.ReleaseFactory.build( release_id=product_id) db.seed_models(existing_release) (flexmock(request).should_receive('get').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}'.format(release_correction_id)) .and_return(get_correction_response)) (flexmock(request).should_receive('post').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}/details'.format(release_correction_id), json=correction_details_data) .and_return(post_correction_details_ok_response)) post_response = client.post( '/product/{}/correction/{}/details'.format( product_id, release_correction_id), headers=valid_headers, data=json.dumps(correction_details_data)) post_response_body = json.loads(post_response.data.decode()) assert post_response.status_code == 201 assert post_response_body == post_correction_details_ok_response.json() @db.test_schema def test_post_correction_details_with_updated_by( client, get_correction_response, post_correction_details_ok_response, release_correction_id, valid_headers, correction_details_data, correction_details_data_with_ownership, product_id): """Test that alw Orchard-User-Id populates last_updated as vendor.""" existing_release = release_factory.ReleaseFactory.build( release_id=product_id) db.seed_models(existing_release) valid_headers[header_constants.ORCHARD_USER_ID] = 'alw:123' (flexmock(request).should_receive('get').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}'.format(release_correction_id)) .and_return(get_correction_response)) (flexmock(request).should_receive('post').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}/details'.format(release_correction_id), json=correction_details_data_with_ownership) .once().and_return(post_correction_details_ok_response)) post_response = client.post( '/product/{}/correction/{}/details'.format( product_id, release_correction_id), headers=valid_headers, data=json.dumps(correction_details_data)) assert post_response.status_code == 201 def test_post_correction_details_not_owner( account_id, client, product_id, valid_headers_for_vendor, release_correction_id, correction_details_data): """Test that a request is rejected if label does not own the product.""" not_owner_response = MagicMock(status_code=403) (flexmock(request).should_receive('head').with_args( services.OWS_PRODUCT, '/vendor/{}/product/{}'.format(account_id, product_id)).and_return( not_owner_response)) post_response = client.post( '/product/{}/correction/{}/details'.format( product_id, release_correction_id), headers=valid_headers_for_vendor, data=json.dumps(correction_details_data)) assert post_response.status_code == 403 def test_post_correction_details_invalid_orchard_user( client, correction_details_data, product_id, valid_headers, release_correction_id): """Test that a request is rejected if Orchard-User-Id header invalid.""" headers_with_bad_orchard_user_id = valid_headers headers_with_bad_orchard_user_id[ header_constants.ORCHARD_USER_ID] = 'Some Schmoe' post_response = client.post( '/product/{}/correction/{}/details'.format( product_id, release_correction_id), headers=valid_headers, data=json.dumps(correction_details_data)) post_response_body = json.loads(post_response.data.decode()) assert post_response.status_code == 400 assert header_constants.ORCHARD_USER_ID in post_response_body['message'] @db.test_schema def test_post_correction_details_validation_failure( client, correction_details_data, get_correction_response, release_correction_id, valid_headers): """Test status code upon successfully creating correction details.""" existing_release = release_factory.ReleaseFactory.build( release_id=987) db.seed_models(existing_release) (flexmock(request).should_receive('get').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}'.format(release_correction_id)) .and_return(get_correction_response)) post_response = client.post( '/product/{}/correction/{}/details'.format( 987, release_correction_id), headers=valid_headers, data=json.dumps(correction_details_data)) post_response_body = json.loads(post_response.data.decode()) assert post_response.status_code == 400 assert post_response_body == { 'code': 'validation_error', 'message': 'The release_correction does not reference ' 'the correct product'} @db.test_schema def test_post_correction_details_for_genre_change( client, get_correction_response, post_correction_details_ok_response, release_correction_id, valid_headers, correction_details_data_with_genre_fields, correction_details_data_with_ownership, product_id, change_genre_request_data, orchard_user_id): """Test that alw Orchard-User-Id populates last_updated as vendor.""" existing_release = release_factory.ReleaseFactory.build( release_id=product_id) db.seed_models(existing_release) valid_headers[header_constants.ORCHARD_USER_ID] = orchard_user_id (flexmock(request).should_receive('get').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}'.format(release_correction_id)) .and_return(get_correction_response)) change_genre_request_data.update({'correction_id': 123}) ows_track_request_headers = { 'Orchard-User-Id': 'alw:123', 'Orchard-Profile-Type': 'LabelProfile', 'Orchard-Profile-Id': '123' } (flexmock(request).should_receive('post').with_args( services.OWS_TRACK, '/product/{}/tracks/change_genre'.format(product_id), json=change_genre_request_data, headers=ows_track_request_headers) .once().and_return(MagicMock(status_code=200))) (flexmock(request).should_receive('post').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}/details'.format(release_correction_id), json=correction_details_data_with_genre_fields) .once().and_return(post_correction_details_ok_response)) post_response = client.post( '/product/{}/correction/{}/details'.format(product_id, release_correction_id), headers={header_constants.ORCHARD_USER_ID: orchard_user_id, 'Content-Type': 'application/json'}, data=json.dumps(correction_details_data_with_genre_fields) ) assert post_response.status_code == 201