|
| 1 | +const { expect } = require('chai'); |
| 2 | +const nock = require('nock'); |
| 3 | +const logger = require('@elastic.io/component-logger')(); |
| 4 | +const common = require('../../lib/common.js'); |
| 5 | +const testCommon = require('../common.js'); |
| 6 | +const { prepareBinaryData, getAttachment } = require('../../lib/helpers/attachment'); |
| 7 | + |
| 8 | +describe('attachment helper', () => { |
| 9 | + nock(testCommon.instanceUrl, { encodedQueryParams: true }) |
| 10 | + .get(`/services/data/v${common.globalConsts.SALESFORCE_API_VERSION}/sobjects/Document/describe`) |
| 11 | + .times(2) |
| 12 | + .reply(200, { |
| 13 | + fields: [ |
| 14 | + { |
| 15 | + name: 'Body', |
| 16 | + type: 'base64', |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'ContentType', |
| 20 | + }, |
| 21 | + ], |
| 22 | + }); |
| 23 | + const configuration = { |
| 24 | + secretId: testCommon.secretId, |
| 25 | + sobject: 'Document', |
| 26 | + }; |
| 27 | + |
| 28 | + describe('prepareBinaryData test', () => { |
| 29 | + nock(process.env.ELASTICIO_API_URI) |
| 30 | + .get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${testCommon.secretId}`) |
| 31 | + .times(2) |
| 32 | + .reply(200, testCommon.secret); |
| 33 | + |
| 34 | + it('should upload attachment utilizeAttachment:true', async () => { |
| 35 | + const msg = { |
| 36 | + body: { |
| 37 | + Name: 'Attachment', |
| 38 | + }, |
| 39 | + attachments: { |
| 40 | + 'Fox.jpeg': { |
| 41 | + 'content-type': 'image/jpeg', |
| 42 | + size: 126564, |
| 43 | + url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Everest_kalapatthar.jpg/800px-Everest_kalapatthar.jpg', |
| 44 | + }, |
| 45 | + }, |
| 46 | + }; |
| 47 | + await prepareBinaryData(msg, { ...configuration, utilizeAttachment: true }, { logger }); |
| 48 | + expect(msg.body.Name).to.eql('Attachment'); |
| 49 | + expect(msg.body.ContentType).to.eql('image/jpeg'); |
| 50 | + expect(Object.prototype.hasOwnProperty.call(msg.body, 'Body')).to.eql(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should discard attachment utilizeAttachment:false', async () => { |
| 54 | + const msg = { |
| 55 | + body: { |
| 56 | + Name: 'Without Attachment', |
| 57 | + }, |
| 58 | + attachments: { |
| 59 | + 'Fox.jpeg': { |
| 60 | + 'content-type': 'image/jpeg', |
| 61 | + size: 126564, |
| 62 | + url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Everest_kalapatthar.jpg/800px-Everest_kalapatthar.jpg', |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + await prepareBinaryData(msg, configuration, { logger }); |
| 67 | + expect(msg.body.Name).to.eql('Without Attachment'); |
| 68 | + expect(Object.prototype.hasOwnProperty.call(msg.body, 'Body')).to.eql(false); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('getAttachment test', async () => { |
| 73 | + it('should getAttachment', async () => { |
| 74 | + nock(process.env.ELASTICIO_API_URI) |
| 75 | + .get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${testCommon.secretId}`) |
| 76 | + .times(2) |
| 77 | + .reply(200, testCommon.secret); |
| 78 | + nock(testCommon.instanceUrl) |
| 79 | + .get('/services/data/v46.0/sobjects/Attachment/00P2R00001DYjNVUA1/Body') |
| 80 | + .reply(200, { hello: 'world' }); |
| 81 | + const objectContent = { |
| 82 | + Body: '/services/data/v46.0/sobjects/Attachment/00P2R00001DYjNVUA1/Body', |
| 83 | + }; |
| 84 | + const result = await getAttachment(configuration, objectContent, { logger }); |
| 85 | + expect(result).to.eql({ |
| 86 | + attachment: { |
| 87 | + url: 'http://file.storage.server/file', |
| 88 | + }, |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments