|
| 1 | +const helper = require('./test-helper') |
| 2 | +const assert = require('assert') |
| 3 | +const suite = new helper.Suite() |
| 4 | +const pgpass = require('pgpass') |
| 5 | + |
| 6 | +class Wait { |
| 7 | + constructor() { |
| 8 | + this.promise = new Promise((resolve) => { |
| 9 | + this.resolve = resolve |
| 10 | + }) |
| 11 | + } |
| 12 | + |
| 13 | + until() { |
| 14 | + return this.promise |
| 15 | + } |
| 16 | + |
| 17 | + done(time) { |
| 18 | + if (time) { |
| 19 | + setTimeout(this.resolve.bind(this), time) |
| 20 | + } else { |
| 21 | + this.resolve() |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +suite.test('password callback is called with conenction params', async function () { |
| 27 | + const wait = new Wait() |
| 28 | + const client = helper.client({ |
| 29 | + user: 'foo', |
| 30 | + database: 'bar', |
| 31 | + host: 'baz', |
| 32 | + password: async (params) => { |
| 33 | + assert.equal(params.user, 'foo') |
| 34 | + assert.equal(params.database, 'bar') |
| 35 | + assert.equal(params.host, 'baz') |
| 36 | + wait.done(10) |
| 37 | + return 'password' |
| 38 | + }, |
| 39 | + }) |
| 40 | + client.connection.emit('authenticationCleartextPassword') |
| 41 | + await wait.until() |
| 42 | + assert.equal(client.user, 'foo') |
| 43 | + assert.equal(client.database, 'bar') |
| 44 | + assert.equal(client.host, 'baz') |
| 45 | + assert.equal(client.connectionParameters.password, 'password') |
| 46 | +}) |
| 47 | + |
| 48 | +suite.test('cleartext password auth does not crash with null password using pg-pass', async function () { |
| 49 | + process.env.PGPASSFILE = `${__dirname}/pgpass.file` |
| 50 | + // set this to undefined so pgpass will use the file |
| 51 | + delete process.env.PGPASSWORD |
| 52 | + const wait = new Wait() |
| 53 | + const client = helper.client({ |
| 54 | + host: 'foo', |
| 55 | + port: 5432, |
| 56 | + database: 'bar', |
| 57 | + user: 'baz', |
| 58 | + password: (params) => { |
| 59 | + return new Promise((resolve) => { |
| 60 | + pgpass(params, (pass) => { |
| 61 | + wait.done(10) |
| 62 | + resolve(pass) |
| 63 | + }) |
| 64 | + }) |
| 65 | + }, |
| 66 | + }) |
| 67 | + client.connection.emit('authenticationCleartextPassword') |
| 68 | + await wait.until() |
| 69 | + assert.equal(client.password, 'quz') |
| 70 | +}) |
0 commit comments