From 2d7f9fd4c03dce3ff403b80d165f21077f851c36 Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Thu, 18 Jul 2019 15:12:58 -0400 Subject: [PATCH] test: Add tests for dynamic password --- .../connection/dynamic-password.js | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 test/integration/connection/dynamic-password.js diff --git a/test/integration/connection/dynamic-password.js b/test/integration/connection/dynamic-password.js new file mode 100644 index 000000000..852d24ae8 --- /dev/null +++ b/test/integration/connection/dynamic-password.js @@ -0,0 +1,113 @@ +'use strict' +const assert = require('assert') +const helper = require('./../test-helper') +const suite = new helper.Suite() +const pg = require('../../../lib/index'); +const Client = pg.Client; + +const password = process.env.PGPASSWORD || null; +const sleep = millis => new Promise(resolve => setTimeout(resolve, millis)) + +suite.testAsync('Get password from a sync function', function () { + let wasCalled = false + function getPassword() { + wasCalled = true + return password; + } + const client = new Client({ + password: getPassword, + }) + return client.connect() + .then(() => { + assert.ok(wasCalled, 'Our password function should have been called') + return client.end() + }) +}) + +suite.testAsync('Throw error from a sync function', function () { + let wasCalled = false + const myError = new Error('Oops!') + function getPassword() { + wasCalled = true + throw myError + } + const client = new Client({ + password: getPassword, + }) + let wasThrown = false + return client.connect() + .catch(err => { + assert.equal(err, myError, 'Our sync error should have been thrown') + wasThrown = true + }) + .then(() => { + assert.ok(wasCalled, 'Our password function should have been called') + assert.ok(wasThrown, 'Our error should have been thrown') + return client.end() + }) +}) + +suite.testAsync('Get password from a function asynchronously', function () { + let wasCalled = false + function getPassword() { + wasCalled = true + return sleep(100).then(() => password) + } + const client = new Client({ + password: getPassword, + }) + return client.connect() + .then(() => { + assert.ok(wasCalled, 'Our password function should have been called') + return client.end() + }) +}) + +suite.testAsync('Throw error from an async function', function () { + let wasCalled = false + const myError = new Error('Oops!') + function getPassword() { + wasCalled = true + return sleep(100).then(() => { + throw myError + }) + } + const client = new Client({ + password: getPassword, + }) + let wasThrown = false + return client.connect() + .catch(err => { + assert.equal(err, myError, 'Our async error should have been thrown') + wasThrown = true + }) + .then(() => { + assert.ok(wasCalled, 'Our password function should have been called') + assert.ok(wasThrown, 'Our error should have been thrown') + return client.end() + }) +}) + +suite.testAsync('Password function must return a string', function () { + let wasCalled = false + function getPassword() { + wasCalled = true + // Return a password that is not a string + return 12345 + } + const client = new Client({ + password: getPassword, + }) + let wasThrown = false + return client.connect() + .catch(err => { + assert.ok(err instanceof TypeError, 'A TypeError should have been thrown') + assert.equal(err.message, 'Password must be a string') + wasThrown = true + }) + .then(() => { + assert.ok(wasCalled, 'Our password function should have been called') + assert.ok(wasThrown, 'Our error should have been thrown') + return client.end() + }) +})