Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishikunnath2747 committed May 23, 2024
1 parent 36fbdb0 commit db8861b
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@sap/cds": "^7",
"@sap/cds-lsp": "^7.6.1",
"@sap/xssec": "^3.6.1",
"axios": "^1.7.0",
"dotenv": "^16.3.1",
"express": "^4",
"node-cache": "^5.1.2",
Expand Down
197 changes: 197 additions & 0 deletions test/integration/attachments-sdm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
const credentials = require('./credentials.json');

describe('Attachments Integration Tests', () => {
let token;
let sampleAttachmentID
let appUrl = credentials.appUrl
let incidentID = credentials.incidentID;
let serviceName = 'processor';
let entityName = 'Incidents';
let srvpath = 'ProcessorService';

//Generating the CAP Application token
beforeAll(async () => {
try {
const authRes = await axios.post(
`${credentials.authUrl}`,
{},
{
auth: {
username: credentials.clientID,
password: credentials.clientSecret
}
}
);

token = authRes.data.access_token;

} catch (error) {
expect(error).toBeUndefined;
}
});

it('should create an attachment and check if it has been created', async () => {
try {
const config = {
headers: { 'Authorization': "Bearer " + token }
};

//Setting draft mode and uploading the attachment
await axios.post(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=true)/${srvpath}.draftEdit`,
{
PreserveChanges: true,
},
config
);

const postData = {
up__ID: incidentID,
filename: "sample.pdf",
mimeType: "application/pdf",
createdAt: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(),
createdBy: "[email protected]",
modifiedBy: "[email protected]"
};

response = await axios.post(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=false)/attachments`,
postData,
config
)

if (response.data && response.data.ID) {
const formDataPut = new FormData();
const pdfStream = fs.createReadStream("./sample.pdf");
pdfStream.on('error', (error) => console.log('Error reading file:', error));
formDataPut.append('content', pdfStream);
sampleAttachmentID = response.data.ID

//Uploading the actual content into the created attachment
await axios.put(
`https://${appUrl}/odata/v4/${serviceName}/Incidents_attachments(up__ID=${incidentID},ID=${sampleAttachmentID},IsActiveEntity=false)/content`,
formDataPut,
config,
);
}

await axios.post(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=false)/${srvpath}.draftPrepare`,
{
SideEffectsQualifier: ""
},
config
);

config.headers['Content-Type'] = 'application/json';
response = await axios.post(`
https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=false)/${srvpath}.draftActivate`,
{},
config
);

//Checking to see whether the attachment was created
response = await axios.get(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=true)/attachments(up__ID=${incidentID},ID=${sampleAttachmentID},IsActiveEntity=true)/content`,
config
);
expect(response.status).toBe(200);
expect(response.data).toNotBeUndefined;
expect(response.headers['content-type']).toBe('application/pdf');
} catch (error) {
expect(error).toBeUndefined;
}
});

it('should read the created attachment', async () => {
try {
const config = {
headers: { 'Authorization': "Bearer " + token }
}

const response = await axios.get(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=true)/attachments(up__ID=${incidentID},ID=${sampleAttachmentID},IsActiveEntity=true)/content`,
config
);

expect(response.status).toBe(200);
expect(response.headers['content-type']).toBe('application/pdf');
} catch (error) {
console.error(error);
}
});

it('should respond with 400 when the attachment is not found', async () => {
let nonExistentincidentID = 'Incorrect Incident ID';
let nonExistentID = 'Incorrect attachment ID';

try {
const config = {
headers: { 'Authorization': "Bearer " + token }
};

await axios.get(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${nonExistentincidentID},IsActiveEntity=true)/attachments(up__ID=${nonExistentincidentID},ID=${nonExistentID},IsActiveEntity=true)/content`,
config
);

throw new Error('Expected request to fail but it succeeded'); // This line will be executed if the previous line does not throw an error.

} catch (error) {
expect(error.response.status).toBe(400);
}
});

it('should delete an attachment and make sure it doesnt exist', async () => {
const config = {
headers: { 'Authorization': "Bearer " + token }
};

try {

//Setting draft mode and uploading the attachment
await axios.post(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=true)/${srvpath}.draftEdit`,
{
PreserveChanges: true,
},
config
);

await axios.delete(
`https://${appUrl}/odata/v4/${serviceName}/Incidents_attachments(up__ID=${incidentID},ID=${sampleAttachmentID},IsActiveEntity=false)`,
config
);

await axios.post(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=false)/${srvpath}.draftPrepare`,
{
SideEffectsQualifier: ""
},
config
);

config.headers['Content-Type'] = 'application/json';

await axios.post(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=false)/${srvpath}.draftActivate`,
{},
config
);

//Making sure the attachment doesn't exist
response = await axios.get(
`https://${appUrl}/odata/v4/${serviceName}/${entityName}(ID=${incidentID},IsActiveEntity=true)/attachments(up__ID=${incidentID},ID=${sampleAttachmentID},IsActiveEntity=true)/content`,
config
);
} catch (error) {
expect(error.response.status).toBe(404);
}

});


});
7 changes: 7 additions & 0 deletions test/integration/credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"appUrl" : "APP_URL",
"incidentID": "INCIDENT_ID",
"authUrl": "AUTH_URL",
"clientID": "CLIENT_ID",
"clientSecret": "CLIENT_SECRET"
}
Binary file added test/integration/sample.pdf
Binary file not shown.

0 comments on commit db8861b

Please sign in to comment.