@salesforce/core 3.19.0 → 3.19.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +6 -18
  3. package/lib/config/aliasesConfig.d.ts +12 -0
  4. package/lib/config/aliasesConfig.js +27 -0
  5. package/lib/config/authInfoConfig.d.ts +19 -0
  6. package/lib/config/authInfoConfig.js +35 -0
  7. package/lib/config/configAggregator.d.ts +7 -5
  8. package/lib/config/configAggregator.js +25 -9
  9. package/lib/config/configFile.js +2 -2
  10. package/lib/config/configGroup.d.ts +141 -0
  11. package/lib/config/configGroup.js +224 -0
  12. package/lib/config/configStore.d.ts +2 -2
  13. package/lib/config/configStore.js +1 -2
  14. package/lib/config/envVars.d.ts +2 -3
  15. package/lib/config/envVars.js +8 -13
  16. package/lib/config/tokensConfig.d.ts +10 -0
  17. package/lib/config/tokensConfig.js +28 -0
  18. package/lib/crypto/keyChainImpl.js +1 -1
  19. package/lib/exported.d.ts +1 -1
  20. package/lib/exported.js +6 -5
  21. package/lib/global.d.ts +8 -0
  22. package/lib/global.js +10 -0
  23. package/lib/org/authInfo.d.ts +3 -2
  24. package/lib/org/authInfo.js +55 -37
  25. package/lib/org/authRemover.d.ts +6 -5
  26. package/lib/org/authRemover.js +22 -16
  27. package/lib/org/connection.js +3 -2
  28. package/lib/org/org.d.ts +12 -4
  29. package/lib/org/org.js +28 -26
  30. package/lib/org/scratchOrgCreate.js +5 -8
  31. package/lib/{globalInfo → stateAggregator}/accessors/aliasAccessor.d.ts +79 -1
  32. package/lib/{globalInfo → stateAggregator}/accessors/aliasAccessor.js +119 -2
  33. package/lib/stateAggregator/accessors/orgAccessor.d.ts +50 -0
  34. package/lib/stateAggregator/accessors/orgAccessor.js +197 -0
  35. package/lib/{globalInfo → stateAggregator}/accessors/sandboxAccessor.d.ts +12 -1
  36. package/lib/{globalInfo → stateAggregator}/accessors/sandboxAccessor.js +22 -2
  37. package/lib/stateAggregator/accessors/tokenAccessor.d.ts +28 -0
  38. package/lib/{globalInfo → stateAggregator}/accessors/tokenAccessor.js +34 -2
  39. package/lib/{globalInfo → stateAggregator}/globalInfoConfig.d.ts +11 -8
  40. package/lib/{globalInfo → stateAggregator}/globalInfoConfig.js +7 -4
  41. package/lib/stateAggregator/index.d.ts +7 -0
  42. package/lib/{globalInfo → stateAggregator}/index.js +5 -1
  43. package/lib/{globalInfo → stateAggregator}/sfdxDataHandler.d.ts +7 -1
  44. package/lib/{globalInfo → stateAggregator}/sfdxDataHandler.js +25 -2
  45. package/lib/stateAggregator/stateAggregator.d.ts +20 -0
  46. package/lib/stateAggregator/stateAggregator.js +38 -0
  47. package/lib/{globalInfo → stateAggregator}/types.d.ts +25 -10
  48. package/lib/{globalInfo → stateAggregator}/types.js +3 -0
  49. package/lib/testSetup.d.ts +30 -6
  50. package/lib/testSetup.js +79 -11
  51. package/lib/util/sfdcUrl.d.ts +3 -9
  52. package/lib/util/sfdcUrl.js +29 -28
  53. package/messages/core.md +10 -0
  54. package/messages/scratchOrgCreate.md +0 -4
  55. package/messages/scratchOrgInfoApi.md +4 -0
  56. package/package.json +4 -4
  57. package/lib/globalInfo/accessors/orgAccessor.d.ts +0 -13
  58. package/lib/globalInfo/accessors/orgAccessor.js +0 -45
  59. package/lib/globalInfo/accessors/tokenAccessor.d.ts +0 -13
  60. package/lib/globalInfo/index.d.ts +0 -6
@@ -20,6 +20,9 @@ function deepCopy(data) {
20
20
  return JSON.parse(JSON.stringify(data));
21
21
  }
22
22
  exports.deepCopy = deepCopy;
23
+ /**
24
+ * @deprecated use StateAggregator instead.
25
+ */
23
26
  class GlobalInfo extends configFile_1.ConfigFile {
24
27
  constructor() {
25
28
  super(...arguments);
@@ -57,16 +60,16 @@ class GlobalInfo extends configFile_1.ConfigFile {
57
60
  };
58
61
  }
59
62
  get orgs() {
60
- return new orgAccessor_1.OrgAccessor(this);
63
+ return new orgAccessor_1.GlobalInfoOrgAccessor(this);
61
64
  }
62
65
  get tokens() {
63
- return new tokenAccessor_1.TokenAccessor(this);
66
+ return new tokenAccessor_1.GlobaInfoTokenAccessor(this);
64
67
  }
65
68
  get aliases() {
66
- return new aliasAccessor_1.AliasAccessor(this);
69
+ return new aliasAccessor_1.GlobalInfoAliasAccessor(this);
67
70
  }
68
71
  get sandboxes() {
69
- return new sandboxAccessor_1.SandboxAccessor(this);
72
+ return new sandboxAccessor_1.GlobalInfoSandboxAccessor(this);
70
73
  }
71
74
  set(key, value) {
72
75
  if ((0, ts_types_1.isPlainObject)(value)) {
@@ -0,0 +1,7 @@
1
+ export * from './globalInfoConfig';
2
+ export * from './sfdxDataHandler';
3
+ export * from './types';
4
+ export { GlobalInfoOrgAccessor, OrgAccessor } from './accessors/orgAccessor';
5
+ export { GlobalInfoAliasAccessor, AliasAccessor } from './accessors/aliasAccessor';
6
+ export { GlobaInfoTokenAccessor, TokenAccessor } from './accessors/tokenAccessor';
7
+ export * from './stateAggregator';
@@ -16,14 +16,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.TokenAccessor = exports.AliasAccessor = exports.OrgAccessor = void 0;
19
+ exports.TokenAccessor = exports.GlobaInfoTokenAccessor = exports.AliasAccessor = exports.GlobalInfoAliasAccessor = exports.OrgAccessor = exports.GlobalInfoOrgAccessor = void 0;
20
20
  __exportStar(require("./globalInfoConfig"), exports);
21
21
  __exportStar(require("./sfdxDataHandler"), exports);
22
22
  __exportStar(require("./types"), exports);
23
23
  var orgAccessor_1 = require("./accessors/orgAccessor");
24
+ Object.defineProperty(exports, "GlobalInfoOrgAccessor", { enumerable: true, get: function () { return orgAccessor_1.GlobalInfoOrgAccessor; } });
24
25
  Object.defineProperty(exports, "OrgAccessor", { enumerable: true, get: function () { return orgAccessor_1.OrgAccessor; } });
25
26
  var aliasAccessor_1 = require("./accessors/aliasAccessor");
27
+ Object.defineProperty(exports, "GlobalInfoAliasAccessor", { enumerable: true, get: function () { return aliasAccessor_1.GlobalInfoAliasAccessor; } });
26
28
  Object.defineProperty(exports, "AliasAccessor", { enumerable: true, get: function () { return aliasAccessor_1.AliasAccessor; } });
27
29
  var tokenAccessor_1 = require("./accessors/tokenAccessor");
30
+ Object.defineProperty(exports, "GlobaInfoTokenAccessor", { enumerable: true, get: function () { return tokenAccessor_1.GlobaInfoTokenAccessor; } });
28
31
  Object.defineProperty(exports, "TokenAccessor", { enumerable: true, get: function () { return tokenAccessor_1.TokenAccessor; } });
32
+ __exportStar(require("./stateAggregator"), exports);
29
33
  //# sourceMappingURL=index.js.map
@@ -11,7 +11,7 @@ interface Changes<T> {
11
11
  deleted: string[];
12
12
  }
13
13
  export declare class SfdxDataHandler {
14
- handlers: (AuthHandler | AliasesHandler | SandboxesHandler)[];
14
+ handlers: (AuthHandler | AliasesHandler | SandboxesHandler | TokensHandler)[];
15
15
  private original;
16
16
  write(latest?: SfInfo): Promise<void>;
17
17
  merge(sfData?: SfInfo): Promise<SfInfo>;
@@ -50,4 +50,10 @@ export declare class SandboxesHandler extends BaseHandler<SfInfoKeys.SANDBOXES>
50
50
  listAllSandboxes(): Promise<SfSandbox[]>;
51
51
  private findChanges;
52
52
  }
53
+ export declare class TokensHandler extends BaseHandler<SfInfoKeys.TOKENS> {
54
+ private static SFDX_TOKENS_FILENAME;
55
+ sfKey: typeof SfInfoKeys.TOKENS;
56
+ migrate(): Promise<Pick<SfInfo, SfInfoKeys.TOKENS>>;
57
+ write(latest: SfInfo): Promise<void>;
58
+ }
53
59
  export {};
@@ -6,7 +6,7 @@
6
6
  * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.SandboxesHandler = exports.AliasesHandler = exports.AuthHandler = exports.SfdxDataHandler = void 0;
9
+ exports.TokensHandler = exports.SandboxesHandler = exports.AliasesHandler = exports.AuthHandler = exports.SfdxDataHandler = void 0;
10
10
  const path_1 = require("path");
11
11
  const fs = require("fs");
12
12
  const kit_1 = require("@salesforce/kit");
@@ -29,7 +29,7 @@ function isEqual(object1, object2) {
29
29
  }
30
30
  class SfdxDataHandler {
31
31
  constructor() {
32
- this.handlers = [new AuthHandler(), new AliasesHandler(), new SandboxesHandler()];
32
+ this.handlers = [new AuthHandler(), new AliasesHandler(), new SandboxesHandler(), new TokensHandler()];
33
33
  }
34
34
  async write(latest = globalInfoConfig_1.GlobalInfo.emptyDataModel) {
35
35
  await Promise.all(this.handlers.map((handler) => handler.write(latest, this.original)));
@@ -306,4 +306,27 @@ class SandboxesHandler extends BaseHandler {
306
306
  exports.SandboxesHandler = SandboxesHandler;
307
307
  // The regular expression that filters files stored in $HOME/.sfdx
308
308
  SandboxesHandler.sandboxFilenameFilterRegEx = /^(00D.*?)\.sandbox\.json$/;
309
+ class TokensHandler extends BaseHandler {
310
+ constructor() {
311
+ super(...arguments);
312
+ this.sfKey = types_1.SfInfoKeys.TOKENS;
313
+ }
314
+ async migrate() {
315
+ const filePath = (0, path_1.join)(global_1.Global.SFDX_DIR, TokensHandler.SFDX_TOKENS_FILENAME);
316
+ try {
317
+ const x = await fs.promises.readFile(filePath, 'utf8');
318
+ const tokens = (0, kit_1.parseJson)(x);
319
+ return { [this.sfKey]: tokens };
320
+ }
321
+ catch (e) {
322
+ return { [this.sfKey]: {} };
323
+ }
324
+ }
325
+ async write(latest) {
326
+ const filePath = (0, path_1.join)(global_1.Global.SFDX_DIR, TokensHandler.SFDX_TOKENS_FILENAME);
327
+ await fs.promises.writeFile(filePath, JSON.stringify(latest[types_1.SfInfoKeys.TOKENS], null, 2));
328
+ }
329
+ }
330
+ exports.TokensHandler = TokensHandler;
331
+ TokensHandler.SFDX_TOKENS_FILENAME = 'tokens.json';
309
332
  //# sourceMappingURL=sfdxDataHandler.js.map
@@ -0,0 +1,20 @@
1
+ import { AsyncOptionalCreatable } from '@salesforce/kit';
2
+ import { AliasAccessor } from './accessors/aliasAccessor';
3
+ import { OrgAccessor } from './accessors/orgAccessor';
4
+ import { SandboxAccessor } from './accessors/sandboxAccessor';
5
+ import { TokenAccessor } from './accessors/tokenAccessor';
6
+ export declare class StateAggregator extends AsyncOptionalCreatable {
7
+ private static instance;
8
+ aliases: AliasAccessor;
9
+ orgs: OrgAccessor;
10
+ sandboxes: SandboxAccessor;
11
+ tokens: TokenAccessor;
12
+ static getInstance(): Promise<StateAggregator>;
13
+ /**
14
+ * Clear the cache to force reading from disk.
15
+ *
16
+ * *NOTE: Only call this method if you must and you know what you are doing.*
17
+ */
18
+ static clearInstance(): void;
19
+ protected init(): Promise<void>;
20
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2022, salesforce.com, inc.
4
+ * All rights reserved.
5
+ * Licensed under the BSD 3-Clause license.
6
+ * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.StateAggregator = void 0;
10
+ const kit_1 = require("@salesforce/kit");
11
+ const aliasAccessor_1 = require("./accessors/aliasAccessor");
12
+ const orgAccessor_1 = require("./accessors/orgAccessor");
13
+ const sandboxAccessor_1 = require("./accessors/sandboxAccessor");
14
+ const tokenAccessor_1 = require("./accessors/tokenAccessor");
15
+ class StateAggregator extends kit_1.AsyncOptionalCreatable {
16
+ static async getInstance() {
17
+ if (!StateAggregator.instance) {
18
+ StateAggregator.instance = await StateAggregator.create();
19
+ }
20
+ return StateAggregator.instance;
21
+ }
22
+ /**
23
+ * Clear the cache to force reading from disk.
24
+ *
25
+ * *NOTE: Only call this method if you must and you know what you are doing.*
26
+ */
27
+ static clearInstance() {
28
+ delete StateAggregator.instance;
29
+ }
30
+ async init() {
31
+ this.orgs = await orgAccessor_1.OrgAccessor.create();
32
+ this.sandboxes = await sandboxAccessor_1.SandboxAccessor.create();
33
+ this.aliases = await aliasAccessor_1.AliasAccessor.create();
34
+ this.tokens = await tokenAccessor_1.TokenAccessor.create();
35
+ }
36
+ }
37
+ exports.StateAggregator = StateAggregator;
38
+ //# sourceMappingURL=stateAggregator.js.map
@@ -1,4 +1,8 @@
1
1
  import { JsonMap } from '@salesforce/ts-types';
2
+ import { AuthFields } from '../org';
3
+ /**
4
+ * @deprecated
5
+ */
2
6
  export declare enum SfInfoKeys {
3
7
  ORGS = "orgs",
4
8
  TOKENS = "tokens",
@@ -9,12 +13,13 @@ export declare type Timestamp = {
9
13
  timestamp: string;
10
14
  };
11
15
  export declare type SfEntry = JsonMap;
12
- export declare type SfOrg = {
13
- username: string;
14
- orgId: string;
15
- instanceUrl: string;
16
- accessToken?: string;
17
- } & SfEntry;
16
+ /**
17
+ * @deprecated
18
+ */
19
+ export declare type SfOrg = AuthFields & SfEntry;
20
+ /**
21
+ * @deprecated
22
+ */
18
23
  export interface SfOrgs {
19
24
  [key: string]: SfOrg & Timestamp;
20
25
  }
@@ -23,16 +28,21 @@ export declare type SfToken = {
23
28
  url: string;
24
29
  user?: string;
25
30
  } & SfEntry;
26
- export interface SfTokens {
31
+ /**
32
+ */
33
+ export declare type SfTokens = {
27
34
  [key: string]: SfToken & Timestamp;
28
- }
35
+ };
29
36
  /**
30
37
  * The key will always be the alias and the value will always be the username, e.g.
31
38
  * { "MyAlias": "user@salesforce.com" }
32
39
  */
33
- export interface SfAliases {
40
+ export declare type SfAliases = {
34
41
  [alias: string]: string;
35
- }
42
+ };
43
+ /**
44
+ * @deprecated
45
+ */
36
46
  export declare type SfSandbox = {
37
47
  sandboxOrgId: string;
38
48
  prodOrgUsername: string;
@@ -45,10 +55,15 @@ export declare type SfSandbox = {
45
55
  * The key will always be the sandbox username and the value will always be the
46
56
  * production org username
47
57
  * { "user@salesforce.com.mysandbox": "user@salesforce.com" }
58
+ *
59
+ * @deprecated
48
60
  */
49
61
  export interface SfSandboxes {
50
62
  [sandboxOrgId: string]: SfSandbox;
51
63
  }
64
+ /**
65
+ * @deprecated
66
+ */
52
67
  export declare type SfInfo = {
53
68
  [SfInfoKeys.ORGS]: SfOrgs;
54
69
  [SfInfoKeys.TOKENS]: SfTokens;
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SfInfoKeys = void 0;
4
+ /**
5
+ * @deprecated
6
+ */
4
7
  var SfInfoKeys;
5
8
  (function (SfInfoKeys) {
6
9
  SfInfoKeys["ORGS"] = "orgs";
@@ -6,7 +6,8 @@ import { ConfigContents } from './config/configStore';
6
6
  import { Logger } from './logger';
7
7
  import { SfError } from './sfError';
8
8
  import { CometClient, CometSubscription, Message, StreamingExtension } from './status/streamingClient';
9
- import { SfOrg } from './globalInfo';
9
+ import { AuthFields, SandboxFields } from './org';
10
+ import { AliasGroup } from './config/aliasesConfig';
10
11
  /**
11
12
  * Different parts of the system that are mocked out. They can be restored for
12
13
  * individual tests. Test's stubs should always go on the DEFAULT which is exposed
@@ -19,6 +20,7 @@ export interface SandboxTypes {
19
20
  PROJECT: sinon.SinonSandbox;
20
21
  CONNECTION: sinon.SinonSandbox;
21
22
  FS: sinonType.SinonSandbox;
23
+ ORGS: sinonType.SinonSandbox;
22
24
  }
23
25
  /**
24
26
  * Different hooks into {@link ConfigFile} used for testing instead of doing file IO.
@@ -83,11 +85,16 @@ export interface TestContext {
83
85
  */
84
86
  configStubs: {
85
87
  [configName: string]: Optional<ConfigStub>;
86
- GlobalInfo?: ConfigStub;
87
- Aliases?: ConfigStub;
88
- SfProjectJson?: ConfigStub;
88
+ AliasesConfig?: ConfigStub;
89
+ AuthInfoConfig?: ConfigStub;
89
90
  SfdxConfig?: ConfigStub;
91
+ SfProjectJson?: ConfigStub;
92
+ TokensConfig?: ConfigStub;
90
93
  };
94
+ /**
95
+ * An record of stubs created during instantaion.
96
+ */
97
+ stubs?: Record<string, sinonType.SinonStub>;
91
98
  /**
92
99
  * A function used when resolving the local path. Calls localPathResolverSync by default.
93
100
  *
@@ -150,7 +157,11 @@ export interface TestContext {
150
157
  */
151
158
  setConfigStubContents(name: string, value: ConfigContents): void;
152
159
  inProject(inProject: boolean): void;
160
+ stubAuths(...orgs: MockTestOrgData[]): Promise<void>;
161
+ stubSandboxes(...orgs: MockTestSandboxData[]): Promise<void>;
162
+ stubAliases(aliases: Record<string, string>, group?: AliasGroup): void;
153
163
  }
164
+ export declare const uniqid: () => string;
154
165
  /**
155
166
  * Instantiate a @salesforce/core test context. This is automatically created by `const $$ = testSetup()`
156
167
  * but is useful if you don't want to have a global stub of @salesforce/core and you want to isolate it to
@@ -197,7 +208,7 @@ export declare const instantiateContext: (sinon?: any) => TestContext;
197
208
  * ```
198
209
  * @param testContext
199
210
  */
200
- export declare const stubContext: (testContext: TestContext) => void;
211
+ export declare const stubContext: (testContext: TestContext) => Record<string, sinonType.SinonStub>;
201
212
  /**
202
213
  * Restore a @salesforce/core test context. This is automatically stubbed in the global beforeEach created by
203
214
  * `const $$ = testSetup()` but is useful if you don't want to have a global stub of @salesforce/core and you
@@ -402,5 +413,18 @@ export declare class MockTestOrgData {
402
413
  makeDevHub(): void;
403
414
  createUser(user: string): MockTestOrgData;
404
415
  getMockUserInfo(): JsonMap;
405
- getConfig(): Promise<SfOrg>;
416
+ getConfig(): Promise<AuthFields>;
417
+ }
418
+ export declare class MockTestSandboxData {
419
+ id: string;
420
+ sandboxOrgId: string;
421
+ prodOrgUsername: string;
422
+ sandboxName?: string;
423
+ username?: string;
424
+ constructor(id?: string, options?: Partial<{
425
+ prodOrgUsername: string;
426
+ name: string;
427
+ username: string;
428
+ }>);
429
+ getConfig(): Promise<SandboxFields>;
406
430
  }
package/lib/testSetup.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MockTestOrgData = exports.StreamingMockCometClient = exports.StreamingMockCometSubscription = exports.StreamingMockSubscriptionCall = exports.shouldThrow = exports.unexpectedResult = exports.testSetup = exports.restoreContext = exports.stubContext = exports.instantiateContext = void 0;
3
+ exports.MockTestSandboxData = exports.MockTestOrgData = exports.StreamingMockCometClient = exports.StreamingMockCometSubscription = exports.StreamingMockSubscriptionCall = exports.shouldThrow = exports.unexpectedResult = exports.testSetup = exports.restoreContext = exports.stubContext = exports.instantiateContext = exports.uniqid = void 0;
4
4
  /*
5
5
  * Copyright (c) 2020, salesforce.com, inc.
6
6
  * All rights reserved.
@@ -24,22 +24,25 @@ const messages_1 = require("./messages");
24
24
  const sfError_1 = require("./sfError");
25
25
  const sfProject_1 = require("./sfProject");
26
26
  const streamingClient_1 = require("./status/streamingClient");
27
- const globalInfo_1 = require("./globalInfo");
27
+ const stateAggregator_1 = require("./stateAggregator");
28
+ const sandboxAccessor_1 = require("./stateAggregator/accessors/sandboxAccessor");
29
+ const aliasesConfig_1 = require("./config/aliasesConfig");
28
30
  const global_1 = require("./global");
29
31
  const uniqid = () => {
30
32
  return (0, crypto_1.randomBytes)(16).toString('hex');
31
33
  };
34
+ exports.uniqid = uniqid;
32
35
  function getTestLocalPath(uid) {
33
36
  return (0, path_1.join)((0, os_1.tmpdir)(), uid, 'sfdx_core', 'local');
34
37
  }
35
38
  function getTestGlobalPath(uid) {
36
39
  return (0, path_1.join)((0, os_1.tmpdir)(), uid, 'sfdx_core', 'global');
37
40
  }
38
- function retrieveRootPathSync(isGlobal, uid = uniqid()) {
41
+ function retrieveRootPathSync(isGlobal, uid = (0, exports.uniqid)()) {
39
42
  return isGlobal ? getTestGlobalPath(uid) : getTestLocalPath(uid);
40
43
  }
41
44
  // eslint-disable-next-line @typescript-eslint/require-await
42
- async function retrieveRootPath(isGlobal, uid = uniqid()) {
45
+ async function retrieveRootPath(isGlobal, uid = (0, exports.uniqid)()) {
43
46
  return retrieveRootPathSync(isGlobal, uid);
44
47
  }
45
48
  function defaultFakeConnectionRequest() {
@@ -90,12 +93,13 @@ const instantiateContext = (sinon) => {
90
93
  CRYPTO: sinon.createSandbox(),
91
94
  CONNECTION: sinon.createSandbox(),
92
95
  FS: sinon.createSandbox(),
96
+ ORGS: sinon.createSandbox(),
93
97
  },
94
98
  TEST_LOGGER: new logger_1.Logger({
95
99
  name: 'SFDX_Core_Test_Logger',
96
100
  }).useMemoryLogging(),
97
- id: uniqid(),
98
- uniqid,
101
+ id: (0, exports.uniqid)(),
102
+ uniqid: exports.uniqid,
99
103
  configStubs: {},
100
104
  // eslint-disable-next-line @typescript-eslint/require-await
101
105
  localPathRetriever: async (uid) => getTestLocalPath(uid),
@@ -134,6 +138,34 @@ const instantiateContext = (sinon) => {
134
138
  testContext.SANDBOXES.PROJECT.stub(sfProject_1.SfProject, 'resolveProjectPathSync').throws(new sfError_1.SfError('', 'InvalidProjectWorkspaceError'));
135
139
  }
136
140
  },
141
+ async stubAuths(...orgs) {
142
+ const entries = (await Promise.all(orgs.map(async (org) => [org.username, await org.getConfig()])));
143
+ const orgMap = new Map(entries);
144
+ (0, ts_sinon_1.stubMethod)(testContext.SANDBOX, stateAggregator_1.OrgAccessor.prototype, 'getAllFiles').returns([...orgMap.keys()].map((o) => `${o}.json`));
145
+ (0, ts_sinon_1.stubMethod)(testContext.SANDBOX, stateAggregator_1.OrgAccessor.prototype, 'hasFile').callsFake((username) => {
146
+ return orgMap.has(username);
147
+ });
148
+ const retrieveContents = async function () {
149
+ var _a;
150
+ const username = (0, path_1.basename)(this.path.replace('.json', ''));
151
+ return Promise.resolve((_a = orgMap.get(username)) !== null && _a !== void 0 ? _a : {});
152
+ };
153
+ this.configStubs.AuthInfoConfig = { retrieveContents };
154
+ },
155
+ async stubSandboxes(...sandboxes) {
156
+ const entries = (await Promise.all(sandboxes.map(async (sanbox) => [sanbox.username, await sanbox.getConfig()])));
157
+ const sandboxMap = new Map(entries);
158
+ (0, ts_sinon_1.stubMethod)(testContext.SANDBOX, sandboxAccessor_1.SandboxAccessor.prototype, 'getAllFiles').returns([...sandboxMap.keys()].map((o) => `${o}.sandbox.json`));
159
+ const retrieveContents = async function () {
160
+ var _a;
161
+ const username = (0, path_1.basename)(this.path.replace('.sandbox.json', ''));
162
+ return Promise.resolve((_a = sandboxMap.get(username)) !== null && _a !== void 0 ? _a : {});
163
+ };
164
+ this.configStubs.SandboxOrgConfig = { retrieveContents };
165
+ },
166
+ stubAliases(aliases, group = aliasesConfig_1.AliasGroup.ORGS) {
167
+ this.configStubs.AliasesConfig = { contents: { [group]: aliases } };
168
+ },
137
169
  };
138
170
  return testContext;
139
171
  };
@@ -166,6 +198,7 @@ const stubContext = (testContext) => {
166
198
  // Turn off the interoperability feature so that we don't have to mock
167
199
  // the old .sfdx config files
168
200
  global_1.Global.SFDX_INTEROPERABILITY = false;
201
+ const stubs = {};
169
202
  // Most core files create a child logger so stub this to return our test logger.
170
203
  (0, ts_sinon_1.stubMethod)(testContext.SANDBOX, logger_1.Logger, 'child').returns(Promise.resolve(testContext.TEST_LOGGER));
171
204
  (0, ts_sinon_1.stubMethod)(testContext.SANDBOX, logger_1.Logger, 'childFromRoot').returns(testContext.TEST_LOGGER);
@@ -230,8 +263,8 @@ const stubContext = (testContext) => {
230
263
  writeSync.call(this);
231
264
  }
232
265
  };
233
- (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.CONFIG, configFile_1.ConfigFile.prototype, 'writeSync').callsFake(writeSync);
234
- (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.CONFIG, configFile_1.ConfigFile.prototype, 'write').callsFake(write);
266
+ stubs.configWriteSync = (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.CONFIG, configFile_1.ConfigFile.prototype, 'writeSync').callsFake(writeSync);
267
+ stubs.configWrite = (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.CONFIG, configFile_1.ConfigFile.prototype, 'write').callsFake(write);
235
268
  (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.CRYPTO, crypto_2.Crypto.prototype, 'getKeyChain').callsFake(() => Promise.resolve({
236
269
  setPassword: () => Promise.resolve(),
237
270
  getPassword: (data, cb) => cb(undefined, '12345678901234567890123456789012'),
@@ -243,8 +276,23 @@ const stubContext = (testContext) => {
243
276
  }
244
277
  return testContext.fakeConnectionRequest.call(this, request, options);
245
278
  });
279
+ stubs.configExists = (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.ORGS, stateAggregator_1.OrgAccessor.prototype, 'exists').callsFake(async function (username) {
280
+ // @ts-expect-error because private member
281
+ if ([...this.contents.keys()].includes(username))
282
+ return Promise.resolve(true);
283
+ else
284
+ return Promise.resolve(false);
285
+ });
286
+ stubs.configRemove = (0, ts_sinon_1.stubMethod)(testContext.SANDBOXES.ORGS, stateAggregator_1.OrgAccessor.prototype, 'remove').callsFake(async function (username) {
287
+ // @ts-expect-error because private member
288
+ if ([...this.contents.keys()].includes(username))
289
+ return Promise.resolve(true);
290
+ else
291
+ return Promise.resolve(false);
292
+ });
246
293
  // Always start with the default and tests beforeEach or it methods can override it.
247
294
  testContext.fakeConnectionRequest = defaultFakeConnectionRequest;
295
+ return stubs;
248
296
  };
249
297
  exports.stubContext = stubContext;
250
298
  /**
@@ -271,7 +319,9 @@ const restoreContext = (testContext) => {
271
319
  Object.values(testContext.SANDBOXES).forEach((theSandbox) => theSandbox.restore());
272
320
  testContext.configStubs = {};
273
321
  // Give each test run a clean GlobalInstance
274
- globalInfo_1.GlobalInfo.clearInstance();
322
+ stateAggregator_1.GlobalInfo.clearInstance();
323
+ // Give each test run a clean StateAggregator
324
+ stateAggregator_1.StateAggregator.clearInstance();
275
325
  };
276
326
  exports.restoreContext = restoreContext;
277
327
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -281,7 +331,7 @@ const _testSetup = (sinon) => {
281
331
  // Allow each test to have their own config aggregator
282
332
  // @ts-ignore clear for testing.
283
333
  delete configAggregator_1.ConfigAggregator.instance;
284
- (0, exports.stubContext)(testContext);
334
+ testContext.stubs = (0, exports.stubContext)(testContext);
285
335
  });
286
336
  afterEach(() => {
287
337
  (0, exports.restoreContext)(testContext);
@@ -473,7 +523,7 @@ exports.StreamingMockCometClient = StreamingMockCometClient;
473
523
  * Mock class for OrgData.
474
524
  */
475
525
  class MockTestOrgData {
476
- constructor(id = uniqid(), options) {
526
+ constructor(id = (0, exports.uniqid)(), options) {
477
527
  this.testId = id;
478
528
  this.userId = `user_id_${this.testId}`;
479
529
  this.orgId = `${this.testId}`;
@@ -552,4 +602,22 @@ class MockTestOrgData {
552
602
  }
553
603
  }
554
604
  exports.MockTestOrgData = MockTestOrgData;
605
+ class MockTestSandboxData {
606
+ constructor(id = (0, exports.uniqid)(), options) {
607
+ this.id = id;
608
+ this.sandboxOrgId = id;
609
+ this.prodOrgUsername = (options === null || options === void 0 ? void 0 : options.prodOrgUsername) || `admin_${id}@gb.org`;
610
+ this.sandboxName = (options === null || options === void 0 ? void 0 : options.name) || `sandbox_${id}`;
611
+ this.username = (options === null || options === void 0 ? void 0 : options.username) || `${this.prodOrgUsername}.sandbox`;
612
+ }
613
+ async getConfig() {
614
+ return {
615
+ sandboxOrgId: this.sandboxOrgId,
616
+ prodOrgUsername: this.prodOrgUsername,
617
+ sandboxName: this.sandboxName,
618
+ sandboxUsername: this.username,
619
+ };
620
+ }
621
+ }
622
+ exports.MockTestSandboxData = MockTestSandboxData;
555
623
  //# sourceMappingURL=testSetup.js.map
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { URL } from 'url';
3
+ export declare function getLoginAudienceCombos(audienceUrl: string, loginUrl: string): [string, string][];
3
4
  export declare class SfdcUrl extends URL {
4
5
  /**
5
6
  * Salesforce URLs
@@ -27,7 +28,7 @@ export declare class SfdcUrl extends URL {
27
28
  /**
28
29
  * Tests whether this url is an internal Salesforce domain
29
30
  *
30
- * @returns {boolean} true if this is a internal domain
31
+ * @returns {boolean} true if this is an internal domain
31
32
  */
32
33
  isInternalUrl(): boolean;
33
34
  /**
@@ -57,6 +58,7 @@ export declare class SfdcUrl extends URL {
57
58
  /**
58
59
  * Tests whether this url is a sandbox url
59
60
  *
61
+ * @Deprecated - identification of a sandbox instance by URL alone is not deterministic
60
62
  * @param createdOrgInstance The Salesforce instance the org was created on. e.g. `cs42`
61
63
  * @returns {boolean}
62
64
  */
@@ -67,12 +69,4 @@ export declare class SfdcUrl extends URL {
67
69
  * @returns {boolean} true if this domain is a lightning domain
68
70
  */
69
71
  isLightningDomain(): boolean;
70
- /**
71
- * Tests whether this url is a sandbox url
72
- * otherwise tries to resolve dns cnames and then look if any is sandbox url
73
- *
74
- * @param createdOrgInstance The Salesforce instance the org was created on. e.g. `cs42`
75
- * @returns {Promise<boolean>} true if this domain resolves to sanbox url
76
- */
77
- private resolvesToSandbox;
78
72
  }
@@ -6,13 +6,37 @@
6
6
  * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.SfdcUrl = void 0;
9
+ exports.SfdcUrl = exports.getLoginAudienceCombos = void 0;
10
10
  const url_1 = require("url");
11
11
  const kit_1 = require("@salesforce/kit");
12
12
  const ts_types_1 = require("@salesforce/ts-types");
13
13
  const myDomainResolver_1 = require("../status/myDomainResolver");
14
14
  const logger_1 = require("../logger");
15
15
  const lifecycleEvents_1 = require("../lifecycleEvents");
16
+ function getLoginAudienceCombos(audienceUrl, loginUrl) {
17
+ const filtered = [
18
+ [loginUrl, loginUrl],
19
+ [SfdcUrl.SANDBOX, SfdcUrl.SANDBOX],
20
+ [SfdcUrl.PRODUCTION, SfdcUrl.PRODUCTION],
21
+ [audienceUrl, audienceUrl],
22
+ [audienceUrl, SfdcUrl.PRODUCTION],
23
+ [audienceUrl, SfdcUrl.SANDBOX],
24
+ [loginUrl, audienceUrl],
25
+ [loginUrl, SfdcUrl.PRODUCTION],
26
+ [loginUrl, SfdcUrl.SANDBOX],
27
+ [SfdcUrl.PRODUCTION, audienceUrl],
28
+ [SfdcUrl.SANDBOX, audienceUrl],
29
+ ].filter(([login, audience]) => !((login === SfdcUrl.PRODUCTION && audience === SfdcUrl.SANDBOX) ||
30
+ (login === SfdcUrl.SANDBOX && audience === SfdcUrl.PRODUCTION)));
31
+ const reduced = filtered.reduce((acc, [login, audience]) => {
32
+ const l = new url_1.URL(login);
33
+ const a = new url_1.URL(audience);
34
+ acc.set(`${l.origin}:${a.origin}`, [login, audience]);
35
+ return acc;
36
+ }, new Map());
37
+ return [...reduced.values()];
38
+ }
39
+ exports.getLoginAudienceCombos = getLoginAudienceCombos;
16
40
  class SfdcUrl extends url_1.URL {
17
41
  constructor(input, base) {
18
42
  super(input.toString(), base);
@@ -45,13 +69,6 @@ class SfdcUrl extends url_1.URL {
45
69
  this.logger.debug(`Audience URL overridden by env var SFDX_AUDIENCE_URL=${envVarVal}`);
46
70
  return envVarVal;
47
71
  }
48
- if (this.isInternalUrl()) {
49
- // This is for internal developers when just doing authorize
50
- return this.origin;
51
- }
52
- if (await this.resolvesToSandbox(createdOrgInstance)) {
53
- return SfdcUrl.SANDBOX;
54
- }
55
72
  if ((createdOrgInstance && /^gs1/gi.test(createdOrgInstance)) || /(gs1.my.salesforce.com)/gi.test(this.origin)) {
56
73
  return 'https://gs1.salesforce.com';
57
74
  }
@@ -81,7 +98,7 @@ class SfdcUrl extends url_1.URL {
81
98
  /**
82
99
  * Tests whether this url is an internal Salesforce domain
83
100
  *
84
- * @returns {boolean} true if this is a internal domain
101
+ * @returns {boolean} true if this is an internal domain
85
102
  */
86
103
  isInternalUrl() {
87
104
  const INTERNAL_URL_PARTS = [
@@ -157,9 +174,11 @@ class SfdcUrl extends url_1.URL {
157
174
  /**
158
175
  * Tests whether this url is a sandbox url
159
176
  *
177
+ * @Deprecated - identification of a sandbox instance by URL alone is not deterministic
160
178
  * @param createdOrgInstance The Salesforce instance the org was created on. e.g. `cs42`
161
179
  * @returns {boolean}
162
180
  */
181
+ // TODO: how to get rid of this?
163
182
  isSandboxUrl(createdOrgInstance) {
164
183
  return ((createdOrgInstance && /^cs|s$/gi.test(createdOrgInstance)) ||
165
184
  this.origin.endsWith('sandbox.my.salesforce.mil') ||
@@ -177,25 +196,7 @@ class SfdcUrl extends url_1.URL {
177
196
  * @returns {boolean} true if this domain is a lightning domain
178
197
  */
179
198
  isLightningDomain() {
180
- return /\.lightning\.force\.com/.test(this.origin);
181
- }
182
- /**
183
- * Tests whether this url is a sandbox url
184
- * otherwise tries to resolve dns cnames and then look if any is sandbox url
185
- *
186
- * @param createdOrgInstance The Salesforce instance the org was created on. e.g. `cs42`
187
- * @returns {Promise<boolean>} true if this domain resolves to sanbox url
188
- */
189
- async resolvesToSandbox(createdOrgInstance) {
190
- if (this.isSandboxUrl(createdOrgInstance)) {
191
- return true;
192
- }
193
- const myDomainResolver = await myDomainResolver_1.MyDomainResolver.create({ url: this });
194
- const cnames = await myDomainResolver.getCnames();
195
- return cnames.some((cname) => {
196
- const url = new SfdcUrl(`https://${cname}`);
197
- return url.isSandboxUrl();
198
- });
199
+ return /\.lightning\.force\.com/.test(this.origin) || /\.lightning\.crmforce\.mil/.test(this.origin);
199
200
  }
200
201
  }
201
202
  exports.SfdcUrl = SfdcUrl;