Skip to content

Commit cb9d09d

Browse files
eric-pSAPschiwekM
andauthored
Prevent non-existent entity from deleting (#296)
Addressing issue #286. Should return an error when trying to delete a non-existent entity --------- Co-authored-by: Marten Schiwek <marten.schiwek@sap.com>
1 parent 51e2b5d commit cb9d09d

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The `@cap-js/attachments` package is a [CDS plugin](https://cap.cloud.sap/docs/n
1515
* [Storage Targets](#storage-targets)
1616
* [Malware Scanner](#malware-scanner)
1717
* [Visibility Control](#visibility-control-for-attachments-ui-facet-generation)
18-
* [Non-Draft Uploading](non-draft-upload)
18+
* [Non-Draft Uploading](#non-draft-upload)
1919
* [Releases](#releases)
2020
* [Minimum UI5 and CAP NodeJS Version](#minimum-ui5-and-cap-nodejs-version)
2121
* [Architecture Overview](#architecture-overview)

lib/basic.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ class AttachmentsService extends cds.Service {
240240
.filter(assoc => req?.target?.associations[assoc]._target['@_is_media_data'])
241241
if (attachmentCompositions.length > 0) {
242242
const diffData = await req.diff()
243+
if (!diffData || Object.keys(diffData).length === 0) {
244+
return
245+
}
243246
const queries = []
244247
for (const attachmentsComp of attachmentCompositions) {
245248
let deletedAttachments = []

tests/integration/attachments.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ const { createReadStream } = cds.utils.fs
77
const { join } = cds.utils.path
88

99
const app = path.join(__dirname, "../incidents-app")
10-
const { test, expect, axios, GET, POST, DELETE } = cds.test(app)
10+
const { test, expect, axios, GET, POST, DELETE: _DELETE } = cds.test(app)
1111
axios.defaults.auth = { username: "alice" }
12-
12+
const DELETE = async function () {
13+
try {
14+
return await _DELETE(...arguments)
15+
} catch (e) {
16+
return e.response ?? e
17+
}
18+
}
1319
let utils = null
1420
const incidentID = "3ccf474c-3881-44b7-99fb-59a2a4668418"
1521

@@ -196,6 +202,18 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
196202
).to.be.rejectedWith(/404/)
197203
})
198204

205+
it("Deleting a non existing root does not crash the application", async () => {
206+
const response = await DELETE(
207+
`odata/v4/processor/Incidents(ID=${incidentID},IsActiveEntity=true)`
208+
)
209+
expect(response.status).to.equal(204)
210+
211+
const response2 = await DELETE(
212+
`odata/v4/processor/Incidents(ID=${incidentID},IsActiveEntity=true)`
213+
)
214+
expect(response2.status).to.equal(404)
215+
})
216+
199217
it("Cancel draft where parent has composed key", async () => {
200218

201219
await POST(

0 commit comments

Comments
 (0)