|
| 1 | +const cds = require('@sap/cds') |
| 2 | +const { GET, POST, PUT, DELETE , expect, axios} = cds.test(__dirname + '/..', '--with-mocks') |
| 3 | +const { createReadStream } = cds.utils.fs; |
| 4 | +const { join } = cds.utils.path; |
| 5 | +axios.defaults.auth = { username: 'alice' } |
| 6 | + |
| 7 | +jest.setTimeout(11111) |
| 8 | + |
| 9 | +describe('Test attachments service', () => { |
| 10 | + let draftId = null; |
| 11 | + let docId = null; |
| 12 | + |
| 13 | + it('Create an incident ', async () => { |
| 14 | + const { status, statusText, data } = await POST(`/odata/v4/processor/Incidents`, { |
| 15 | + title: 'Urgent attention required !', |
| 16 | + status_code: 'N' |
| 17 | + }) |
| 18 | + draftId = data.ID |
| 19 | + expect(status).to.equal(201) |
| 20 | + expect(statusText).to.equal('Created') |
| 21 | + }) |
| 22 | + |
| 23 | + it('+ Activate the draft', async () => { |
| 24 | + const response = await POST( |
| 25 | + `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate` |
| 26 | + ) |
| 27 | + expect(response.status).to.eql(201) |
| 28 | + |
| 29 | + }) |
| 30 | + |
| 31 | + |
| 32 | + describe('Test the file upload', () => { |
| 33 | + it(`Should Close the Incident-${draftId}`, async () => { |
| 34 | + const { status } = await POST( |
| 35 | + `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)/ProcessorService.draftEdit`, |
| 36 | + { |
| 37 | + PreserveChanges: true |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | + const content = createReadStream(join(__dirname, "../xmpls/SolarPanelReport.pdf")); |
| 43 | + const attachRes = await POST(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/attachments`, |
| 44 | + { |
| 45 | + up__ID: draftId, |
| 46 | + filename: "SolarPanelReport.pdf", |
| 47 | + mimeType: "application/pdf", |
| 48 | + status: "Clean", |
| 49 | + createdAt: new Date(), |
| 50 | + }, { headers: { 'Content-Type': 'application/json' } }); |
| 51 | + |
| 52 | + console.log(attachRes); |
| 53 | + docId = attachRes.data.ID; |
| 54 | + console.log("doc id"+docId); |
| 55 | + // Upload the file content with PUT |
| 56 | + const uploadResp = await PUT( |
| 57 | + `/odata/v4/processor/Incidents_attachments(up__ID=${draftId},ID=${docId},IsActiveEntity=false)/content`, |
| 58 | + content, |
| 59 | + { headers: { 'Content-Type': 'application/pdf' } } |
| 60 | + ); |
| 61 | + expect(uploadResp.status).to.equal(204); |
| 62 | + // add attachments here |
| 63 | + |
| 64 | + |
| 65 | + const response = await POST( |
| 66 | + `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate` |
| 67 | + ) |
| 68 | + expect(response.status).to.eql(200) |
| 69 | + }) |
| 70 | + |
| 71 | + |
| 72 | + }) |
| 73 | + it('Check the uploaded file', async () => { |
| 74 | + const response = await GET(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)/attachments(up__ID=${draftId},ID=${docId},IsActiveEntity=true)/content`); |
| 75 | + expect(response.status).to.equal(200); |
| 76 | + expect(response.data).to.not.be.undefined; |
| 77 | + }) |
| 78 | + |
| 79 | + it('- Delete the Incident', async () => { |
| 80 | + const response = await DELETE(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)`) |
| 81 | + expect(response.status).to.eql(204) |
| 82 | + }) |
| 83 | +}) |
0 commit comments