Skip to content

Commit 043b1b4

Browse files
authored
Update tests (#77)
* Update attachments test
1 parent b610b8d commit 043b1b4

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
"watch": "cds watch",
2828
"start": "cds-serve",
2929
"test": "npx jest --silent",
30+
"add-attachments": "npm add @cap-js/attachments && cp -r xmpls/attachments.cds ./db && cp xmpls/attachments.test.js ./test",
3031
"add-change-tracking": "npm add @cap-js/change-tracking && cp xmpls/change-tracking.cds ./srv && cp xmpls/change-tracking.test.js ./test",
3132
"add-telemetry": "npm add @cap-js/telemetry",
32-
"add-attachments": "npm add @cap-js/attachments && cp -r xmpls/attachments.cds ./db",
3333
"add-notifications": "npm add @cap-js/notifications && cp xmpls/alert-notifications.js ./srv && cp xmpls/notification-types.json ./srv",
3434
"add-audit-log": "npm add @cap-js/audit-logging && cp xmpls/data-privacy.cds ./srv && cp xmpls/audit-log.test.js ./test",
3535
"add-remote-service": "./.github/workflows/checkout remote-service",

xmpls/SolarPanelReport.pdf

54.5 KB
Binary file not shown.

xmpls/attachments.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)