"""Functional tests for the POST /product/{product_id}/correction endpoint.""" import json from unittest import mock from flexmock import flexmock from owsrequest import request import pytest 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 @pytest.fixture def product_id(): """Return a fake product id for testing.""" return 12345 @pytest.fixture def release_correction_id(): """Return a fake release correction id for testing.""" return 67890 @pytest.fixture def delete_correction_ok_response(): """Return an OK response from workflow to stub deleting a correction.""" delete_response = mock.MagicMock(status_code=202) delete_response.json = mock.MagicMock(return_value={ 'message': 'release correction successfully deleted' }) return delete_response @pytest.fixture def delete_correction_asset_ok_response(): """Return an OK response from assets to stub deleting a correction.""" delete_response = mock.MagicMock(status_code=200) delete_response.json = mock.MagicMock(return_value={ 'message': 'ok' }) return delete_response @pytest.fixture def delete_correction_asset_not_found_response(): """Return NOT FOUND response from assets to stub deleting a correction.""" delete_response = mock.MagicMock(status_code=404) delete_response.json = mock.MagicMock(return_value={ 'message': 'not found' }) return delete_response @pytest.fixture def ows_product_document_response(): """Mock ows-product product response.""" mock_product_response = mock.MagicMock() mock_product_response.status_code = 200 mock_product_response.json.return_value = {'vendor_id': 1} return mock_product_response @db.test_schema def test_delete_product_correction_success( client, get_correction_response, delete_correction_ok_response, product_id, release_correction_id, delete_correction_asset_not_found_response, valid_headers, ows_product_document_response): """Test status code upon successfully create a correction.""" 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, '/product/{}'.format(product_id), ).and_return(ows_product_document_response)) (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('delete').with_args( services.OWS_ASSETS, '/v2/product/{}/corrections'.format(product_id), ).and_return(delete_correction_asset_not_found_response)) (flexmock(request).should_receive('delete').with_args( services.OWS_ASSETS, '/product/{}/corrections'.format(product_id), ).and_return(delete_correction_asset_not_found_response)) (flexmock(request).should_receive('delete').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}'.format(release_correction_id), ).and_return(delete_correction_ok_response)) delete_response = client.delete( '/product/{}/correction/{}'.format(product_id, release_correction_id), headers=valid_headers) delete_response_body = json.loads(delete_response.data.decode()) assert delete_response.status_code == 202 assert delete_response_body == delete_correction_ok_response.json() @db.test_schema def test_delete_product_correction_success_no_asset( client, get_correction_response, delete_correction_ok_response, product_id, release_correction_id, delete_correction_asset_ok_response, valid_headers, ows_product_document_response): """Test status code upon successfully create a correction.""" 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, '/product/{}'.format(product_id), ).and_return(ows_product_document_response)) (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('delete').with_args( services.OWS_ASSETS, '/v2/product/{}/corrections'.format(product_id), ).and_return(delete_correction_asset_ok_response)) (flexmock(request).should_receive('delete').with_args( services.OWS_ASSETS, '/product/{}/corrections'.format(product_id), ).and_return(delete_correction_asset_ok_response)) (flexmock(request).should_receive('delete').with_args( services.OWS_PRODUCT_WORKFLOW, '/correction/{}'.format(release_correction_id), ).and_return(delete_correction_ok_response)) delete_response = client.delete( '/product/{}/correction/{}'.format(product_id, release_correction_id), headers=valid_headers) delete_response_body = json.loads(delete_response.data.decode()) assert delete_response.status_code == 202 assert delete_response_body == delete_correction_ok_response.json() def test_delete_product_correction_not_owner( account_id, client, product_id, release_correction_id, valid_headers_for_vendor): """Test that a request is rejected if label does not own the product.""" not_owner_response = mock.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.delete( '/product/{}/correction/{}'.format(product_id, release_correction_id), headers=valid_headers_for_vendor) assert post_response.status_code == 403 def test_delete_product_correction_invalid_orchard_user( client, product_id, release_correction_id, valid_headers): """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] = 'Orchie McUserdude' delete_response = client.delete( '/product/{}/correction/{}'.format(product_id, release_correction_id), headers=headers_with_bad_orchard_user_id) delete_response_body = json.loads(delete_response.data.decode()) assert delete_response.status_code == 400 assert header_constants.ORCHARD_USER_ID in delete_response_body['message'] @db.test_schema def test_delete_correction_details_validation_failure( client, get_correction_response, release_correction_id, valid_headers, ows_product_document_response): """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)) (flexmock(request).should_receive('get').with_args( services.OWS_PRODUCT, '/product/{}'.format(987), ).and_return(ows_product_document_response)) delete_response = client.delete( '/product/{}/correction/{}'.format(987, release_correction_id), headers=valid_headers) delete_response_body = json.loads(delete_response.data.decode()) assert delete_response.status_code == 400 assert delete_response_body == { 'code': 'validation_error', 'message': 'The release_correction does not reference ' 'the correct product'}