Skip to content

Commit

Permalink
fix assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-akolodziejczyk committed Nov 25, 2024
1 parent cbc15a0 commit 6a73d3c
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions test/authentication/testExternalBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('External browser authentication tests', function () {
const provideBrowserCredentialsPath = '/externalbrowser/provideBrowserCredentials.js';
const login = connParameters.snowflakeTestBrowserUser;
const password = connParameters.snowflakeAuthTestOktaPass;

let connection;

describe('External browser tests', async () => {
before(async () => {
Expand All @@ -17,68 +17,59 @@ describe('External browser authentication tests', function () {

afterEach(async () => {
await cleanBrowserProcesses();
if (connection !== undefined && connection.isUp()) {
await testUtil.destroyConnectionAsync(connection);
}
});

it('Successful connection', async () => {
const connectionOption = { ...connParameters.externalBrowser, clientStoreTemporaryCredential: false };
const connection = await snowflake.createConnection(connectionOption);
const connectAsyncPromise = connection.connectAsync(function (err, connection) {
connectionHandler(err, connection);
});
connection = await snowflake.createConnection(connectionOption);
const provideCredentialsPromise = execWithTimeout('node', [provideBrowserCredentialsPath, 'success', login, password], 15000);
await Promise.all([connectAsyncPromise, provideCredentialsPromise]);
await Promise.allSettled([connection.connectAsync(function () {}), provideCredentialsPromise]);
await verifyConnectionIsUp(connection);
});

it('Wrong credentials', async () => {
const login = 'itsnotanaccount.com';
const password = 'fakepassword';
const connectionOption = { ...connParameters.externalBrowser, clientStoreTemporaryCredential: false };
const connection = await snowflake.createConnection(connectionOption);
const connectionOption = { ...connParameters.externalBrowser, browserActionTimeout: 5000, clientStoreTemporaryCredential: false };
connection = await snowflake.createConnection(connectionOption);

connection.connectAsync(function (err, connection) {
connectionHandler(err, connection);
});
const provideCredentialsPromise = execWithTimeout('node', [provideBrowserCredentialsPath, 'fail', login, password]);
await Promise.all([provideCredentialsPromise]);

await Promise.allSettled([connection.connectAsync(function () {}), provideCredentialsPromise]);
await verifyConnectionIsNotUp(connection);
});

it('External browser timeout', async () => {
const connectionOption = { ...connParameters.externalBrowser, browserActionTimeout: 100, clientStoreTemporaryCredential: false };
const connection = await snowflake.createConnection(connectionOption);

const connectAsyncPromise = connection.connectAsync(function (err, connection) {
timeoutConnectionHandler(err, connection);
});
connection = await snowflake.createConnection(connectionOption);

const connectToBrowserPromise = execWithTimeout('node', [provideBrowserCredentialsPath, 'timeout']);
await Promise.all([connectAsyncPromise, connectToBrowserPromise]);
await Promise.allSettled([connection.connectAsync(function () {}), connectToBrowserPromise]);
await verifyConnectionIsNotUp(connection);
});
});
});

async function timeoutConnectionHandler(err, timeout) {
try {
assert.ok(err, `Browser action timed out after ${timeout} ms.`);
} catch (err){
await assert.fail(err);
}
}

async function cleanBrowserProcesses() {
exec('pkill -f chromium');
exec('pkill -f xdg-open');
}

function connectionHandler(err, connection) {
assert.ok(connection.isUp(), 'Connection is not active');
testUtil.destroyConnection(connection, function () {
try {
assert.ok(!connection.isUp(), 'Connection is not closed');
} catch (err) {
assert.fail(err);
}
});
async function verifyConnectionIsUp(connection) {
assert.ok(await connection.isValidAsync());
await testUtil.executeCmdAsync(connection, 'Select 1');
}

async function verifyConnectionIsNotUp(connection) {
try {
assert(!(await connection.isValidAsync()));
await testUtil.executeCmdAsync(connection, 'Select 1');
assert.fail('Expected error was not thrown');
} catch (error) {
assert.strictEqual(error.message, 'Unable to perform operation because a connection was never established.');
}
}

function execWithTimeout(command, args, timeout = 5000) {
Expand Down

0 comments on commit 6a73d3c

Please sign in to comment.