|
15 | 15 | import io |
16 | 16 | import json |
17 | 17 | import unittest |
18 | | -from unittest.mock import mock_open, patch |
19 | 18 | import ctypes |
20 | 19 | import warnings |
21 | 20 | from cryptography.hazmat.primitives import hashes, serialization |
@@ -1347,6 +1346,64 @@ def test_signing_manifest_v2(self): |
1347 | 1346 |
|
1348 | 1347 | output.close() |
1349 | 1348 |
|
| 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 | + |
1350 | 1407 | class TestStream(unittest.TestCase): |
1351 | 1408 | def setUp(self): |
1352 | 1409 | # Create a temporary file for testing |
|
0 commit comments