Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests #77

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"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",
Expand Down
84 changes: 84 additions & 0 deletions xmpls/attachments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const cds = require('@sap/cds/lib')
const { default: axios } = require('axios')
const { GET, POST, DELETE, PUT, expect } = cds.test(__dirname + '../../')
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, "util/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 () => {
navinkrishnan marked this conversation as resolved.
Show resolved Hide resolved
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)
})
})