1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.MockTestOrgData = exports.StreamingMockCometClient = exports.StreamingMockCometSubscription = exports.StreamingMockSubscriptionCall = exports.shouldThrow = exports.unexpectedResult = exports.testSetup = exports.restoreContext = exports.stubContext = exports.instantiateContext = void 0;
|
10 | const crypto_1 = require("crypto");
|
11 | const events_1 = require("events");
|
12 | const os_1 = require("os");
|
13 | const path_1 = require("path");
|
14 | const kit_1 = require("@salesforce/kit");
|
15 | const ts_sinon_1 = require("@salesforce/ts-sinon");
|
16 | const ts_types_1 = require("@salesforce/ts-types");
|
17 | const configAggregator_1 = require("./config/configAggregator");
|
18 | const configFile_1 = require("./config/configFile");
|
19 | const connection_1 = require("./connection");
|
20 | const crypto_2 = require("./crypto");
|
21 | const logger_1 = require("./logger");
|
22 | const messages_1 = require("./messages");
|
23 | const sfdxError_1 = require("./sfdxError");
|
24 | const sfdxProject_1 = require("./sfdxProject");
|
25 | const streamingClient_1 = require("./status/streamingClient");
|
26 | const uniqid = () => {
|
27 | return crypto_1.randomBytes(16).toString('hex');
|
28 | };
|
29 | function getTestLocalPath(uid) {
|
30 | return path_1.join(os_1.tmpdir(), uid, 'sfdx_core', 'local');
|
31 | }
|
32 | function getTestGlobalPath(uid) {
|
33 | return path_1.join(os_1.tmpdir(), uid, 'sfdx_core', 'global');
|
34 | }
|
35 | function retrieveRootPathSync(isGlobal, uid = uniqid()) {
|
36 | return isGlobal ? getTestGlobalPath(uid) : getTestLocalPath(uid);
|
37 | }
|
38 |
|
39 | async function retrieveRootPath(isGlobal, uid = uniqid()) {
|
40 | return retrieveRootPathSync(isGlobal, uid);
|
41 | }
|
42 | function defaultFakeConnectionRequest() {
|
43 | return Promise.resolve(ts_types_1.ensureAnyJson({ records: [] }));
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const instantiateContext = (sinon) => {
|
68 | if (!sinon) {
|
69 | try {
|
70 | sinon = require('sinon');
|
71 | }
|
72 | catch (e) {
|
73 | throw new Error('The package sinon was not found. Add it to your package.json and pass it in to testSetup(sinon.sandbox)');
|
74 | }
|
75 | }
|
76 |
|
77 |
|
78 | messages_1.Messages.importMessagesDirectory(path_1.join(__dirname));
|
79 |
|
80 | const defaultSandbox = sinon.createSandbox();
|
81 | const testContext = {
|
82 | SANDBOX: defaultSandbox,
|
83 | SANDBOXES: {
|
84 | DEFAULT: defaultSandbox,
|
85 | CONFIG: sinon.createSandbox(),
|
86 | PROJECT: sinon.createSandbox(),
|
87 | CRYPTO: sinon.createSandbox(),
|
88 | CONNECTION: sinon.createSandbox(),
|
89 | },
|
90 | TEST_LOGGER: new logger_1.Logger({
|
91 | name: 'SFDX_Core_Test_Logger',
|
92 | }).useMemoryLogging(),
|
93 | id: uniqid(),
|
94 | uniqid,
|
95 | configStubs: {},
|
96 |
|
97 | localPathRetriever: async (uid) => getTestLocalPath(uid),
|
98 | localPathRetrieverSync: getTestLocalPath,
|
99 |
|
100 | globalPathRetriever: async (uid) => getTestGlobalPath(uid),
|
101 | globalPathRetrieverSync: getTestGlobalPath,
|
102 | rootPathRetriever: retrieveRootPath,
|
103 | rootPathRetrieverSync: retrieveRootPathSync,
|
104 | fakeConnectionRequest: defaultFakeConnectionRequest,
|
105 | getConfigStubContents(name, group) {
|
106 | const stub = this.configStubs[name];
|
107 | if (stub && stub.contents) {
|
108 | if (group && stub.contents[group]) {
|
109 | return ts_types_1.ensureJsonMap(stub.contents[group]);
|
110 | }
|
111 | else {
|
112 | return stub.contents;
|
113 | }
|
114 | }
|
115 | return {};
|
116 | },
|
117 | setConfigStubContents(name, value) {
|
118 | if (ts_types_1.ensureString(name) && ts_types_1.isJsonMap(value)) {
|
119 | this.configStubs[name] = value;
|
120 | }
|
121 | },
|
122 | inProject(inProject = true) {
|
123 | testContext.SANDBOXES.PROJECT.restore();
|
124 | if (inProject) {
|
125 | testContext.SANDBOXES.PROJECT.stub(sfdxProject_1.SfdxProject, 'resolveProjectPath').callsFake(() => testContext.localPathRetriever(testContext.id));
|
126 | testContext.SANDBOXES.PROJECT.stub(sfdxProject_1.SfdxProject, 'resolveProjectPathSync').callsFake(() => testContext.localPathRetrieverSync(testContext.id));
|
127 | }
|
128 | else {
|
129 | testContext.SANDBOXES.PROJECT.stub(sfdxProject_1.SfdxProject, 'resolveProjectPath').rejects(new sfdxError_1.SfdxError('InvalidProjectWorkspace'));
|
130 | testContext.SANDBOXES.PROJECT.stub(sfdxProject_1.SfdxProject, 'resolveProjectPathSync').throws(new sfdxError_1.SfdxError('InvalidProjectWorkspace'));
|
131 | }
|
132 | },
|
133 | };
|
134 | return testContext;
|
135 | };
|
136 | exports.instantiateContext = instantiateContext;
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | const stubContext = (testContext) => {
|
162 |
|
163 | ts_sinon_1.stubMethod(testContext.SANDBOX, logger_1.Logger, 'child').returns(Promise.resolve(testContext.TEST_LOGGER));
|
164 | ts_sinon_1.stubMethod(testContext.SANDBOX, logger_1.Logger, 'childFromRoot').returns(testContext.TEST_LOGGER);
|
165 | testContext.inProject(true);
|
166 | testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile, 'resolveRootFolder').callsFake((isGlobal) => testContext.rootPathRetriever(isGlobal, testContext.id));
|
167 | testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile, 'resolveRootFolderSync').callsFake((isGlobal) => testContext.rootPathRetrieverSync(isGlobal, testContext.id));
|
168 | ts_sinon_1.stubMethod(testContext.SANDBOXES.PROJECT, sfdxProject_1.SfdxProjectJson.prototype, 'doesPackageExist').callsFake(() => true);
|
169 | const initStubForRead = (configFile) => {
|
170 | const stub = testContext.configStubs[configFile.constructor.name] || {};
|
171 |
|
172 |
|
173 | configFile.getPath();
|
174 |
|
175 | configFile.hasRead = true;
|
176 | return stub;
|
177 | };
|
178 | const readSync = function (newContents) {
|
179 | const stub = initStubForRead(this);
|
180 | this.setContentsFromObject(newContents || stub.contents || {});
|
181 | return this.getContents();
|
182 | };
|
183 | const read = async function () {
|
184 | const stub = initStubForRead(this);
|
185 | if (stub.readFn) {
|
186 | return await stub.readFn.call(this);
|
187 | }
|
188 | if (stub.retrieveContents) {
|
189 | return readSync.call(this, await stub.retrieveContents.call(this));
|
190 | }
|
191 | else {
|
192 | return readSync.call(this);
|
193 | }
|
194 | };
|
195 |
|
196 |
|
197 | testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile.prototype, 'readSync').callsFake(readSync);
|
198 | testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile.prototype, 'read').callsFake(read);
|
199 | const writeSync = function (newContents) {
|
200 | if (!testContext.configStubs[this.constructor.name]) {
|
201 | testContext.configStubs[this.constructor.name] = {};
|
202 | }
|
203 | const stub = testContext.configStubs[this.constructor.name];
|
204 | if (!stub)
|
205 | return;
|
206 | this.setContents(newContents || this.getContents());
|
207 | stub.contents = this.toObject();
|
208 | };
|
209 | const write = async function (newContents) {
|
210 | if (!testContext.configStubs[this.constructor.name]) {
|
211 | testContext.configStubs[this.constructor.name] = {};
|
212 | }
|
213 | const stub = testContext.configStubs[this.constructor.name];
|
214 | if (!stub)
|
215 | return;
|
216 | if (stub.writeFn) {
|
217 | return await stub.writeFn.call(this, newContents);
|
218 | }
|
219 | if (stub.updateContents) {
|
220 | writeSync.call(this, await stub.updateContents.call(this));
|
221 | }
|
222 | else {
|
223 | writeSync.call(this);
|
224 | }
|
225 | };
|
226 | ts_sinon_1.stubMethod(testContext.SANDBOXES.CONFIG, configFile_1.ConfigFile.prototype, 'writeSync').callsFake(writeSync);
|
227 | ts_sinon_1.stubMethod(testContext.SANDBOXES.CONFIG, configFile_1.ConfigFile.prototype, 'write').callsFake(write);
|
228 | ts_sinon_1.stubMethod(testContext.SANDBOXES.CRYPTO, crypto_2.Crypto.prototype, 'getKeyChain').callsFake(() => Promise.resolve({
|
229 | setPassword: () => Promise.resolve(),
|
230 | getPassword: (data, cb) => cb(undefined, '12345678901234567890123456789012'),
|
231 | }));
|
232 | ts_sinon_1.stubMethod(testContext.SANDBOXES.CONNECTION, connection_1.Connection.prototype, 'isResolvable').resolves(true);
|
233 | ts_sinon_1.stubMethod(testContext.SANDBOXES.CONNECTION, connection_1.Connection.prototype, 'request').callsFake(function (request, options) {
|
234 | if (request === `${this.instanceUrl}/services/data`) {
|
235 | return Promise.resolve([{ version: '42.0' }]);
|
236 | }
|
237 | return testContext.fakeConnectionRequest.call(this, request, options);
|
238 | });
|
239 |
|
240 | testContext.fakeConnectionRequest = defaultFakeConnectionRequest;
|
241 | };
|
242 | exports.stubContext = stubContext;
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | const restoreContext = (testContext) => {
|
263 | testContext.SANDBOX.restore();
|
264 | Object.values(testContext.SANDBOXES).forEach((theSandbox) => theSandbox.restore());
|
265 | testContext.configStubs = {};
|
266 | };
|
267 | exports.restoreContext = restoreContext;
|
268 |
|
269 | const _testSetup = (sinon) => {
|
270 | const testContext = exports.instantiateContext(sinon);
|
271 | beforeEach(() => {
|
272 |
|
273 |
|
274 | delete configAggregator_1.ConfigAggregator.instance;
|
275 | exports.stubContext(testContext);
|
276 | });
|
277 | afterEach(() => {
|
278 | exports.restoreContext(testContext);
|
279 | });
|
280 | return testContext;
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | exports.testSetup = kit_1.once(_testSetup);
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | exports.unexpectedResult = new sfdxError_1.SfdxError('This code was expected to fail', 'UnexpectedResult');
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | async function shouldThrow(f) {
|
339 | await f;
|
340 | throw exports.unexpectedResult;
|
341 | }
|
342 | exports.shouldThrow = shouldThrow;
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | var StreamingMockSubscriptionCall;
|
348 | (function (StreamingMockSubscriptionCall) {
|
349 | StreamingMockSubscriptionCall[StreamingMockSubscriptionCall["CALLBACK"] = 0] = "CALLBACK";
|
350 | StreamingMockSubscriptionCall[StreamingMockSubscriptionCall["ERRORBACK"] = 1] = "ERRORBACK";
|
351 | })(StreamingMockSubscriptionCall = exports.StreamingMockSubscriptionCall || (exports.StreamingMockSubscriptionCall = {}));
|
352 |
|
353 |
|
354 |
|
355 | class StreamingMockCometSubscription extends events_1.EventEmitter {
|
356 | constructor(options) {
|
357 | super();
|
358 | this.options = options;
|
359 | }
|
360 | |
361 |
|
362 |
|
363 |
|
364 |
|
365 | callback(callback) {
|
366 | if (this.options.subscriptionCall === StreamingMockSubscriptionCall.CALLBACK) {
|
367 | setTimeout(() => {
|
368 | callback();
|
369 | super.emit(StreamingMockCometSubscription.SUBSCRIPTION_COMPLETE);
|
370 | }, 0);
|
371 | }
|
372 | }
|
373 | |
374 |
|
375 |
|
376 |
|
377 |
|
378 | errback(callback) {
|
379 | if (this.options.subscriptionCall === StreamingMockSubscriptionCall.ERRORBACK) {
|
380 | const error = this.options.subscriptionErrbackError;
|
381 | if (!error)
|
382 | return;
|
383 | setTimeout(() => {
|
384 | callback(error);
|
385 | super.emit(StreamingMockCometSubscription.SUBSCRIPTION_FAILED);
|
386 | }, 0);
|
387 | }
|
388 | }
|
389 | }
|
390 | exports.StreamingMockCometSubscription = StreamingMockCometSubscription;
|
391 | StreamingMockCometSubscription.SUBSCRIPTION_COMPLETE = 'subscriptionComplete';
|
392 | StreamingMockCometSubscription.SUBSCRIPTION_FAILED = 'subscriptionFailed';
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | class StreamingMockCometClient extends streamingClient_1.CometClient {
|
399 | |
400 |
|
401 |
|
402 |
|
403 |
|
404 | constructor(options) {
|
405 | super();
|
406 | this.options = options;
|
407 | if (!this.options.messagePlaylist) {
|
408 | this.options.messagePlaylist = [{ id: this.options.id }];
|
409 | }
|
410 | }
|
411 | |
412 |
|
413 |
|
414 |
|
415 | addExtension(extension) { }
|
416 | |
417 |
|
418 |
|
419 |
|
420 | disable(label) { }
|
421 | |
422 |
|
423 |
|
424 |
|
425 |
|
426 | handshake(callback) {
|
427 | setTimeout(() => {
|
428 | callback();
|
429 | }, 0);
|
430 | }
|
431 | |
432 |
|
433 |
|
434 |
|
435 | setHeader(name, value) { }
|
436 | |
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 | subscribe(channel, callback) {
|
443 | const subscription = new StreamingMockCometSubscription(this.options);
|
444 | subscription.on('subscriptionComplete', () => {
|
445 | if (!this.options.messagePlaylist)
|
446 | return;
|
447 | Object.values(this.options.messagePlaylist).forEach((message) => {
|
448 | setTimeout(() => {
|
449 | callback(message);
|
450 | }, 0);
|
451 | });
|
452 | });
|
453 | return subscription;
|
454 | }
|
455 | |
456 |
|
457 |
|
458 | disconnect() {
|
459 | return Promise.resolve();
|
460 | }
|
461 | }
|
462 | exports.StreamingMockCometClient = StreamingMockCometClient;
|
463 |
|
464 |
|
465 |
|
466 | class MockTestOrgData {
|
467 | constructor(id = uniqid(), options) {
|
468 | this.testId = id;
|
469 | this.userId = `user_id_${this.testId}`;
|
470 | this.orgId = `${this.testId}`;
|
471 | this.username = (options === null || options === void 0 ? void 0 : options.username) || `admin_${this.testId}@gb.org`;
|
472 | this.loginUrl = `http://login.${this.testId}.salesforce.com`;
|
473 | this.instanceUrl = `http://instance.${this.testId}.salesforce.com`;
|
474 | this.clientId = `${this.testId}/client_id`;
|
475 | this.clientSecret = `${this.testId}/client_secret`;
|
476 | this.authcode = `${this.testId}/authcode`;
|
477 | this.accessToken = `${this.testId}/accessToken`;
|
478 | this.refreshToken = `${this.testId}/refreshToken`;
|
479 | this.redirectUri = `http://${this.testId}/localhost:1717/OauthRedirect`;
|
480 | }
|
481 | createDevHubUsername(username) {
|
482 | this.devHubUsername = username;
|
483 | }
|
484 | makeDevHub() {
|
485 | kit_1.set(this, 'isDevHub', true);
|
486 | }
|
487 | createUser(user) {
|
488 | const userMock = new MockTestOrgData();
|
489 | userMock.username = user;
|
490 | userMock.alias = this.alias;
|
491 | userMock.devHubUsername = this.devHubUsername;
|
492 | userMock.orgId = this.orgId;
|
493 | userMock.loginUrl = this.loginUrl;
|
494 | userMock.instanceUrl = this.instanceUrl;
|
495 | userMock.clientId = this.clientId;
|
496 | userMock.clientSecret = this.clientSecret;
|
497 | userMock.redirectUri = this.redirectUri;
|
498 | return userMock;
|
499 | }
|
500 | getMockUserInfo() {
|
501 | return {
|
502 | Id: this.userId,
|
503 | Username: this.username,
|
504 | LastName: `user_lastname_${this.testId}`,
|
505 | Alias: this.alias || 'user_alias_blah',
|
506 | TimeZoneSidKey: `user_timezonesidkey_${this.testId}`,
|
507 | LocaleSidKey: `user_localesidkey_${this.testId}`,
|
508 | EmailEncodingKey: `user_emailencodingkey_${this.testId}`,
|
509 | ProfileId: `user_profileid_${this.testId}`,
|
510 | LanguageLocaleKey: `user_languagelocalekey_${this.testId}`,
|
511 | Email: `user_email@${this.testId}.com`,
|
512 | };
|
513 | }
|
514 | async getConfig() {
|
515 | const crypto = await crypto_2.Crypto.create();
|
516 | const config = {};
|
517 | config.orgId = this.orgId;
|
518 | const accessToken = crypto.encrypt(this.accessToken);
|
519 | if (accessToken) {
|
520 | config.accessToken = accessToken;
|
521 | }
|
522 | const refreshToken = crypto.encrypt(this.refreshToken);
|
523 | if (refreshToken) {
|
524 | config.refreshToken = refreshToken;
|
525 | }
|
526 | config.instanceUrl = this.instanceUrl;
|
527 | config.loginUrl = this.loginUrl;
|
528 | config.username = this.username;
|
529 | config.createdOrgInstance = 'CS1';
|
530 | config.created = '1519163543003';
|
531 | config.userId = this.userId;
|
532 |
|
533 | if (this.devHubUsername) {
|
534 | config.devHubUsername = this.devHubUsername;
|
535 | }
|
536 | const isDevHub = ts_types_1.getBoolean(this, 'isDevHub');
|
537 | if (isDevHub) {
|
538 | config.isDevHub = isDevHub;
|
539 | }
|
540 | return config;
|
541 | }
|
542 | }
|
543 | exports.MockTestOrgData = MockTestOrgData;
|
544 |
|
\ | No newline at end of file |