|
| 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 | +import { $$, expect, test } from '@salesforce/command/lib/test'; |
| 8 | +import { Org, MyDomainResolver, Messages } from '@salesforce/core'; |
| 9 | +import { stubMethod } from '@salesforce/ts-sinon'; |
| 10 | +import * as utils from '../../../../src/shared/utils'; |
| 11 | + |
| 12 | +Messages.importMessagesDirectory(__dirname); |
| 13 | +const messages = Messages.loadMessages('@salesforce/plugin-org', 'open'); |
| 14 | + |
| 15 | +const orgId = '000000000000000'; |
| 16 | +const username = '[email protected]'; |
| 17 | +const testPath = '/lightning/whatever'; |
| 18 | +const testInstance = 'https://cs1.my.salesforce.com'; |
| 19 | +const accessToken = 'testAccessToken'; |
| 20 | +const expectedDefaultUrl = `${testInstance}/secur/frontdoor.jsp?sid=${accessToken}`; |
| 21 | +const expectedUrl = `${expectedDefaultUrl}&retURL=${encodeURIComponent(testPath)}`; |
| 22 | + |
| 23 | +const testJsonStructure = (response: object) => { |
| 24 | + expect(response).to.have.property('url'); |
| 25 | + expect(response).to.have.property('username').equal(username); |
| 26 | + expect(response).to.have.property('orgId').equal(orgId); |
| 27 | + return true; |
| 28 | +}; |
| 29 | + |
| 30 | +describe('open commands', () => { |
| 31 | + const spies = new Map(); |
| 32 | + afterEach(() => spies.clear()); |
| 33 | + |
| 34 | + beforeEach(async function () { |
| 35 | + $$.SANDBOX.restore(); |
| 36 | + stubMethod($$.SANDBOX, Org, 'create').resolves(Org.prototype); |
| 37 | + stubMethod($$.SANDBOX, Org.prototype, 'getField').withArgs(Org.Fields.INSTANCE_URL).returns(testInstance); |
| 38 | + stubMethod($$.SANDBOX, Org.prototype, 'refreshAuth').resolves({}); |
| 39 | + stubMethod($$.SANDBOX, Org.prototype, 'getOrgId').returns(orgId); |
| 40 | + stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns(username); |
| 41 | + stubMethod($$.SANDBOX, Org.prototype, 'getConnection').returns({ |
| 42 | + accessToken, |
| 43 | + }); |
| 44 | + spies.set('open', stubMethod($$.SANDBOX, utils, 'openUrl').resolves()); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('url generation', () => { |
| 48 | + test |
| 49 | + .stdout() |
| 50 | + .command(['force:org:open', '--json', '--targetusername', username, '--urlonly']) |
| 51 | + .it('org without a url defaults to proper default', (ctx) => { |
| 52 | + const response = JSON.parse(ctx.stdout); |
| 53 | + expect(response.status).to.equal(0); |
| 54 | + expect(testJsonStructure(response.result)).to.be.true; |
| 55 | + expect(response.result.url).to.equal(expectedDefaultUrl); |
| 56 | + }); |
| 57 | + |
| 58 | + test |
| 59 | + .stdout() |
| 60 | + .command(['force:org:open', '--json', '--targetusername', username, '--urlonly', '--path', testPath]) |
| 61 | + .it('org with a url is built correctly', (ctx) => { |
| 62 | + const response = JSON.parse(ctx.stdout); |
| 63 | + expect(response.status).to.equal(0); |
| 64 | + expect(testJsonStructure(response.result)).to.be.true; |
| 65 | + expect(response.result.url).to.equal(expectedUrl); |
| 66 | + }); |
| 67 | + |
| 68 | + test |
| 69 | + .do(() => { |
| 70 | + process.env.FORCE_OPEN_URL = testPath; |
| 71 | + }) |
| 72 | + .finally(() => { |
| 73 | + delete process.env.FORCE_OPEN_URL; |
| 74 | + }) |
| 75 | + .stdout() |
| 76 | + .command(['force:org:open', '--json', '--targetusername', username, '--urlonly']) |
| 77 | + .it('can read url from env', (ctx) => { |
| 78 | + const response = JSON.parse(ctx.stdout); |
| 79 | + expect(response.status).to.equal(0); |
| 80 | + expect(testJsonStructure(response.result)).to.be.true; |
| 81 | + expect(response.result.url).to.equal(expectedUrl); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('domain resolution, with callout', () => { |
| 86 | + beforeEach(() => { |
| 87 | + stubMethod($$.SANDBOX, MyDomainResolver, 'create').resolves(MyDomainResolver.prototype); |
| 88 | + }); |
| 89 | + test |
| 90 | + .do(() => { |
| 91 | + spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').resolves('1.1.1.1')); |
| 92 | + }) |
| 93 | + .stdout() |
| 94 | + .command(['force:org:open', '--json', '--targetusername', username, '--path', testPath]) |
| 95 | + .it('waits on domains that need time to resolve', (ctx) => { |
| 96 | + const response = JSON.parse(ctx.stdout); |
| 97 | + expect(response.status).to.equal(0); |
| 98 | + expect(testJsonStructure(response.result)).to.be.true; |
| 99 | + expect(response.result.url).to.equal(expectedUrl); |
| 100 | + |
| 101 | + expect(spies.get('resolver').callCount).to.equal(1); |
| 102 | + }); |
| 103 | + |
| 104 | + test |
| 105 | + .do(() => { |
| 106 | + spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').rejects()); |
| 107 | + }) |
| 108 | + .stdout() |
| 109 | + .command(['force:org:open', '--json', '--targetusername', username, '--path', testPath]) |
| 110 | + .it('handles domain timeouts', (ctx) => { |
| 111 | + const response = JSON.parse(ctx.stdout); |
| 112 | + expect(spies.get('resolver').callCount).to.equal(1); |
| 113 | + expect(spies.get('open').callCount).to.equal(0); |
| 114 | + expect(response.status).to.equal(1); |
| 115 | + expect(response.message).to.equal(messages.getMessage('domainTimeoutError')); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('domain resolution, no callout', () => { |
| 120 | + beforeEach(() => { |
| 121 | + stubMethod($$.SANDBOX, MyDomainResolver, 'create').resolves(MyDomainResolver.prototype); |
| 122 | + spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').resolves('1.1.1.1')); |
| 123 | + }); |
| 124 | + it('does not wait for domains on internal urls'); |
| 125 | + |
| 126 | + test |
| 127 | + .do(() => { |
| 128 | + process.env.SFDX_CONTAINER_MODE = 'true'; |
| 129 | + }) |
| 130 | + .finally(() => { |
| 131 | + delete process.env.SFDX_CONTAINER_MODE; |
| 132 | + }) |
| 133 | + .stdout() |
| 134 | + .command(['force:org:open', '--json', '--targetusername', username, '--path', testPath]) |
| 135 | + .it('does not wait for domains in container mode, even without urlonly', (ctx) => { |
| 136 | + const response = JSON.parse(ctx.stdout); |
| 137 | + expect(response.status).to.equal(0); |
| 138 | + expect(testJsonStructure(response.result)).to.be.true; |
| 139 | + expect(response.result.url).to.equal(expectedUrl); |
| 140 | + |
| 141 | + expect(spies.get('resolver').callCount).to.equal(0); |
| 142 | + }); |
| 143 | + |
| 144 | + test |
| 145 | + .do(() => { |
| 146 | + process.env.SFDX_DOMAIN_RETRY = '0'; |
| 147 | + }) |
| 148 | + .finally(() => { |
| 149 | + delete process.env.SFDX_DOMAIN_RETRY; |
| 150 | + }) |
| 151 | + .stdout() |
| 152 | + .command(['force:org:open', '--json', '--targetusername', username, '--path', testPath]) |
| 153 | + .it('does not wait for domains when timeouts are zero, even without urlonly', (ctx) => { |
| 154 | + const response = JSON.parse(ctx.stdout); |
| 155 | + expect(response.status).to.equal(0); |
| 156 | + expect(testJsonStructure(response.result)).to.be.true; |
| 157 | + expect(spies.get('resolver').callCount).to.equal(0); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('human output', () => { |
| 162 | + test |
| 163 | + .do(() => { |
| 164 | + spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').resolves('1.1.1.1')); |
| 165 | + }) |
| 166 | + .stdout() |
| 167 | + .command(['force:org:open', '--targetusername', username, '--path', testPath]) |
| 168 | + .it('calls open and outputs proper success message', (ctx) => { |
| 169 | + expect(ctx.stdout).to.include(messages.getMessage('humanSuccess', [orgId, username, expectedUrl])); |
| 170 | + expect(spies.get('resolver').callCount).to.equal(1); |
| 171 | + expect(spies.get('open').callCount).to.equal(1); |
| 172 | + }); |
| 173 | + |
| 174 | + test |
| 175 | + .do(() => { |
| 176 | + spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').rejects()); |
| 177 | + }) |
| 178 | + .stderr() |
| 179 | + .command(['force:org:open', '--targetusername', username, '--path', testPath]) |
| 180 | + .it('throws on dns fail', (ctx) => { |
| 181 | + expect(ctx.stderr).to.contain(messages.getMessage('domainTimeoutError')); |
| 182 | + expect(spies.get('resolver').callCount).to.equal(1); |
| 183 | + expect(spies.get('open').callCount).to.equal(0); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments