diff --git a/package.json b/package.json index 04e208f..89044fe 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,9 @@ "watch": "cds watch", "start": "cds-serve", "test": "npx jest --silent", + "add-attachments": "npm add @cap-js/attachments && cp -r xmpls/attachments.cds ./db && cp xmpls/attachments.test.js ./test", "add-change-tracking": "npm add @cap-js/change-tracking && cp xmpls/change-tracking.cds ./srv && cp xmpls/change-tracking.test.js ./test", "add-telemetry": "npm add @cap-js/telemetry", - "add-attachments": "npm add @cap-js/attachments && cp -r xmpls/attachments.cds ./db", "add-notifications": "npm add @cap-js/notifications && cp xmpls/alert-notifications.js ./srv && cp xmpls/notification-types.json ./srv", "add-audit-log": "npm add @cap-js/audit-logging && cp xmpls/data-privacy.cds ./srv && cp xmpls/audit-log.test.js ./test", "add-remote-service": "./.github/workflows/checkout remote-service", diff --git a/xmpls/SolarPanelReport.pdf b/xmpls/SolarPanelReport.pdf new file mode 100644 index 0000000..0e29aa2 Binary files /dev/null and b/xmpls/SolarPanelReport.pdf differ diff --git a/xmpls/attachments.test.js b/xmpls/attachments.test.js new file mode 100644 index 0000000..eec6b76 --- /dev/null +++ b/xmpls/attachments.test.js @@ -0,0 +1,83 @@ +const cds = require('@sap/cds') +const { GET, POST, PUT, DELETE , expect, axios} = cds.test(__dirname + '/..', '--with-mocks') +const { createReadStream } = cds.utils.fs; +const { join } = cds.utils.path; +axios.defaults.auth = { username: 'alice' } + +jest.setTimeout(11111) + +describe('Test attachments service', () => { + let draftId = null; + let docId = null; + + it('Create an incident ', async () => { + const { status, statusText, data } = await POST(`/odata/v4/processor/Incidents`, { + title: 'Urgent attention required !', + status_code: 'N' + }) + draftId = data.ID + expect(status).to.equal(201) + expect(statusText).to.equal('Created') + }) + + it('+ Activate the draft', async () => { + const response = await POST( + `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate` + ) + expect(response.status).to.eql(201) + + }) + + + describe('Test the file upload', () => { + it(`Should Close the Incident-${draftId}`, async () => { + const { status } = await POST( + `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)/ProcessorService.draftEdit`, + { + PreserveChanges: true + } + ) + + + const content = createReadStream(join(__dirname, "../xmpls/SolarPanelReport.pdf")); + const attachRes = await POST(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/attachments`, + { + up__ID: draftId, + filename: "SolarPanelReport.pdf", + mimeType: "application/pdf", + status: "Clean", + createdAt: new Date(), + }, { headers: { 'Content-Type': 'application/json' } }); + + console.log(attachRes); + docId = attachRes.data.ID; + console.log("doc id"+docId); + // Upload the file content with PUT + const uploadResp = await PUT( + `/odata/v4/processor/Incidents_attachments(up__ID=${draftId},ID=${docId},IsActiveEntity=false)/content`, + content, + { headers: { 'Content-Type': 'application/pdf' } } + ); + expect(uploadResp.status).to.equal(204); + // add attachments here + + + const response = await POST( + `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate` + ) + expect(response.status).to.eql(200) + }) + + + }) + it('Check the uploaded file', async () => { + const response = await GET(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)/attachments(up__ID=${draftId},ID=${docId},IsActiveEntity=true)/content`); + expect(response.status).to.equal(200); + expect(response.data).to.not.be.undefined; + }) + + it('- Delete the Incident', async () => { + const response = await DELETE(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)`) + expect(response.status).to.eql(204) + }) +})