Skip to content

Commit eed499c

Browse files
committed
fix: Test API redirect
1 parent 4c660e5 commit eed499c

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/c2pa/c2pa.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,7 @@ def add_resource(self, uri: str, stream: Any):
17541754

17551755
def add_ingredient(self, ingredient_json: str, format: str, source: Any):
17561756
"""Add an ingredient to the builder.
1757+
The added ingredient's source should be a stream-like object, eg. and open file.
17571758
17581759
Args:
17591760
ingredient_json: The JSON ingredient definition
@@ -1772,6 +1773,7 @@ def add_ingredient_from_stream(
17721773
format: str,
17731774
source: Any):
17741775
"""Add an ingredient from a stream to the builder.
1776+
Explicitly named API requiring a stream.
17751777
17761778
Args:
17771779
ingredient_json: The JSON ingredient definition
@@ -1804,6 +1806,44 @@ def add_ingredient_from_stream(
18041806
raise C2paError(
18051807
Builder._ERROR_MESSAGES['ingredient_error'].format("Unknown error"))
18061808

1809+
def add_ingredient_from_file_path(
1810+
self,
1811+
ingredient_json: str,
1812+
format: str,
1813+
filepath: str):
1814+
"""Add an ingredient from a file path to the builder.
1815+
THis is a legacy method.
1816+
1817+
.. deprecated:: 0.13.0
1818+
This method is deprecated and will be removed in a future version.
1819+
Use :meth:`add_ingredient` with a file stream instead.
1820+
1821+
Args:
1822+
ingredient_json: The JSON ingredient definition
1823+
format: The MIME type or extension of the ingredient
1824+
filepath: The path to the file containing the ingredient data
1825+
1826+
Raises:
1827+
C2paError: If there was an error adding the ingredient
1828+
C2paError.Encoding: If the ingredient JSON or format contains invalid UTF-8 characters
1829+
FileNotFoundError: If the file at the specified path does not exist
1830+
"""
1831+
warnings.warn(
1832+
"add_ingredient_from_file_path is deprecated and will be removed in a future version. "
1833+
"Use add_ingredient with a file stream instead.",
1834+
DeprecationWarning,
1835+
stacklevel=2
1836+
)
1837+
1838+
try:
1839+
with open(filepath, 'rb') as file_stream:
1840+
self.add_ingredient_from_stream(ingredient_json, format, file_stream)
1841+
except FileNotFoundError:
1842+
raise FileNotFoundError(f"File not found: {filepath}")
1843+
except Exception as e:
1844+
# Re-raise C2paError and other exceptions as-is
1845+
raise e
1846+
18071847
def to_archive(self, stream: Any):
18081848
"""Write an archive of the builder to a stream.
18091849

tests/test_unit_tests.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import io
1616
import json
1717
import unittest
18-
from unittest.mock import mock_open, patch
1918
import ctypes
2019
import warnings
2120
from cryptography.hazmat.primitives import hashes, serialization
@@ -1347,6 +1346,64 @@ def test_signing_manifest_v2(self):
13471346

13481347
output.close()
13491348

1349+
def test_builder_add_ingredient_from_file_path(self):
1350+
"""Test Builder class add_ingredient_from_file_path method."""
1351+
# Suppress the specific deprecation warning for this test, as this is a legacy method
1352+
with warnings.catch_warnings():
1353+
warnings.simplefilter("ignore", DeprecationWarning)
1354+
1355+
# Test creating builder from JSON
1356+
builder = Builder.from_json(self.manifestDefinition)
1357+
assert builder._builder is not None
1358+
1359+
# Test adding ingredient from file path
1360+
ingredient_json = '{"test": "ingredient_from_file_path"}'
1361+
builder.add_ingredient_from_file_path(ingredient_json, "image/jpeg", self.testPath)
1362+
1363+
builder.close()
1364+
1365+
def test_builder_sign_with_ingredient_from_file(self):
1366+
"""Test Builder class operations with an ingredient added from file path."""
1367+
# Test creating builder from JSON
1368+
builder = Builder.from_json(self.manifestDefinition)
1369+
assert builder._builder is not None
1370+
1371+
# Test adding ingredient from file path
1372+
ingredient_json = '{"title": "Test Ingredient From File"}'
1373+
# Suppress the specific deprecation warning for this test, as this is a legacy method
1374+
with warnings.catch_warnings():
1375+
warnings.simplefilter("ignore", DeprecationWarning)
1376+
builder.add_ingredient_from_file_path(ingredient_json, "image/jpeg", self.testPath3)
1377+
1378+
with open(self.testPath2, "rb") as file:
1379+
output = io.BytesIO(bytearray())
1380+
builder.sign(self.signer, "image/jpeg", file, output)
1381+
output.seek(0)
1382+
reader = Reader("image/jpeg", output)
1383+
json_data = reader.json()
1384+
manifest_data = json.loads(json_data)
1385+
1386+
# Verify active manifest exists
1387+
self.assertIn("active_manifest", manifest_data)
1388+
active_manifest_id = manifest_data["active_manifest"]
1389+
1390+
# Verify active manifest object exists
1391+
self.assertIn("manifests", manifest_data)
1392+
self.assertIn(active_manifest_id, manifest_data["manifests"])
1393+
active_manifest = manifest_data["manifests"][active_manifest_id]
1394+
1395+
# Verify ingredients array exists in active manifest
1396+
self.assertIn("ingredients", active_manifest)
1397+
self.assertIsInstance(active_manifest["ingredients"], list)
1398+
self.assertTrue(len(active_manifest["ingredients"]) > 0)
1399+
1400+
# Verify the first ingredient's title matches what we set
1401+
first_ingredient = active_manifest["ingredients"][0]
1402+
self.assertEqual(first_ingredient["title"], "Test Ingredient From File")
1403+
1404+
builder.close()
1405+
1406+
13501407
class TestStream(unittest.TestCase):
13511408
def setUp(self):
13521409
# Create a temporary file for testing

0 commit comments

Comments
 (0)