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 all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file added xmpls/SolarPanelReport.pdf
Binary file not shown.
83 changes: 83 additions & 0 deletions xmpls/attachments.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})