Skip to content

Commit 219bdb5

Browse files
committed
add wrapper tests
1 parent cf5f72f commit 219bdb5

File tree

5 files changed

+319
-167
lines changed

5 files changed

+319
-167
lines changed

lib/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports.globalConsts = {
22
SALESFORCE_API_VERSION: process.env.SALESFORCE_API_VERSION || '46.0',
3+
REFRESH_TOKEN_RETRIES: process.env.REFRESH_TOKEN_RETRIES ? parseInt(process.env.REFRESH_TOKEN_RETRIES, 10) : 10,
34
};

lib/helpers/wrapper.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
/* eslint-disable no-await-in-loop */
22
const { SalesForceClient } = require('../salesForceClient');
33
const { getCredentials, refreshToken } = require('../helpers/oauth2Helper');
4+
const { REFRESH_TOKEN_RETRIES } = require('../common.js').globalConsts;
45

56
let client;
67

78
exports.callJSForceMethod = async function callJSForceMethod(configuration, method, options) {
8-
this.logger.trace('Incoming configuration: %j', configuration);
9+
this.logger.info('Preparing SalesForce Client...');
910
let accessToken;
1011
let instanceUrl;
1112
const { secretId } = configuration;
1213
if (secretId) {
14+
this.logger.info('Fetching credentials by secretId');
1315
const credentials = await getCredentials(this, secretId);
14-
this.logger.trace('Fetched credentials: %j', credentials);
1516
accessToken = credentials.access_token;
1617
instanceUrl = credentials.instance_url;
1718
} else {
19+
this.logger.info('Fetching credentials from configuration');
1820
accessToken = configuration.oauth.access_token;
1921
instanceUrl = configuration.oauth.instance_url;
2022
}
2123
let result;
2224
let isSuccess = false;
23-
let iteration = 3;
25+
let iteration = REFRESH_TOKEN_RETRIES;
2426
do {
2527
iteration -= 1;
2628
try {
27-
this.logger.info('Iteration: %s', iteration);
29+
this.logger.info('Iteration: %s', REFRESH_TOKEN_RETRIES - iteration);
2830
const cfg = {
2931
...configuration,
3032
access_token: accessToken,
3133
instance_url: instanceUrl,
3234
};
3335
if (!client || Object.entries(client.configuration).toString() !== Object.entries(cfg).toString()) {
34-
this.logger.info('Try to create connection', iteration);
36+
this.logger.info('Try to create SalesForce Client', REFRESH_TOKEN_RETRIES - iteration);
37+
this.logger.trace('Creating SalesForce Client with configuration: %j', cfg);
3538
client = new SalesForceClient(this, cfg);
36-
this.logger.info('Connection is created');
39+
this.logger.info('SalesForce Client is created');
3740
}
38-
this.logger.info('Trying to call method %s with options: %j', method, options);
41+
this.logger.info('Trying to call method %s', method);
42+
this.logger.debug('Trying to call method %s with options: %j', method, options);
3943
result = await client[method](options);
40-
this.logger.trace('Execution result: %j', result);
44+
this.logger.debug('Execution result: %j', result);
4145
isSuccess = true;
4246
this.logger.info('Method is executed successfully');
4347
break;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 { callJSForceMethod } = require('../../lib/helpers/wrapper');
7+
8+
describe('wrapper helper test', async () => {
9+
const secretId = 'secretId';
10+
let configuration;
11+
let secret;
12+
let invalidSecret;
13+
14+
before(async () => {
15+
if (fs.existsSync('.env')) {
16+
// eslint-disable-next-line global-require
17+
require('dotenv').config();
18+
}
19+
process.env.ELASTICIO_API_URI = 'https://app.example.io';
20+
process.env.ELASTICIO_API_USERNAME = 'user';
21+
process.env.ELASTICIO_API_KEY = 'apiKey';
22+
process.env.ELASTICIO_WORKSPACE_ID = 'workspaceId';
23+
secret = {
24+
data: {
25+
attributes: {
26+
credentials: {
27+
access_token: process.env.ACCESS_TOKEN,
28+
instance_url: process.env.INSTANCE_URL,
29+
},
30+
},
31+
},
32+
};
33+
invalidSecret = {
34+
data: {
35+
attributes: {
36+
credentials: {
37+
access_token: 'access_token',
38+
instance_url: process.env.INSTANCE_URL,
39+
},
40+
},
41+
},
42+
};
43+
44+
configuration = {
45+
secretId,
46+
sobject: 'Contact',
47+
};
48+
});
49+
50+
it('should succeed call describe method, credentials from config', async () => {
51+
const cfg = {
52+
sobject: 'Contact',
53+
oauth: {
54+
access_token: process.env.ACCESS_TOKEN,
55+
instance_url: process.env.INSTANCE_URL,
56+
},
57+
};
58+
const result = await callJSForceMethod.call({ logger }, cfg, 'describe');
59+
expect(result.name).to.eql('Contact');
60+
});
61+
62+
it('should succeed call describe method', async () => {
63+
nock(process.env.ELASTICIO_API_URI)
64+
.get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`)
65+
.reply(200, secret);
66+
const result = await callJSForceMethod.call({ logger }, configuration, 'describe');
67+
expect(result.name).to.eql('Contact');
68+
});
69+
70+
it('should refresh token and succeed call describe method', async () => {
71+
nock(process.env.ELASTICIO_API_URI)
72+
.get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`)
73+
.reply(200, invalidSecret)
74+
.post(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}/refresh`)
75+
.reply(200, secret);
76+
const result = await callJSForceMethod.call({ logger }, configuration, 'describe');
77+
expect(result.name).to.eql('Contact');
78+
});
79+
});

0 commit comments

Comments
 (0)