diff --git a/inspirehep/modules/workflows/tasks/submission.py b/inspirehep/modules/workflows/tasks/submission.py index b5576ae5d0..32e05b0c1e 100644 --- a/inspirehep/modules/workflows/tasks/submission.py +++ b/inspirehep/modules/workflows/tasks/submission.py @@ -198,8 +198,9 @@ def send_robotupload( @wraps(send_robotupload) def _send_robotupload(obj, eng): is_update = obj.extra_data.get('is-update') + is_article = obj.workflow.name == 'article' - if is_update and not current_app.config.get('FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY', False): + if is_article and is_update and not current_app.config.get('FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY', False): obj.log.info( 'skipping upload to legacy, feature flag ``FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY`` is disabled.') return diff --git a/inspirehep/modules/workflows/tasks/upload.py b/inspirehep/modules/workflows/tasks/upload.py index a9ee619f61..8ccd29569e 100644 --- a/inspirehep/modules/workflows/tasks/upload.py +++ b/inspirehep/modules/workflows/tasks/upload.py @@ -52,9 +52,10 @@ def _get_updated_record(obj): return updated_record is_update = obj.extra_data.get('is-update') + is_article = obj.workflow.name == 'article' if is_update: - if not current_app.config.get('FEATURE_FLAG_ENABLE_MERGER', False): + if is_article and not current_app.config.get('FEATURE_FLAG_ENABLE_MERGER', False): obj.log.info( 'skipping update record, feature flag ``FEATURE_FLAG_ENABLE_MERGER`` is disabled.' ) diff --git a/tests/integration/workflows/test_workflows_tasks_upload.py b/tests/integration/workflows/test_workflows_tasks_upload.py index 68b7eb2dd6..b378837c3c 100644 --- a/tests/integration/workflows/test_workflows_tasks_upload.py +++ b/tests/integration/workflows/test_workflows_tasks_upload.py @@ -26,6 +26,8 @@ from flask import current_app from invenio_workflows import workflow_object_class +from invenio_workflows.models import Workflow + # FIXME: otherwise this task is not found by Celery. from inspirehep.modules.orcid.tasks import orcid_push # noqa: F401 @@ -40,6 +42,7 @@ def test_store_record_does_not_raise_in_the_orcid_receiver(mock_attempt_push, ap } with patch.dict(current_app.config, config): + workflow_obj = Workflow(name='article') obj = workflow_object_class.create({ '$schema': 'http://localhost:5000/schemas/records/hep.json', '_collections': [ @@ -62,6 +65,6 @@ def test_store_record_does_not_raise_in_the_orcid_receiver(mock_attempt_push, ap 'titles': [ {'title': 'title'}, ], - }) + }, workflow=workflow_obj) store_record(obj, None) # Does not raise. diff --git a/tests/unit/workflows/test_workflows_tasks_submission.py b/tests/unit/workflows/test_workflows_tasks_submission.py index d8352f9b5e..d8b522e7f6 100644 --- a/tests/unit/workflows/test_workflows_tasks_submission.py +++ b/tests/unit/workflows/test_workflows_tasks_submission.py @@ -38,7 +38,7 @@ wait_webcoll, ) -from mocks import MockEng, MockObj, MockUser +from mocks import MockEng, MockObj, MockUser, MockWorkflow @patch('inspirehep.modules.workflows.tasks.submission.User') @@ -747,3 +747,297 @@ def test_prepare_keywords_does_nothing_if_no_keywords_were_predicted(): assert validate(result['keywords'], subschema) is None assert expected == result['keywords'] + + +def test_send_robotupload_new_authors_when_feature_flag_is_disabled(): + with requests_mock.Mocker() as requests_mocker: + requests_mocker.register_uri( + 'POST', 'http://inspirehep.net/batchuploader/robotupload/insert', + text='[INFO] foo bar baz' + ) + + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': False + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/authors.json', + 'name': { + 'preferred_name': 'Jessica Jones', + 'value': 'Jones, Jessica' + } + } + extra_data = {} + + obj = MockObj(data, extra_data) + obj.workflow = MockWorkflow('author') + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + assert _send_robotupload(obj, eng) is None + + expected = ( + 'Robotupload sent!' + '[INFO] foo bar baz' + 'end of upload' + ) + result = obj.log._info.getvalue() + + assert expected == result + + +def test_send_robotupload_update_authors_when_feature_flag_is_disabled(): + with requests_mock.Mocker() as requests_mocker: + requests_mocker.register_uri( + 'POST', 'http://inspirehep.net/batchuploader/robotupload/insert', + text='[INFO] foo bar baz' + ) + + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': False + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/authors.json', + 'name': { + 'preferred_name': 'Jessica Jones', + 'value': 'Jones, Jessica' + } + } + extra_data = { + 'is-update': True + } + + obj = MockObj(data, extra_data) + obj.workflow = MockWorkflow('author') + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + assert _send_robotupload(obj, eng) is None + + expected = ( + 'Robotupload sent!' + '[INFO] foo bar baz' + 'end of upload' + ) + result = obj.log._info.getvalue() + + assert expected == result + + +def test_send_robotupload_new_authors_when_feature_flag_is_enabled(): + with requests_mock.Mocker() as requests_mocker: + requests_mocker.register_uri( + 'POST', 'http://inspirehep.net/batchuploader/robotupload/insert', + text='[INFO] foo bar baz' + ) + + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': True + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/authors.json', + 'name': { + 'preferred_name': 'Jessica Jones', + 'value': 'Jones, Jessica' + } + } + extra_data = { + 'is-update': True + } + + obj = MockObj(data, extra_data) + obj.workflow = MockWorkflow('author') + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + assert _send_robotupload(obj, eng) is None + + expected = ( + 'Robotupload sent!' + '[INFO] foo bar baz' + 'end of upload' + ) + result = obj.log._info.getvalue() + + assert expected == result + + +def test_send_robotupload_update_authors_when_feature_flag_is_enabled(): + with requests_mock.Mocker() as requests_mocker: + requests_mocker.register_uri( + 'POST', 'http://inspirehep.net/batchuploader/robotupload/insert', + text='[INFO] foo bar baz' + ) + + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': True + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/authors.json', + 'name': { + 'preferred_name': 'Jessica Jones', + 'value': 'Jones, Jessica' + } + } + extra_data = { + 'is-update': True + } + + obj = MockObj(data, extra_data) + obj.workflow = MockWorkflow('author') + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + assert _send_robotupload(obj, eng) is None + + expected = ( + 'Robotupload sent!' + '[INFO] foo bar baz' + 'end of upload' + ) + result = obj.log._info.getvalue() + + assert expected == result + + +def test_send_robotupload_new_article_when_feature_flag_is_disabled(): + with requests_mock.Mocker() as requests_mocker: + requests_mocker.register_uri( + 'POST', 'http://inspirehep.net/batchuploader/robotupload/insert', + text='[INFO] foo bar baz' + ) + + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': False + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/hep.json', + } + + extra_data = {} + + obj = MockObj(data, extra_data) + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + assert _send_robotupload(obj, eng) is None + + expected = ( + 'Robotupload sent!' + '[INFO] foo bar baz' + 'end of upload' + ) + result = obj.log._info.getvalue() + + assert expected == result + + +def test_send_robotupload_update_article_when_feature_flag_is_disabled(): + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': False + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/hep.json', + } + + extra_data = { + 'is-update': True + } + + obj = MockObj(data, extra_data) + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + expected_log = 'skipping upload to legacy, feature flag ``FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY`` is disabled.' + + assert _send_robotupload(obj, eng) is None + assert expected_log in obj.log._info.getvalue() + + +def test_send_robotupload_update_article_when_feature_flag_is_enabled(): + with requests_mock.Mocker() as requests_mocker: + requests_mocker.register_uri( + 'POST', 'http://inspirehep.net/batchuploader/robotupload/insert', + text='[INFO] foo bar baz' + ) + + config = { + 'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net', + 'PRODUCTION_MODE': True, + 'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': True + } + + with patch.dict(current_app.config, config), \ + patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'): + data = { + '$schema': 'http://localhost:5000/schemas/records/hep.json', + } + + extra_data = { + 'is-update': True + } + + obj = MockObj(data, extra_data) + eng = MockEng() + + _send_robotupload = send_robotupload( + mode='insert', + ) + + assert _send_robotupload(obj, eng) is None + + expected = ( + 'Robotupload sent!' + '[INFO] foo bar baz' + 'end of upload' + ) + result = obj.log._info.getvalue() + + assert expected == result