|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import * as querystring from 'querystring'; |
| 9 | +import { expect } from '@salesforce/command/lib/test'; |
| 10 | +import { TestSession } from '@salesforce/cli-plugins-testkit'; |
| 11 | +import { execCmd } from '@salesforce/cli-plugins-testkit'; |
| 12 | +import { asDictionary, AnyJson, Dictionary, getString, isArray } from '@salesforce/ts-types'; |
| 13 | + |
| 14 | +const verifyHumanResults = ( |
| 15 | + lines: string[], |
| 16 | + defaultUsername: string, |
| 17 | + aliasedUsername: string, |
| 18 | + verbose = false |
| 19 | +): void => { |
| 20 | + expect(lines.length).to.have.greaterThan(0); |
| 21 | + const devHubLine = lines.find((line) => line.includes(process.env.TESTKIT_HUB_USERNAME)); |
| 22 | + expect(devHubLine).to.be.ok; |
| 23 | + expect(devHubLine).to.include('(D)'); |
| 24 | + expect(devHubLine).to.include('Connected'); |
| 25 | + const defaultUserLine = lines.find((line) => line.includes(defaultUsername)); |
| 26 | + expect(defaultUserLine).to.be.ok; |
| 27 | + expect(defaultUserLine).to.include('(U)'); |
| 28 | + const aliasUserLine = lines.find((line) => line.includes(aliasedUsername)); |
| 29 | + expect(aliasUserLine).to.be.ok; |
| 30 | + expect(aliasUserLine).to.include('anAlias'); |
| 31 | + // verbose mode should display sractch org Id and dev hub org Id |
| 32 | + if (verbose) { |
| 33 | + expect(defaultUserLine.match(/00D/g)).to.have.lengthOf(2, defaultUserLine); |
| 34 | + expect(aliasUserLine.match(/00D/g)).to.have.lengthOf(2, aliasUserLine); |
| 35 | + } else { |
| 36 | + expect(defaultUserLine.match(/00D/g)).to.have.lengthOf(1, defaultUserLine); |
| 37 | + expect(aliasUserLine.match(/00D/g)).to.have.lengthOf(1, aliasUserLine); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +describe('Org Command NUT', () => { |
| 42 | + let session: TestSession; |
| 43 | + let defaultUsername: string; |
| 44 | + let aliasedUsername: string; |
| 45 | + let defaultUserOrgId: string; |
| 46 | + let aliasUserOrgId: string; |
| 47 | + before(() => { |
| 48 | + session = TestSession.create({ |
| 49 | + project: { name: 'forceOrgList' }, |
| 50 | + setupCommands: [ |
| 51 | + 'sfdx force:org:create -f config/project-scratch-def.json --setdefaultusername --wait 10', |
| 52 | + 'sfdx force:org:create -f config/project-scratch-def.json --setalias anAlias --wait 10', |
| 53 | + ], |
| 54 | + }); |
| 55 | + |
| 56 | + if (isArray<AnyJson>(session.setup)) { |
| 57 | + defaultUsername = getString(session.setup[0], 'result.username'); |
| 58 | + defaultUserOrgId = getString(session.setup[0], 'result.orgId'); |
| 59 | + aliasedUsername = getString(session.setup[1], 'result.username'); |
| 60 | + aliasUserOrgId = getString(session.setup[1], 'result.orgId'); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + after(async () => { |
| 65 | + await session?.clean(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('List Orgs', () => { |
| 69 | + it('should list all orgs', () => { |
| 70 | + const listResult = execCmd<Dictionary>('force:org:list --json', { ensureExitCode: 0 }).jsonOutput.result; |
| 71 | + expect(listResult).to.have.property('nonScratchOrgs'); |
| 72 | + expect(listResult.nonScratchOrgs).to.have.length(1); |
| 73 | + expect(listResult).to.have.property('scratchOrgs'); |
| 74 | + expect(listResult.scratchOrgs).to.have.length(2); |
| 75 | + const nonScratchOrgs = asDictionary(listResult.nonScratchOrgs[0]); |
| 76 | + const scratchOrgs = listResult.scratchOrgs as unknown[]; |
| 77 | + expect(scratchOrgs.map((scratchOrg) => getString(scratchOrg, 'username'))).to.deep.equal([ |
| 78 | + defaultUsername, |
| 79 | + aliasedUsername, |
| 80 | + ]); |
| 81 | + expect(scratchOrgs.map((org) => asDictionary(org)).find((org) => org.username === defaultUsername)).to.include({ |
| 82 | + defaultMarker: '(U)', |
| 83 | + isDefaultUsername: true, |
| 84 | + }); |
| 85 | + expect(scratchOrgs.map((org) => asDictionary(org)).find((org) => org.username === aliasedUsername)).to.include({ |
| 86 | + alias: 'anAlias', |
| 87 | + }); |
| 88 | + expect(nonScratchOrgs).to.include( |
| 89 | + { |
| 90 | + username: process.env.TESTKIT_HUB_USERNAME, |
| 91 | + defaultMarker: '(D)', |
| 92 | + isDevHub: true, |
| 93 | + connectedStatus: 'Connected', |
| 94 | + }, |
| 95 | + JSON.stringify(nonScratchOrgs) |
| 96 | + ); |
| 97 | + }); |
| 98 | + it('should list orgs - skipconnectionstatus', () => { |
| 99 | + const listResult = execCmd<Dictionary>('force:org:list --skipconnectionstatus --json', { ensureExitCode: 0 }) |
| 100 | + .jsonOutput.result; |
| 101 | + const nonScratchOrgs = asDictionary(listResult.nonScratchOrgs[0]); |
| 102 | + expect(nonScratchOrgs).to.include( |
| 103 | + { |
| 104 | + username: process.env.TESTKIT_HUB_USERNAME, |
| 105 | + defaultMarker: '(D)', |
| 106 | + isDevHub: true, |
| 107 | + }, |
| 108 | + JSON.stringify(nonScratchOrgs) |
| 109 | + ); |
| 110 | + }); |
| 111 | + it('should list orgs in a human readable form', () => { |
| 112 | + const lines = (execCmd('force:org:list', { ensureExitCode: 0 }).shellOutput.stdout as string).split('\n'); |
| 113 | + verifyHumanResults(lines, defaultUsername, aliasedUsername); |
| 114 | + }); |
| 115 | + it('should list additional information with --verbose', () => { |
| 116 | + const lines = (execCmd('force:org:list --verbose', { ensureExitCode: 0 }).shellOutput.stdout as string).split( |
| 117 | + '\n' |
| 118 | + ); |
| 119 | + verifyHumanResults(lines, defaultUsername, aliasedUsername, true); |
| 120 | + }); |
| 121 | + }); |
| 122 | + describe('Org Display', () => { |
| 123 | + it('should display org information for default username', () => { |
| 124 | + const result = execCmd<Dictionary>('force:org:display --json', { ensureExitCode: 0 }).jsonOutput.result; |
| 125 | + expect(result).to.be.ok; |
| 126 | + expect(result).to.include({ |
| 127 | + devHubId: process.env.TESTKIT_HUB_USERNAME, |
| 128 | + username: defaultUsername, |
| 129 | + }); |
| 130 | + }); |
| 131 | + it('should display scratch org information for alias', () => { |
| 132 | + const result = execCmd<Dictionary>(`force:org:display -u ${aliasedUsername} --json`, { ensureExitCode: 0 }) |
| 133 | + .jsonOutput.result; |
| 134 | + expect(result).to.be.ok; |
| 135 | + expect(result).to.include({ |
| 136 | + devHubId: process.env.TESTKIT_HUB_USERNAME, |
| 137 | + username: aliasedUsername, |
| 138 | + }); |
| 139 | + }); |
| 140 | + it('should display human readable org information for default username', () => { |
| 141 | + const lines = (execCmd<Dictionary>('force:org:display', { ensureExitCode: 0 }).shellOutput |
| 142 | + .stdout as string).split('\n'); |
| 143 | + expect(lines.length).to.have.greaterThan(0); |
| 144 | + const usernameLine = lines.find((line) => line.includes('Username')); |
| 145 | + expect(usernameLine).to.include(defaultUsername); |
| 146 | + }); |
| 147 | + it('should display human readable scratch org information for alias', () => { |
| 148 | + const lines = (execCmd(`force:org:display -u ${aliasedUsername}`, { ensureExitCode: 0 }).shellOutput |
| 149 | + .stdout as string).split('\n'); |
| 150 | + expect(lines.length).to.have.greaterThan(0); |
| 151 | + const usernameLine = lines.find((line) => line.includes('Username')); |
| 152 | + expect(usernameLine).to.include(aliasedUsername); |
| 153 | + }); |
| 154 | + }); |
| 155 | + describe('Org Open', () => { |
| 156 | + it('should produce the URL for an org in json', () => { |
| 157 | + const result = execCmd<Dictionary>(`force:org:open -u ${defaultUsername} --urlonly --json`, { ensureExitCode: 0 }) |
| 158 | + .jsonOutput.result; |
| 159 | + expect(result).to.be.ok; |
| 160 | + expect(result).to.include({ orgId: defaultUserOrgId, username: defaultUsername }); |
| 161 | + }); |
| 162 | + it('should produce the URL with given path for an org in json', () => { |
| 163 | + const result = execCmd(`force:org:open -u ${aliasedUsername} --urlonly --path "foo/bar/baz" --json`, { |
| 164 | + ensureExitCode: 0, |
| 165 | + }).jsonOutput.result; |
| 166 | + expect(result).to.be.ok; |
| 167 | + expect(result).to.include({ orgId: aliasUserOrgId, username: aliasedUsername }); |
| 168 | + expect(result) |
| 169 | + .to.property('url') |
| 170 | + .to.include(`retURL=${querystring.escape('foo/bar/baz')}`); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments