Skip to content

Commit

Permalink
schemas: remove fields on documents and deposits
Browse files Browse the repository at this point in the history
Move the schema processing to dedicated classes.

* Removes collections and subdivisions on documents and deposits schema.
* Closes rero#752.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Jan 27, 2022
1 parent 2422b85 commit 37e036b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 85 deletions.
31 changes: 25 additions & 6 deletions sonar/json_schemas/deposits_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,33 @@ def process(self):
"""
schema = super().process()

if not current_user_record or current_user_record.is_moderator:
organisation = []
if current_user_record:
organisation = current_user_record.replace_refs() \
.get('organisation')

if not current_user_record or (current_user_record.is_moderator and \
organisation.get('isDedicated')):
return schema

# Remove some fields on json for the shared organisation
if organisation.get('isShared'):
# Remove collections field
schema['properties']['metadata']['properties']\
.pop('collections', None)
propertiesOrder = schema['properties']['metadata']\
.get('propertiesOrder', [])
if 'collections' in propertiesOrder:
schema['properties']['metadata']['propertiesOrder']\
.remove('collections')

# Remove subdivisions field
schema['properties']['diffusion']['properties'].pop(
'subdivisions', None)
order = schema['properties']['diffusion']['propertiesOrder']
schema['properties']['diffusion']['propertiesOrder'] = [
v for v in order if v != 'subdivisions'
]
'subdivisions', None)
propertiesOrder = schema['properties']['diffusion']\
.get('propertiesOrder', [])
if 'subdivisions' in propertiesOrder:
schema['properties']['diffusion']['propertiesOrder']\
.remove('subdivisions')

return schema
10 changes: 9 additions & 1 deletion sonar/json_schemas/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@
"""Factory for JSON schema."""

from .deposits_json_schema import DepositsJSONSchema
from .documents_json_schema import DocumentsJSONSchema
from .json_schema_base import JSONSchemaBase
from .organisations_json_schema import OrganisationsJSONSchema
from .projects_json_schema import ProjectsJSONSchema
from .users_json_schema import UsersJSONSchema


class JSONSchemaFactory():
"""Factory for JSON schema."""

SCHEMAS = {
'deposits': DepositsJSONSchema
'deposits': DepositsJSONSchema,
'documents': DocumentsJSONSchema,
'organisations': OrganisationsJSONSchema,
'projects': ProjectsJSONSchema,
'users': UsersJSONSchema
}

@staticmethod
Expand Down
50 changes: 0 additions & 50 deletions sonar/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,56 +183,6 @@ def schemas(record_type):
"""
try:
json_schema = JSONSchemaFactory.create(record_type)
schema = json_schema.get_schema()

# TODO: Maybe find a proper way to do this.
if record_type in [
'users', 'documents'
] and not current_user.is_anonymous and current_user_record:
if record_type == 'users':
# If user is admin, restrict available roles list.
if current_user_record.is_admin:
reachable_roles = current_user_record.\
get_all_reachable_roles()

schema['properties']['role']['form']['options'] = []
for role in reachable_roles:
schema['properties']['role']['form']['options'].append(
{
'label': 'role_{role}'.format(role=role),
'value': role
})

schema['properties']['role'][
'enum'] = current_user_record.get_all_reachable_roles(
)
# User cannot select role
else:
schema['properties'].pop('role')
if 'role' in schema.get('propertiesOrder', []):
schema['propertiesOrder'].remove('role')

if not current_user_record.is_superuser:
schema['properties'].pop('organisation')
if 'organisation' in schema.get('propertiesOrder', []):
schema['propertiesOrder'].remove('organisation')

if (record_type == 'projects' and not current_user.is_anonymous and
current_user_record and not current_user_record.is_superuser):
schema['properties']['metadata']['properties'].pop(
'organisation', None)
schema['properties']['metadata']['propertiesOrder'].remove(
'organisation')

# Remove modes fields if user does not have superuser role.
if (record_type == 'organisations' and
not current_user_record.is_superuser):
if 'isDedicated' in schema.get('propertiesOrder', []):
schema['propertiesOrder'].remove('isDedicated')

if 'isShared' in schema.get('propertiesOrder', []):
schema['propertiesOrder'].remove('isShared')

return jsonify({'schema': json_schema.process()})
except JSONSchemaNotFound:
abort(404)
Expand Down
26 changes: 18 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ def app_config(app_config):
def make_organisation(app, db, bucket_location, without_oaiset_signals):
"""Factory for creating organisation."""

def _make_organisation(code):
def _make_organisation(code, is_shared=True):
data = {
'code': code,
'name': code,
'isShared': True,
'isDedicated': False,
'isShared': is_shared,
'isDedicated': not is_shared,
'documentsCustomField1': {
'label': [{
'language': 'eng',
Expand Down Expand Up @@ -185,11 +185,13 @@ def roles(base_app, db):
def make_user(app, db, make_organisation):
"""Factory for creating user."""

def _make_user(role_name, organisation='org', access=None):
def _make_user(
role_name, organisation='org', organisation_is_shared=True,
access=None):
name = role_name

if organisation:
make_organisation(organisation)
make_organisation(organisation, is_shared=organisation_is_shared)
name = organisation + name

email = '{name}@rero.ch'.format(name=name)
Expand Down Expand Up @@ -268,6 +270,16 @@ def moderator(make_user):
return make_user('moderator', access='admin-access')


@pytest.fixture()
def moderator_dedicated(make_user):
"""Create moderator organisation dedicated."""
return make_user(
'moderator',
organisation='dedicated',
organisation_is_shared=False,
access='admin-access')


@pytest.fixture()
def submitter(make_user):
"""Create submitter."""
Expand All @@ -289,7 +301,7 @@ def superuser(make_user):
@pytest.fixture()
def document_json(app, db, bucket_location, organisation):
"""JSON document fixture."""
data = {
return {
'identifiedBy': [{
'value': 'urn:nbn:ch:rero-006-108713',
'type': 'bf:Urn'
Expand Down Expand Up @@ -409,8 +421,6 @@ def document_json(app, db, bucket_location, organisation):
'customField1': ['Test']
}

return data


@pytest.fixture()
def make_document(db, document_json, make_organisation, pdf_file, embargo_date):
Expand Down
87 changes: 67 additions & 20 deletions tests/ui/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,52 +134,74 @@ def test_logged_user(app, client, superuser, admin, moderator, submitter,
assert not res.json['metadata']['permissions']['deposits']['list']


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
assert res.json['schema']['properties'].get('organisation')
def test_schema_no_existing(client, user):
"""Test schema no existing."""
login_user_via_session(client, email=user['email'])
res = client.get(url_for('sonar.schemas', record_type='not_existing'))
assert res.status_code == 404

res = client.get(url_for('sonar.schemas', record_type='users'))

def test_schema_documents(client, user, superuser):
"""Test schema documents."""
res = client.get(url_for('sonar.schemas', record_type='documents'))
assert res.status_code == 200
assert res.json['schema']['properties'].get('organisation')
assert res.json['schema']['properties'].get('role')

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

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

login_user_via_session(client, email=superuser['email'])
res = client.get(url_for('sonar.schemas', record_type='documents'))
assert res.status_code == 200
assert not res.json['schema']['properties'].get('organisation')
assert not res.json['schema']['properties'].get('collections')
assert 'collections' not in res.json['schema']['propertiesOrder']
assert not res.json['schema']['properties'].get('subdivisions')
assert 'subdivisions' not in res.json['schema']['propertiesOrder']
assert res.json['schema']['properties'].get('organisation')
assert 'organisation' in res.json['schema']['propertiesOrder']

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

def test_schema_users(client, admin):
"""Test schema users."""
res = client.get(url_for('sonar.schemas', record_type='users'))
assert res.status_code == 200
assert res.json['schema']['properties'].get('organisation')
assert res.json['schema']['properties'].get('role')

login_user_via_session(client, email=admin['email'])
res = client.get(url_for('sonar.schemas', record_type='users'))
assert res.status_code == 200
assert not res.json['schema']['properties'].get('organisation')
assert res.json['schema']['properties'].get('role')
assert len(res.json['schema']['properties']['role']['enum']) == 4

res = client.get(url_for('sonar.schemas', record_type='not_existing'))
assert res.status_code == 404

# Organisations, with admin user --> no fields `isShared` and `isDedicated`
def test_schema_organisations(client, admin, user):
"""Test schema organisations."""
login_user_via_session(client, email=admin['email'])
# admin user --> no fields `isShared` and `isDedicated`
res = client.get(url_for('sonar.schemas', record_type='organisations'))
assert res.status_code == 200
assert 'isShared' not in res.json['schema']['propertiesOrder']
assert 'isDedicated' not in res.json['schema']['propertiesOrder']

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

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

# Projects

def test_schema_projects(client, user):
"""Test schema projects."""
login_user_via_session(client, email=user['email'])
res = client.get(url_for('sonar.schemas', record_type='projects'))
assert res.status_code == 200
assert not res.json['schema']['properties']['metadata']['properties'].get(
Expand All @@ -188,20 +210,45 @@ def test_schemas(client, admin, user, submitter, moderator):
assert 'organisation' not in res.json['schema']['properties']['metadata'][
'propertiesOrder']


def test_schema_deposits(client, moderator, submitter, moderator_dedicated):
"""Test schema deposits."""
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[
# Moderator with shared organisation
assert not res.json[
'schema']['properties']['diffusion']['properties'].get('subdivisions')
assert 'subdivisions' in res.json[
assert 'subdivisions' not in res.json[
'schema']['properties']['diffusion']['propertiesOrder']
assert not res.json[
'schema']['properties']['metadata']['properties'].get('collections')
assert 'collections' not in res.json[
'schema']['properties']['metadata']['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[
assert 'subdivisions' not in res.json[
'schema']['properties']['diffusion']['propertiesOrder']
assert not res.json[
'schema']['properties']['metadata']['properties'].get('collections')
assert 'collections' not in res.json[
'schema']['properties']['metadata']['propertiesOrder']

# Moderator with dedicated organisation
login_user_via_session(client, email=moderator_dedicated['email'])
res = client.get(url_for('sonar.schemas', record_type='deposits'))
assert res.status_code == 200
assert res.json[
'schema']['properties']['metadata']['properties'].get('collections')
assert 'collections' in res.json[
'schema']['properties']['metadata']['propertiesOrder']
assert res.json[
'schema']['properties']['diffusion']['properties'].get('subdivisions')
assert 'subdivisions' in res.json[
'schema']['properties']['diffusion']['propertiesOrder']


Expand Down

0 comments on commit 37e036b

Please sign in to comment.