-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admin must have role for a library to generate inventory report. (PP-…
- Loading branch information
Showing
3 changed files
with
129 additions
and
34 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import logging | ||
from http import HTTPStatus | ||
from types import SimpleNamespace | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
@@ -11,10 +13,11 @@ | |
) | ||
from palace.manager.api.admin.problem_details import ADMIN_NOT_AUTHORIZED | ||
from palace.manager.api.overdrive import OverdriveAPI | ||
from palace.manager.api.problem_details import LIBRARY_NOT_FOUND | ||
from palace.manager.core.opds_import import OPDSAPI | ||
from palace.manager.sqlalchemy.model.admin import Admin, AdminRole | ||
from palace.manager.sqlalchemy.util import create | ||
from palace.manager.util.problem_detail import ProblemDetailException | ||
from palace.manager.util.problem_detail import ProblemDetail, ProblemDetailException | ||
from tests.fixtures.api_admin import AdminControllerFixture | ||
from tests.fixtures.api_controller import ControllerFixture | ||
from tests.fixtures.database import DatabaseTransactionFixture | ||
|
@@ -64,6 +67,89 @@ def test_generate_inventory_and_hold_reports( | |
email_address=email_address, library_id=library_id | ||
) | ||
|
||
@patch( | ||
"palace.manager.api.admin.controller.report.generate_inventory_and_hold_reports" | ||
) | ||
def test_generate_report_authorization( | ||
self, | ||
mock_generate_reports, | ||
db: DatabaseTransactionFixture, | ||
flask_app_fixture: FlaskAppFixture, | ||
caplog: pytest.LogCaptureFixture, | ||
): | ||
caplog.set_level( | ||
logging.INFO, | ||
"palace.manager.api.admin.controller.report", | ||
) | ||
|
||
task_id = 7 | ||
mock_generate_reports.delay.return_value = SimpleNamespace(id=task_id) | ||
log_message_suffix = f"Task Request Id: {task_id})" | ||
|
||
controller = ReportController(db.session) | ||
method = controller.generate_inventory_report | ||
|
||
library1 = db.library() | ||
library2 = db.library() | ||
|
||
sysadmin_email = "[email protected]" | ||
librarian_email = "[email protected]" | ||
|
||
sysadmin = flask_app_fixture.admin_user( | ||
email=sysadmin_email, role=AdminRole.SYSTEM_ADMIN | ||
) | ||
librarian1 = flask_app_fixture.admin_user( | ||
email=librarian_email, role=AdminRole.LIBRARIAN, library=library1 | ||
) | ||
|
||
collection = db.collection( | ||
protocol=OPDSAPI.label(), | ||
settings={"data_source": "test", "external_account_id": "http://url"}, | ||
) | ||
collection.libraries = [library1, library2] | ||
|
||
def assert_and_clear_caplog( | ||
response: Response | ProblemDetail, email: str | ||
) -> None: | ||
assert isinstance(response, Response) | ||
assert response.status_code == 202 | ||
assert "The completed reports will be sent to" in response.get_json().get( | ||
"message" | ||
) | ||
assert email in response.get_json().get("message") | ||
assert log_message_suffix in caplog.text | ||
caplog.clear() | ||
|
||
# Sysadmin can get info for any library. | ||
with flask_app_fixture.test_request_context( | ||
"/", admin=sysadmin, library=library1 | ||
): | ||
assert_and_clear_caplog(method(), sysadmin_email) | ||
|
||
with flask_app_fixture.test_request_context( | ||
"/", admin=sysadmin, library=library2 | ||
): | ||
assert_and_clear_caplog(method(), sysadmin_email) | ||
|
||
# The librarian for library 1 can get info only for that library... | ||
with flask_app_fixture.test_request_context( | ||
"/", admin=librarian1, library=library1 | ||
): | ||
assert_and_clear_caplog(method(), librarian_email) | ||
# ... since it does not have an admin role for library2. | ||
with flask_app_fixture.test_request_context( | ||
"/", admin=librarian1, library=library2 | ||
): | ||
with pytest.raises(ProblemDetailException) as exc: | ||
method() | ||
assert exc.value.problem_detail == ADMIN_NOT_AUTHORIZED | ||
|
||
# A library must be provided. | ||
with flask_app_fixture.test_request_context("/", admin=sysadmin, library=None): | ||
with pytest.raises(ProblemDetailException) as exc: | ||
method() | ||
assert exc.value.problem_detail == LIBRARY_NOT_FOUND | ||
|
||
def test_inventory_report_info( | ||
self, db: DatabaseTransactionFixture, flask_app_fixture: FlaskAppFixture | ||
): | ||
|
@@ -125,8 +211,9 @@ def test_inventory_report_info( | |
|
||
# A library must be provided. | ||
with flask_app_fixture.test_request_context("/", admin=sysadmin, library=None): | ||
admin_response_none = controller.inventory_report_info() | ||
assert admin_response_none.status_code == 404 | ||
with pytest.raises(ProblemDetailException) as exc: | ||
controller.inventory_report_info() | ||
assert exc.value.problem_detail == LIBRARY_NOT_FOUND | ||
|
||
@pytest.mark.parametrize( | ||
"protocol, settings, expect_collection", | ||
|