Skip to content

Commit 45a6daa

Browse files
refactoring
1 parent b19f8da commit 45a6daa

File tree

3 files changed

+114
-111
lines changed

3 files changed

+114
-111
lines changed

test/envelopeTests.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
1-
const settings = require('../config/appsettings.json');
1+
const fs = require('fs');
2+
const path = require('path');
23
const chai = require('chai');
34
const chaiExclude = require('chai-exclude');
45
const expect = chai.expect;
56
const should = chai.should();
6-
const { sendEnvelopeForEmbeddedSigning, makeEnvelope: makeEnvelopeForEmbeddedSigning, makeRecipientViewRequest } = require('../embeddedSigning');
7-
const { sendEnvelope, makeEnvelope: makeEnvelopeForSigningViaEmail, document1 } = require('../lib/eSignature/examples/signingViaEmail')
8-
const fs = require('fs');
9-
const path = require('path');
10-
const { authenticate, areEqual } = require('./testHelpers');
11-
const { signerClientId, pingUrl, returnUrl, TEST_PDF_FILE, TEST_DOCX_FILE, BASE_PATH, CC_EMAIL, CC_NAME } = require('./constants')
7+
8+
const settings = require('../config/appsettings.json');
9+
const {
10+
sendEnvelopeForEmbeddedSigning,
11+
makeEnvelope: makeEnvelopeForEmbeddedSigning,
12+
makeRecipientViewRequest
13+
} = require('../embeddedSigning');
14+
const {
15+
sendEnvelope,
16+
makeEnvelope: makeEnvelopeForSigningViaEmail,
17+
document1
18+
} = require('../lib/eSignature/examples/signingViaEmail')
19+
const { TEST_TIMEOUT_MS, authenticate } = require('./testHelpers');
20+
21+
const {
22+
signerClientId,
23+
pingUrl,
24+
returnUrl,
25+
TEST_PDF_FILE,
26+
TEST_DOCX_FILE,
27+
BASE_PATH,
28+
CC_EMAIL,
29+
CC_NAME
30+
} = require('./constants')
1231

1332
chai.use(chaiExclude);
1433

1534
let ACCOUNT_ID;
1635
let ACCESS_TOKEN;
1736

18-
describe ('envelopeTests', function() {
37+
describe ('EnvelopesApi tests:', function() {
1938
before(async function() {
20-
this.timeout(0);
39+
this.timeout(TEST_TIMEOUT_MS);
2140

2241
const { accountId, accessToken } = await authenticate();
2342

43+
should.exist(accountId);
44+
should.exist(accessToken);
45+
2446
ACCOUNT_ID = accountId;
2547
ACCESS_TOKEN = accessToken;
2648
});
2749

2850
it('sendEnvelopeForEmbeddedSigning method should create an envelope and a recipients view for the envelope if correct data is provided', async function() {
29-
this.timeout(0);
51+
this.timeout(TEST_TIMEOUT_MS);
3052

3153
const envelopeArgs = {
3254
signerEmail: settings.signerEmail,
@@ -50,7 +72,7 @@ describe ('envelopeTests', function() {
5072
});
5173

5274
it('makeEnvelope method of embeddedSigning example should create the correct envelope definition if correct data is provided', async function() {
53-
this.timeout(0);
75+
this.timeout(TEST_TIMEOUT_MS);
5476

5577
const envelopeArgs = {
5678
signerEmail: settings.signerEmail,
@@ -101,7 +123,7 @@ describe ('envelopeTests', function() {
101123
});
102124

103125
it('makeRecipientView method should create the correct recipient view request url if correct data is provided', async function() {
104-
this.timeout(0);
126+
this.timeout(TEST_TIMEOUT_MS);
105127

106128
const envelopeArgs = {
107129
signerEmail: settings.signerEmail,
@@ -128,7 +150,7 @@ describe ('envelopeTests', function() {
128150
});
129151

130152
it('signViaEmail method creates the envelope and sends it via email when correct data is provided', async function() {
131-
this.timeout(0);
153+
this.timeout(TEST_TIMEOUT_MS);
132154

133155
const envelopeArgs = {
134156
signerEmail: settings.signerEmail,
@@ -152,7 +174,7 @@ describe ('envelopeTests', function() {
152174
});
153175

154176
it('makeEnvelope method of signViaEmail example should create the correct envelope definition if correct data is provided', async function() {
155-
this.timeout(0);
177+
this.timeout(TEST_TIMEOUT_MS);
156178

157179
const document1Text = `
158180
<!DOCTYPE html>
@@ -254,7 +276,7 @@ describe ('envelopeTests', function() {
254276
});
255277

256278
it('document1 should return correct html document if correct data is provided', async function() {
257-
this.timeout(0);
279+
this.timeout(TEST_TIMEOUT_MS);
258280

259281
const envelopeArgs = {
260282
signerEmail: settings.signerEmail,

test/templateTests.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
1-
const settings = require('../config/appsettings.json');
1+
const fs = require('fs');
22
const chai = require('chai');
33
const expect = chai.expect;
44
const should = chai.should();
5-
const fs = require('fs');
5+
6+
const settings = require('../config/appsettings.json');
67
const { createTemplate, makeTemplate } = require('../lib/eSignature/examples/createTemplate');
7-
const { sendEnvelopeFromTemplate, makeEnvelope: makeEnvelopeForUsingTemplate } = require('../lib/eSignature/examples/useTemplate');
8-
const { addDocToTemplate, makeEnvelope: makeEnvelopeForAddingDoc, document1: document1ForAddingDoc, makeRecipientViewRequest: makeRecipientViewRequestForAddingDoc } = require('../lib/eSignature/examples/addDocToTemplate')
8+
const {
9+
sendEnvelopeFromTemplate,
10+
makeEnvelope: makeEnvelopeForUsingTemplate
11+
} = require('../lib/eSignature/examples/useTemplate');
12+
const {
13+
addDocToTemplate,
14+
makeEnvelope: makeEnvelopeForAddingDoc,
15+
document1: document1ForAddingDoc,
16+
makeRecipientViewRequest: makeRecipientViewRequestForAddingDoc
17+
} = require('../lib/eSignature/examples/addDocToTemplate')
918
const { setTabValues, makeEnvelope: makeEnvelopeForSetTabValues } = require('../lib/eSignature/examples/setTabValues')
10-
const { authenticate, areEqual } = require('./testHelpers');
11-
const { TEST_TEMPLATE_PDF_FILE, TEST_TEMPLATE_DOCX_FILE, TEMPLATE_NAME, BASE_PATH, signerClientId, returnUrl, pingUrl, CC_EMAIL, CC_NAME } = require('./constants')
19+
const { TEST_TIMEOUT_MS, authenticate, areEqual } = require('./testHelpers');
20+
21+
const {
22+
TEST_TEMPLATE_PDF_FILE,
23+
TEST_TEMPLATE_DOCX_FILE,
24+
TEMPLATE_NAME,
25+
BASE_PATH,
26+
signerClientId,
27+
returnUrl,
28+
pingUrl,
29+
CC_EMAIL,
30+
CC_NAME
31+
} = require('./constants')
1232

1333
let ACCOUNT_ID;
1434
let ACCESS_TOKEN;
1535
let TEMPLATE_ID;
1636

17-
describe ('templateTests', function() {
37+
describe ('TemplatesApi tests:', function() {
1838
before(async function() {
19-
this.timeout(0);
39+
this.timeout(TEST_TIMEOUT_MS);
2040

2141
const { accountId, accessToken } = await authenticate();
22-
42+
43+
should.exist(accountId);
44+
should.exist(accessToken);
45+
2346
ACCOUNT_ID = accountId;
2447
ACCESS_TOKEN = accessToken;
2548
});
2649

2750
it('createTemplate method should create the correct template definition if correct data is provided', async function() {
28-
this.timeout(0);
51+
this.timeout(TEST_TIMEOUT_MS);
2952

3053
const newTemplateName = `${TEMPLATE_NAME}_${Date.now()}`;
3154
const args = {
@@ -47,7 +70,7 @@ describe ('templateTests', function() {
4770
});
4871

4972
it('makeTemplate method of createTemplate example should create correct template definition if correct data is provided', async function() {
50-
this.timeout(0);
73+
this.timeout(TEST_TIMEOUT_MS);
5174

5275
const args = {
5376
templateName: TEMPLATE_NAME,
@@ -236,7 +259,7 @@ describe ('templateTests', function() {
236259
});
237260

238261
it('useTemplate method should create the envelope with template if correct data is provided', async function() {
239-
this.timeout(0);
262+
this.timeout(TEST_TIMEOUT_MS);
240263

241264
const envelopeArgs = {
242265
templateId: TEMPLATE_ID,
@@ -258,7 +281,7 @@ describe ('templateTests', function() {
258281
});
259282

260283
it('makeEnvelope method of useTemplate example should create correct envelope definition if correct data is provided', async function() {
261-
this.timeout(0);
284+
this.timeout(TEST_TIMEOUT_MS);
262285

263286
const envelopeArgs = {
264287
templateId: TEMPLATE_ID,
@@ -292,7 +315,7 @@ describe ('templateTests', function() {
292315
});
293316

294317
it('addDocToTemplate method should correctly add document to a template if correct data is provided', async function() {
295-
this.timeout(0);
318+
this.timeout(TEST_TIMEOUT_MS);
296319

297320
const envelopeArgs = {
298321
templateId: TEMPLATE_ID,
@@ -320,7 +343,7 @@ describe ('templateTests', function() {
320343
});
321344

322345
it('makeEnvelope method of addDocToTemplate should create the correct envelope definition if correct data is provided', async function() {
323-
this.timeout(0);
346+
this.timeout(TEST_TIMEOUT_MS);
324347

325348
const item = 'Item';
326349
const quantity = '5';
@@ -451,8 +474,8 @@ describe ('templateTests', function() {
451474
});
452475

453476
it('document1 method of addDocToTemplate example should return correct HTML document if correct data is provided', async function() {
454-
this.timeout(0);
455-
477+
this.timeout(TEST_TIMEOUT_MS);
478+
456479
const item = 'Item';
457480
const quantity = '5';
458481
const envelopeArgs = {
@@ -497,7 +520,7 @@ describe ('templateTests', function() {
497520
});
498521

499522
it('makeRecipientView method of addDocToTemplate example should create the correct recipient view request url if correct data is provided', async function() {
500-
this.timeout(0);
523+
this.timeout(TEST_TIMEOUT_MS);
501524

502525
const envelopeArgs = {
503526
signerEmail: settings.signerEmail,
@@ -524,7 +547,7 @@ describe ('templateTests', function() {
524547
});
525548

526549
it('setTabValues method should correctly set the tab values of template if correct data is provided', async function() {
527-
this.timeout(0);
550+
this.timeout(TEST_TIMEOUT_MS);
528551

529552
const envelopeArgs = {
530553
signerEmail: settings.signerEmail,
@@ -547,7 +570,7 @@ describe ('templateTests', function() {
547570
});
548571

549572
it('makeEnvelope method of setTabValues example should create correct envelope definition if correct data is provided', async function() {
550-
this.timeout(0);
573+
this.timeout(TEST_TIMEOUT_MS);
551574

552575
const envelopeArgs = {
553576
signerEmail: settings.signerEmail,

test/testHelpers.js

Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,48 @@ const fs = require('fs');
33
const path = require('path');
44

55
const settings = require('../config/appsettings.json');
6-
const { REDIRECT_URI, BASE_PATH, OAUTH_BASE_PATH, PRIVATE_KEY_FILENAME, EXPIRES_IN, signerClientId, pingUrl, returnUrl, TEST_PDF_FILE, SCOPES } = require('./constants');
6+
const { REDIRECT_URI, BASE_PATH, OAUTH_BASE_PATH, PRIVATE_KEY_FILENAME, EXPIRES_IN, SCOPES } = require('./constants');
7+
8+
const TEST_TIMEOUT_MS = 10000;
79

810
const apiClient = new docusign.ApiClient({
911
basePath: BASE_PATH,
1012
oAuthBasePath: OAUTH_BASE_PATH
1113
});
1214

1315
const authenticate = async () => {
14-
// IMPORTANT NOTE:
15-
// the first time you ask for a JWT access token, you should grant access by making the following call
16-
// get DocuSign OAuth authorization url:
16+
try {
17+
const privateKeyFile = fs.readFileSync(path.resolve(__dirname, PRIVATE_KEY_FILENAME));
18+
const res = await apiClient.requestJWTUserToken(settings.dsJWTClientId, settings.impersonatedUserGuid, SCOPES, privateKeyFile, EXPIRES_IN);
1719

18-
const authorizationUrl = apiClient.getJWTUri(settings.dsJWTClientId, REDIRECT_URI, OAUTH_BASE_PATH);
19-
// open DocuSign OAuth authorization url in the browser, login and grant access
20-
console.log('OAuth authorization url:', authorizationUrl);
21-
// END OF NOTE
22-
23-
const privateKeyFile = fs.readFileSync(path.resolve(__dirname, PRIVATE_KEY_FILENAME));
24-
const res = await apiClient.requestJWTUserToken(settings.dsJWTClientId, settings.impersonatedUserGuid, SCOPES, privateKeyFile, EXPIRES_IN);
25-
26-
const accessToken = res.body.access_token;
27-
apiClient.addDefaultHeader('Authorization', `Bearer ${accessToken}`);
28-
const userInfo = await apiClient.getUserInfo(accessToken);
29-
30-
const accountId = userInfo.accounts[0].accountId;
31-
const baseUri = userInfo.accounts[0].baseUri;
32-
const accountDomain = baseUri.split('/v2');
33-
apiClient.setBasePath(`${accountDomain[0]}/restapi`);
20+
const accessToken = res.body.access_token;
21+
apiClient.addDefaultHeader('Authorization', `Bearer ${accessToken}`);
22+
const userInfo = await apiClient.getUserInfo(accessToken);
3423

35-
return { accessToken, accountId, baseUri };
24+
const accountId = userInfo.accounts[0].accountId;
25+
26+
return { accessToken, accountId };
27+
} catch(error) {
28+
const body = error.response && error.response.body;
29+
30+
// Determine the source of the error.
31+
if (body) {
32+
if (body.error && body.error === 'consent_required') {
33+
// The first time you ask for a JWT access token.
34+
// get DocuSign OAuth authorization url:
35+
const authorizationUrl = apiClient.getJWTUri(settings.dsJWTClientId, REDIRECT_URI, OAUTH_BASE_PATH);
36+
37+
// open DocuSign OAuth authorization url in the browser, login and grant access
38+
const consentMessage = `You should grant access by making the following call: ${authorizationUrl}`;
39+
console.log(consentMessage);
40+
throw new Error(consentMessage);
41+
} else {
42+
// Consent has been granted. Show status code for DocuSign API error.
43+
throw new Error(`\nAPI problem: Status code ${error.response.status}, message body:
44+
${JSON.stringify(body, null, 4)}\n\n`);
45+
}
46+
}
47+
}
3648
};
3749

3850
const areEqual = (obj1, obj2) => {
@@ -41,58 +53,4 @@ const areEqual = (obj1, obj2) => {
4153
|| JSON.parse(JSON.stringify(obj1)) == JSON.parse(JSON.stringify(obj2));
4254
}
4355

44-
const getToken = async function _getToken() {
45-
// Data used
46-
// dsConfig.dsClientId
47-
// dsConfig.impersonatedUserGuid
48-
// dsConfig.privateKey
49-
// dsConfig.dsOauthServer
50-
const privateKeyFile = fs.readFileSync(path.resolve(__dirname, PRIVATE_KEY_FILENAME));
51-
52-
const jwtLifeSec = 10 * 60, // requested lifetime for the JWT is 10 min
53-
dsApi = new docusign.ApiClient();
54-
dsApi.setOAuthBasePath(OAUTH_BASE_PATH); // it should be domain only.
55-
const results = await dsApi.requestJWTUserToken(settings.dsClientId,
56-
settings.impersonatedUserGuid, SCOPES, privateKeyFile,
57-
jwtLifeSec);
58-
59-
return {
60-
accessToken: results.body.access_token,
61-
};
62-
}
63-
64-
const getUserInfo = async function _getUserInfo(accessToken){
65-
// Data used:
66-
// dsConfig.targetAccountId
67-
// dsConfig.dsOauthServer
68-
// DsJwtAuth.accessToken
69-
70-
const dsApi = new docusign.ApiClient()
71-
, targetAccountId = settings.targetAccountId
72-
, baseUriSuffix = '/restapi';
73-
74-
dsApi.setOAuthBasePath(OAUTH_BASE_PATH); // it have to be domain name
75-
const results = await dsApi.getUserInfo(accessToken);
76-
77-
let accountInfo;
78-
if (!Boolean(targetAccountId)) {
79-
// find the default account
80-
accountInfo = results.accounts.find(account =>
81-
account.isDefault === "true");
82-
} else {
83-
// find the matching account
84-
accountInfo = results.accounts.find(account => account.accountId == targetAccountId);
85-
}
86-
if (typeof accountInfo === 'undefined') {
87-
throw new Error (`Target account ${targetAccountId} not found!`);
88-
}
89-
90-
const accountId = accountInfo.accountId;
91-
const basePath = accountInfo.baseUri + baseUriSuffix;
92-
return {
93-
accountId,
94-
basePath
95-
}
96-
}
97-
98-
module.exports = { authenticate, areEqual, getToken, getUserInfo }
56+
module.exports = { TEST_TIMEOUT_MS, authenticate, areEqual }

0 commit comments

Comments
 (0)