From ee7a774b23218aebbb680d01daaf68550d303005 Mon Sep 17 00:00:00 2001 From: Kelly Huntlin Date: Tue, 11 Feb 2025 11:26:04 -0800 Subject: [PATCH] use get free port util function --- test/unit/authentication/authentication_test.js | 15 ++++++++++++--- test/unit/mock/mock_test_util.js | 4 ++-- test/unit/test_util.js | 12 ++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/test/unit/authentication/authentication_test.js b/test/unit/authentication/authentication_test.js index 5f0703bb6..bc1b50fd9 100644 --- a/test/unit/authentication/authentication_test.js +++ b/test/unit/authentication/authentication_test.js @@ -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; @@ -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' }; @@ -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: {} }; @@ -204,6 +212,7 @@ describe('external browser authentication', function () { webbrowser = require('webbrowser'); httpclient = require('httpclient'); + const availablePort = await getPortFree(); const fastFailConnectionConfig = { getBrowserActionTimeout: () => 10, @@ -211,7 +220,7 @@ describe('external browser authentication', function () { getAuthenticator: () => credentials.authenticator, getServiceName: () => '', getDisableConsoleLogin: () => true, - getSamlRedirectUri: () => credentials.samlRedirectUri, + getSamlRedirectUri: () => `localhost:${availablePort}`, host: 'fakehost' }; diff --git a/test/unit/mock/mock_test_util.js b/test/unit/mock/mock_test_util.js index 490758c74..5163ac8f0 100644 --- a/test/unit/mock/mock_test_util.js +++ b/test/unit/mock/mock_test_util.js @@ -2,6 +2,7 @@ * Copyright (c) 2015-2024 Snowflake Computing Inc. All rights reserved. */ +const { getPortFree } = require('../test_util'); const Core = require('./../../../lib/core'); const MockHttpClient = require('./mock_http_client'); @@ -106,7 +107,6 @@ const connectionOptionsExternalBrowser = username: 'fakeusername', account: 'fakeaccount', authenticator: 'EXTERNALBROWSER', - samlRedirectUri: 'localhost:3000' }; const connectionOptionsidToken = @@ -167,7 +167,7 @@ const connectionOptionsOkta = getRetryTimeout: () => 300, getRetrySfMaxLoginRetries: () => 7, getDisableSamlURLCheck: () => false, - getSamlRedirectUri: () => 'localhost:3000' + getSamlRedirectUri: () => '' }; exports.connectionOptions = diff --git a/test/unit/test_util.js b/test/unit/test_util.js index b0f42ef40..65a31848d 100644 --- a/test/unit/test_util.js +++ b/test/unit/test_util.js @@ -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)); + }); + }); +}; \ No newline at end of file