diff --git a/README.md b/README.md index ec9869711..017cf99b9 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ npm test ``` or ``` -npm test:unit +npm run test:unit ``` To run single test file use `test:single` script, e.g. run tests in `test/unit/snowflake_test.js` only: @@ -68,7 +68,26 @@ npm run test:single -- test/unit/snowflake_test.js Run integration tests: ``` -npm test:integration +npm run test:integration +``` + +Manual test +---------------------------------------------------------------------- + +Specify env variables: + +``` +export RUN_MANUAL_TESTS_ONLY=true +export SNOWFLAKE_TEST_OKTA_USER= +export SNOWFLAKE_TEST_OKTA_PASS= +export SNOWFLAKE_TEST_OKTA_AUTH= +export SNOWFLAKE_TEST_OAUTH_TOKEN= +export SNOWFLAKE_TEST_BROWSER_USER= +``` + +Run manual connection test for different authenticators +``` +npm run test:manual ``` Getting the code coverage diff --git a/package.json b/package.json index 74dfe75c4..1ee9f7e31 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,8 @@ "test:ci": "mocha -timeout 180000 --recursive --full-trace test/**/*.js", "test:ci:coverage": "nyc npm run test:ci", "test:ci:withSystemTests": "mocha -timeout 180000 --recursive --full-trace test/**/*.js system_test/*.js", - "test:ci:withSystemTests:coverage": "nyc npm run test:ci:withSystemTests" + "test:ci:withSystemTests:coverage": "nyc npm run test:ci:withSystemTests", + "test:manual": "mocha -timeout 180000 --full-trace --full-trace test/integration/testManualConnection.js" }, "author": { "name": "Snowflake Computing, Inc.", @@ -77,4 +78,4 @@ "url": "https://www.snowflake.com/" }, "license": "Apache-2.0" -} +} \ No newline at end of file diff --git a/test/integration/testConnection.js b/test/integration/testConnection.js index 8b3220382..aa8c978f6 100644 --- a/test/integration/testConnection.js +++ b/test/integration/testConnection.js @@ -1,47 +1,40 @@ /* * Copyright (c) 2015-2019 Snowflake Computing Inc. All rights reserved. */ -const snowflake = require('./../../lib/snowflake'); -const async = require('async'); -const assert = require('assert'); -const connOption = require('./connectionOptions'); -const testUtil = require('./testUtil'); -const Util = require('./../../lib/util'); -const Core = require('./../../lib/core'); +const snowflake = require("./../../lib/snowflake"); +const async = require("async"); +const assert = require("assert"); +const connOption = require("./connectionOptions"); +const testUtil = require("./testUtil"); +const Util = require("./../../lib/util"); +const Core = require("./../../lib/core"); const stderr = require("test-console").stderr; -describe('Connection test', function () -{ - it('return tokens in qaMode', function () - { - const coreInst = Core({ - qaMode: true, - httpClientClass: require('./../../lib/http/node'), - loggerClass: require('./../../lib/logger/node'), - client: - { - version: Util.driverVersion, - environment: process.versions - } - }); - const connection = coreInst.createConnection(connOption.valid); - assert.deepEqual(connection.getTokens(), { - masterToken: undefined, - masterTokenExpirationTime: undefined, - sessionToken: undefined, - sessionTokenExpirationTime: undefined - }); - } - ); +describe("Connection test", function () { + it("return tokens in qaMode", function () { + const coreInst = Core({ + qaMode: true, + httpClientClass: require("./../../lib/http/node"), + loggerClass: require("./../../lib/logger/node"), + client: { + version: Util.driverVersion, + environment: process.versions, + }, + }); + const connection = coreInst.createConnection(connOption.valid); + assert.deepEqual(connection.getTokens(), { + masterToken: undefined, + masterTokenExpirationTime: undefined, + sessionToken: undefined, + sessionTokenExpirationTime: undefined, + }); + }); - it('does not return tokens when not in qaMode', function () - { - const connection = snowflake.createConnection(connOption.valid); - assert.deepEqual(connection.getTokens(), {}); - } - ) - ; - it('Simple Connect', async function () { + it("does not return tokens when not in qaMode", function () { + const connection = snowflake.createConnection(connOption.valid); + assert.deepEqual(connection.getTokens(), {}); + }); + it("Simple Connect", async function () { const connection = snowflake.createConnection(connOption.valid); await testUtil.connectAsync(connection); @@ -50,52 +43,47 @@ describe('Connection test', function () assert.ok(!connection.isUp(), "still active"); }); - it('Wrong Username', function (done) - { + it("Wrong Username", function (done) { var connection = snowflake.createConnection(connOption.wrongUserName); - connection.connect(function (err) - { - assert.ok(err, 'Username is an empty string'); - assert.equal('Incorrect username or password was specified.', err["message"]); + connection.connect(function (err) { + assert.ok(err, "Username is an empty string"); + assert.equal( + "Incorrect username or password was specified.", + err["message"] + ); done(); }); }); - it('Wrong Password', function (done) - { + it("Wrong Password", function (done) { var connection = snowflake.createConnection(connOption.wrongPwd); - connection.connect(function (err) - { - assert.ok(err, 'Password is an empty string'); - assert.equal('Incorrect username or password was specified.', err["message"]); + connection.connect(function (err) { + assert.ok(err, "Password is an empty string"); + assert.equal( + "Incorrect username or password was specified.", + err["message"] + ); done(); }); }); - it('Multiple Client', function (done) - { + it("Multiple Client", function (done) { const totalConnections = 10; const connections = []; - for (let i = 0; i < totalConnections; i++) - { + for (let i = 0; i < totalConnections; i++) { connections.push(snowflake.createConnection(connOption.valid)); } let completedConnection = 0; - for (let i = 0; i < totalConnections; i++) - { - connections[i].connect(function (err, conn) - { + for (let i = 0; i < totalConnections; i++) { + connections[i].connect(function (err, conn) { testUtil.checkError(err); conn.execute({ - sqlText: 'select 1', - complete: function (err) - { + sqlText: "select 1", + complete: function (err) { testUtil.checkError(err); - testUtil.destroyConnection(conn, function () - { - }); + testUtil.destroyConnection(conn, function () {}); completedConnection++; - } + }, }); }); } @@ -103,440 +91,134 @@ describe('Connection test', function () const maxSleep = 60000; let sleepFromStartChecking = 0; - const timeout = () => setTimeout(() => - { - sleepFromStartChecking += sleepMs; - if (completedConnection === totalConnections) - { - done(); - } - else if (sleepFromStartChecking <= maxSleep) - { - timeout(); - } - else - { - done(`Max after ${maxSleep} it's expected to complete ${totalConnections} but completed ${completedConnection}`) - } - }, sleepMs); + const timeout = () => + setTimeout(() => { + sleepFromStartChecking += sleepMs; + if (completedConnection === totalConnections) { + done(); + } else if (sleepFromStartChecking <= maxSleep) { + timeout(); + } else { + done( + `Max after ${maxSleep} it's expected to complete ${totalConnections} but completed ${completedConnection}` + ); + } + }, sleepMs); timeout(); }); }); -// Skipped - requires manual interaction to enter credentials on browser -describe.skip('Connection test - external browser', function () { - this.timeout(10000); - - it('Simple Connect', function (done) { - const connection = snowflake.createConnection(connOption.externalBrowser); - - connection.connectAsync(function (err, connection) { - try { - assert.ok(connection.isUp(), 'not active'); - testUtil.destroyConnection(connection, function (err, r) { - try { - assert.ok(!connection.isUp(), 'not active'); - done(); - } catch (err) { - done(err); - } - }); - } catch (err) { - done(err); - } - }); - }); - - - it('Connect - external browser timeout', function (done) { - const connection = snowflake.createConnection(connOption.externalBrowserWithShortTimeout); - - connection.connectAsync(function (err) { - try { - const browserActionTimeout = connOption.externalBrowserWithShortTimeout.browserActionTimeout; - assert.ok(err, `Browser action timed out after ${browserActionTimeout} ms.`); - done(); - } catch (err) { - done(err); - } - }); - }); - - it('Mismatched Username', function (done) { - var connection = snowflake.createConnection(connOption.externalBrowserMismatchUser); - connection.connectAsync(function (err) { - try { - assert.ok(err, 'Logged in with different user than one on connection string'); - assert.equal('The user you were trying to authenticate as differs from the user currently logged in at the IDP.', err['message']); - done(); - } catch (err) { - done(err); - } - }); - }); -}); - -describe.skip('Connection test - keypair', function () -{ - it('Simple Connect - specify private key', function (done) - { - var connection = snowflake.createConnection(connOption.keypairPrivateKey); - - async.series([ - function (callback) - { - connection.connect(function (err) - { - done(err); - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(connection.isUp(), "not active"); - callback(); - }, - function (callback) - { - connection.destroy(function (err) - { - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(!connection.isUp(), "still active"); - callback(); - }, - ], - ); - }); - - it.skip('Simple Connect - specify encrypted private key path and passphrase', function (done) - { - var connection = snowflake.createConnection(connOption.keypairPathEncrypted); - - async.series([ - function (callback) - { - connection.connect(function (err) - { - done(err); - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(connection.isUp(), "not active"); - callback(); - }, - function (callback) - { - connection.destroy(function (err) - { - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(!connection.isUp(), "still active"); - callback(); - }, - ], - ); - }); - - it.skip('Simple Connect - specify unencrypted private key path without passphrase', function (done) - { - var connection = snowflake.createConnection(connOption.keypairPathEncrypted); - - async.series([ - function (callback) - { - connection.connect(function (err) - { - done(err); - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(connection.isUp(), "not active"); - callback(); - }, - function (callback) - { - connection.destroy(function (err) - { - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(!connection.isUp(), "still active"); - callback(); - }, - ], - ); - }); - - it('Wrong JWT token', function (done) - { - var connection = snowflake.createConnection(connOption.keypairWrongToken); - connection.connect(function (err) - { - try - { - assert.ok(err, 'Incorrect JWT token is passed.'); - assert.equal('JWT token is invalid.', err["message"]); - done(); - } - catch (err) - { - done(err); - } - }) - }); -}); - -// Skipped - requires manual interaction to obtain oauth token -describe.skip('Connection test - oauth', function () -{ - it('Simple Connect', function (done) - { - var connection = snowflake.createConnection(connOption.oauth); - - async.series([ - function (callback) - { - connection.connect(function (err) - { - done(err); - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(connection.isUp(), "not active"); - callback(); - }, - function (callback) - { - connection.destroy(function (err) - { - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(!connection.isUp(), "still active"); - callback(); - }, - ], - ); - }); - - it('Mismatched Username', function (done) - { - var connection = snowflake.createConnection(connOption.oauthMismatchUser); - connection.connect(function (err) - { - try - { - assert.ok(err, 'Logged in with different user than one on connection string'); - assert.equal('The user you were trying to authenticate as differs from the user tied to the access token.', err["message"]); - done(); - } - catch (err) - { - done(err); - } - }) - }); -}); - -describe.skip('Connection test - okta', function () -{ - it('Simple Connect', function (done) - { - var connection = snowflake.createConnection(connOption.okta); - - async.series([ - function (callback) - { - connection.connectAsync(function (err) - { - done(err); - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(connection.isUp(), "not active"); - callback(); - }, - function (callback) - { - connection.destroy(function (err) - { - assert.ok(!err, JSON.stringify(err)); - callback(); - }); - }, - function (callback) - { - assert.ok(!connection.isUp(), "still active"); - callback(); - }, - ], - ); - }); -}); - -describe('Connection test - validate default parameters', function () -{ - it('Valid "warehouse" parameter', function () - { - const output = stderr.inspectSync(() => - { +describe("Connection test - validate default parameters", function () { + it('Valid "warehouse" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - warehouse: 'testWarehouse', - validateDefaultParameters: true + warehouse: "testWarehouse", + validateDefaultParameters: true, }); }); assert.deepEqual(output, []); }); - it('Invalid "warehouse" parameter', function () - { - const output = stderr.inspectSync(() => - { + it('Invalid "warehouse" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - waerhouse: 'testWarehouse', - validateDefaultParameters: true + waerhouse: "testWarehouse", + validateDefaultParameters: true, }); }); - assert.deepEqual(output, - [ - "\"waerhouse\" is an unknown connection parameter\n", - "Did you mean \"warehouse\"\n" - ]); + assert.deepEqual(output, [ + '"waerhouse" is an unknown connection parameter\n', + 'Did you mean "warehouse"\n', + ]); }); - it('Valid "database" parameter', function () - { - const output = stderr.inspectSync(() => - { + it('Valid "database" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - database: 'testDatabase', - validateDefaultParameters: true + database: "testDatabase", + validateDefaultParameters: true, }); }); assert.deepEqual(output, []); }); - it('Invalid "db" parameter', function () - { - const output = stderr.inspectSync(() => - { + it('Invalid "db" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - db: 'testDb', - validateDefaultParameters: true + db: "testDb", + validateDefaultParameters: true, }); }); - assert.deepEqual(output, - [ - "\"db\" is an unknown connection parameter\n" - ]); + assert.deepEqual(output, ['"db" is an unknown connection parameter\n']); }); - it('Invalid "database" parameter', function () - { - const output = stderr.inspectSync(() => - { + it('Invalid "database" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - datbse: 'testDatabase', - validateDefaultParameters: true + datbse: "testDatabase", + validateDefaultParameters: true, }); }); - assert.deepEqual(output, - [ - "\"datbse\" is an unknown connection parameter\n", - "Did you mean \"database\"\n" - ]); + assert.deepEqual(output, [ + '"datbse" is an unknown connection parameter\n', + 'Did you mean "database"\n', + ]); }); - it('Valid "schema" parameter', function () - { - const output = stderr.inspectSync(() => - { + it('Valid "schema" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - schema: 'testSchema', - validateDefaultParameters: true + schema: "testSchema", + validateDefaultParameters: true, }); }); assert.deepEqual(output, []); }); - it('Invalid "schema" parameter', function () - { - const output = stderr.inspectSync(() => - { + it('Invalid "schema" parameter', function () { + const output = stderr.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, password: connOption.valid.password, - shcema: 'testSchema', - validateDefaultParameters: true + shcema: "testSchema", + validateDefaultParameters: true, }); }); - assert.deepEqual(output, - [ - "\"shcema\" is an unknown connection parameter\n", - "Did you mean \"schema\"\n" - ]); + assert.deepEqual(output, [ + '"shcema" is an unknown connection parameter\n', + 'Did you mean "schema"\n', + ]); }); }); -describe('Connection test - connection pool', function () -{ +describe("Connection test - connection pool", function () { this.timeout(30000); - it('1 min connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 10, - min: 1 - }); + it("1 min connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 10, + min: 1, + }); assert.equal(connectionPool.max, 10); assert.equal(connectionPool.min, 1); @@ -545,13 +227,11 @@ describe('Connection test - connection pool', function () done(); }); - it('5 min connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 10, - min: 5 - }); + it("5 min connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 10, + min: 5, + }); assert.equal(connectionPool.max, 10); assert.equal(connectionPool.min, 5); @@ -560,13 +240,11 @@ describe('Connection test - connection pool', function () done(); }); - it('10 min connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 10, - min: 10 - }); + it("10 min connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 10, + min: 10, + }); assert.equal(connectionPool.max, 10); assert.equal(connectionPool.min, 10); @@ -575,13 +253,11 @@ describe('Connection test - connection pool', function () done(); }); - it('min greater than max connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 10 - }); + it("min greater than max connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 10, + }); assert.equal(connectionPool.max, 5); assert.equal(connectionPool.min, 5); @@ -590,13 +266,11 @@ describe('Connection test - connection pool', function () done(); }); - it('1 max connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 1, - min: 0 - }); + it("1 max connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 1, + min: 0, + }); assert.equal(connectionPool.max, 1); assert.equal(connectionPool.min, 0); @@ -608,13 +282,11 @@ describe('Connection test - connection pool', function () done(); }); - it('1 max connection and acquire() more than 1', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 1, - min: 0 - }); + it("1 max connection and acquire() more than 1", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 1, + min: 0, + }); assert.equal(connectionPool.max, 1); assert.equal(connectionPool.min, 0); @@ -628,13 +300,11 @@ describe('Connection test - connection pool', function () done(); }); - it('5 max connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + it("5 max connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); assert.equal(connectionPool.max, 5); assert.equal(connectionPool.min, 0); @@ -654,13 +324,11 @@ describe('Connection test - connection pool', function () done(); }); - it('5 max connections and acquire() more than 5', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + it("5 max connections and acquire() more than 5", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); assert.equal(connectionPool.max, 5); assert.equal(connectionPool.min, 0); @@ -682,13 +350,11 @@ describe('Connection test - connection pool', function () done(); }); - it('10 max connection', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 10, - min: 0 - }); + it("10 max connection", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 10, + min: 0, + }); assert.equal(connectionPool.max, 10); assert.equal(connectionPool.min, 0); @@ -718,13 +384,11 @@ describe('Connection test - connection pool', function () done(); }); - it('10 max connections and acquire() more than 10', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 10, - min: 0 - }); + it("10 max connections and acquire() more than 10", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 10, + min: 0, + }); assert.equal(connectionPool.max, 10); assert.equal(connectionPool.min, 0); @@ -756,13 +420,11 @@ describe('Connection test - connection pool', function () done(); }); - it('acquire() 1 connection and release()', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + it("acquire() 1 connection and release()", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); // Acquire one connection const resourcePromise1 = connectionPool.acquire(); @@ -773,34 +435,30 @@ describe('Connection test - connection pool', function () async.series( [ - function (callback) - { + function (callback) { // Once acquired, release the connection - resourcePromise1.then(function (connection) - { + resourcePromise1.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 0); - connectionPool.release(connection).then(() => - { + connectionPool.release(connection).then(() => { // One connection should be available for use assert.equal(connectionPool.available, 1); callback(); }); }); - } + }, ], - done); + done + ); }); - it('acquire() 5 connections and release()', function (done) - { + it("acquire() 5 connections and release()", function (done) { // Create the connection pool - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); // Acquire 5 connections const resourcePromise1 = connectionPool.acquire(); @@ -815,92 +473,76 @@ describe('Connection test - connection pool', function () async.series( [ - function (callback) - { + function (callback) { // Once acquired, release the connection - resourcePromise1.then(function (connection) - { + resourcePromise1.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 4); - connectionPool.release(connection).then(() => - { + connectionPool.release(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, release the connection - resourcePromise2.then(function (connection) - { + resourcePromise2.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 3); - connectionPool.release(connection).then(() => - { + connectionPool.release(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, release the connection - resourcePromise3.then(function (connection) - { + resourcePromise3.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 2); - connectionPool.release(connection).then(() => - { + connectionPool.release(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, release the connection - resourcePromise4.then(function (connection) - { + resourcePromise4.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 1); - connectionPool.release(connection).then(() => - { + connectionPool.release(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, release the connection - resourcePromise5.then(function (connection) - { + resourcePromise5.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 0); - connectionPool.release(connection).then(() => - { + connectionPool.release(connection).then(() => { assert.equal(connectionPool.available, 1); callback(); }); }); }, ], - done); + done + ); }); - it('acquire() 1 connection and destroy()', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + it("acquire() 1 connection and destroy()", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); // Acquire a connection const resourcePromise1 = connectionPool.acquire(); @@ -910,13 +552,11 @@ describe('Connection test - connection pool', function () assert.equal(connectionPool.spareResourceCapacity, 4); // Once acquired, destroy the connection - resourcePromise1.then(function (connection) - { + resourcePromise1.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 0); - connectionPool.destroy(connection).then(() => - { + connectionPool.destroy(connection).then(() => { // No connection should be available for use assert.equal(connectionPool.available, 0); done(); @@ -924,13 +564,11 @@ describe('Connection test - connection pool', function () }); }); - it('acquire() 5 connections and destroy()', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + it("acquire() 5 connections and destroy()", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); // Acquire 5 connections const resourcePromise1 = connectionPool.acquire(); @@ -945,234 +583,201 @@ describe('Connection test - connection pool', function () async.series( [ - function (callback) - { + function (callback) { // Once acquired, destroy the connection - resourcePromise1.then(function (connection) - { + resourcePromise1.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 4); - connectionPool.destroy(connection).then(() => - { + connectionPool.destroy(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, destroy the connection - resourcePromise2.then(function (connection) - { + resourcePromise2.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 3); - connectionPool.destroy(connection).then(() => - { + connectionPool.destroy(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, destroy the connection - resourcePromise3.then(function (connection) - { + resourcePromise3.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 2); - connectionPool.destroy(connection).then(() => - { + connectionPool.destroy(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, destroy the connection - resourcePromise4.then(function (connection) - { + resourcePromise4.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 1); - connectionPool.destroy(connection).then(() => - { + connectionPool.destroy(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, - function (callback) - { + function (callback) { // Once acquired, destroy the connection - resourcePromise5.then(function (connection) - { + resourcePromise5.then(function (connection) { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.pending, 0); - connectionPool.destroy(connection).then(() => - { + connectionPool.destroy(connection).then(() => { assert.equal(connectionPool.available, 0); callback(); }); }); }, ], - done); + done + ); }); - it('use()', function (done) - { - var connectionPool = snowflake.createPool(connOption.valid, - { - max: 5, - min: 0 - }); + it("use()", function (done) { + var connectionPool = snowflake.createPool(connOption.valid, { + max: 5, + min: 0, + }); assert.equal(connectionPool.size, 0); // Use the connection pool, automatically creates a new connection - connectionPool.use(async (connection) => - { - assert.ok(connection.isUp(), "not active"); - assert.equal(connectionPool.size, 1); - assert.equal(connectionPool.pending, 0); - assert.equal(connectionPool.spareResourceCapacity, 4); - assert.equal(connectionPool.available, 0); - }).then(() => - { - assert.equal(connectionPool.size, 1); - assert.equal(connectionPool.available, 1); - - // Use the connection pool, will use the existing connection - connectionPool.use(async (connection) => - { + connectionPool + .use(async (connection) => { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.size, 1); assert.equal(connectionPool.pending, 0); assert.equal(connectionPool.spareResourceCapacity, 4); assert.equal(connectionPool.available, 0); - }).then(() => - { + }) + .then(() => { assert.equal(connectionPool.size, 1); assert.equal(connectionPool.available, 1); - done(); + + // Use the connection pool, will use the existing connection + connectionPool + .use(async (connection) => { + assert.ok(connection.isUp(), "not active"); + assert.equal(connectionPool.size, 1); + assert.equal(connectionPool.pending, 0); + assert.equal(connectionPool.spareResourceCapacity, 4); + assert.equal(connectionPool.available, 0); + }) + .then(() => { + assert.equal(connectionPool.size, 1); + assert.equal(connectionPool.available, 1); + done(); + }); }); - }); }); - it('wrong password - use', async function () - { - var connectionPool = snowflake.createPool(connOption.wrongPwd, - { - max: 10, - min: 1 - }); + it("wrong password - use", async function () { + var connectionPool = snowflake.createPool(connOption.wrongPwd, { + max: 10, + min: 1, + }); assert.equal(connectionPool.max, 10); assert.equal(connectionPool.min, 1); assert.equal(connectionPool.size, 1); - try - { + try { // Use the connection pool, automatically creates a new connection - await connectionPool.use(async (connection) => - { + await connectionPool.use(async (connection) => { assert.ok(connection.isUp(), "not active"); assert.equal(connectionPool.size, 1); }); - } - catch (err) - { - assert.strictEqual(err.message, "Incorrect username or password was specified."); + } catch (err) { + assert.strictEqual( + err.message, + "Incorrect username or password was specified." + ); } }); - it('wrong password - acquire', async function () - { - var connectionPool = snowflake.createPool(connOption.wrongPwd, - { - max: 10, - min: 1 - }); - - assert.equal(connectionPool.max, 10); - assert.equal(connectionPool.min, 1); - assert.equal(connectionPool.size, 1); - - try - { - await connectionPool.acquire(); - } - catch (err) - { - assert.strictEqual(err.message, "Incorrect username or password was specified."); - } + it("wrong password - acquire", async function () { + var connectionPool = snowflake.createPool(connOption.wrongPwd, { + max: 10, + min: 1, }); + + assert.equal(connectionPool.max, 10); + assert.equal(connectionPool.min, 1); + assert.equal(connectionPool.size, 1); + + try { + await connectionPool.acquire(); + } catch (err) { + assert.strictEqual( + err.message, + "Incorrect username or password was specified." + ); + } + }); }); -describe('Connection Test - Heartbeat', () => -{ +describe("Connection Test - Heartbeat", () => { let connection; - before(async () => - { + before(async () => { connection = snowflake.createConnection(connOption.valid); await testUtil.connectAsync(connection); }); - after(async () => - { + after(async () => { await testUtil.destroyConnectionAsync(connection); }); - it('call heartbeat url with default callback', () => - { + it("call heartbeat url with default callback", () => { connection.heartbeat(); }); - it('call heartbeat url with callback', done => - { - connection.heartbeat(err => err ? done(err) : done()); + it("call heartbeat url with callback", (done) => { + connection.heartbeat((err) => (err ? done(err) : done())); }); - it('call heartbeat url as promise', async () => - { + it("call heartbeat url as promise", async () => { const rows = await connection.heartbeatAsync(); - assert.deepEqual(rows, [ { '1': 1 } ]); + assert.deepEqual(rows, [{ 1: 1 }]); }); }); -describe('Connection Test - isValid', () => -{ +describe("Connection Test - isValid", () => { let connection; - beforeEach(async () => - { + beforeEach(async () => { connection = snowflake.createConnection(connOption.valid); await testUtil.connectAsync(connection); }); - afterEach(async () => - { - if (connection.isUp()) - { + afterEach(async () => { + if (connection.isUp()) { await testUtil.destroyConnectionAsync(connection); } }); - it('connection is valid after connect', async () => - { + it("connection is valid after connect", async () => { const result = await connection.isValidAsync(); assert.equal(result, true); }); - it('connection is invalid after destroy', async () => - { + it("connection is invalid after destroy", async () => { await testUtil.destroyConnectionAsync(connection); const result = await connection.isValidAsync(); diff --git a/test/integration/testManualConnection.js b/test/integration/testManualConnection.js new file mode 100644 index 000000000..ce05e7ab1 --- /dev/null +++ b/test/integration/testManualConnection.js @@ -0,0 +1,262 @@ +const snowflake = require("./../../lib/snowflake"); +const async = require("async"); +const assert = require("assert"); +const connOption = require("./connectionOptions"); +const testUtil = require("./testUtil"); + +if (process.env.RUN_MANUAL_TESTS_ONLY == "true") { + describe.only("Run manual tests", function () { + describe("Connection test - external browser", function () { + it("Simple Connect", function (done) { + const connection = snowflake.createConnection( + connOption.externalBrowser + ); + + connection.connectAsync(function (err, connection) { + try { + assert.ok(connection.isUp(), "not active"); + testUtil.destroyConnection(connection, function (err, r) { + try { + assert.ok(!connection.isUp(), "not active"); + done(); + } catch (err) { + done(err); + } + }); + } catch (err) { + done(err); + } + }); + }); + + it("Connect - external browser timeout", function (done) { + const connection = snowflake.createConnection( + connOption.externalBrowserWithShortTimeout + ); + + connection.connectAsync(function (err) { + try { + const browserActionTimeout = + connOption.externalBrowserWithShortTimeout.browserActionTimeout; + assert.ok( + err, + `Browser action timed out after ${browserActionTimeout} ms.` + ); + done(); + } catch (err) { + done(err); + } + }); + }); + + it("Mismatched Username", function (done) { + const connection = snowflake.createConnection( + connOption.externalBrowserMismatchUser + ); + connection.connectAsync(function (err) { + try { + assert.ok( + err, + "Logged in with different user than one on connection string" + ); + assert.equal( + "The user you were trying to authenticate as differs from the user currently logged in at the IDP.", + err["message"] + ); + done(); + } catch (err) { + done(err); + } + }); + }); + }); + + describe("Connection test - oauth", function () { + it("Simple Connect", function (done) { + const connection = snowflake.createConnection(connOption.oauth); + + async.series([ + function (callback) { + connection.connect(function (err) { + done(err); + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(connection.isUp(), "not active"); + callback(); + }, + function (callback) { + connection.destroy(function (err) { + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(!connection.isUp(), "still active"); + callback(); + }, + ]); + }); + + it("Mismatched Username", function (done) { + const connection = snowflake.createConnection( + connOption.oauthMismatchUser + ); + connection.connect(function (err) { + try { + assert.ok( + err, + "Logged in with different user than one on connection string" + ); + assert.equal( + "The user you were trying to authenticate as differs from the user tied to the access token.", + err["message"] + ); + done(); + } catch (err) { + done(err); + } + }); + }); + }); + + describe("Connection test - okta", function () { + it("Simple Connect", function (done) { + const connection = snowflake.createConnection(connOption.okta); + + async.series([ + function (callback) { + connection.connectAsync(function (err) { + done(err); + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(connection.isUp(), "not active"); + callback(); + }, + function (callback) { + connection.destroy(function (err) { + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(!connection.isUp(), "still active"); + callback(); + }, + ]); + }); + }); + + describe("Connection test - keypair", function () { + it("Simple Connect - specify private key", function (done) { + const connection = snowflake.createConnection( + connOption.keypairPrivateKey + ); + + async.series([ + function (callback) { + connection.connect(function (err) { + done(err); + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(connection.isUp(), "not active"); + callback(); + }, + function (callback) { + connection.destroy(function (err) { + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(!connection.isUp(), "still active"); + callback(); + }, + ]); + }); + + it("Simple Connect - specify encrypted private key path and passphrase", function (done) { + const connection = snowflake.createConnection( + connOption.keypairPathEncrypted + ); + + async.series([ + function (callback) { + connection.connect(function (err) { + done(err); + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(connection.isUp(), "not active"); + callback(); + }, + function (callback) { + connection.destroy(function (err) { + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(!connection.isUp(), "still active"); + callback(); + }, + ]); + }); + + it("Simple Connect - specify unencrypted private key path without passphrase", function (done) { + const connection = snowflake.createConnection( + connOption.keypairPathEncrypted + ); + + async.series([ + function (callback) { + connection.connect(function (err) { + done(err); + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(connection.isUp(), "not active"); + callback(); + }, + function (callback) { + connection.destroy(function (err) { + assert.ok(!err, JSON.stringify(err)); + callback(); + }); + }, + function (callback) { + assert.ok(!connection.isUp(), "still active"); + callback(); + }, + ]); + }); + + it("Wrong JWT token", function (done) { + const connection = snowflake.createConnection( + connOption.keypairWrongToken + ); + connection.connect(function (err) { + try { + assert.ok(err, "Incorrect JWT token is passed."); + assert.equal("JWT token is invalid.", err["message"]); + done(); + } catch (err) { + done(err); + } + }); + }); + }); + }); +}