|
1 | 1 | 'use strict' |
2 | | -var helper = require(__dirname + '/../test-helper') |
3 | | -var pg = helper.pg |
| 2 | +const helper = require('./../test-helper') |
| 3 | +const pg = helper.pg |
| 4 | +const suite = new helper.Suite() |
| 5 | +const { native } = helper.args |
4 | 6 |
|
5 | | -var suite = new helper.Suite() |
| 7 | +/** |
| 8 | + * This test only executes if the env variables SCRAM_TEST_PGUSER and |
| 9 | + * SCRAM_TEST_PGPASSWORD are defined. You can override additional values |
| 10 | + * for the host, port and database with other SCRAM_TEST_ prefixed vars. |
| 11 | + * If the variables are not defined the test will be skipped. |
| 12 | + * |
| 13 | + * SQL to create test role: |
| 14 | + * |
| 15 | + * SET password_encryption = 'scram-sha-256'; |
| 16 | + * CREATE ROLE scram_test login password 'test4scram'; |
| 17 | + * |
| 18 | + * Add the following entries to pg_hba.conf: |
| 19 | + * |
| 20 | + * host all scram_test ::1/128 scram-sha-256 |
| 21 | + * host all scram_test 0.0.0.0/0 scram-sha-256 |
| 22 | + * |
| 23 | + * Then run this file with after exporting: |
| 24 | + * |
| 25 | + * SCRAM_TEST_PGUSER=scram_test |
| 26 | + * SCRAM_TEST_PGPASSWORD=test4scram |
| 27 | + */ |
6 | 28 |
|
7 | | -/* |
8 | | -SQL to create test role: |
| 29 | +// Base config for SCRAM tests |
| 30 | +const config = { |
| 31 | + user: process.env.SCRAM_TEST_PGUSER, |
| 32 | + password: process.env.SCRAM_TEST_PGPASSWORD, |
| 33 | + host: process.env.SCRAM_TEST_PGHOST, // optional |
| 34 | + port: process.env.SCRAM_TEST_PGPORT, // optional |
| 35 | + database: process.env.SCRAM_TEST_PGDATABASE, // optional |
| 36 | +} |
9 | 37 |
|
10 | | -set password_encryption = 'scram-sha-256'; |
11 | | -create role npgtest login password 'test'; |
| 38 | +if (native) { |
| 39 | + suite.testAsync('skipping SCRAM tests (on native)', () => {}) |
| 40 | + return |
| 41 | +} |
| 42 | +if (!config.user || !config.password) { |
| 43 | + suite.testAsync('skipping SCRAM tests (missing env)', () => {}) |
| 44 | + return |
| 45 | +} |
12 | 46 |
|
13 | | -pg_hba: |
14 | | -host all npgtest ::1/128 scram-sha-256 |
15 | | -host all npgtest 0.0.0.0/0 scram-sha-256 |
16 | | -
|
17 | | -
|
18 | | -*/ |
19 | | -/* |
20 | | -suite.test('can connect using sasl/scram', function () { |
21 | | -var connectionString = 'pg://npgtest:test@localhost/postgres' |
22 | | -const pool = new pg.Pool({ connectionString: connectionString }) |
23 | | -pool.connect( |
24 | | -assert.calls(function (err, client, done) { |
25 | | -assert.ifError(err, 'should have connected') |
26 | | -done() |
27 | | -}) |
28 | | -) |
| 47 | +suite.testAsync('can connect using sasl/scram', async () => { |
| 48 | + const client = new pg.Client(config) |
| 49 | + let usingSasl = false |
| 50 | + client.connection.once('authenticationSASL', () => { |
| 51 | + usingSasl = true |
| 52 | + }) |
| 53 | + await client.connect() |
| 54 | + assert.ok(usingSasl, 'Should be using SASL for authentication') |
| 55 | + await client.end() |
29 | 56 | }) |
30 | 57 |
|
31 | | -suite.test('sasl/scram fails when password is wrong', function () { |
32 | | -var connectionString = 'pg://npgtest:bad@localhost/postgres' |
33 | | -const pool = new pg.Pool({ connectionString: connectionString }) |
34 | | -pool.connect( |
35 | | -assert.calls(function (err, client, done) { |
36 | | -assert.ok(err, 'should have a connection error') |
37 | | -done() |
38 | | -}) |
39 | | -) |
| 58 | +suite.testAsync('sasl/scram fails when password is wrong', async () => { |
| 59 | + const client = new pg.Client({ |
| 60 | + ...config, |
| 61 | + password: config.password + 'append-something-to-make-it-bad', |
| 62 | + }) |
| 63 | + let usingSasl = false |
| 64 | + client.connection.once('authenticationSASL', () => { |
| 65 | + usingSasl = true |
| 66 | + }) |
| 67 | + await assert.rejects( |
| 68 | + () => client.connect(), |
| 69 | + { |
| 70 | + code: '28P01', |
| 71 | + }, |
| 72 | + 'Error code should be for a password error' |
| 73 | + ) |
| 74 | + assert.ok(usingSasl, 'Should be using SASL for authentication') |
40 | 75 | }) |
41 | | -*/ |
|
0 commit comments