def test_whitelist_regex_any(self): regex = '.*' with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=regex): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_called_once_with(self.orcid, self.recid, self.oauth_token)
def test_whitelist_regex_none(self): regex = '^$' with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=regex): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_not_called()
def test_get_all_inspire_putcodes_happy_flow(self): with override_config(ORCID_APP_CREDENTIALS={'consumer_key': self.source_client_id_path}): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) putcodes_recids = list(putcode_getter.get_all_inspire_putcodes_and_recids_iter()) assert len(putcodes_recids) == 297 for _, recid in putcodes_recids: assert int(recid)
def test_happy_flow(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_called_once_with(self.orcid, self.recid, self.oauth_token) self.mock_pusher.return_value.push.assert_called_once()
def test_push_new_work_already_existing(self): with override_config( FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_new_work_already_existing(self): # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config(ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == 47160445 assert not self.cache.has_work_content_changed(self.inspire_record)
def test_main_feature_flag(self): regex = '.*' with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=False, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=regex): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_not_called()
def test_whitelist_regex_some(self): regex = '^(0000-0002-7638-5686|0000-0002-7638-5687)$' with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=regex): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_called_once_with(self.orcid, self.recid, self.oauth_token)
def test_push_new_work_already_existing_putcode_exception(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config(ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(exceptions.PutcodeNotFoundInOrcidException): pusher.push() assert self.cache.has_work_content_changed(self.inspire_record)
def test_get_author_putcodes(isolated_app, mock_logger, vcr_cassette): with override_config(**CONFIG): pairs = get_author_putcodes('0000-0002-1825-0097', 'fake-token') assert pairs == [('4328', '912978')] assert '912977' in mock_logger.message assert vcr_cassette.all_played
def test_new_record(self): recid = 9999912587 record_json = { '$schema': 'http://localhost:5000/schemas/records/hep.json', 'document_type': [ 'article', ], 'control_number': recid, 'titles': [ { 'title': 'Jessica Jones', }, ], '_collections': ['Literature'], 'references': [{'record': { '$ref': 'http://localhost:5000/api/literature/1498589'}}] } inspire_record = InspireRecord.create(record_json) with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ mock.patch('inspirehep.modules.records.receivers.push_access_tokens') as mock_push_access_tokens, \ mock.patch('inspirehep.modules.orcid.tasks.orcid_push.apply_async') as mock_apply_async: mock_push_access_tokens.get_access_tokens.return_value = [('myorcid', 'mytoken')] inspire_record.commit() mock_apply_async.assert_called_once_with( kwargs={'orcid': 'myorcid', 'oauth_token': 'mytoken', 'kwargs_to_pusher': {'record_db_version': inspire_record.model.version_id}, 'rec_id': recid}, queue='orcid_push') _delete_record('lit', recid)
def test_delete_work_cache_miss(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config( ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): assert not pusher.push()
def test_push_new_work_already_existent_putcode_exception(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config(ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(exceptions.PutcodeNotFoundInOrcidException): pusher.push() assert self.cache.has_work_content_changed(self.inspire_record)
def test_happy_flow(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == 51551656 assert not self.cache.has_work_content_changed(self.inspire_record)
def test_putcode_not_found(self): self.orcid = '0000-0002-0942-3697' with override_config(ORCID_APP_CREDENTIALS={ 'consumer_key': self.source_client_id_path }): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): list(putcode_getter.get_all_inspire_putcodes_and_recids_iter())
def test_stale_record_db_version(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ pytest.raises(domain_exceptions.StaleRecordDBVersionException): orcid_push(self.orcid, self.recid, self.oauth_token, kwargs_to_pusher=dict(record_db_version=100))
def test_get_putcodes_and_recids_by_identifiers_iter(self, ): self.orcid = '0000-0002-0942-3697' id_doi1 = ExternalIdentifier('doi', '10.1000/test.orcid.push') id_doi2 = ExternalIdentifier('doi', '10.1000/orcid-test-andrea-rossoni') with override_config(ORCID_APP_CREDENTIALS={'consumer_key': self.source_client_id_path}): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) putcodes_recids = list(putcode_getter.get_putcodes_and_recids_by_identifiers_iter([id_doi1, id_doi2])) assert putcodes_recids == [(51548299, '999'), (51344802, '1680808')]
def test_exc_in_apply_celery_task_with_retry(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ mock.patch('inspirehep.modules.orcid.utils.apply_celery_task_with_retry') as apply_celery_task_with_retry_mock, \ pytest.raises(TimeLimitExceeded): apply_celery_task_with_retry_mock.side_effect = TimeLimitExceeded pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push()
def test_get_all_inspire_putcodes_with_recids(self): self.orcid = '0000-0002-5073-0816' with override_config(ORCID_APP_CREDENTIALS={ 'consumer_key': self.source_client_id_path }): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) putcodes_recids = list( putcode_getter.get_all_inspire_putcodes_and_recids_iter()) assert putcodes_recids == [('51341099', '20'), ('51341192', '20')]
def test_push_new_work_already_existent(self): # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config( ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == '47160445' assert not self.cache.has_work_content_changed(self.inspire_record)
def test_get_all_inspire_putcodes_happy_flow(self): with override_config(ORCID_APP_CREDENTIALS={ 'consumer_key': self.source_client_id_path }): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) putcodes_recids = list( putcode_getter.get_all_inspire_putcodes_and_recids_iter()) assert len(putcodes_recids) == 297 for _, recid in putcodes_recids: assert int(recid)
def test_delete_work_invalid_token(self): access_token = 'tokeninvalid' TestRemoteToken.create_for_orcid(self.orcid, access_token=access_token) pusher = domain_models.OrcidPusher(self.orcid, self.recid, access_token) # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config(ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(exceptions.TokenInvalidDeletedException): pusher.push() assert not push_access_tokens.get_access_tokens([self.orcid]) assert push_access_tokens.is_access_token_invalid(access_token)
def test_happy_flow(self): with override_config( FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == 51551656 assert not self.cache.has_work_content_changed(self.inspire_record)
def test_duplicated_external_identifier_pusher_exception(self): del self.clashing_record.json['deleted'] self.clashing_record.json['titles'] = [{'source': 'submitter', 'title': 'title1'}] with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(domain_exceptions.DuplicatedExternalIdentifierPusherException): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push()
def test_new_record_already_existent_error_and_putcode_not_found(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch.object(OrcidCache, 'read_work_putcode', return_value=None): with pytest.raises(PutcodeNotFoundInCacheException): push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert vcr_cassette.all_played
def test_distributed_lock_with_new_record(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.distributed_lock', wraps=distributed_lock) as mock_distributed_lock: push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) mock_distributed_lock.assert_called_with('orcid:0000-0002-1825-0097', blocking=True) assert vcr_cassette.all_played
def test_new_record_already_existent_error(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_called_with(self.orcid, self.oauth_token) assert vcr_cassette.all_played
def test_override_config(app): from flask import current_app old_config = copy.copy(current_app.config) with override_config(myusernametestoverrideconfig='john', mypasswordtestoverrideconfig='secret'): assert current_app.config['myusernametestoverrideconfig'] == 'john' assert current_app.config['mypasswordtestoverrideconfig'] == 'secret' assert 'myusernametestoverrideconfig' not in current_app.config assert 'mypasswordtestoverrideconfig' not in current_app.config assert current_app.config == old_config
def test_retry_not_triggered(self): self.mock_pusher.return_value.push.side_effect = IOError with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ mock.patch('inspirehep.modules.orcid.tasks.orcid_push.retry') as mock_orcid_push_task_retry, \ pytest.raises(IOError): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_called_once_with(self.orcid, self.recid, self.oauth_token) self.mock_pusher.return_value.push.assert_called_once() mock_orcid_push_task_retry.assert_not_called()
def test_get_putcodes_and_recids_by_identifiers_iter(self, ): self.orcid = '0000-0002-0942-3697' id_doi1 = ExternalIdentifier('doi', '10.1000/test.orcid.push') id_doi2 = ExternalIdentifier('doi', '10.1000/orcid-test-andrea-rossoni') with override_config(ORCID_APP_CREDENTIALS={ 'consumer_key': self.source_client_id_path }): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) putcodes_recids = list( putcode_getter.get_putcodes_and_recids_by_identifiers_iter( [id_doi1, id_doi2])) assert putcodes_recids == [(51548299, '999'), (51344802, '1680808')]
def test_override_config(app): from flask import current_app old_config = copy.copy(current_app.config) with override_config( myusernametestoverrideconfig='john', mypasswordtestoverrideconfig='secret'): assert current_app.config['myusernametestoverrideconfig'] == 'john' assert current_app.config['mypasswordtestoverrideconfig'] == 'secret' assert 'myusernametestoverrideconfig' not in current_app.config assert 'mypasswordtestoverrideconfig' not in current_app.config assert current_app.config == old_config
def test_duplicated_external_identifier_pusher_exception(self): del self.clashing_record.json['deleted'] self.clashing_record.json['titles'] = [{ 'source': 'submitter', 'title': 'title1' }] with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(domain_exceptions.DuplicatedExternalIdentifierPusherException): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push()
def test_updated_record_cache_hit_same_hash(self): # Fill the cache with the right putcode and same hash. self.cache.write_work_putcode(self.putcode, self.inspire_record) with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_not_called()
def test_push_to_orcid_update_no_cache( vcr_cassette, redis_setup, ): rec_id = 4328 orcid = '0000-0002-2169-2152' token = 'fake-token' with override_config(**CONFIG): # Push update orcid_push(orcid, rec_id, token) # Check that the record was added: assert vcr_cassette.all_played
def test_retry_triggered(self): exc = RequestException() exc.response = mock.Mock() exc.request = mock.Mock() self.mock_pusher.return_value.push.side_effect = exc with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ mock.patch('inspirehep.modules.orcid.tasks.orcid_push.retry', side_effect=RequestException) as mock_orcid_push_task_retry, \ pytest.raises(RequestException): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_called_once_with(self.orcid, self.recid, self.oauth_token) self.mock_pusher.return_value.push.assert_called_once() mock_orcid_push_task_retry.assert_called_once()
def test_updated_record_cache_hit(self, vcr_cassette): # Fill the cache with the right putcode but no hash. self.cache.write_work_putcode(self.putcode) with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_not_called() assert vcr_cassette.all_played
def test_existing_record(self): recid = 736770 inspire_record = get_db_record('lit', recid) # from demosite data. with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ mock.patch('inspirehep.modules.records.receivers.push_access_tokens') as mock_push_access_tokens, \ mock.patch('inspirehep.modules.orcid.tasks.orcid_push.apply_async') as mock_apply_async: mock_push_access_tokens.get_access_tokens.return_value = [('myorcid', 'mytoken')] inspire_record.commit() mock_apply_async.assert_called_once_with( kwargs={'orcid': 'myorcid', 'oauth_token': 'mytoken', 'kwargs_to_pusher': {'record_db_version': inspire_record.model.version_id}, 'rec_id': recid}, queue='orcid_push')
def test_push_to_orcid_update_with_cache( vcr_cassette, redis_setup, ): rec_id = 4328 orcid = '0000-0002-2169-2152' token = 'fake-token' with override_config(**CONFIG): # Push as new orcid_push(orcid, rec_id, token) # Push the updated record orcid_push(orcid, rec_id, token) # Check that the update happened: assert vcr_cassette.all_played
def test_push_to_orcid_with_putcode_but_without_hash( vcr_cassette, redis_setup, ): rec_id = 4328 orcid = '0000-0002-2169-2152' token = 'fake-token' with override_config(**CONFIG): # Fetch the putcodes, no hashes present yet recache_author_putcodes(orcid, token) # Push the record orcid_push(orcid, rec_id, token) # Check that the update request didn't happen: assert vcr_cassette.all_played
def test_recache_author_putcodes(isolated_app, vcr_cassette): orcid = '0000-0002-1825-0097' oauth_token = 'fake-token' recid1 = '4328' putcode1 = '912978' recid2 = '4327' putcode2 = '912977' # putcode3 = '912979' # Non inspire. with override_config(**CONFIG): recache_author_putcodes(orcid, oauth_token) # Ensure the putcodes have been cached. cache = OrcidCache(orcid, recid1) assert cache.read_work_putcode() == putcode1 cache = OrcidCache(orcid, recid2) assert cache.read_work_putcode() == putcode2 assert vcr_cassette.all_played
def test_push_new_work_happy_flow(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record)
def test_delete_work_cache_miss(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config(ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): assert not pusher.push()
def test_push_new_work_already_existing(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_new_work_invalid_data_orcid(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ pytest.raises(exceptions.InputDataInvalidException): orcid_push('0000-0002-0000-XXXX', self.recid, self.oauth_token)
def test_putcode_not_found(self): self.orcid = '0000-0002-0942-3697' with override_config(ORCID_APP_CREDENTIALS={'consumer_key': self.source_client_id_path}): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): list(putcode_getter.get_all_inspire_putcodes_and_recids_iter())
def test_get_all_inspire_putcodes_with_recids(self): self.orcid = '0000-0002-5073-0816' with override_config(ORCID_APP_CREDENTIALS={'consumer_key': self.source_client_id_path}): putcode_getter = OrcidPutcodeGetter(self.orcid, self.oauth_token) putcodes_recids = list(putcode_getter.get_all_inspire_putcodes_and_recids_iter()) assert putcodes_recids == [('51341099', '20'), ('51341192', '20')]