-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
permissions: update and delete permissions api for records
* Removes update and delete permissions from document serializer. * Removes unused codes. * Increases test code coverage. Co-Authored-by: Aly Badr <aly.badr@rero.ch>
- Loading branch information
Aly Badr
committed
Feb 17, 2020
1 parent
e311bed
commit 9be018c
Showing
9 changed files
with
255 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Permissions for all modules.""" | ||
|
||
|
||
from flask import jsonify | ||
|
||
from .utils import get_record_class_update_permission_from_route | ||
|
||
|
||
def jsonify_response( | ||
can_update=False, can_delete=False, reasons={}): | ||
"""Jsonify api response.""" | ||
return jsonify({ | ||
'update': {'can': can_update}, | ||
'delete': {'can': can_delete, 'reasons': reasons} | ||
}) | ||
|
||
|
||
def record_permissions(record_pid=None, route_name=None): | ||
"""Return record permissions.""" | ||
try: | ||
rec_class, update_permission = \ | ||
get_record_class_update_permission_from_route(route_name) | ||
record = rec_class.get_record_by_pid(record_pid) | ||
can_update = update_permission(record).can() | ||
if not record: | ||
return jsonify_response(reasons='Record not found.') | ||
|
||
if record.can_delete: | ||
return jsonify_response( | ||
can_update=can_update, | ||
can_delete=True) | ||
else: | ||
return jsonify_response( | ||
reasons=record.reasons_not_to_delete(), | ||
can_update=can_update) | ||
except Exception as error: | ||
return jsonify({'status': 'error: Bad request'}), 400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Blueprint used for loading templates for all modules.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from functools import wraps | ||
|
||
from flask import Blueprint, jsonify | ||
from flask_login import current_user | ||
|
||
from .permissions import record_permissions | ||
from ..permissions import librarian_permission | ||
|
||
api_blueprint = Blueprint( | ||
'api_blueprint', | ||
__name__, | ||
url_prefix='' | ||
) | ||
|
||
|
||
def check_authentication(func): | ||
"""Decorator to check authentication for permissions HTTP API.""" | ||
@wraps(func) | ||
def decorated_view(*args, **kwargs): | ||
if not current_user.is_authenticated: | ||
return jsonify({'status': 'error: Unauthorized'}), 401 | ||
if not librarian_permission.require().can(): | ||
return jsonify({'status': 'error: Forbidden'}), 403 | ||
return func(*args, **kwargs) | ||
|
||
return decorated_view | ||
|
||
|
||
@api_blueprint.route( | ||
'/permissions/<route_name>/<record_pid>', methods=['GET']) | ||
@check_authentication | ||
def permissions(route_name, record_pid): | ||
"""HTTP GET request for record permissions. | ||
Required parameters: route_name, record_pid | ||
""" | ||
return record_permissions( | ||
record_pid=record_pid, route_name=route_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Test record permissions API.""" | ||
|
||
|
||
from flask import url_for | ||
from invenio_accounts.testutils import login_user_via_session | ||
from utils import get_json | ||
|
||
|
||
def test_document_permissions( | ||
client, document, librarian_martigny_no_email, | ||
patron_martigny_no_email, ebook_1): | ||
"""Test document permissions.""" | ||
# failed: invlaid document pid is given | ||
res = client.get( | ||
url_for( | ||
'api_blueprint.permissions', | ||
route_name='documents', | ||
record_pid='no_pid' | ||
) | ||
) | ||
assert res.status_code == 401 | ||
# failed: no logged user | ||
res = client.get( | ||
url_for( | ||
'api_blueprint.permissions', | ||
route_name='documents', | ||
record_pid=document.pid | ||
) | ||
) | ||
assert res.status_code == 401 | ||
|
||
# failed: logged patron and a valid document pid is given | ||
login_user_via_session(client, patron_martigny_no_email.user) | ||
res = client.get( | ||
url_for( | ||
'api_blueprint.permissions', | ||
route_name='documents', | ||
record_pid=document.pid | ||
) | ||
) | ||
assert res.status_code == 403 | ||
|
||
# success: logged user and a valid document pid is given | ||
login_user_via_session(client, librarian_martigny_no_email.user) | ||
res = client.get( | ||
url_for( | ||
'api_blueprint.permissions', | ||
route_name='documents', | ||
record_pid=document.pid | ||
) | ||
) | ||
assert res.status_code == 200 | ||
data = get_json(res) | ||
assert 'update' in data | ||
assert 'delete' in data | ||
|
||
# success: logged user and a valid document pid is given | ||
login_user_via_session(client, librarian_martigny_no_email.user) | ||
res = client.get( | ||
url_for( | ||
'api_blueprint.permissions', | ||
route_name='documents', | ||
record_pid=ebook_1.pid | ||
) | ||
) | ||
assert res.status_code == 200 | ||
data = get_json(res) | ||
assert 'update' in data | ||
assert 'delete' in data | ||
|
||
# failed: invlaid route name | ||
res = client.get( | ||
url_for( | ||
'api_blueprint.permissions', | ||
route_name='no_route', | ||
record_pid=document.pid | ||
) | ||
) | ||
assert res.status_code == 400 |
Oops, something went wrong.