-
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.
Adds API to get library-specific info on inventory reports.
- Loading branch information
Showing
6 changed files
with
240 additions
and
2 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,18 @@ | ||
from pydantic import Field | ||
|
||
from palace.manager.util.flask_util import CustomBaseModel | ||
|
||
|
||
class InventoryReportCollectionInfo(CustomBaseModel): | ||
"""Collection information.""" | ||
|
||
id: int = Field(..., description="Collection identifier.") | ||
name: str = Field(..., description="Collection name.") | ||
|
||
|
||
class InventoryReportInfo(CustomBaseModel): | ||
"""Inventory report information.""" | ||
|
||
collections: list[InventoryReportCollectionInfo] = Field( | ||
..., description="List of collections." | ||
) |
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 |
---|---|---|
|
@@ -4,10 +4,19 @@ | |
import pytest | ||
from flask import Response | ||
|
||
from palace.manager.api.admin.controller import ReportController | ||
from palace.manager.api.admin.model.inventory_report import ( | ||
InventoryReportCollectionInfo, | ||
InventoryReportInfo, | ||
) | ||
from palace.manager.api.admin.problem_details import ADMIN_NOT_AUTHORIZED | ||
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 tests.fixtures.api_admin import AdminControllerFixture | ||
from tests.fixtures.api_controller import ControllerFixture | ||
from tests.fixtures.database import DatabaseTransactionFixture | ||
from tests.fixtures.flask import FlaskAppFixture | ||
|
||
|
||
class ReportControllerFixture(AdminControllerFixture): | ||
|
@@ -52,3 +61,106 @@ def test_generate_inventory_and_hold_reports( | |
mock_generate_reports.delay.assert_called_once_with( | ||
email_address=email_address, library_id=library_id | ||
) | ||
|
||
def test_inventory_report_info( | ||
self, db: DatabaseTransactionFixture, flask_app_fixture: FlaskAppFixture | ||
): | ||
controller = ReportController(db.session) | ||
|
||
library1 = db.library() | ||
library2 = db.library() | ||
|
||
sysadmin = flask_app_fixture.admin_user( | ||
email="[email protected]", role=AdminRole.SYSTEM_ADMIN | ||
) | ||
librarian1 = flask_app_fixture.admin_user( | ||
email="[email protected]", role=AdminRole.LIBRARIAN, library=library1 | ||
) | ||
|
||
collection = db.collection() | ||
collection.libraries = [library1, library2] | ||
|
||
success_payload_dict = InventoryReportInfo( | ||
collections=[ | ||
InventoryReportCollectionInfo( | ||
id=collection.id, name=collection.integration_configuration.name | ||
) | ||
] | ||
).api_dict() | ||
|
||
# Sysadmin can get info for any library. | ||
with flask_app_fixture.test_request_context( | ||
"/", admin=sysadmin, library=library1 | ||
): | ||
admin_response1 = controller.inventory_report_info() | ||
assert admin_response1.status_code == 200 | ||
assert admin_response1.get_json() == success_payload_dict | ||
|
||
with flask_app_fixture.test_request_context( | ||
"/", admin=sysadmin, library=library2 | ||
): | ||
admin_response2 = controller.inventory_report_info() | ||
assert admin_response2.status_code == 200 | ||
assert admin_response2.get_json() == success_payload_dict | ||
|
||
# The librarian for library 1 can get info only for that library... | ||
with flask_app_fixture.test_request_context( | ||
"/", admin=librarian1, library=library1 | ||
): | ||
librarian1_response1 = controller.inventory_report_info() | ||
assert librarian1_response1.status_code == 200 | ||
assert librarian1_response1.get_json() == success_payload_dict | ||
# ... 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: | ||
controller.inventory_report_info() | ||
assert exc.value.problem_detail == ADMIN_NOT_AUTHORIZED | ||
|
||
# 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 | ||
|
||
@pytest.mark.parametrize( | ||
"settings, expect_collection", | ||
( | ||
({}, True), | ||
({"include_in_inventory_report": False}, False), | ||
({"include_in_inventory_report": True}, True), | ||
), | ||
) | ||
def test_inventory_report_info_reportable_collections( | ||
self, | ||
db: DatabaseTransactionFixture, | ||
flask_app_fixture: FlaskAppFixture, | ||
settings: dict, | ||
expect_collection: bool, | ||
): | ||
controller = ReportController(db.session) | ||
sysadmin = flask_app_fixture.admin_user(role=AdminRole.SYSTEM_ADMIN) | ||
|
||
library = db.library() | ||
collection = db.collection() | ||
collection.libraries = [library] | ||
collection._set_settings(**settings) | ||
|
||
expected_collections = ( | ||
[InventoryReportCollectionInfo(id=collection.id, name=collection.name)] | ||
if expect_collection | ||
else [] | ||
) | ||
expected_collection_count = 1 if expect_collection else 0 | ||
success_payload_dict = InventoryReportInfo( | ||
collections=expected_collections | ||
).api_dict() | ||
assert len(expected_collections) == expected_collection_count | ||
|
||
# Sysadmin can get info for any library. | ||
with flask_app_fixture.test_request_context( | ||
"/", admin=sysadmin, library=library | ||
): | ||
response = controller.inventory_report_info() | ||
assert response.status_code == 200 | ||
assert response.get_json() == success_payload_dict |
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