Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/pg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bench:
@find benchmark -name "*-bench.js" | $(node-command)

test-unit:
@chmod 600 test/unit/client/pgpass.file
@find test/unit -name "*-tests.js" | $(node-command)

test-connection:
Expand Down
2 changes: 1 addition & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class Client extends EventEmitter {
if (typeof this.password === 'function') {
this._Promise
.resolve()
.then(() => this.password())
.then(() => this.password(this.connectionParameters))
.then((pass) => {
if (pass !== undefined) {
if (typeof pass !== 'string') {
Expand Down
70 changes: 70 additions & 0 deletions packages/pg/test/unit/client/password-callback-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const helper = require('./test-helper')
const assert = require('assert')
const suite = new helper.Suite()
const pgpass = require('pgpass')

class Wait {
constructor() {
this.promise = new Promise((resolve) => {
this.resolve = resolve
})
}

until() {
return this.promise
}

done(time) {
if (time) {
setTimeout(this.resolve.bind(this), time)
} else {
this.resolve()
}
}
}

suite.test('password callback is called with conenction params', async function () {
const wait = new Wait()
const client = helper.client({
user: 'foo',
database: 'bar',
host: 'baz',
password: async (params) => {
assert.equal(params.user, 'foo')
assert.equal(params.database, 'bar')
assert.equal(params.host, 'baz')
wait.done(10)
return 'password'
},
})
client.connection.emit('authenticationCleartextPassword')
await wait.until()
assert.equal(client.user, 'foo')
assert.equal(client.database, 'bar')
assert.equal(client.host, 'baz')
assert.equal(client.connectionParameters.password, 'password')
})

suite.test('cleartext password auth does not crash with null password using pg-pass', async function () {
process.env.PGPASSFILE = `${__dirname}/pgpass.file`
// set this to undefined so pgpass will use the file
delete process.env.PGPASSWORD
const wait = new Wait()
const client = helper.client({
host: 'foo',
port: 5432,
database: 'bar',
user: 'baz',
password: (params) => {
return new Promise((resolve) => {
pgpass(params, (pass) => {
wait.done(10)
resolve(pass)
})
})
},
})
client.connection.emit('authenticationCleartextPassword')
await wait.until()
assert.equal(client.password, 'quz')
})
4 changes: 2 additions & 2 deletions packages/pg/test/unit/client/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const helper = require('../test-helper')
const Connection = require('../../../lib/connection')
const { Client } = helper

const makeClient = function () {
const makeClient = function (config) {
const connection = new Connection({ stream: 'no' })
connection.startup = function () {}
connection.connect = function () {}
connection.query = function (text) {
this.queries.push(text)
}
connection.queries = []
const client = new Client({ connection: connection })
const client = new Client({ connection: connection, ...config })
client.connect()
client.connection.emit('connect')
return client
Expand Down