Skip to content

Commit 2d7f9fd

Browse files
committed
test: Add tests for dynamic password
1 parent 1d331b4 commit 2d7f9fd

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict'
2+
const assert = require('assert')
3+
const helper = require('./../test-helper')
4+
const suite = new helper.Suite()
5+
const pg = require('../../../lib/index');
6+
const Client = pg.Client;
7+
8+
const password = process.env.PGPASSWORD || null;
9+
const sleep = millis => new Promise(resolve => setTimeout(resolve, millis))
10+
11+
suite.testAsync('Get password from a sync function', function () {
12+
let wasCalled = false
13+
function getPassword() {
14+
wasCalled = true
15+
return password;
16+
}
17+
const client = new Client({
18+
password: getPassword,
19+
})
20+
return client.connect()
21+
.then(() => {
22+
assert.ok(wasCalled, 'Our password function should have been called')
23+
return client.end()
24+
})
25+
})
26+
27+
suite.testAsync('Throw error from a sync function', function () {
28+
let wasCalled = false
29+
const myError = new Error('Oops!')
30+
function getPassword() {
31+
wasCalled = true
32+
throw myError
33+
}
34+
const client = new Client({
35+
password: getPassword,
36+
})
37+
let wasThrown = false
38+
return client.connect()
39+
.catch(err => {
40+
assert.equal(err, myError, 'Our sync error should have been thrown')
41+
wasThrown = true
42+
})
43+
.then(() => {
44+
assert.ok(wasCalled, 'Our password function should have been called')
45+
assert.ok(wasThrown, 'Our error should have been thrown')
46+
return client.end()
47+
})
48+
})
49+
50+
suite.testAsync('Get password from a function asynchronously', function () {
51+
let wasCalled = false
52+
function getPassword() {
53+
wasCalled = true
54+
return sleep(100).then(() => password)
55+
}
56+
const client = new Client({
57+
password: getPassword,
58+
})
59+
return client.connect()
60+
.then(() => {
61+
assert.ok(wasCalled, 'Our password function should have been called')
62+
return client.end()
63+
})
64+
})
65+
66+
suite.testAsync('Throw error from an async function', function () {
67+
let wasCalled = false
68+
const myError = new Error('Oops!')
69+
function getPassword() {
70+
wasCalled = true
71+
return sleep(100).then(() => {
72+
throw myError
73+
})
74+
}
75+
const client = new Client({
76+
password: getPassword,
77+
})
78+
let wasThrown = false
79+
return client.connect()
80+
.catch(err => {
81+
assert.equal(err, myError, 'Our async error should have been thrown')
82+
wasThrown = true
83+
})
84+
.then(() => {
85+
assert.ok(wasCalled, 'Our password function should have been called')
86+
assert.ok(wasThrown, 'Our error should have been thrown')
87+
return client.end()
88+
})
89+
})
90+
91+
suite.testAsync('Password function must return a string', function () {
92+
let wasCalled = false
93+
function getPassword() {
94+
wasCalled = true
95+
// Return a password that is not a string
96+
return 12345
97+
}
98+
const client = new Client({
99+
password: getPassword,
100+
})
101+
let wasThrown = false
102+
return client.connect()
103+
.catch(err => {
104+
assert.ok(err instanceof TypeError, 'A TypeError should have been thrown')
105+
assert.equal(err.message, 'Password must be a string')
106+
wasThrown = true
107+
})
108+
.then(() => {
109+
assert.ok(wasCalled, 'Our password function should have been called')
110+
assert.ok(wasThrown, 'Our error should have been thrown')
111+
return client.end()
112+
})
113+
})

0 commit comments

Comments
 (0)