Exemple #1
0
 def setup(self):
     factory = TestRecordMetadata.create_from_file(
         __name__, "test_orcid_tasks_orcid_push_TestOrcidPush.json")
     self.orcid = "0000-0003-1134-6827"
     self.recid = factory.record_metadata.json["control_number"]
     self.inspire_record = factory.inspire_record
     self.cache = OrcidCache(self.orcid, self.recid)
     self.oauth_token = get_local_access_tokens(self.orcid) or "mytoken"
Exemple #2
0
 def setup(self):
     self.recid = "1936475"
     self.putcode = "myputcode"
     self.hash_value = "myhash"
     self.orcid = "0000-0002-76YY-56XX"
     self.hash_value = "sha1:acbc7dad4fd46e0deb60d6681c244a67e4be2543"
     factory = TestRecordMetadata.create_from_file(
         __name__, "test_orcid_cache_record.json")
     self.inspire_record = factory.inspire_record
     self.cache = OrcidCache(self.orcid, self.recid)
Exemple #3
0
class TestOrcidPushTask(object):
    # NOTE: Only a few test here (1 happy flow and a few error flows). Exhaustive
    # testing is done in the domain model tests.
    def setup(self):
        factory = TestRecordMetadata.create_from_file(
            __name__, "test_orcid_tasks_orcid_push_TestOrcidPush.json")
        self.orcid = "0000-0003-1134-6827"
        self.recid = factory.record_metadata.json["control_number"]
        self.inspire_record = factory.inspire_record
        self.cache = OrcidCache(self.orcid, self.recid)
        self.oauth_token = get_local_access_tokens(self.orcid) or "mytoken"

    def setup_method(self, method):
        cache_module.CACHE_PREFIX = get_fqn(method)

    def teardown(self):
        self.cache.delete_work_putcode()
        cache_module.CACHE_PREFIX = None

    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_push_new_work_invalid_data_orcid(self):
        with override_config(
                FEATURE_FLAG_ENABLE_ORCID_PUSH=True,
                FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=".*",
        ), pytest.raises(domain_exceptions.InputDataInvalidException):
            orcid_push("0000-0003-0000-XXXX", self.recid, self.oauth_token)

    def test_push_new_work_already_existing(self):
        self.cache.delete_work_putcode()
        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_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 cache(self):
     return OrcidCache(self.orcid, self.recid)
 def cache_clashing(self):
     return OrcidCache(self.orcid, self.clashing_recid)
Exemple #6
0
 def test_delete_work_putcode_non_existing(self):
     recid = "0000"
     cache = OrcidCache(self.orcid, recid)
     cache.delete_work_putcode()
     assert not self.cache.read_work_putcode()
Exemple #7
0
    def test_has_work_content_changed_yes(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)

        self.inspire_record["titles"][0]["title"] = "mytitle"
        cache = OrcidCache(self.orcid, self.recid)
        assert cache.has_work_content_changed(self.inspire_record)
Exemple #8
0
    def test_has_work_content_changed_no(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)

        cache = OrcidCache(self.orcid, self.recid)
        assert not cache.has_work_content_changed(self.inspire_record)
Exemple #9
0
class TestOrcidCache(object):
    def setup(self):
        self.recid = "1936475"
        self.putcode = "myputcode"
        self.hash_value = "myhash"
        self.orcid = "0000-0002-76YY-56XX"
        self.hash_value = "sha1:acbc7dad4fd46e0deb60d6681c244a67e4be2543"
        factory = TestRecordMetadata.create_from_file(
            __name__, "test_orcid_cache_record.json")
        self.inspire_record = factory.inspire_record
        self.cache = OrcidCache(self.orcid, self.recid)

    def setup_method(self, method):
        cache_module.CACHE_PREFIX = get_fqn(method)

    def teardown(self):
        """
        Cleanup the cache after each test (as atm there is no cache isolation).
        """
        self.cache.delete_work_putcode()
        cache_module.CACHE_PREFIX = None

    def test_read_write_new_key(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)
        putcode = self.cache.read_work_putcode()
        assert putcode == self.putcode

    def test_read_write_existent_key(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)
        self.cache.write_work_putcode("0000", self.inspire_record)
        putcode = self.cache.read_work_putcode()
        assert putcode == "0000"

    def test_read_non_existent_key(self):
        putcode = self.cache.read_work_putcode()
        assert not putcode

    def test_has_work_content_changed_no(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)

        cache = OrcidCache(self.orcid, self.recid)
        assert not cache.has_work_content_changed(self.inspire_record)

    def test_has_work_content_changed_yes(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)

        self.inspire_record["titles"][0]["title"] = "mytitle"
        cache = OrcidCache(self.orcid, self.recid)
        assert cache.has_work_content_changed(self.inspire_record)

    def test_write_work_putcode_do_recompute(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)

        self.cache.read_work_putcode()
        assert self.cache._cached_hash_value == self.hash_value

    def test_write_work_putcode_do_not_recompute(self):
        self.cache.write_work_putcode(self.putcode)

        self.cache.read_work_putcode()
        assert not self.cache._cached_hash_value

    def test_delete_work_putcode(self):
        self.cache.write_work_putcode(self.putcode, self.inspire_record)
        putcode = self.cache.read_work_putcode()
        assert putcode == self.putcode

        self.cache.delete_work_putcode()
        assert not self.cache.read_work_putcode()

    def test_delete_work_putcode_non_existing(self):
        recid = "0000"
        cache = OrcidCache(self.orcid, recid)
        cache.delete_work_putcode()
        assert not self.cache.read_work_putcode()
class TestOrcidPushTask(object):
    # NOTE: Only a few test here (1 happy flow and a few error flows). Exhaustive
    # testing is done in the domain model tests.
    def setup(self):
        factory = TestRecordMetadata.create_from_file(
            __name__, "test_orcid_tasks_orcid_push_TestOrcidPush.json")
        self.orcid = "0000-0003-1134-6827"
        self.recid = factory.record_metadata.json["control_number"]
        self.inspire_record = factory.inspire_record
        self.cache = OrcidCache(self.orcid, self.recid)
        self.oauth_token = get_local_access_tokens(self.orcid) or "mytoken"

    def setup_method(self, method):
        cache_module.CACHE_PREFIX = get_fqn(method)

    def teardown(self):
        self.cache.delete_work_putcode()
        cache_module.CACHE_PREFIX = None

    def test_push_new_work_happy_flow(self, override_config):
        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_push_new_work_invalid_data_orcid(self, override_config):
        with override_config(
                FEATURE_FLAG_ENABLE_ORCID_PUSH=True,
                FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=".*",
        ), pytest.raises(domain_exceptions.InputDataInvalidException):
            orcid_push("0000-0003-0000-XXXX", self.recid, self.oauth_token)

    def test_push_new_work_already_existing(self, override_config):
        self.cache.delete_work_putcode()
        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_stale_record_db_version(self, override_config):
        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_push_new_work_moved_permanently_orcid_account_exception(
            self, override_config):
        exc = MovedPermanentlyException()
        exc.args = (
            "{'response-code': 301, 'developer-message': '301 Moved Permanently: This account is deprecated. Please refer to account: https://qa.orcid.org/0000-0003-1134-6827. ORCID https://qa.orcid.org/0000-1111-0000-0000', 'user-message': 'The resource was not found.'",
        )
        self._patcher = mock.patch(
            "inspirehep.orcid.domain_models.OrcidPusher._post_or_put_work")
        self.mock_pusher = self._patcher.start()
        self.mock_pusher.side_effect = exc
        with override_config(
                FEATURE_FLAG_ENABLE_ORCID_PUSH=True,
                FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX=".*",
        ), mock.patch("inspirehep.orcid.domain_models.utils.update_moved_orcid"
                      ) as mock_update_orcid, pytest.raises(
                          MovedPermanentlyException):
            orcid_push(self.orcid, self.recid, self.oauth_token)

        assert mock_update_orcid.mock_calls[0][1] == (
            "0000-0003-1134-6827",
            "0000-1111-0000-0000",
        )
        self._patcher.stop()