Skip to content

Commit

Permalink
global: fixes for tests, formatting
Browse files Browse the repository at this point in the history
Signed-off-by: pamfilos <pamfilosf@gmail.com>
  • Loading branch information
pamfilos committed Dec 18, 2023
1 parent 7d04794 commit bad22ad
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
3 changes: 1 addition & 2 deletions cap/modules/deposit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.local import LocalProxy

from cap.modules.deposit.egroups import CERNEgroupMixin
from cap.modules.deposit.errors import (
DepositValidationError,
DisconnectWebhookError,
Expand Down Expand Up @@ -99,8 +100,6 @@
get_existing_or_register_user,
)

from cap.modules.deposit.egroups import CERNEgroupMixin

_datastore = LocalProxy(lambda: current_app.extensions["security"].datastore)

PRESERVE_FIELDS = (
Expand Down
2 changes: 1 addition & 1 deletion cap/modules/deposit/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def links_factory(pid, record=None, record_hit=None, **kwargs):
)

for action in extract_actions_from_class(CAPDeposit):
if action != "review":
if action not in ["review", "egroups"]:
links[action] = api_url_for('depid_actions', pid, action=action)

return links
9 changes: 6 additions & 3 deletions cap/modules/deposit/serializers/schemas/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def remove_skip_values(self, data):
keys = ["can_review", "review", "x_cap_permission", "egroups"]

for key in keys:
if data.get(key, "") is None:
if not data.get(key, ""):
del data[key]

return data
Expand All @@ -116,7 +116,7 @@ def remove_skip_values(self, data):
can_admin = fields.Method("can_user_admin", dump_only=True)
can_review = fields.Method("can_user_review", dump_only=True)
review = fields.Method("get_review", dump_only=True)
links = fields.Method("get_links_with_review", dump_only=True)
links = fields.Method("get_links", dump_only=True)
x_cap_permission = fields.Dict(attribute="x-cap-permission", dump_only=True)

def get_webhooks(self, obj):
Expand Down Expand Up @@ -177,12 +177,15 @@ def get_review(self, obj):
else:
return None

def get_links_with_review(self, obj):
def get_links(self, obj):
links = obj["links"]

if obj["deposit"].schema_is_reviewable():
links["review"] = links["publish"].replace("publish", "review")

if obj["deposit"].schema_egroups_enabled():
links["egroups"] = links["publish"].replace("publish", "egroups")

return links

def can_user_update(self, obj):
Expand Down
4 changes: 2 additions & 2 deletions cap/modules/records/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
search_responsify,
)

from cap.modules.records.serializers.author_xml import AuthorXMLSerializer
from cap.modules.records.serializers.csv import CSVSerializer
from cap.modules.records.serializers.json import (
CAPJSONSerializer,
RecordSerializer,
)
from cap.modules.records.serializers.schemas.common import (
CommonRecordMetadataSchema,
)
from cap.modules.records.serializers.author_xml import AuthorXMLSerializer
from cap.modules.records.serializers.csv import CSVSerializer
from cap.modules.records.serializers.schemas.json import (
BasicDepositSchema,
PermissionsDepositSchema,
Expand Down
18 changes: 17 additions & 1 deletion cap/modules/records/serializers/schemas/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
from flask_login import current_user
from invenio_files_rest.models import ObjectVersion
from invenio_files_rest.serializer import ObjectVersionSchema
from marshmallow import Schema, ValidationError, fields, validates_schema
from marshmallow import (
Schema,
ValidationError,
fields,
post_dump,
validates_schema,
)

from cap.modules.records.utils import url_to_api_url
from cap.modules.schemas.resolvers import resolve_schema_by_url
Expand Down Expand Up @@ -173,6 +179,16 @@ def get_metadata(self, obj):
class CommonRecordSchema(CommonRecordMetadataSchema, StrictKeysMixin):
"""Base record schema."""

@post_dump
def remove_skip_values(self, data):
keys = ["egroups"]

for key in keys:
if not data.get(key, ''):
del data[key]

return data

id = fields.Str(attribute='pid.pid_value', dump_only=True)

schema = fields.Method('get_schema', dump_only=True)
Expand Down
4 changes: 3 additions & 1 deletion cap/modules/records/serializers/schemas/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def remove_skip_values(self, data):
keys = ["can_review", "review", "egroups"]

for key in keys:
if data.get(key, '') is None:
if not data.get(key, ''):
del data[key]

return data
Expand Down Expand Up @@ -161,6 +161,8 @@ def get_links_with_review(self, obj):
action="review",
)

links.pop('egroups', None)

return links


Expand Down
12 changes: 10 additions & 2 deletions cap/modules/schemas/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,21 @@ def permissions(
# create all combinations of actions and roles
try:
actions_roles = list(itertools.product(requested_actions, roles))
schema.process_action_roles(schema_action, actions_roles)
roles_logs = schema.process_action_roles(schema_action, actions_roles)
# create all combinations of actions and users
actions_users = list(itertools.product(requested_actions, users))
schema.process_action_users(schema_action, actions_users)
users_logs = schema.process_action_users(schema_action, actions_users)
except IntegrityError:
return click.secho("Action user/role already exists.", fg="red")
click.secho("Process finished.", fg="green")
errors = [log for log in roles_logs if log.get('status') == 'error']
errors += [log for log in users_logs if log.get('status') == 'error']

for e in errors:
click.secho(
f"User/Role \"{e.get('role')}\" - \"{e.get('message')}\"",
fg="yellow",
)


@fixtures.command()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cli/test_schema_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ def test_action_allow_read_schema_fails_if_already_in_db(app, db, users, create_
res = cli_runner('fixtures permissions -p read -r test-users@cern.ch --schema --allow test-schema')

assert res.exit_code == 0
assert 'Action user/role already exists.' in res.output
assert 'Already exists' in res.output
2 changes: 1 addition & 1 deletion tests/integration/test_get_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def test_get_record_with_form_json_serializer(
},
'can_update': True,
'is_owner': True,
'can_review': False,
# 'can_review': False,
'created': rec.created.strftime('%Y-%m-%dT%H:%M:%S.%f+00:00'),
'updated': rec.updated.strftime('%Y-%m-%dT%H:%M:%S.%f+00:00'),
'created_by': {'email': example_user.email, 'profile': {}},
Expand Down

0 comments on commit bad22ad

Please sign in to comment.