From c2df828f94f3a2f2a6c56178d42ee8a6bc876f43 Mon Sep 17 00:00:00 2001 From: anikachurilova <nyuta.churilova@gmail.com> Date: Fri, 25 Aug 2023 17:09:45 +0200 Subject: [PATCH 1/2] oai-pmh: read oai sets prefix from app config * closes https://github.com/zenodo/rdm-project/issues/202 --- .../oaiserver/services/services.py | 17 ++++++++-------- tests/conftest.py | 20 +------------------ 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/invenio_rdm_records/oaiserver/services/services.py b/invenio_rdm_records/oaiserver/services/services.py index 3991e39a9..7646cc5ed 100644 --- a/invenio_rdm_records/oaiserver/services/services.py +++ b/invenio_rdm_records/oaiserver/services/services.py @@ -34,10 +34,10 @@ class OAIPMHServerService(Service): """OAI-PMH service.""" - def __init__(self, config, extra_reserved_prefixes=None): + def __init__(self, config, extra_reserved_prefixes={}): """Init service with config.""" super().__init__(config) - self.extra_reserved_prefixes = extra_reserved_prefixes or {} + self.extra_reserved_prefixes = extra_reserved_prefixes @property def schema(self): @@ -51,11 +51,10 @@ def links_item_tpl(self): self.config.links_item, ) - @property - def reserved_prefixes(self): + def _reserved_prefixes(self): """Get OAI-PMH set prefix from config.""" - _reserved_prefixes = set([current_app.config["COMMUNITIES_OAI_SETS_PREFIX"]]) - return _reserved_prefixes.union(self.extra_reserved_prefixes) + reserved_prefixes = set([current_app.config["COMMUNITIES_OAI_SETS_PREFIX"]]) + return reserved_prefixes.union(self.extra_reserved_prefixes) def _get_one(self, raise_error=True, **kwargs): """Retrieve set based on provided arguments.""" @@ -73,11 +72,11 @@ def _get_one(self, raise_error=True, **kwargs): def _validate_spec(self, spec): """Checks the validity of the provided spec.""" # Reserved for community integration - if spec.startswith(tuple(self.reserved_prefixes)): + if spec.startswith(tuple(self._reserved_prefixes())): raise ValidationError( _( "The spec must not start with any of the following list '{prefix}'.".format( - prefix=list(self.reserved_prefixes) + prefix=list(self._reserved_prefixes()) ) ), field_name="spec", @@ -105,7 +104,7 @@ def create(self, identity, data, uow=None): raise_errors=True, ) self._validate_spec(valid_data["spec"]) - system_created = valid_data["spec"].startswith(tuple(self.reserved_prefixes)) + system_created = valid_data["spec"].startswith(tuple(self._reserved_prefixes())) new_set = OAISet(**valid_data, system_created=system_created) existing_set, errors = self._get_one(spec=new_set.spec, raise_error=False) diff --git a/tests/conftest.py b/tests/conftest.py index 04ef5f078..bccc1474a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,6 @@ See https://pytest-invenio.readthedocs.io/ for documentation on which test fixtures are available. """ -from invenio_rdm_records.services.permissions import RDMRequestsPermissionPolicy # Monkey patch Werkzeug 2.1 # Flask-Login uses the safe_str_cmp method which has been removed in Werkzeug @@ -159,7 +158,6 @@ def app_config(app_config, mock_datacite_client): "PIDSTORE_RECID_FIELD", "RECORDS_PERMISSIONS_RECORD_POLICY", "RECORDS_REST_ENDPOINTS", - "REQUESTS_PERMISSION_POLICY", ] for config_key in supported_configurations: @@ -300,11 +298,9 @@ def app_config(app_config, mock_datacite_client): app_config["RDM_RESOURCE_ACCESS_TOKENS_ENABLED"] = True - # Disable the automatic creation of moderation requests after publishing a record. + # Users are verified by default. This will disable the automatic creation of moderation requests after publishing a record. # When testing unverified users, there is a "unverified_user" fixture for that purpose. app_config["ACCOUNTS_DEFAULT_USERS_VERIFIED"] = True - app_config["RDM_USER_MODERATION_ENABLED"] = False - app_config["REQUESTS_PERMISSION_POLICY"] = RDMRequestsPermissionPolicy app_config["COMMUNITIES_OAI_SETS_PREFIX"] = "community-" return app_config @@ -1039,16 +1035,6 @@ def identity_simple(users): return i -@pytest.fixture() -def anonymous_identity(users): - """Simple identity fixture.""" - user = users[1] - i = Identity(user.id) - i.provides.add(UserNeed(user.id)) - i.provides.add(Need(method="system_role", value="any_user")) - return i - - @pytest.fixture(scope="module") def languages_type(app): """Lanuage vocabulary type.""" @@ -1636,8 +1622,6 @@ def verified_user(UserFixture, app, db): ) u.create(app, db) u.user.verified_at = datetime.utcnow() - # Dumping `is_verified` requires authenticated user in tests - u.identity.provides.add(Need(method="system_role", value="authenticated_user")) return u @@ -1650,8 +1634,6 @@ def unverified_user(UserFixture, app, db): ) u.create(app, db) u.user.verified_at = None - # Dumping `is_verified` requires authenticated user in tests - u.identity.provides.add(Need(method="system_role", value="authenticated_user")) return u From 332ba47ce3abf5d04ad3b0de704b9873834733e9 Mon Sep 17 00:00:00 2001 From: anikachurilova <nyuta.churilova@gmail.com> Date: Fri, 25 Aug 2023 17:09:45 +0200 Subject: [PATCH 2/2] oai-pmh: read oai sets prefix from app config * closes https://github.com/zenodo/rdm-project/issues/202 --- .../oaiserver/services/services.py | 17 ++++++++-------- tests/conftest.py | 20 +------------------ 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/invenio_rdm_records/oaiserver/services/services.py b/invenio_rdm_records/oaiserver/services/services.py index 3991e39a9..7646cc5ed 100644 --- a/invenio_rdm_records/oaiserver/services/services.py +++ b/invenio_rdm_records/oaiserver/services/services.py @@ -34,10 +34,10 @@ class OAIPMHServerService(Service): """OAI-PMH service.""" - def __init__(self, config, extra_reserved_prefixes=None): + def __init__(self, config, extra_reserved_prefixes={}): """Init service with config.""" super().__init__(config) - self.extra_reserved_prefixes = extra_reserved_prefixes or {} + self.extra_reserved_prefixes = extra_reserved_prefixes @property def schema(self): @@ -51,11 +51,10 @@ def links_item_tpl(self): self.config.links_item, ) - @property - def reserved_prefixes(self): + def _reserved_prefixes(self): """Get OAI-PMH set prefix from config.""" - _reserved_prefixes = set([current_app.config["COMMUNITIES_OAI_SETS_PREFIX"]]) - return _reserved_prefixes.union(self.extra_reserved_prefixes) + reserved_prefixes = set([current_app.config["COMMUNITIES_OAI_SETS_PREFIX"]]) + return reserved_prefixes.union(self.extra_reserved_prefixes) def _get_one(self, raise_error=True, **kwargs): """Retrieve set based on provided arguments.""" @@ -73,11 +72,11 @@ def _get_one(self, raise_error=True, **kwargs): def _validate_spec(self, spec): """Checks the validity of the provided spec.""" # Reserved for community integration - if spec.startswith(tuple(self.reserved_prefixes)): + if spec.startswith(tuple(self._reserved_prefixes())): raise ValidationError( _( "The spec must not start with any of the following list '{prefix}'.".format( - prefix=list(self.reserved_prefixes) + prefix=list(self._reserved_prefixes()) ) ), field_name="spec", @@ -105,7 +104,7 @@ def create(self, identity, data, uow=None): raise_errors=True, ) self._validate_spec(valid_data["spec"]) - system_created = valid_data["spec"].startswith(tuple(self.reserved_prefixes)) + system_created = valid_data["spec"].startswith(tuple(self._reserved_prefixes())) new_set = OAISet(**valid_data, system_created=system_created) existing_set, errors = self._get_one(spec=new_set.spec, raise_error=False) diff --git a/tests/conftest.py b/tests/conftest.py index 77efb99e1..101814e52 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,6 @@ See https://pytest-invenio.readthedocs.io/ for documentation on which test fixtures are available. """ -from invenio_rdm_records.services.permissions import RDMRequestsPermissionPolicy # Monkey patch Werkzeug 2.1 # Flask-Login uses the safe_str_cmp method which has been removed in Werkzeug @@ -159,7 +158,6 @@ def app_config(app_config, mock_datacite_client): "PIDSTORE_RECID_FIELD", "RECORDS_PERMISSIONS_RECORD_POLICY", "RECORDS_REST_ENDPOINTS", - "REQUESTS_PERMISSION_POLICY", ] for config_key in supported_configurations: @@ -300,11 +298,9 @@ def app_config(app_config, mock_datacite_client): app_config["RDM_RESOURCE_ACCESS_TOKENS_ENABLED"] = True - # Disable the automatic creation of moderation requests after publishing a record. + # Users are verified by default. This will disable the automatic creation of moderation requests after publishing a record. # When testing unverified users, there is a "unverified_user" fixture for that purpose. app_config["ACCOUNTS_DEFAULT_USERS_VERIFIED"] = True - app_config["RDM_USER_MODERATION_ENABLED"] = False - app_config["REQUESTS_PERMISSION_POLICY"] = RDMRequestsPermissionPolicy app_config["COMMUNITIES_OAI_SETS_PREFIX"] = "community-" return app_config @@ -1039,16 +1035,6 @@ def identity_simple(users): return i -@pytest.fixture() -def anonymous_identity(users): - """Simple identity fixture.""" - user = users[1] - i = Identity(user.id) - i.provides.add(UserNeed(user.id)) - i.provides.add(Need(method="system_role", value="any_user")) - return i - - @pytest.fixture(scope="module") def languages_type(app): """Lanuage vocabulary type.""" @@ -1637,8 +1623,6 @@ def verified_user(UserFixture, app, db): ) u.create(app, db) u.user.verified_at = datetime.utcnow() - # Dumping `is_verified` requires authenticated user in tests - u.identity.provides.add(Need(method="system_role", value="authenticated_user")) return u @@ -1651,8 +1635,6 @@ def unverified_user(UserFixture, app, db): ) u.create(app, db) u.user.verified_at = None - # Dumping `is_verified` requires authenticated user in tests - u.identity.provides.add(Need(method="system_role", value="authenticated_user")) return u