-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdynamic-password.js
113 lines (107 loc) · 3.34 KB
/
dynamic-password.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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', () => {
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', () => {
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', () => {
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', () => {
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', () => {
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()
})
})