feat: add PUT endpoint to update letters and manage associated images

This commit is contained in:
Your Name
2026-04-12 04:08:14 +05:30
parent d6076bca16
commit 47474f8ae2
4 changed files with 64 additions and 6 deletions
+49 -1
View File
@@ -68,6 +68,34 @@ class LetterAPITest(APITestCase):
self.assertEqual(Letter.objects.get().type, "KEPT")
self.assertEqual(Letter.objects.get().user, self.user)
def test_update_draft_letter_with_public_id(self):
"""Test API can successfully update an existing letter with new values."""
letter = Letter.objects.create(
user=self.user,
type="KEPT",
status="DRAFT",
public_id="4281edcc-5459-4ff2-bb5e-669fb44e0757",
encrypted_content="enc_xyz==",
encrypted_metadata="enc_meta==",
encrypted_dek="enc_dek==",
)
payload = {
"public_id": letter.public_id,
"type": "KEPT",
"encrypted_content": "enc_abc==",
"encrypted_metadata": "enc_meta==",
"encrypted_dek": "enc_dek==",
}
response = self.client.put(self.url + letter.public_id + "/", payload)
self.assertEqual(response.status_code, 200)
self.assertEqual(Letter.objects.count(), 1)
self.assertEqual(Letter.objects.get().status, "DRAFT")
self.assertEqual(Letter.objects.get().type, "KEPT")
self.assertEqual(Letter.objects.get().user, self.user)
self.assertEqual(Letter.objects.get().encrypted_content, "enc_abc==")
self.assertEqual(Letter.objects.get().encrypted_metadata, "enc_meta==")
self.assertEqual(Letter.objects.get().encrypted_dek, "enc_dek==")
def test_encrypted_dek_is_required_when_storing_encrypted_content_and_metadata(self):
"""encrypted_dek is required when encrypted_content and encrypted_metadata are present"""
payload = {"type": "KEPT", "encrypted_content": "enc_xyz==", "encrypted_metadata": "enc_meta=="}
@@ -88,6 +116,7 @@ class LetterAPITest(APITestCase):
image2 = SimpleUploadedFile("enc_img2.bin", b"encrypted_bytes_2", content_type="application/octet-stream")
payload = {
"public_id": "4281edcc-5459-4ff2-bb5e-669fb44e0757",
"type": "SENT",
"status": "SEALED",
"encrypted_content": "enc_content==",
@@ -96,12 +125,31 @@ class LetterAPITest(APITestCase):
"image_files": [image1, image2],
}
response = self.client.post(self.url, payload, format="multipart")
response = self.client.put(self.url + payload["public_id"] + "/", payload, format="multipart")
self.assertEqual(response.status_code, 201)
self.assertEqual(Letter.objects.count(), 1)
self.assertEqual(LetterImage.objects.count(), 2)
def test_cleanup_images_when_letter_is_updated(self):
letter = Letter.objects.create(user=self.user, type="KEPT", status="DRAFT")
LetterImage.objects.create(letter=letter, file_name="old1.bin", file=ContentFile(b"data", name="del.bin"))
LetterImage.objects.create(letter=letter, file_name="old2.bin", file=ContentFile(b"data", name="del.bin"))
response = self.client.put(
f"/api/letters/{letter.public_id}/",
data={
"encrypted_content": "new_enc==",
"encrypted_metadata": "new_meta==",
"encrypted_dek": "new_dek==",
"image_files": [ContentFile(b"data", name="new.bin")],
},
format="multipart",
)
self.assertEqual(response.status_code, 200)
self.assertEqual(LetterImage.objects.count(), 1)
class LetterImageModelTest(TestCase):
def setUp(self):