From db8861b27c9d79a1e2a2bbd555e08ab25dba9156 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+prophecy2747@users.noreply.github.com> Date: Thu, 23 May 2024 14:13:24 +0530 Subject: [PATCH] Integration tests --- package.json | 1 + test/integration/attachments-sdm.test.js | 197 +++++++++++++++++++++++ test/integration/credentials.json | 7 + test/integration/sample.pdf | Bin 0 -> 6298 bytes 4 files changed, 205 insertions(+) create mode 100644 test/integration/attachments-sdm.test.js create mode 100644 test/integration/credentials.json create mode 100644 test/integration/sample.pdf diff --git a/package.json b/package.json index 5f01c7e..ff95595 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/integration/attachments-sdm.test.js b/test/integration/attachments-sdm.test.js new file mode 100644 index 0000000..6000596 --- /dev/null +++ b/test/integration/attachments-sdm.test.js @@ -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: "integration.test@sap.com", + modifiedBy: "integraion.test@sap.com" + }; + + 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); + } + + }); + + +}); \ No newline at end of file diff --git a/test/integration/credentials.json b/test/integration/credentials.json new file mode 100644 index 0000000..0a8ae75 --- /dev/null +++ b/test/integration/credentials.json @@ -0,0 +1,7 @@ +{ + "appUrl" : "APP_URL", + "incidentID": "INCIDENT_ID", + "authUrl": "AUTH_URL", + "clientID": "CLIENT_ID", + "clientSecret": "CLIENT_SECRET" +} \ No newline at end of file diff --git a/test/integration/sample.pdf b/test/integration/sample.pdf new file mode 100644 index 0000000000000000000000000000000000000000..fadf9f86c8b9c70372f61b898530e09ce47ac308 GIT binary patch literal 6298 zcmai22|SeR+qM-3m9iAdn=HvPyQLzGJ&digGsa*TMq}(DWjVHlWQkYRY zO)8w~a~n~L?;B_HhiAnBC9{BeLqoH9<|q;Su%T}!@-4W@1#ay%y7W ziId}igwROuc_a?8(zW|5OGZ)cw(DVZa_)D_BEHvUN1uA zv-|!T%MZ>dMkISHm&$;rpe0BlKCGh5JNX zC$uSS?4g;(6yU5xY>c$T3I!+qm~IUn%Z3o#9e%_Bexe5s|0MyYejZ@NhB&9K-xPv7 z6+nQmjRB}J!ISJo!4o_I)OP`f>`n#Wdjj+Xf*bS+4n&+9*%z>cfdaH5S{9)ID4^i7 z3X0YcFmXSu(NF(Ow;_d$Hz7~~OVFgcCIB@h_)-CD0E!`#$P^O~9G(EstAg=_1L!Ry z%F1AEK)YLFQ&Tt~M;C+I>6Q6mK;ahy2B33LReDX`!GZ!%b%Hk$PcYU}`?IPDIXL3q z>;7x4Gp_oWnhf$q(CXytI(LfzVv7R)>_GQ1ZC)0mNS*VBOyQy8-s;;ldAKD{JD5IX z)QS+nYw&PfykOtY{%MzCTceS>#^i8}EBjn2`twNIM!Wgw;8bP*pwnDne>H>Q`URFI zsu|;XRF)af#}AEGyZ1b4S!UY1i*bh;P#=z>y%PcZ)&%dFMX6f?@;qn zy1`J~vu`svUPK-vx}5={(JOa6NSuF?>555t>psLL%j3l)Nkljt!I#CJT_Q5xa*MQ7 z@K&m(BPCgUB1^#h`Mz4z^_UvNku=zN{QH{WR@iX~FVKA*ckg*f@{k>lz@ zxygz>T*iHRihcC~2kyTF1w*Bx-SOe|de$}95$ABZH_-wBtGkCmQK%n$iEQ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