Skip to content

Commit

Permalink
collections: allow submitter to search in collections.
Browse files Browse the repository at this point in the history
* Fixes deposit creation with a submitter without a main file.

Co-Authored-by:Johnny Mariéthoz <johnny.mariethoz@rero.ch>
  • Loading branch information
jma committed Nov 4, 2021
1 parent dcd9574 commit 076e0f3
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 108 deletions.
6 changes: 5 additions & 1 deletion sonar/json_schemas/deposits_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ def process(self):
"""
schema = super().process()

if current_user_record.is_moderator:
if not current_user_record or current_user_record.is_moderator:
return schema

schema['properties']['diffusion']['properties'].pop(
'subdivisions', None)
order = schema['properties']['diffusion']['propertiesOrder']
schema['properties']['diffusion']['propertiesOrder'] = [
v for v in order if v != 'subdivisions'
]

return schema
13 changes: 10 additions & 3 deletions sonar/modules/collections/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def list(cls, user, record=None):
:return: True is action can be done
:rtype: bool
"""
return user and user.is_admin
return user and (user.is_admin or user.is_submitter)

@classmethod
def create(cls, user, record=None):
Expand All @@ -47,7 +47,7 @@ def create(cls, user, record=None):
:return: True is action can be done
:rtype: bool
"""
return cls.list(user, record)
return bool(user and user.is_admin)

@classmethod
def read(cls, user, record):
Expand All @@ -61,7 +61,7 @@ def read(cls, user, record):
if user and user.is_superuser:
return True

if not cls.create(user, record):
if not (user and user.is_submitter):
return False

record = Record.get_record_by_pid(record['pid'])
Expand All @@ -78,6 +78,9 @@ def update(cls, user, record):
:return: True is action can be done
:rtype: bool
"""
# not admin
if not (user and user.is_admin):
return False
return cls.read(user, record)

@classmethod
Expand All @@ -89,6 +92,10 @@ def delete(cls, user, record):
:return: True if action can be done
:rtype: bool
"""
# not admin
if not (user and user.is_admin):
return False

results = DocumentSearch().filter(
'term', collections__pid=record['pid']).source(includes=['pid'])

Expand Down
106 changes: 13 additions & 93 deletions sonar/modules/deposits/jsonschemas/deposits/deposit-v1.0.0_src.json
Original file line number Diff line number Diff line change
Expand Up @@ -748,101 +748,21 @@
"title": "Collection",
"type": "object",
"additionnalProperties": false,
"oneOf": [
{
"title": "Existing collection",
"properties": {
"$ref": {
"type": "string",
"pattern": "^https://sonar.ch/api/collections/.*?$",
"form": {
"remoteTypeahead": {
"type": "collections",
"field": "name.value.suggest",
"label": "label"
}
}
}
},
"required": [
"$ref"
]
},
{
"title": "Add a new collection",
"properties": {
"name": {
"title": "Names",
"type": "array",
"minItems": 1,
"items": {
"title": "Name",
"type": "object",
"additionalProperties": false,
"properties": {
"value": {
"title": "Value",
"type": "string",
"minLength": 1
},
"language": {
"$ref": "language-v1.0.0.json"
}
},
"propertiesOrder": [
"language",
"value"
],
"required": [
"value",
"language"
]
}

},
"description": {
"title": "Descriptions",
"type": "array",
"minItems": 0,
"items": {
"title": "Description",
"type": "object",
"additionalProperties": false,
"properties": {
"value": {
"title": "Value",
"type": "string",
"minLength": 1,
"form": {
"type": "textarea",
"templateOptions": {
"rows": 5
}
}
},
"language": {
"$ref": "language-v1.0.0.json"
}
},
"propertiesOrder": [
"language",
"value"
],
"required": [
"value",
"language"
]
}
"properties": {
"$ref": {
"type": "string",
"pattern": "^https://sonar.ch/api/collections/.*?$",
"form": {
"remoteTypeahead": {
"type": "collections",
"field": "name.value.suggest",
"label": "label"
}
},
"propertiesOrder": [
"name",
"description"
],
"required": [
"name"
]
}
}
},
"required": [
"$ref"
]
},
"form": {
Expand Down
9 changes: 5 additions & 4 deletions tests/api/collections/test_collections_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def test_list(app, client, make_organisation, make_collection, superuser,
# Logged as submitter
login_user_via_session(client, email=submitter['email'])
res = client.get(url_for('invenio_records_rest.coll_list'))
assert res.status_code == 403
assert res.status_code == 200

# Logged as moderator
login_user_via_session(client, email=moderator['email'])
res = client.get(url_for('invenio_records_rest.coll_list'))
assert res.status_code == 403
assert res.status_code == 200
assert res.json['hits']['total']['value'] == 1

# Logged as admin
login_user_via_session(client, email=admin['email'])
Expand Down Expand Up @@ -145,14 +146,14 @@ def test_read(client, make_organisation, make_collection, superuser, admin,
res = client.get(
url_for('invenio_records_rest.coll_item',
pid_value=collection1['pid']))
assert res.status_code == 403
assert res.status_code == 200

# Logged as moderator
login_user_via_session(client, email=moderator['email'])
res = client.get(
url_for('invenio_records_rest.coll_item',
pid_value=collection1['pid']))
assert res.status_code == 403
assert res.status_code == 200

# Logged as admin
login_user_via_session(client, email=admin['email'])
Expand Down
5 changes: 0 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,6 @@ def deposit_json(collection, subdivision):
'collections': [{
'$ref':
f'https://sonar.ch/api/collections/{collection["pid"]}'
}, {
'name': [{
'language': 'eng',
'value': 'New collection'
}]
}],
'classification':
'543',
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/deposits/test_deposits_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_create_document(app, db, project, client, deposit, submitter,
},
'publicNote': 'Published version'
}]
assert len(document['collections']) == 2
assert len(document['collections']) == 1
assert document['classification'] == [{
'type': 'bf:ClassificationUdc',
'classificationPortion': '543'
Expand Down
24 changes: 23 additions & 1 deletion tests/ui/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_logged_user(app, client, superuser, admin, moderator, submitter,
assert not res.json['metadata']['permissions']['deposits']['list']


def test_schemas(client, admin, user):
def test_schemas(client, admin, user, submitter, moderator):
"""Test JSON schemas endpoint."""
res = client.get(url_for('sonar.schemas', record_type='documents'))
assert res.status_code == 200
Expand All @@ -145,12 +145,18 @@ def test_schemas(client, admin, user):
assert res.json['schema']['properties'].get('organisation')
assert res.json['schema']['properties'].get('role')

res = client.get(url_for('sonar.schemas', record_type='deposits'))
assert res.status_code == 200

login_user_via_session(client, email=admin['email'])

res = client.get(url_for('sonar.schemas', record_type='documents'))
assert res.status_code == 200
assert not res.json['schema']['properties'].get('organisation')

res = client.get(url_for('sonar.schemas', record_type='deposits'))
assert res.status_code == 200

res = client.get(url_for('sonar.schemas', record_type='users'))
assert res.status_code == 200
assert not res.json['schema']['properties'].get('organisation')
Expand Down Expand Up @@ -182,6 +188,22 @@ def test_schemas(client, admin, user):
assert 'organisation' not in res.json['schema']['properties']['metadata'][
'propertiesOrder']

login_user_via_session(client, email=moderator['email'])
res = client.get(url_for('sonar.schemas', record_type='deposits'))
assert res.status_code == 200
assert res.json[
'schema']['properties']['diffusion']['properties'].get('subdivisions')
assert 'subdivisions' in res.json[
'schema']['properties']['diffusion']['propertiesOrder']

login_user_via_session(client, email=submitter['email'])
res = client.get(url_for('sonar.schemas', record_type='deposits'))
assert res.status_code == 200
assert not res.json[
'schema']['properties']['diffusion']['properties'].get('subdivisions')
assert not 'subdivisions' in res.json[
'schema']['properties']['diffusion']['propertiesOrder']


def test_profile(client, user):
"""Test profile page."""
Expand Down

0 comments on commit 076e0f3

Please # to comment.