-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
* added access field to file schema * updated metadata field to be nested with a new schema * added tests for setting file metadata
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
from PIL import Image | ||
from werkzeug.utils import secure_filename | ||
import dictdiffer | ||
|
||
|
||
def publish_record_with_images( | ||
|
@@ -112,15 +113,20 @@ def test_iiif_info( | |
recid = publish_record_with_images(client, file_id, minimal_record, headers) | ||
response = client.get(f"/iiif/record:{recid}:{file_id}/info.json") | ||
assert response.status_code == 200 | ||
assert response.json == { | ||
"@context": "http://iiif.io/api/image/2/context.json", | ||
"profile": ["http://iiif.io/api/image/2/level2.json"], | ||
"protocol": "http://iiif.io/api/image", | ||
"@id": f"https://127.0.0.1:5000/api/iiif/record:{recid}:{file_id}", | ||
"tiles": [{"width": 256, "scaleFactors": [1, 2, 4, 8, 16, 32, 64]}], | ||
"width": 1280, | ||
"height": 1024, | ||
} | ||
assert not list( | ||
Check failure on line 116 in tests/resources/test_iiif_image_api.py GitHub Actions / Tests (3.9, postgresql14, opensearch2, 18.x)
Check failure on line 116 in tests/resources/test_iiif_image_api.py GitHub Actions / Tests (3.9, postgresql14, opensearch2, 20.x)
Check failure on line 116 in tests/resources/test_iiif_image_api.py GitHub Actions / Tests (3.12, postgresql14, opensearch2, 18.x)
Check failure on line 116 in tests/resources/test_iiif_image_api.py GitHub Actions / Tests (3.12, postgresql14, opensearch2, 20.x)
|
||
dictdiffer.diff( | ||
response.json, | ||
{ | ||
"@context": "http://iiif.io/api/image/2/context.json", | ||
"profile": ["http://iiif.io/api/image/2/level2.json"], | ||
"protocol": "http://iiif.io/api/image", | ||
"@id": f"https://127.0.0.1:5000/api/iiif/record:{recid}:{file_id}", | ||
"tiles": [{"width": 256, "scaleFactors": [1, 2, 4, 8, 16, 32, 64]}], | ||
"width": 1280, | ||
"height": 1024, | ||
}, | ||
) | ||
) | ||
|
||
## Testing with filename with a slash ## | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-RDM-Records is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
"""Test RDM records files (metadata).""" | ||
|
||
from io import BytesIO | ||
|
||
import pytest | ||
from marshmallow.exceptions import ValidationError | ||
|
||
from invenio_rdm_records.proxies import current_rdm_records_service | ||
|
||
|
||
def test_valid_metadata_set(running_app, db, location, minimal_record, identity_simple): | ||
"""Test setting file metadata.""" | ||
data = minimal_record.copy() | ||
data["files"] = {"enabled": True} | ||
service = current_rdm_records_service | ||
|
||
file_service = service.files | ||
|
||
# Create | ||
draft = service.create(identity_simple, data) | ||
|
||
# Initialize files and add valid metadata | ||
metadata = { | ||
"page": 1, | ||
"encoding": "utf-8", | ||
"language": "en", | ||
"charset": "utf-8", | ||
"type": "pdf", | ||
} | ||
service.draft_files.init_files( | ||
identity_simple, draft.id, data=[{"key": "test.pdf", "metadata": metadata}] | ||
) | ||
service.draft_files.set_file_content( | ||
identity_simple, draft.id, "test.pdf", BytesIO(b"test file") | ||
) | ||
service.draft_files.commit_file(identity_simple, draft.id, "test.pdf") | ||
|
||
# Publish the record | ||
record = service.publish(identity_simple, draft.id) | ||
|
||
# Get file metadata | ||
result = file_service.list_files(identity_simple, draft.id) | ||
assert result.to_dict()["entries"][0]["metadata"] == metadata | ||
Check failure on line 49 in tests/services/files/test_metadata_files.py GitHub Actions / Tests (3.9, postgresql14, opensearch2, 18.x)
Check failure on line 49 in tests/services/files/test_metadata_files.py GitHub Actions / Tests (3.9, postgresql14, opensearch2, 20.x)
Check failure on line 49 in tests/services/files/test_metadata_files.py GitHub Actions / Tests (3.12, postgresql14, opensearch2, 18.x)
Check failure on line 49 in tests/services/files/test_metadata_files.py GitHub Actions / Tests (3.12, postgresql14, opensearch2, 20.x)
|
||
assert result.to_dict()["entries"][0]["access"]["hidden"] is False # default value | ||
|
||
|
||
def test_invalid_metadata_set( | ||
running_app, db, location, minimal_record, identity_simple | ||
): | ||
"""Test setting file metadata with invalid data.""" | ||
data = minimal_record.copy() | ||
data["files"] = {"enabled": True} | ||
service = current_rdm_records_service | ||
|
||
# Create | ||
draft = service.create(identity_simple, data) | ||
|
||
# Initialize files and add valid metadata | ||
metadata = { | ||
"page": 1, | ||
"encoding": "utf-8", | ||
"language": "en", | ||
"charset": "utf-8", | ||
"type": "pdf", | ||
"invalid": "invalid", | ||
} | ||
with pytest.raises(ValidationError): | ||
service.draft_files.init_files( | ||
identity_simple, draft.id, data=[{"key": "test.pdf", "metadata": metadata}] | ||
) |