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 == 47160445
    def test_push_updated_work_invalid_data_putcode(self):
        self.cache.write_work_putcode('00000')

        pusher = domain_models.OrcidPusher(self.orcid, self.recid,
                                           self.oauth_token)
        result_putcode = pusher.push()
        assert result_putcode == 47160445
    def test_push_updated_work_invalid_data_putcode(self):
        self.cache.write_work_putcode('00000')

        pusher = domain_models.OrcidPusher(self.orcid, self.recid,
                                           self.oauth_token)
        with pytest.raises(exceptions.InputDataInvalidException):
            pusher.push()
    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_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_push_new_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_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_push_updated_work_invalid_data_orcid(self):
        orcid = '0000-0002-0000-XXXX'
        self.cache = OrcidCache(orcid, self.recid)
        self.putcode = '46985330'
        self.cache.write_work_putcode(self.putcode)

        pusher = domain_models.OrcidPusher(orcid, self.recid, self.oauth_token)
        with pytest.raises(exceptions.InputDataInvalidException):
            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.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_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_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_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_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_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_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_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_record_not_found(self):
     with pytest.raises(exceptions.RecordNotFoundException):
         domain_models.OrcidPusher(self.orcid, 'notfound25697xxx',
                                   self.oauth_token)
 def test_record_non_existing(self):
     self.recid = 'doesnotexists'
     with pytest.raises(domain_exceptions.RecordNotFoundException):
         domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token)
 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)
     with pytest.raises(exceptions.InputDataInvalidException):
         pusher.push()
 def test_push_new_work_invalid_data_token(self):
     pusher = domain_models.OrcidPusher(self.orcid, self.recid,
                                        'tokeninvalid')
     with pytest.raises(exceptions.InputDataInvalidException):
         pusher.push()
    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()
 def test_push_new_work_happy_flow(self):
     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_stale_record_db_version(self):
     with pytest.raises(domain_exceptions.StaleRecordDBVersionException):
         domain_models.OrcidPusher(self.orcid,
                                   self.recid,
                                   self.oauth_token,
                                   record_db_version=10)