-
Notifications
You must be signed in to change notification settings - Fork 52
Add --sandbox-only flag to force check of allowed orgs to ensure they are only Sandboxes. #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zerkz
wants to merge
8
commits into
salesforcecli:main
Choose a base branch
from
zerkz:zdware-add-sandbox-check-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1469d50
Fix apex tests tool name in readme for mcp json example.
zerkz e62de0b
Merge pull request #1 from zerkz/zdware-fix-readme
zerkz 82940d9
wip
zerkz 2325de8
run yarn build to build packages
zerkz 33f5f52
revert logging change
zerkz 37f3cf8
add back deletion of binaries/built artifacts
zerkz e33535c
remove unneeded logging of 'success' at checking sandbox-only mode
zerkz fbdcc42
Merge branch 'main' into zdware-add-sandbox-check-flag
zerkz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,14 @@ | |
|
||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { AuthInfo, ConfigAggregator, ConfigInfo, OrgConfigProperties, type OrgAuthorization } from '@salesforce/core'; | ||
import { | ||
AuthInfo, | ||
Connection, | ||
ConfigAggregator, | ||
ConfigInfo, | ||
OrgConfigProperties, | ||
type OrgAuthorization, | ||
} from '@salesforce/core'; | ||
import { type SanitizedOrgAuthorization } from '@salesforce/mcp-provider-api'; | ||
import { | ||
getAllAllowedOrgs, | ||
|
@@ -25,6 +32,7 @@ import { | |
sanitizeOrgs, | ||
findOrgByUsernameOrAlias, | ||
filterAllowedOrgs, | ||
validateSandboxOrgs, | ||
} from '../../src/utils/auth.js'; | ||
import Cache from '../../src/utils/cache.js'; | ||
|
||
|
@@ -941,4 +949,206 @@ describe('auth tests', () => { | |
expect(configAggregatorCreateStub.calledTwice).to.be.true; // ConfigAggregator is called every time to get the path. | ||
}); | ||
}); | ||
|
||
describe('validateSandboxOrgs', () => { | ||
let authInfoCreateStub: sinon.SinonStub; | ||
let connectionCreateStub: sinon.SinonStub; | ||
let connectionQueryStub: sinon.SinonStub; | ||
let consoleErrorStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
authInfoCreateStub = sandbox.stub(AuthInfo, 'create'); | ||
connectionQueryStub = sandbox.stub(); | ||
connectionCreateStub = sandbox.stub(Connection, 'create'); | ||
consoleErrorStub = sandbox.stub(console, 'error'); | ||
|
||
// Default connection setup | ||
const mockConnection = { | ||
query: connectionQueryStub, | ||
}; | ||
connectionCreateStub.resolves(mockConnection); | ||
}); | ||
|
||
it('should pass validation when all orgs are sandboxes', async () => { | ||
const mockOrgs: SanitizedOrgAuthorization[] = [ | ||
{ | ||
username: '[email protected]', | ||
aliases: ['sandbox1'], | ||
instanceUrl: 'https://sandbox1.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: true, | ||
orgId: '00D000000000001EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
{ | ||
username: '[email protected]', | ||
aliases: ['sandbox2'], | ||
instanceUrl: 'https://sandbox2.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: true, | ||
orgId: '00D000000000002EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
]; | ||
|
||
const mockAuthInfo = { username: 'test' }; | ||
authInfoCreateStub.resolves(mockAuthInfo); | ||
|
||
// Mock SOQL query returning IsSandbox = true | ||
connectionQueryStub.resolves({ | ||
records: [{ IsSandbox: true }], | ||
}); | ||
|
||
// Should not throw | ||
await validateSandboxOrgs(mockOrgs); | ||
|
||
expect(authInfoCreateStub.calledTwice).to.be.true; | ||
expect(connectionQueryStub.calledTwice).to.be.true; | ||
}); | ||
|
||
it('should throw error when production org is detected', async () => { | ||
const mockOrgs: SanitizedOrgAuthorization[] = [ | ||
{ | ||
username: '[email protected]', | ||
aliases: ['prod'], | ||
instanceUrl: 'https://login.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: false, | ||
orgId: '00D000000000001EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
]; | ||
|
||
const mockAuthInfo = { username: 'test' }; | ||
authInfoCreateStub.resolves(mockAuthInfo); | ||
|
||
// Mock SOQL query returning IsSandbox = false (production org) | ||
connectionQueryStub.resolves({ | ||
records: [{ IsSandbox: false }], | ||
}); | ||
|
||
try { | ||
await validateSandboxOrgs(mockOrgs); | ||
expect.fail('Should have thrown an error'); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(Error); | ||
expect((error as Error).message).to.include('Production org(s) detected'); | ||
expect((error as Error).message).to.include('prod'); | ||
} | ||
}); | ||
|
||
it('should throw error with multiple production orgs', async () => { | ||
const mockOrgs: SanitizedOrgAuthorization[] = [ | ||
{ | ||
username: '[email protected]', | ||
aliases: ['prod1'], | ||
instanceUrl: 'https://login.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: false, | ||
orgId: '00D000000000001EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
{ | ||
username: '[email protected]', | ||
aliases: ['prod2'], | ||
instanceUrl: 'https://login.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: false, | ||
orgId: '00D000000000002EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
]; | ||
|
||
const mockAuthInfo = { username: 'test' }; | ||
authInfoCreateStub.resolves(mockAuthInfo); | ||
|
||
// Mock SOQL query returning IsSandbox = false for both orgs | ||
connectionQueryStub.resolves({ | ||
records: [{ IsSandbox: false }], | ||
}); | ||
|
||
try { | ||
await validateSandboxOrgs(mockOrgs); | ||
expect.fail('Should have thrown an error'); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(Error); | ||
expect((error as Error).message).to.include('Production org(s) detected'); | ||
expect((error as Error).message).to.include('prod1'); | ||
expect((error as Error).message).to.include('prod2'); | ||
} | ||
}); | ||
|
||
|
||
it('should warn but not fail when org validation connection fails', async () => { | ||
const mockOrgs: SanitizedOrgAuthorization[] = [ | ||
{ | ||
username: '[email protected]', | ||
aliases: ['broken'], | ||
instanceUrl: 'https://broken.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: false, | ||
orgId: '00D000000000001EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
]; | ||
|
||
authInfoCreateStub.rejects(new Error('Connection failed')); | ||
|
||
// Should not throw, but should log warning | ||
await validateSandboxOrgs(mockOrgs); | ||
|
||
expect(consoleErrorStub.called).to.be.true; | ||
expect(consoleErrorStub.firstCall.args[0]).to.include('Warning: Unable to validate sandbox status'); | ||
}); | ||
|
||
it('should use username when no alias is available', async () => { | ||
const mockOrgs: SanitizedOrgAuthorization[] = [ | ||
{ | ||
username: '[email protected]', | ||
aliases: null, | ||
instanceUrl: 'https://login.salesforce.com', | ||
isScratchOrg: false, | ||
isDevHub: false, | ||
isSandbox: false, | ||
orgId: '00D000000000001EAA', | ||
oauthMethod: 'web', | ||
isExpired: false, | ||
configs: null, | ||
}, | ||
]; | ||
|
||
const mockAuthInfo = { username: 'test' }; | ||
authInfoCreateStub.resolves(mockAuthInfo); | ||
|
||
connectionQueryStub.resolves({ | ||
records: [{ IsSandbox: false }], | ||
}); | ||
|
||
try { | ||
await validateSandboxOrgs(mockOrgs); | ||
expect.fail('Should have thrown an error'); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(Error); | ||
expect((error as Error).message).to.include('[email protected]'); | ||
} | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could possibly speed up this section/logic by putting this unit of work into it's own promise, tossing it in an array, and doing
Promise.all()
. Might get rid of the need for all these eslint disables!