Skip to content

Commit

Permalink
use get free port util function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Huntlin committed Feb 11, 2025
1 parent 10650d3 commit ee7a774
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
15 changes: 12 additions & 3 deletions test/unit/authentication/authentication_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const AuthOkta = require('./../../../lib/authentication/auth_okta');
const AuthIDToken = require('./../../../lib/authentication/auth_idtoken');
const AuthenticationTypes = require('./../../../lib/authentication/authentication_types');
const MockTestUtil = require('./../mock/mock_test_util');
const { getPortFree } = require('../test_util');

// get connection options to connect to this mock snowflake instance
const mockConnectionOptions = MockTestUtil.connectionOptions;
Expand Down Expand Up @@ -117,13 +118,14 @@ describe('external browser authentication', function () {

const credentials = connectionOptionsExternalBrowser;
const BROWSER_ACTION_TIMEOUT = 10000;

const connectionConfig = {
getBrowserActionTimeout: () => BROWSER_ACTION_TIMEOUT,
getProxy: () => {},
getAuthenticator: () => credentials.authenticator,
getServiceName: () => '',
getDisableConsoleLogin: () => true,
getSamlRedirectUri: () => credentials.samlRedirectUri,
getSamlRedirectUri: () => '',
host: 'fakehost'
};

Expand Down Expand Up @@ -166,7 +168,13 @@ describe('external browser authentication', function () {
});

it('external browser - get success', async function () {
const auth = new AuthWeb(connectionConfig, httpclient, webbrowser.open);
const availablePort = await getPortFree();
const localConnectionConfig = {
...connectionConfig,
getSamlRedirectUri: () => `localhost:${availablePort}`
};

const auth = new AuthWeb(localConnectionConfig, httpclient, webbrowser.open);
await auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username);

const body = { data: {} };
Expand Down Expand Up @@ -204,14 +212,15 @@ describe('external browser authentication', function () {

webbrowser = require('webbrowser');
httpclient = require('httpclient');
const availablePort = await getPortFree();

const fastFailConnectionConfig = {
getBrowserActionTimeout: () => 10,
getProxy: () => {},
getAuthenticator: () => credentials.authenticator,
getServiceName: () => '',
getDisableConsoleLogin: () => true,
getSamlRedirectUri: () => credentials.samlRedirectUri,
getSamlRedirectUri: () => `localhost:${availablePort}`,
host: 'fakehost'
};

Expand Down
4 changes: 2 additions & 2 deletions test/unit/mock/mock_test_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2015-2024 Snowflake Computing Inc. All rights reserved.
*/

const { getPortFree } = require('../test_util');

Check failure on line 5 in test/unit/mock/mock_test_util.js

View workflow job for this annotation

GitHub Actions / Run lint

'getPortFree' is assigned a value but never used
const Core = require('./../../../lib/core');
const MockHttpClient = require('./mock_http_client');

Expand Down Expand Up @@ -106,7 +107,6 @@ const connectionOptionsExternalBrowser =
username: 'fakeusername',
account: 'fakeaccount',
authenticator: 'EXTERNALBROWSER',
samlRedirectUri: 'localhost:3000'
};

const connectionOptionsidToken =
Expand Down Expand Up @@ -167,7 +167,7 @@ const connectionOptionsOkta =
getRetryTimeout: () => 300,
getRetrySfMaxLoginRetries: () => 7,
getDisableSamlURLCheck: () => false,
getSamlRedirectUri: () => 'localhost:3000'
getSamlRedirectUri: () => ''
};

exports.connectionOptions =
Expand Down
12 changes: 12 additions & 0 deletions test/unit/test_util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
const net = require('net');

module.exports.sleepAsync = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};

module.exports.getPortFree = function () {
return new Promise(res => {
const srv = net.createServer();
srv.listen(0, () => {
const port = srv.address().port;
srv.close(() => res(port));
});
});
};

0 comments on commit ee7a774

Please sign in to comment.