Skip to content

Commit 4554d98

Browse files
committed
tests: add tests for the read-only mode
1 parent 54f3991 commit 4554d98

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

tests/resources/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2022 TU Wien.
4+
#
5+
# Invenio-Requests is free software; you can redistribute it and/or modify it
6+
# under the terms of the MIT License; see LICENSE file for more details.
7+
8+
"""Pytest configuration.
9+
10+
See https://pytest-invenio.readthedocs.io/ for documentation on which test fixtures
11+
are available.
12+
"""
13+
14+
import pytest
15+
16+
17+
@pytest.fixture()
18+
def rw_app(app):
19+
"""Fixture that resets the read-only mode before and after tests.
20+
21+
This is done in order to prevent permission issues with other fixtures when the
22+
app isn't freshly initialized for each test.
23+
"""
24+
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = False
25+
yield app
26+
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = False

tests/resources/events/test_request_events_resources.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# Copyright (C) 2021 CERN.
44
# Copyright (C) 2021 Northwestern University.
5+
# Copyright (C) 2022 TU Wien.
56
#
67
# Invenio-Requests is free software; you can redistribute it and/or
78
# modify it under the terms of the MIT License; see LICENSE file for more
@@ -215,3 +216,67 @@ def test_empty_comment(
215216
)
216217
assert 400 == response.status_code
217218
assert expected_json == response.json
219+
220+
221+
#
222+
# Read-only mode
223+
#
224+
225+
226+
def test_comment_request_ro(
227+
rw_app, client_logged_as, headers, events_resource_data, example_request
228+
):
229+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
230+
request_id = example_request.id
231+
client = client_logged_as("admin@example.org")
232+
233+
# Commenting on a request in read-only mode should fail
234+
response = client.post(
235+
f"/requests/{request_id}/comments", headers=headers, json=events_resource_data
236+
)
237+
assert response.status_code == 403
238+
239+
240+
def test_update_comment_request_ro(
241+
rw_app, client_logged_as, headers, events_resource_data, example_request
242+
):
243+
request_id = example_request.id
244+
client = client_logged_as("admin@example.org")
245+
246+
response = client.post(
247+
f"/requests/{request_id}/comments", headers=headers, json=events_resource_data
248+
)
249+
comment_id = response.json["id"]
250+
assert response.status_code == 201
251+
252+
# Updating the comment in read-only mode should fail
253+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
254+
data = copy.deepcopy(events_resource_data)
255+
data["payload"]["content"] = "I've revised my comment."
256+
response = client.put(
257+
f"/requests/{request_id}/comments/{comment_id}",
258+
headers=headers,
259+
json=data,
260+
)
261+
assert response.status_code == 403
262+
263+
264+
def test_delete_comment_request_ro(
265+
rw_app, client_logged_as, headers, events_resource_data, example_request
266+
):
267+
request_id = example_request.id
268+
client = client_logged_as("admin@example.org")
269+
270+
response = client.post(
271+
f"/requests/{request_id}/comments", headers=headers, json=events_resource_data
272+
)
273+
comment_id = response.json["id"]
274+
assert response.status_code == 201
275+
276+
# Updating the comment in read-only mode should fail
277+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
278+
response = client.delete(
279+
f"/requests/{request_id}/comments/{comment_id}",
280+
headers=headers,
281+
)
282+
assert response.status_code == 403

tests/resources/requests/test_requests_resources.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2021 TU Wien.
3+
# Copyright (C) 2021-2022 TU Wien.
44
#
55
# Invenio-Requests is free software; you can redistribute it and/or
66
# modify it under the terms of the MIT License; see LICENSE file for more
@@ -195,3 +195,48 @@ def test_simple_request_flow(app, client_logged_as, headers, example_request):
195195
}
196196
)
197197
assert_api_response(response, 200, expected_data)
198+
199+
200+
#
201+
# Read-only mode
202+
#
203+
204+
205+
def test_update_request_ro(rw_app, client_logged_as, headers, example_request):
206+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
207+
request_id = example_request.id
208+
client = client_logged_as("admin@example.org")
209+
response = client.put(
210+
f"/requests/{request_id}", headers=headers, data=example_request
211+
)
212+
assert response.status_code == 403
213+
214+
215+
def test_delete_request_ro(rw_app, client_logged_as, headers, example_request):
216+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
217+
request_id = example_request.id
218+
client = client_logged_as("admin@example.org")
219+
response = client.delete(f"/requests/{request_id}", headers=headers)
220+
assert response.status_code == 403
221+
222+
223+
def test_submit_request_ro(rw_app, client_logged_as, headers, example_request):
224+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
225+
request_id = example_request.id
226+
client = client_logged_as("admin@example.org")
227+
response = client.post(f"/requests/{request_id}/actions/submit", headers=headers)
228+
assert response.status_code == 403
229+
230+
231+
def test_request_actions_ro(rw_app, client_logged_as, headers, example_request):
232+
request_id = example_request.id
233+
client = client_logged_as("admin@example.org")
234+
response = client.post(f"/requests/{request_id}/actions/submit", headers=headers)
235+
assert response.status_code == 200
236+
237+
for action in ["accept", "decline", "cancel"]:
238+
rw_app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
239+
response = client.post(
240+
f"/requests/{request_id}/actions/{action}", headers=headers
241+
)
242+
assert response.status_code == 403

0 commit comments

Comments
 (0)