Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read-only mode #231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions invenio_vocabularies/services/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020-2021 CERN.
# Copyright (C) 2022 TU Wien.
#
# Invenio-Vocabularies is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
Expand All @@ -9,15 +10,19 @@
"""Vocabulary permissions."""

from invenio_records_permissions import RecordPermissionPolicy
from invenio_records_permissions.generators import AnyUser, SystemProcess
from invenio_records_permissions.generators import (
AnyUser,
DisableIfReadOnly,
SystemProcess,
)


class PermissionPolicy(RecordPermissionPolicy):
"""Permission policy."""

can_search = [SystemProcess(), AnyUser()]
can_read = [SystemProcess(), AnyUser()]
can_create = [SystemProcess()]
can_update = [SystemProcess()]
can_delete = [SystemProcess()]
can_manage = [SystemProcess()]
can_create = [SystemProcess(), DisableIfReadOnly()]
can_update = [SystemProcess(), DisableIfReadOnly()]
can_delete = [SystemProcess(), DisableIfReadOnly()]
can_manage = [SystemProcess(), DisableIfReadOnly()]
43 changes: 43 additions & 0 deletions tests/services/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2020-2021 CERN.
# Copyright (C) 2022 TU Wien.
#
# Invenio-Vocabularies is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
Expand All @@ -13,6 +14,7 @@
import pytest
from invenio_cache import current_cache
from invenio_pidstore.errors import PIDAlreadyExists, PIDDeletedError
from invenio_records_resources.services.errors import PermissionDeniedError
from marshmallow.exceptions import ValidationError
from sqlalchemy.exc import IntegrityError

Expand Down Expand Up @@ -181,3 +183,44 @@ def test_indexed_at_query(app, db, service, identity, lang_type, lang_data):
type="languages",
)
assert res.total == 1


#
# Read-only mode
#


def test_create_ro(app, service, identity, lang_type, lang_data):
"""Try to create a vocabulary in read-only mode."""
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True

pytest.raises(PermissionDeniedError, service.create, identity, lang_data)


def test_update_ro(app, service, identity, lang_type, lang_data):
"""Try to update a vocabulary in read-only mode."""
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = False
item = service.create(identity, lang_data)

app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
pytest.raises(
PermissionDeniedError, service.update, identity, ("languages", "eng"), lang_data
)


def test_delete_ro(app, service, identity, lang_type, lang_data):
"""Try to delete a vocabulary in read-only mode."""
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = False
item = service.create(identity, lang_data)

app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
pytest.raises(PermissionDeniedError, service.delete, identity, ("languages", "eng"))


def test_create_type_ro(app, service, identity):
"""Try to create a vocabulary type in read-only mode."""
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True

pytest.raises(
PermissionDeniedError, service.create_type, identity, "newtype", "new"
)
Loading