Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"klona": "^2.0.6",
"lodash": "^4.18.1",
"mkdirp": "^1.0.4",
"open": "^8.4.2",
"open": "^11.0.4",
"ora": "^5.4.1",
"papaparse": "^5.7.1",
"recheck": "~4.6.2",
Expand Down
12 changes: 10 additions & 2 deletions packages/contentstack-utilities/src/auth-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import cliux from './cli-ux';
import configHandler from './config-handler';
import dotenv from 'dotenv';
import open from 'open';
import http from 'http';
import url from 'url';
import { handleAndLogError } from './logger/log';
Expand Down Expand Up @@ -224,12 +223,21 @@ class AuthHandler {
{ color: 'yellow' },
);
cliux.print(url, { color: 'green' });
await open(url);
await this.openInBrowser(url);
} catch (error) {
throw error;
}
}

/**
* Open a URL in the default browser.
* `open` (v9+) is ESM-only, so it is loaded with a dynamic import, the same way as chalk in ./chalk.ts.
*/
async openInBrowser(url: string): Promise<void> {
const { default: open } = await import('open');
await open(url);
}

async getAccessToken(code: string): Promise<void> {
try {
const data = await this.oauthHandler.exchangeCodeForToken(code);
Expand Down
50 changes: 30 additions & 20 deletions packages/contentstack-utilities/test/unit/auth-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,40 +112,50 @@ describe('Auth Handler', () => {
});

describe('openOAuthURL', () => {
let sandbox, openMock;
let sandbox, originalOAuthHandler;

beforeEach(() => {
sandbox = createSandbox();
openMock = sandbox.stub().resolves();
global.open = openMock;
originalOAuthHandler = authHandler['oauthHandler'];
});

afterEach(() => {
delete global.open;
authHandler['oauthHandler'] = originalOAuthHandler;
sandbox.restore();
});

it('should open the OAuth URL and resolve with the result', async () => {
it('should print the authorize URL and open it in the browser', async () => {
const expectedURL = 'https://example.com/oauth';
const openStub = sandbox.stub().resolves();

sandbox.stub(authHandler, 'setOAuthBaseURL').resolves();
sandbox.stub(crypto, 'createHash').returns({ update: () => {}, digest: () => {} });
sandbox.stub(authHandler, 'OAuthBaseURL').value('https://example.com');
sandbox.stub(authHandler, 'OAuthAppId').value('APP_ID');
sandbox.stub(authHandler, 'OAuthResponseType').value('response_type');
sandbox.stub(authHandler, 'OAuthClientId').value('client_id');
sandbox.stub(authHandler, 'OAuthRedirectURL').value('redirect_uri');
sandbox.stub(authHandler, 'OAuthScope').value('scope');
sandbox.stub(cliux, 'print');
sandbox.stub(cliux, 'error');
authHandler['oauthHandler'] = { authorize: sandbox.stub().resolves(expectedURL) };
const printStub = sandbox.stub(cliux, 'print');
const openInBrowserStub = sandbox.stub(authHandler, 'openInBrowser').resolves();

await authHandler.openOAuthURL();

sandbox.replace(global, 'open', openStub);
assert.calledOnceWithExactly(openInBrowserStub, expectedURL);
assert.calledWith(printStub, expectedURL, { color: 'green' });
});

it('should reject when opening the browser fails', async () => {
authHandler['oauthHandler'] = { authorize: sandbox.stub().resolves('https://example.com/oauth') };
sandbox.stub(cliux, 'print');
sandbox.stub(authHandler, 'openInBrowser').rejects(new Error('Failed to open browser'));

let caught;
try {
await authHandler.openOAuthURL();
} catch (error) {
caught = error;
}
expect(caught).to.be.instanceOf(Error);
expect(caught.message).to.equal('Failed to open browser');
});
});

assert.calledWith(openStub, expectedURL);
} catch (error) {}
describe('openInBrowser', () => {
it('should load the ESM-only open package via dynamic import', async () => {
const openModule = await import('open');
expect(openModule.default).to.be.a('function');
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"figlet": "1.13.0",
"inquirer": "^12.11.1",
"node-machine-id": "^1.1.12",
"open": "^8.4.2",
"ora": "^8.2.0",
"semver": "^7.8.6",
"short-uuid": "^6.0.3",
Expand Down
Loading
Loading