diff --git a/tests/integration/attachments-draft.test.js b/tests/integration/attachments-draft.test.js index 73dc2775..a42cc8e5 100644 --- a/tests/integration/attachments-draft.test.js +++ b/tests/integration/attachments-draft.test.js @@ -6,14 +6,14 @@ const { waitForMalwareDeletion, waitForDeletion, runWithUser, - withUser, uploadDraftAttachment, } = require("../utils/testUtils") const path = require("path") const { Readable } = require("stream") const app = path.resolve(__dirname, "../incidents-app") -const { GET, POST, DELETE, PATCH, PUT } = withUser("alice", cds.test(app)) +const { GET, POST, DELETE, PATCH, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const alice = new cds.User({ id: "alice", roles: { admin: 1, support: 1 } }) const { createReadStream, readFileSync } = cds.utils.fs const { join, basename } = cds.utils.path @@ -95,10 +95,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/pdf", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) @@ -134,11 +130,7 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/pdf", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), content: fakeFileBuffer, - createdBy: "alice", }, ).catch((e) => { expectedError = e @@ -161,10 +153,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: incidentID, filename: "large-stream.pdf", mimeType: "application/pdf", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) @@ -296,10 +284,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { filename: "test.pdf", mimeType: "application/pdf", content: createReadStream(join(__dirname, "content/test.pdf")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) await utils.draftModeSave( @@ -467,7 +451,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: incidentID, filename: "draft-only.pdf", mimeType: "application/pdf", - createdBy: "alice", }, ) expect(secondAttachRes.data.ID).toBeTruthy() @@ -522,10 +505,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { content: createReadStream( join(__dirname, "..", "integration", "content/sample.pdf"), ), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) expect(doc.data.ID).toBeTruthy() @@ -550,10 +529,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__sampleID: sampleID, up__gjahr: gjahr, filename: "myfancyfile.pdf", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) expect(doc.data.ID).toBeTruthy() @@ -789,8 +764,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "testfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(res.data.ID).not.toBeNull() @@ -833,8 +806,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "testfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ], }) @@ -882,8 +853,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "testfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ], details: [ @@ -895,8 +864,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "detailsfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ], }, @@ -991,8 +958,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "detailsfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(res.data.ID).not.toBeNull() @@ -1046,8 +1011,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "nested-draft.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes.data.ID).toBeTruthy() @@ -1177,8 +1140,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: level2ID, filename: "depth3.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes.data.ID).toBeTruthy() @@ -1257,8 +1218,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: level3ID, filename: "depth4.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes.data.ID).toBeTruthy() @@ -1331,8 +1290,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "testfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes.data.ID).not.toBeNull() @@ -1371,8 +1328,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "detailsfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes.data.ID).not.toBeNull() @@ -1408,8 +1363,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "testfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResTest.data.ID).not.toBeNull() @@ -1440,8 +1393,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "detailsfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResDetails.data.ID).not.toBeNull() @@ -1491,8 +1442,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResParent.data.ID).toBeTruthy() @@ -1509,8 +1458,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "childfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResChild.data.ID).toBeTruthy() @@ -1566,8 +1513,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResParent.data.ID).toBeTruthy() @@ -1578,8 +1523,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: detailsID, filename: "childfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResChild.data.ID).toBeTruthy() @@ -1634,8 +1577,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { filename: "testfile.pdf", mimeType: "application/pdf", url: maliciousUrl, - createdAt: new Date(), - createdBy: "alice", }, ) expect(res.data.ID).toBeTruthy() @@ -1684,8 +1625,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => { up__ID: incidentID, filename: "testmal.png", mimeType: "image/png", - createdAt: new Date(), - createdBy: "alice", }, ) expect(res.data.ID).toBeTruthy() diff --git a/tests/integration/attachments-features.test.js b/tests/integration/attachments-features.test.js index 05d645d9..59b60285 100644 --- a/tests/integration/attachments-features.test.js +++ b/tests/integration/attachments-features.test.js @@ -1,14 +1,11 @@ const cds = require("@sap/cds") const { RequestSend } = require("../utils/api") -const { - waitForScanStatus, - newIncident, - withUser, -} = require("../utils/testUtils") +const { waitForScanStatus, newIncident } = require("../utils/testUtils") const path = require("path") const app = path.resolve(__dirname, "../incidents-app") -const { GET, POST, DELETE, PATCH, PUT } = withUser("alice", cds.test(app)) +const { GET, POST, DELETE, PATCH, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const { createReadStream, readFileSync } = cds.utils.fs const { join } = cds.utils.path @@ -159,10 +156,6 @@ describe("Tests for acceptable media types", () => { filename: "sample.pdf", mimeType: "application/pdf", content: createReadStream(join(__dirname, "content/sample.pdf")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ).catch((e) => { expectedError = e @@ -189,11 +182,7 @@ describe("Tests for acceptable media types", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/pdf boundary=something", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), content: createReadStream(join(__dirname, "content/sample.pdf")), - createdBy: "alice", }, ).catch((e) => { expectedError = e @@ -220,11 +209,7 @@ describe("Tests for acceptable media types", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/pdf charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), content: createReadStream(join(__dirname, "content/sample.pdf")), - createdBy: "alice", }, ).catch((e) => { expect(e.status).toEqual(400) @@ -250,10 +235,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) await POST( @@ -263,10 +244,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) const { status: postStatus } = await POST( @@ -276,10 +253,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) expect(postStatus).toEqual(201) @@ -304,10 +277,6 @@ describe("Testing max and min amounts of attachments", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) const { status: deleteStatus } = await DELETE( @@ -340,10 +309,6 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }, @@ -385,26 +350,14 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }, @@ -446,10 +399,6 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", DraftAdministrativeData_DraftUUID: "12345", }, ], @@ -478,28 +427,16 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", DraftAdministrativeData_DraftUUID: "12345", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", DraftAdministrativeData_DraftUUID: "12345", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", DraftAdministrativeData_DraftUUID: "12345", }, ], @@ -542,10 +479,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", DraftAdministrativeData_DraftUUID: "12345", }, ], @@ -558,10 +491,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) await POST( @@ -571,10 +500,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) await INSERT.into( @@ -598,10 +523,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) @@ -769,10 +690,6 @@ describe("Testing max and min amounts of attachments", () => { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", content: createReadStream(join(__dirname, "content/sample-1.jpg")), - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) diff --git a/tests/integration/attachments-non-draft.test.js b/tests/integration/attachments-non-draft.test.js index 478df2f2..e69b18fb 100644 --- a/tests/integration/attachments-non-draft.test.js +++ b/tests/integration/attachments-non-draft.test.js @@ -4,12 +4,12 @@ const { newIncident, waitForDeletion, delay, - withUser, } = require("../utils/testUtils") const path = require("path") const app = path.resolve(__dirname, "../incidents-app") -const { GET, POST, PATCH, DELETE, PUT } = withUser("alice", cds.test(app)) +const { GET, POST, DELETE, PATCH, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const { join } = cds.utils.path const { createReadStream, readFileSync, statSync } = cds.utils.fs @@ -299,8 +299,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, { headers: { "Content-Type": "application/json" } }, ) @@ -312,8 +310,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: detailsID, filename: "childfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResDetails.data.ID).toBeTruthy() @@ -349,8 +345,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, { headers: { "Content-Type": "application/json" } }, ) @@ -362,8 +356,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: detailsID, filename: "childfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResDetails.data.ID).toBeTruthy() @@ -415,8 +407,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResTest.data.url).toBeTruthy() @@ -460,8 +450,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes.data.url).toBeTruthy() @@ -503,8 +491,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: firstID, filename: "file1.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes1.data.ID).toBeTruthy() @@ -515,8 +501,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: secondID, filename: "file2.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachRes2.data.ID).toBeTruthy() @@ -550,8 +534,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: testID, filename: "parentfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, { headers: { "Content-Type": "application/json" } }, ) @@ -563,8 +545,6 @@ describe("Tests for uploading/deleting and fetching attachments through API call up__ID: detailsID, filename: "childfile.pdf", mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) expect(attachResDetails.data.ID).toBeTruthy() @@ -810,28 +790,16 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], hiddenAttachments2: [ { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }) @@ -841,10 +809,6 @@ describe("Testing max and min amounts of attachments", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ).catch((e) => { expect(e.status).toEqual(400) @@ -868,10 +832,6 @@ describe("Testing max and min amounts of attachments", () => { up__ID: incidentID, filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ) await DELETE( @@ -902,10 +862,6 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }, @@ -937,26 +893,14 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }, @@ -995,10 +939,6 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }, @@ -1026,26 +966,14 @@ describe("Testing max and min amounts of attachments", () => { { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, { filename: "sample.pdf", mimeType: "application/jpeg; charset=UTF-8", - createdAt: new Date( - Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000, - ), - createdBy: "alice", }, ], }, @@ -1263,9 +1191,10 @@ describe("Tests for inline single attachment in non-draft mode", () => { const getRes = await GET( `/odata/v4/admin/SingleAttachment(ID=${entity.ID})/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getRes.status).toEqual(200) - expect(getRes.data).toEqual(fileContent) + expect(getRes.data).toEqual(Buffer.from(fileContent)) }) it("Should delete a SingleAttachment and clear all inline fields", async () => { diff --git a/tests/integration/attachments-rename.test.js b/tests/integration/attachments-rename.test.js index f6d45add..b45e04e7 100644 --- a/tests/integration/attachments-rename.test.js +++ b/tests/integration/attachments-rename.test.js @@ -5,12 +5,12 @@ const { newIncident, runWithUser, uploadDraftAttachment, - withUser, } = require("../utils/testUtils") const path = require("path") const app = path.resolve(__dirname, "../incidents-app") -const { GET, POST, DELETE, PUT } = withUser("alice", cds.test(app)) +const { GET, POST, DELETE, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const alice = new cds.User({ id: "alice", roles: { admin: 1, support: 1 } }) const { readFileSync } = cds.utils.fs const { join, basename } = cds.utils.path @@ -50,8 +50,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -62,8 +60,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -74,8 +70,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -134,8 +128,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -187,8 +179,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: initialFilename, mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -199,8 +189,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: initialFilename, mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -211,8 +199,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: initialFilename, mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -401,8 +387,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) await POST( @@ -411,8 +395,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -442,8 +424,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) await POST( @@ -452,8 +432,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) await POST( @@ -462,8 +440,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) await POST( @@ -472,8 +448,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -519,8 +493,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) @@ -531,8 +503,6 @@ describe("Tests for renaming duplicate attachments", () => { up__ID: incidentID, filename: basename(filepath), mimeType: "application/pdf", - createdAt: new Date(), - createdBy: "alice", }, ) diff --git a/tests/integration/attachments-single.test.js b/tests/integration/attachments-single.test.js index f5139a51..16589776 100644 --- a/tests/integration/attachments-single.test.js +++ b/tests/integration/attachments-single.test.js @@ -5,12 +5,12 @@ const { waitForMalwareDeletion, waitForDeletion, runWithUser, - withUser, } = require("../utils/testUtils") const path = require("path") const app = path.resolve(__dirname, "../incidents-app") -const { GET, POST, DELETE, PATCH, PUT } = withUser("alice", cds.test(app)) +const { GET, POST, DELETE, PATCH, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const alice = new cds.User({ id: "alice", roles: { admin: 1, support: 1 } }) const { readFileSync } = cds.utils.fs const { join } = cds.utils.path @@ -77,9 +77,10 @@ describe("Tests for single attachment entity", () => { const getRes = await GET( `/odata/v4/processor/SingleAttachment(ID=${singleAttachment.ID},IsActiveEntity=true)/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getRes.status).toEqual(200) - expect(getRes.data).toEqual(fileContent.toString()) + expect(getRes.data).toEqual(fileContent) }) it("Should delete a SingleAttachment and its attachment", async () => { @@ -166,9 +167,10 @@ describe("Tests for single attachment entity", () => { const getContentRes = await GET( `/odata/v4/processor/SingleAttachment(ID=${singleAttachment.ID},IsActiveEntity=true)/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getContentRes.status).toEqual(200) - expect(getContentRes.data).toEqual(fileContent) + expect(getContentRes.data).toEqual(Buffer.from(fileContent)) }) it("Should fail to upload content that exceeds the size limit", async () => { @@ -444,9 +446,10 @@ describe("Tests for single attachment entity", () => { // Verify v1 content is readable const getV1 = await GET( `/odata/v4/processor/SingleAttachment(ID=${singleAttachment.ID},IsActiveEntity=true)/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getV1.status).toEqual(200) - expect(getV1.data).toEqual(v1Content) + expect(getV1.data).toEqual(Buffer.from(v1Content)) // Re-edit: create a new draft from the active entity await POST( @@ -471,9 +474,10 @@ describe("Tests for single attachment entity", () => { // Verify v2 content is now returned const getV2 = await GET( `/odata/v4/processor/SingleAttachment(ID=${singleAttachment.ID},IsActiveEntity=true)/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getV2.status).toEqual(200) - expect(getV2.data).toEqual(v2Content) + expect(getV2.data).toEqual(Buffer.from(v2Content)) }) it("Should populate myAttachment_url on the active entity after draft activation", async () => { @@ -602,9 +606,10 @@ describe("Tests for single attachment entity", () => { const getRes = await GET( `/odata/v4/processor/SingleAttachment(ID=${singleAttachment.ID},IsActiveEntity=true)/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getRes.status).toEqual(200) - expect(getRes.data).toEqual(fileContent) + expect(getRes.data).toEqual(Buffer.from(fileContent)) }) it("Should not delete blob when discarding a re-edit with no new upload", async () => { @@ -772,9 +777,10 @@ describe("Tests for single attachment entity", () => { const getRes = await GET( `/odata/v4/processor/SingleAttachment(ID=${singleAttachment.ID},IsActiveEntity=false)/myAttachment_content`, + { responseType: "arraybuffer" }, ) expect(getRes.status).toEqual(200) - expect(getRes.data).toEqual(fileContent) + expect(getRes.data).toEqual(Buffer.from(fileContent)) }) it("Should clear inline attachment fields when DeleteInfectedAttachment is triggered with the correct hash", async () => { diff --git a/tests/unit/validateAttachmentMimeType.test.js b/tests/unit/validateAttachmentMimeType.test.js index 1c7661e3..7c97752c 100644 --- a/tests/unit/validateAttachmentMimeType.test.js +++ b/tests/unit/validateAttachmentMimeType.test.js @@ -4,8 +4,9 @@ const path = require("path") const app = path.resolve(__dirname, "../incidents-app") const { validateAttachmentMimeType } = require("../../lib/generic-handlers") -const { withUser, newIncident } = require("../utils/testUtils") -const { GET, POST, PUT } = withUser("alice", cds.test(app)) +const { newIncident } = require("../utils/testUtils") +const { GET, POST, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const { readFileSync } = cds.utils.fs const { join } = cds.utils.path diff --git a/tests/unit/validateAttachmentSize.test.js b/tests/unit/validateAttachmentSize.test.js index 94b905cb..7a5e344e 100644 --- a/tests/unit/validateAttachmentSize.test.js +++ b/tests/unit/validateAttachmentSize.test.js @@ -3,8 +3,9 @@ const cds = require("@sap/cds") const path = require("path") const app = path.resolve(__dirname, "../incidents-app") const { validateAttachmentSize } = require("../../lib/generic-handlers") -const { newIncident, withUser } = require("../utils/testUtils") -const { POST, PUT } = withUser("alice", cds.test(app)) +const { newIncident } = require("../utils/testUtils") +const { POST, PUT, defaults } = cds.test(app) +defaults.auth = { username: "alice" } const { readFileSync } = cds.utils.fs const { join } = cds.utils.path diff --git a/tests/utils/testUtils.js b/tests/utils/testUtils.js index 4d928b01..c3c84381 100644 --- a/tests/utils/testUtils.js +++ b/tests/utils/testUtils.js @@ -183,44 +183,6 @@ async function waitUntil(predicate, timeout = 180000) { throw new Error(`Timeout: condition not met within ${timeout}ms`) } -const { Readable } = require("stream") - -async function unwrapStream(res) { - if (res.data && typeof res.data.getReader === "function") { - const reader = res.data.getReader() - const chunks = [] - while (true) { - const { done, value } = await reader.read() - if (done) break - chunks.push(value) - } - res.data = Buffer.concat(chunks).toString() - } - return res -} - -function withUser(username, test) { - const auth = { auth: { username } } - const wrap = (body) => (Buffer.isBuffer(body) ? Readable.from(body) : body) - const req = - (fn) => - (...args) => - fn(...args).then(unwrapStream) - return { - GET: req((url, opts) => test.GET(url, { ...auth, ...opts })), - POST: req((url, body, opts) => - test.POST(url, wrap(body), { ...auth, ...opts }), - ), - PUT: req((url, body, opts) => - test.PUT(url, wrap(body), { ...auth, ...opts }), - ), - DELETE: req((url, opts) => test.DELETE(url, { ...auth, ...opts })), - PATCH: req((url, body, opts) => - test.PATCH(url, wrap(body), { ...auth, ...opts }), - ), - } -} - /** * Uploads attachment in draft mode using CDS test utilities * @param {Object} utils - RequestSend utility instance @@ -296,6 +258,5 @@ module.exports = { waitForDeletion, waitForMalwareDeletion, runWithUser, - withUser, uploadDraftAttachment, }