Skip to content

Commit ee7a774

Browse files
author
Kelly Huntlin
committed
use get free port util function
1 parent 10650d3 commit ee7a774

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

test/unit/authentication/authentication_test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const AuthOkta = require('./../../../lib/authentication/auth_okta');
1515
const AuthIDToken = require('./../../../lib/authentication/auth_idtoken');
1616
const AuthenticationTypes = require('./../../../lib/authentication/authentication_types');
1717
const MockTestUtil = require('./../mock/mock_test_util');
18+
const { getPortFree } = require('../test_util');
1819

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

118119
const credentials = connectionOptionsExternalBrowser;
119120
const BROWSER_ACTION_TIMEOUT = 10000;
121+
120122
const connectionConfig = {
121123
getBrowserActionTimeout: () => BROWSER_ACTION_TIMEOUT,
122124
getProxy: () => {},
123125
getAuthenticator: () => credentials.authenticator,
124126
getServiceName: () => '',
125127
getDisableConsoleLogin: () => true,
126-
getSamlRedirectUri: () => credentials.samlRedirectUri,
128+
getSamlRedirectUri: () => '',
127129
host: 'fakehost'
128130
};
129131

@@ -166,7 +168,13 @@ describe('external browser authentication', function () {
166168
});
167169

168170
it('external browser - get success', async function () {
169-
const auth = new AuthWeb(connectionConfig, httpclient, webbrowser.open);
171+
const availablePort = await getPortFree();
172+
const localConnectionConfig = {
173+
...connectionConfig,
174+
getSamlRedirectUri: () => `localhost:${availablePort}`
175+
};
176+
177+
const auth = new AuthWeb(localConnectionConfig, httpclient, webbrowser.open);
170178
await auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username);
171179

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

205213
webbrowser = require('webbrowser');
206214
httpclient = require('httpclient');
215+
const availablePort = await getPortFree();
207216

208217
const fastFailConnectionConfig = {
209218
getBrowserActionTimeout: () => 10,
210219
getProxy: () => {},
211220
getAuthenticator: () => credentials.authenticator,
212221
getServiceName: () => '',
213222
getDisableConsoleLogin: () => true,
214-
getSamlRedirectUri: () => credentials.samlRedirectUri,
223+
getSamlRedirectUri: () => `localhost:${availablePort}`,
215224
host: 'fakehost'
216225
};
217226

test/unit/mock/mock_test_util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2015-2024 Snowflake Computing Inc. All rights reserved.
33
*/
44

5+
const { getPortFree } = require('../test_util');
56
const Core = require('./../../../lib/core');
67
const MockHttpClient = require('./mock_http_client');
78

@@ -106,7 +107,6 @@ const connectionOptionsExternalBrowser =
106107
username: 'fakeusername',
107108
account: 'fakeaccount',
108109
authenticator: 'EXTERNALBROWSER',
109-
samlRedirectUri: 'localhost:3000'
110110
};
111111

112112
const connectionOptionsidToken =
@@ -167,7 +167,7 @@ const connectionOptionsOkta =
167167
getRetryTimeout: () => 300,
168168
getRetrySfMaxLoginRetries: () => 7,
169169
getDisableSamlURLCheck: () => false,
170-
getSamlRedirectUri: () => 'localhost:3000'
170+
getSamlRedirectUri: () => ''
171171
};
172172

173173
exports.connectionOptions =

test/unit/test_util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
const net = require('net');
2+
13
module.exports.sleepAsync = function (ms) {
24
return new Promise(resolve => setTimeout(resolve, ms));
35
};
6+
7+
module.exports.getPortFree = function () {
8+
return new Promise(res => {
9+
const srv = net.createServer();
10+
srv.listen(0, () => {
11+
const port = srv.address().port;
12+
srv.close(() => res(port));
13+
});
14+
});
15+
};

0 commit comments

Comments
 (0)