예제 #1
0
def test_push_record_with_orcid_update_uses_lock(mock_get_api, mock_distributed_lock, mock_config):
    push_record_with_orcid(
        recid='4328',
        orcid='0000-0002-1825-0097',
        oauth_token='fake-token',
        put_code='920107',
        old_hash=None,
    )

    mock_distributed_lock.assert_called_with('orcid:0000-0002-1825-0097', blocking=True)
예제 #2
0
    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
예제 #3
0
    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
예제 #4
0
def attempt_push(orcid, rec_id, oauth_token):
    """Push a record to ORCID.

    Args:
        orcid(string): an orcid identifier.
        rec_id(int): inspire record's id to push to ORCID.
        oauth_token(string): orcid token.
    """
    LOGGER.info('Will attempt to push #%s onto %s', rec_id, orcid)

    put_code, previous_hash = get_putcode_and_hash_from_redis(orcid, rec_id)

    if not put_code:
        LOGGER.info('Put-code %s not found in cache - will recache now')
        recache_all_author_putcodes(orcid, oauth_token)
        put_code, previous_hash = get_putcode_and_hash_from_redis(
            orcid, rec_id)

    new_code, new_hash = push_record_with_orcid(
        recid=str(rec_id),
        orcid=orcid,
        oauth_token=oauth_token,
        put_code=put_code,
        old_hash=previous_hash,
    )

    if new_code != put_code or new_hash != previous_hash:
        store_record_in_redis(orcid, rec_id, new_code, new_hash)
예제 #5
0
    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
예제 #6
0
    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()
예제 #7
0
def test_push_record_with_orcid_dont_push_if_no_change(mock_config):
    expected_put_code = '920107'
    expected_hash = 'sha1:2995c60336bce71134ebdc12fc50b1ccaf0fd7cd'

    result_put_code, result_hash = push_record_with_orcid(
        recid='4328',
        orcid='0000-0002-1825-0097',
        oauth_token='fake-token',
        put_code='920107',
        old_hash='sha1:2995c60336bce71134ebdc12fc50b1ccaf0fd7cd',
    )

    assert expected_put_code == result_put_code
    assert expected_hash == result_hash
예제 #8
0
    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
예제 #9
0
def test_push_record_with_orcid_update(mock_config, vcr_cassette):
    expected_put_code = '920107'
    expected_hash = 'sha1:2995c60336bce71134ebdc12fc50b1ccaf0fd7cd'

    result_put_code, result_hash = push_record_with_orcid(
        recid='4328',
        orcid='0000-0002-1825-0097',
        oauth_token='fake-token',
        put_code='920107',
        old_hash=None,
    )

    assert expected_put_code == result_put_code
    assert expected_hash == result_hash
    assert vcr_cassette.all_played
예제 #10
0
def orcid_push(self, orcid, rec_id, oauth_token):
    """Celery task to push a record to ORCID.

    Args:
        self(celery.Task): the task
        orcid(string): an orcid identifier.
        rec_id(int): inspire record's id to push to ORCID.
        oauth_token(string): orcid token.
    """
    if not re.match(current_app.config.get(
            'FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX', '^$'), orcid):
        return None

    try:
        LOGGER.info('Will attempt to push #%s onto %s', rec_id, orcid)
        push_record_with_orcid(
            recid=str(rec_id),
            orcid=orcid,
            oauth_token=oauth_token,
        )
    except Exception as exc:
        LOGGER.info('Orcid push attempt (celery task) for orcid={} and rec_id={}'
                    ' failed:\n{}'.format(orcid, rec_id, traceback.format_exc()))
        raise self.retry(max_retries=3, countdown=300, exc=exc)
예제 #11
0
def test_push_record_with_orcid_new(api_client, mock_orcid_api, mock_config):
    expected_put_code = '123456'
    result_put_code = push_record_with_orcid(
        recid='4328',
        orcid='0000-0002-1825-0097',
        oauth_token='fake-token',
        put_code=None,
    )

    assert expected_put_code == result_put_code

    orcid_requests_made = [
        str(r) for r in mock_orcid_api.request_history if 'orcid' in r.url
    ]
    orcid_requests_expected = [
        'POST https://api.sandbox.orcid.org/v2.0/0000-0002-1825-0097/work'
    ]
    assert orcid_requests_made == orcid_requests_expected
예제 #12
0
def attempt_push(orcid, rec_id, oauth_token):
    """Push a record to ORCID.

    Args:
        orcid(string): an orcid identifier.
        rec_id(int): inspire record's id to push to ORCID.
        oauth_token(string): orcid token.
    """
    put_code = get_putcode_from_redis(orcid, rec_id)

    if not put_code:
        record_put_codes = get_author_putcodes(orcid, oauth_token)

        for rec_id, put_code in record_put_codes:
            store_record_in_redis(orcid, rec_id, put_code)

        put_code = get_putcode_from_redis(orcid, rec_id)

    new_code = push_record_with_orcid(str(rec_id), orcid, oauth_token, put_code)

    if not put_code:
        store_record_in_redis(orcid, rec_id, new_code)