|
17 | 17 | /* eslint-disable @typescript-eslint/ban-ts-comment */ |
18 | 18 |
|
19 | 19 | import { Config } from '@oclif/core'; |
20 | | -import { AuthFields, AuthInfo, SfError } from '@salesforce/core'; |
| 20 | +import { AuthFields, AuthInfo, SfError, Messages } from '@salesforce/core'; |
21 | 21 | import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup'; |
22 | 22 | import { StubbedType, stubInterface, stubMethod } from '@salesforce/ts-sinon'; |
23 | 23 | import { assert, expect } from 'chai'; |
24 | 24 | import { Env } from '@salesforce/kit'; |
25 | 25 | import { SfCommand, Ux } from '@salesforce/sf-plugins-core'; |
26 | | -import LoginWeb, { ExecuteLoginFlowParams } from '../../../../src/commands/org/login/web.js'; |
| 26 | +import LoginWeb, { |
| 27 | + ExecuteLoginFlowParams, |
| 28 | + CODE_BUILDER_STATE_ENV_VAR, |
| 29 | + getVerificationCode, |
| 30 | +} from '../../../../src/commands/org/login/web.js'; |
27 | 31 |
|
28 | 32 | describe('org:login:web', () => { |
| 33 | + Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 34 | + const messages = Messages.loadMessages('@salesforce/plugin-auth', 'web.login'); |
29 | 35 | const $$ = new TestContext(); |
30 | 36 | const testData = new MockTestOrgData(); |
31 | 37 | const config = stubInterface<Config>($$.SANDBOX, { |
@@ -302,4 +308,51 @@ describe('org:login:web', () => { |
302 | 308 | expect(callArgs.clientApp?.username).to.equal('test@example.com'); |
303 | 309 | expect(callArgs.scopes).to.be.undefined; |
304 | 310 | }); |
| 311 | + |
| 312 | + it('should display verification code when CODE_BUILDER_STATE env var is set', async () => { |
| 313 | + const codeBuilderState = 'CODE_BUILDER_STATE'; |
| 314 | + const envStub = $$.SANDBOX.stub(Env.prototype, 'getString'); |
| 315 | + envStub.withArgs(CODE_BUILDER_STATE_ENV_VAR).returns(codeBuilderState); |
| 316 | + envStub.returns(''); // Default for other calls |
| 317 | + |
| 318 | + $$.SANDBOX.stub(Env.prototype, 'getBoolean').returns(false); // Prevent container mode checks |
| 319 | + |
| 320 | + const logStub = stubMethod($$.SANDBOX, SfCommand.prototype, 'log'); |
| 321 | + |
| 322 | + const login = await createNewLoginCommand([], false, undefined); |
| 323 | + await login.run(); |
| 324 | + |
| 325 | + // Verify that log was called with the verification code message |
| 326 | + const verificationCode = getVerificationCode(codeBuilderState); |
| 327 | + const calls = logStub.getCalls(); |
| 328 | + const verificationCodeCall = calls.find( |
| 329 | + (call) => typeof call.args[0] === 'string' && call.args[0].includes(verificationCode) |
| 330 | + ); |
| 331 | + expect(verificationCodeCall).to.exist; |
| 332 | + expect(verificationCode).to.match(/^[0-9a-f]{4}$/); |
| 333 | + expect(verificationCodeCall?.args[0]).to.include(messages.getMessage('verificationCode', [verificationCode])); |
| 334 | + }); |
| 335 | + |
| 336 | + it('should not display verification code when CODE_BUILDER_STATE env var is not set', async () => { |
| 337 | + const envStub = stubMethod($$.SANDBOX, Env.prototype, 'getString'); |
| 338 | + envStub.withArgs('CODE_BUILDER_STATE').returns(undefined); |
| 339 | + envStub.returns(''); |
| 340 | + |
| 341 | + $$.SANDBOX.stub(Env.prototype, 'getBoolean').returns(false); // Prevent container mode checks |
| 342 | + |
| 343 | + const logStub = $$.SANDBOX.stub(SfCommand.prototype, 'log'); |
| 344 | + const logSuccessStub = $$.SANDBOX.stub(SfCommand.prototype, 'logSuccess'); |
| 345 | + |
| 346 | + const login = await createNewLoginCommand([], false, undefined); |
| 347 | + await login.run(); |
| 348 | + |
| 349 | + // Verify that log was NOT called for verification code |
| 350 | + expect(logStub.callCount).to.equal(0); |
| 351 | + const calls = logSuccessStub.getCalls(); |
| 352 | + const verificationCodeCall = calls.find( |
| 353 | + (call) => call.args[0]?.includes('Verification Code') || call.args[0]?.includes('If prompted') |
| 354 | + ); |
| 355 | + expect(verificationCodeCall).to.not.exist; |
| 356 | + expect(logSuccessStub.callCount).to.equal(1); |
| 357 | + }); |
305 | 358 | }); |
0 commit comments