Skip to content

Commit 4b90dc2

Browse files
committed
Add test functions for image delete endpoint
1 parent 9152472 commit 4b90dc2

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/test_submission.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,3 +1256,127 @@ def test_set_submission_image_missing_required_fields(
12561256
)
12571257

12581258
assert response.status_code == 422
1259+
1260+
1261+
def test_delete_submission_pi_image_success(
1262+
db: Session, client: TestClient, logged_in_user, temp_storage_object
1263+
):
1264+
"""Test successfully deleting a PI image from a submission."""
1265+
submission = fakes.MetadataSubmissionFactory(
1266+
author=logged_in_user, author_orcid=logged_in_user.orcid
1267+
)
1268+
fakes.SubmissionRoleFactory(
1269+
submission=submission,
1270+
submission_id=submission.id,
1271+
user_orcid=logged_in_user.orcid,
1272+
role=SubmissionEditorRole.owner,
1273+
)
1274+
db.commit()
1275+
1276+
# Add a PI image to delete
1277+
pi_image = SubmissionImagesObject(
1278+
name="pi-image-to-delete.jpg", size=500000, content_type="image/jpeg"
1279+
)
1280+
submission.pi_image = pi_image
1281+
db.commit()
1282+
1283+
# Verify the image exists in storage
1284+
blob = temp_storage_object(BucketName.SUBMISSION_IMAGES, pi_image.name)
1285+
assert blob.exists() is True
1286+
1287+
# Delete the PI image
1288+
response = client.delete(f"/api/metadata_submission/{submission.id}/image/pi_image")
1289+
assert response.status_code == 204
1290+
1291+
# Verify the image was deleted from the database
1292+
db.refresh(submission)
1293+
assert submission.pi_image is None
1294+
1295+
# Verify the image was deleted from storage
1296+
assert blob.exists() is False # type: ignore
1297+
1298+
1299+
def test_delete_submission_primary_study_image_success(
1300+
db: Session, client: TestClient, logged_in_user, temp_storage_object
1301+
):
1302+
"""Test successfully deleting a primary study image from a submission."""
1303+
submission = fakes.MetadataSubmissionFactory(
1304+
author=logged_in_user, author_orcid=logged_in_user.orcid
1305+
)
1306+
fakes.SubmissionRoleFactory(
1307+
submission=submission,
1308+
submission_id=submission.id,
1309+
user_orcid=logged_in_user.orcid,
1310+
role=SubmissionEditorRole.owner,
1311+
)
1312+
db.commit()
1313+
1314+
# Add a primary study image to delete
1315+
primary_study_image = SubmissionImagesObject(
1316+
name="primary-study-image-to-delete.png", size=600000, content_type="image/png"
1317+
)
1318+
submission.primary_study_image = primary_study_image
1319+
db.commit()
1320+
1321+
# Verify the image exists in storage
1322+
blob = temp_storage_object(BucketName.SUBMISSION_IMAGES, primary_study_image.name)
1323+
assert blob.exists() is True
1324+
1325+
# Delete the primary study image
1326+
response = client.delete(f"/api/metadata_submission/{submission.id}/image/primary_study_image")
1327+
assert response.status_code == 204
1328+
1329+
# Verify the image was deleted from the database
1330+
db.refresh(submission)
1331+
assert submission.primary_study_image is None
1332+
1333+
# Verify the image was deleted from storage
1334+
assert blob.exists() is False # type: ignore
1335+
1336+
1337+
def test_delete_submission_study_images_success(
1338+
db: Session, client: TestClient, logged_in_user, temp_storage_object
1339+
):
1340+
"""Test successfully deleting a study image from a submission."""
1341+
submission = fakes.MetadataSubmissionFactory(
1342+
author=logged_in_user, author_orcid=logged_in_user.orcid
1343+
)
1344+
fakes.SubmissionRoleFactory(
1345+
submission=submission,
1346+
submission_id=submission.id,
1347+
user_orcid=logged_in_user.orcid,
1348+
role=SubmissionEditorRole.owner,
1349+
)
1350+
db.commit()
1351+
1352+
# Add a study image to delete
1353+
image_to_delete = SubmissionImagesObject(
1354+
name="study-image-to-delete.jpg", size=700000, content_type="image/jpeg"
1355+
)
1356+
submission.study_images.append(image_to_delete)
1357+
other_image = SubmissionImagesObject(
1358+
name="other-study-image.jpg", size=800000, content_type="image/jpeg"
1359+
)
1360+
submission.study_images.append(other_image)
1361+
db.commit()
1362+
1363+
# Verify the image exists in storage
1364+
blob_to_delete = temp_storage_object(BucketName.SUBMISSION_IMAGES, image_to_delete.name)
1365+
other_blob = temp_storage_object(BucketName.SUBMISSION_IMAGES, other_image.name)
1366+
assert blob_to_delete.exists() is True
1367+
assert other_blob.exists() is True
1368+
1369+
# Delete the study image
1370+
response = client.delete(
1371+
f"/api/metadata_submission/{submission.id}/image/study_images"
1372+
f"?image_name={image_to_delete.name}"
1373+
)
1374+
assert response.status_code == 204
1375+
1376+
# Verify the image was deleted from the database
1377+
db.refresh(submission)
1378+
assert len(submission.study_images) == 1
1379+
1380+
# Verify the image was deleted from storage
1381+
assert blob_to_delete.exists() is False
1382+
assert other_blob.exists() is True

0 commit comments

Comments
 (0)