def test_push_unhandled_duplicated_external_identifier_pusher_exception( 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) pusher.push() self.factory_clashing.record_metadata.json["titles"] = [{ "source": "submitter", "title": "title1" }] pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push() with pytest.raises( exceptions.DuplicatedExternalIdentifierPusherException): pusher = domain_models.OrcidPusher(self.orcid, self.clashing_recid, self.oauth_token) pusher.push()
def test_happy_flow_put(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"}, ): dois = self.factory.record_metadata.json.pop("dois") pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) self.putcode = pusher.push() self.factory.record_metadata.json["dois"] = dois self.factory_clashing.record_metadata.json["deleted"] = True with pytest.raises( exceptions.DuplicatedExternalIdentifierPusherException): tasks.orcid_push.apply( queue="orcid_push_legacy_tokens", kwargs={ "orcid": self.orcid, "rec_id": self.recid, "oauth_token": self.oauth_token, }, throw=True, ) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == self.putcode assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_updated_work_happy_flow(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == int(self.putcode) 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_happy_flow(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token, record_db_version=1) result_putcode = pusher.push() assert result_putcode
def test_push_cache_hit_content_not_changed(self): putcode = "00000" self.cache.write_work_putcode(putcode, self.inspire_record) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == putcode
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) pusher.push() assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_updated_work_invalid_data_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) with 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_push_updated_work_invalid_data_putcode(self): self.cache.write_work_putcode("00000") self.orcid = self.ORCID_2 pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() # don't care to check the value, it's pain to keep it with # repeatability of the tests assert result_putcode
def test_push_updated_work_invalid_data_orcid(self): self.orcid = "0000-0002-0000-XXXX" access_token = "tokeninvalid" TestRemoteToken.create_for_orcid(self.orcid, access_token=access_token) pusher = domain_models.OrcidPusher(self.orcid, self.recid, access_token) with pytest.raises(exceptions.TokenInvalidDeletedException): pusher.push()
def test_delete_work_cache_putcode_nonexisting(self): self.recid = "000000" TestRecordMetadata.create_from_kwargs(json={ "control_number": self.recid, "deleted": True }) self.cache.write_work_putcode("51391229") pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) assert not pusher.push()
def test_push_updated_work_no_cache(self): self.cache.delete_work_putcode() # 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() self.cache.write_work_putcode(result_putcode) assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_updated_work_happy_flow(self): self.putcode = self.add_work( "test_orcid_domain_models_TestOrcidPusher.xml") self.cache.write_work_putcode(self.putcode) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert int(result_putcode) == self.putcode assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_new_work_already_existing_and_delete_duplicated_records_different_putcodes( self, ): with override_config( ORCID_APP_CREDENTIALS={"consumer_key": "0000-0001-8607-8906"}): self.add_work( "test_orcid_domain_models_TestOrcidPusherPostNewWork_conflicting_no_recid.xml" ) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push() assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_new_work_already_existing_putcode_exception(self): pusher = domain_models.OrcidPusher(self.orcid, self.conflicting_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() self.recid = self.conflicting_recid assert self.cache.has_work_content_changed( self.conflicting_inspire_record)
def test_push_updated_work_invalid_data_putcode(self): self.cache.write_work_putcode("00000") # 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() # don't care to check the value, it's pain to keep it with # repeatability of the tests assert result_putcode assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_new_work_invalid_data_xml(self): # Note: the recorded cassette returns (magically) a proper error. pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) data = b'<work:work xmlns:common="http://www.orcid.org/ns/common" xmlns:work="http://www.orcid.orgsens/work"></work:work>' invalid_xml = etree.fromstringlist([data]) with pytest.raises(exceptions.InputDataInvalidException): with mock.patch( "inspirehep.orcid.domain_models.OrcidConverter.get_xml", return_value=invalid_xml, ): pusher.push()
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.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_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_put(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"}, ): dois = self.factory.record_metadata.json.pop("dois") pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) self.putcode = pusher.push() self.factory.record_metadata.json["dois"] = dois self.factory_clashing.record_metadata.json["deleted"] = True pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert self.putcode == result_putcode assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_cache_hit_content_changed(self): putcode = "00000" cache_inspire_record = copy.deepcopy(self.inspire_record) cache_inspire_record["titles"][0]["title"] = "foo" self.cache.write_work_putcode(putcode, cache_inspire_record) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) with mock.patch.object(OrcidClient, "put_updated_work") as mock_put_updated_work: mock_put_updated_work.return_value.__getitem__.return_value = "0000" pusher.push() mock_put_updated_work.assert_called_once_with(mock.ANY, putcode)
def test_push_new_work_already_existing_duplicated_external_identifier_exception( self, ): # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. self.recid = self.conflicting_recid with override_config( ORCID_APP_CREDENTIALS={"consumer_key": "0000-0001-8607-8906"} ), pytest.raises( exceptions.DuplicatedExternalIdentifierPusherException): pusher = domain_models.OrcidPusher(self.orcid, self.conflicting_recid, self.oauth_token) pusher.push() assert self.cache.has_work_content_changed( self.conflicting_inspire_record)
def test_delete_work_force_delete(self): self.recid = "99" TestRecordMetadata.create_from_kwargs( json={ "control_number": self.recid, "deleted": False, "_private_notes": [{ "value": "orcid-push-force-delete" }], }) self.cache.write_work_putcode("51391229") pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) assert not pusher.push()
def test_push_force_cache_miss(self): putcode = "00000" self.record_metadata.json["_private_notes"] = [{ "value": "orcid-push-force-cache-miss" }] self.cache.write_work_putcode(putcode, self.inspire_record) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) with mock.patch.object(OrcidClient, "post_new_work") as mock_post_new_work: mock_post_new_work.return_value.__getitem__.return_value = "0000" pusher.push() mock_post_new_work.assert_called_once()
def test_happy_flow_put(self, override_config): with override_config( FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=".*", ORCID_APP_CREDENTIALS={"consumer_key": "0000-0001-8607-8906"}, ): dois = self.factory.record_metadata.json.pop("dois") pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) self.putcode = pusher.push() self.factory.record_metadata.json["dois"] = dois self.factory_clashing.record_metadata.json["deleted"] = True with pytest.raises(Retry): # As orcid_push is retrying on DuplicatedExternalIdentifierPusherException # Before it was throwing original exception because of `request.called_directly` # was True before celery task __call__ was fixed in # celery/app/task.py(719)retry() tasks.orcid_push.apply( queue="orcid_push_legacy_tokens", kwargs={ "orcid": self.orcid, "rec_id": self.recid, "oauth_token": self.oauth_token, }, throw=True, ) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == self.putcode assert not self.cache.has_work_content_changed(self.inspire_record)
def test_stale_record_db_version(self): with pytest.raises(exceptions.StaleRecordDBVersionException): domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token, record_db_version=10)
def test_record_non_existing(self): self.recid = "doesnotexists" with pytest.raises(exceptions.RecordNotFoundException): domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token)
def test_delete_work_cache_hit(self): self.cache.write_work_putcode("51389857") pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) assert not pusher.push()
def test_push_new_work_happy_flow(self): self.delete_all_work() pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert not self.cache.has_work_content_changed(self.inspire_record)
def test_push_new_work_invalid_data_orcid(self): orcid = "0000-0002-0000-XXXX" pusher = domain_models.OrcidPusher(orcid, self.recid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): pusher.push()