Skip to content

Commit 8c80e25

Browse files
committed
add attachment tests
1 parent 219bdb5 commit 8c80e25

File tree

7 files changed

+182
-6
lines changed

7 files changed

+182
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.0.0 (September 11, 2020)
2+
3+
* Component uses new `Auth-client` functionality
4+
* All deprecated triggers and actions are deleted
5+
* Code is refactored
16

27
## 1.3.5 (August 21, 2020)
38

lib/helpers/attachment.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ async function downloadFile(url, headers) {
2121
return response.body;
2222
}
2323

24-
// eslint-disable-next-line max-len
2524
exports.prepareBinaryData = async function prepareBinaryData(msg, configuration, emitter) {
2625
let binField;
2726
let attachment;
@@ -63,7 +62,6 @@ exports.prepareBinaryData = async function prepareBinaryData(msg, configuration,
6362
return binField;
6463
};
6564

66-
// eslint-disable-next-line max-len
6765
exports.getAttachment = async function getAttachment(configuration, objectContent, emitter) {
6866
const objectFields = await callJSForceMethod.call(emitter, configuration, 'getObjectFieldsMetaData');
6967

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "salesforce-component",
3-
"version": "1.3.5",
3+
"version": "2.0.0",
44
"description": "elastic.io component that connects to Salesforce API (node.js)",
55
"main": "index.js",
66
"scripts": {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint-disable no-return-assign */
2+
const fs = require('fs');
3+
const logger = require('@elastic.io/component-logger')();
4+
const { expect } = require('chai');
5+
const nock = require('nock');
6+
const { prepareBinaryData } = require('../../lib/helpers/attachment');
7+
8+
describe('attachment helper test', async () => {
9+
const secretId = 'secretId';
10+
let configuration;
11+
let secret;
12+
13+
before(async () => {
14+
if (fs.existsSync('.env')) {
15+
// eslint-disable-next-line global-require
16+
require('dotenv').config();
17+
}
18+
process.env.ELASTICIO_API_URI = 'https://app.example.io';
19+
process.env.ELASTICIO_WORKSPACE_ID = 'workspaceId';
20+
secret = {
21+
data: {
22+
attributes: {
23+
credentials: {
24+
access_token: process.env.ACCESS_TOKEN,
25+
instance_url: process.env.INSTANCE_URL,
26+
},
27+
},
28+
},
29+
};
30+
31+
configuration = {
32+
secretId,
33+
sobject: 'Document',
34+
};
35+
});
36+
describe('prepareBinaryData test', async () => {
37+
it('should discard attachment utilizeAttachment:false', async () => {
38+
nock(process.env.ELASTICIO_API_URI)
39+
.get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`)
40+
.reply(200, secret);
41+
const msg = {
42+
body: {
43+
Name: 'TryTest',
44+
},
45+
attachments: {
46+
'Fox.jpeg': {
47+
'content-type': 'image/jpeg',
48+
size: 126564,
49+
url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Everest_kalapatthar.jpg/800px-Everest_kalapatthar.jpg',
50+
},
51+
},
52+
};
53+
await prepareBinaryData(msg, { ...configuration, utilizeAttachment: false }, { logger });
54+
expect(msg.body.Name).to.eql('TryTest');
55+
expect(Object.prototype.hasOwnProperty.call(msg.body, 'Body')).to.eql(false);
56+
});
57+
58+
it('should upload attachment utilizeAttachment:true', async () => {
59+
nock(process.env.ELASTICIO_API_URI)
60+
.get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`)
61+
.reply(200, secret);
62+
const msg = {
63+
body: {
64+
Name: 'TryTest',
65+
},
66+
attachments: {
67+
'Fox.jpeg': {
68+
'content-type': 'image/jpeg',
69+
size: 126564,
70+
url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Everest_kalapatthar.jpg/800px-Everest_kalapatthar.jpg',
71+
},
72+
},
73+
};
74+
await prepareBinaryData(msg, { ...configuration, utilizeAttachment: true }, { logger });
75+
expect(msg.body.Name).to.eql('TryTest');
76+
expect(msg.body.ContentType).to.eql('image/jpeg');
77+
expect(Object.prototype.hasOwnProperty.call(msg.body, 'Body')).to.eql(true);
78+
});
79+
});
80+
});

spec/helpers/attachment.spec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
});

verifyCredentials.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ const { callJSForceMethod } = require('./lib/helpers/wrapper');
22

33
module.exports = async function verify(credentials) {
44
try {
5-
this.logger.info('Incoming credentials: %j', credentials);
5+
this.logger.trace('Incoming credentials: %j', credentials);
6+
this.logger.info('Going to make request describeGlobal() for verifying credentials...');
67
const result = await callJSForceMethod.call(this, credentials, 'describeGlobal');
7-
this.logger.info('Credentials are valid, sobjects count: %s', result.sobjects.length);
8+
this.logger.info('Credentials are valid, it was found sobjects count: %s', result.sobjects.length);
89
return { verified: true };
910
} catch (e) {
1011
this.logger.error(e);

0 commit comments

Comments
 (0)