forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreation-tests.js
361 lines (330 loc) · 11.2 KB
/
creation-tests.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
'use strict'
const helper = require('../test-helper')
const assert = require('assert')
const ConnectionParameters = require('../../../lib/connection-parameters')
const defaults = require('../../../lib').defaults
const dns = require('dns')
// clear process.env
for (var key in process.env) {
delete process.env[key]
}
const suite = new helper.Suite()
suite.test('ConnectionParameters construction', function () {
assert.ok(new ConnectionParameters(), 'with null config')
assert.ok(new ConnectionParameters({ user: 'asdf' }), 'with config object')
assert.ok(new ConnectionParameters('postgres://localhost/postgres'), 'with connection string')
})
var compare = function (actual, expected, type) {
const expectedDatabase = expected.database === undefined ? expected.user : expected.database
assert.equal(actual.user, expected.user, type + ' user')
assert.equal(actual.database, expectedDatabase, type + ' database')
assert.equal(actual.port, expected.port, type + ' port')
assert.equal(actual.host, expected.host, type + ' host')
assert.equal(actual.password, expected.password, type + ' password')
assert.equal(actual.binary, expected.binary, type + ' binary')
assert.equal(actual.statement_timeout, expected.statement_timeout, type + ' statement_timeout')
assert.equal(actual.lock_timeout, expected.lock_timeout, type + ' lock_timeout')
assert.equal(actual.options, expected.options, type + ' options')
assert.equal(
actual.idle_in_transaction_session_timeout,
expected.idle_in_transaction_session_timeout,
type + ' idle_in_transaction_session_timeout'
)
}
suite.test('ConnectionParameters initializing from defaults', function () {
var subject = new ConnectionParameters()
compare(subject, defaults, 'defaults')
assert.ok(subject.isDomainSocket === false)
})
suite.test('ConnectionParameters initializing from defaults with connectionString set', function () {
var config = {
user: 'brians-are-the-best',
database: 'scoobysnacks',
port: 7777,
password: 'mypassword',
host: 'foo.bar.net',
binary: defaults.binary,
statement_timeout: false,
lock_timeout: false,
idle_in_transaction_session_timeout: false,
options: '-c geqo=off',
}
var original_value = defaults.connectionString
// Just changing this here doesn't actually work because it's no longer in scope when viewed inside of
// of ConnectionParameters() so we have to pass in the defaults explicitly to test it
defaults.connectionString =
'postgres://brians-are-the-best:[email protected]:7777/scoobysnacks?options=-c geqo=off'
var subject = new ConnectionParameters(defaults)
defaults.connectionString = original_value
compare(subject, config, 'defaults-connectionString')
})
suite.test('ConnectionParameters initializing from config', function () {
var config = {
user: 'brian',
database: 'home',
port: 7777,
password: 'pizza',
binary: true,
encoding: 'utf8',
host: 'yo',
ssl: {
asdf: 'blah',
},
statement_timeout: 15000,
lock_timeout: 15000,
idle_in_transaction_session_timeout: 15000,
options: '-c geqo=off',
}
var subject = new ConnectionParameters(config)
compare(subject, config, 'config')
assert.ok(subject.isDomainSocket === false)
})
suite.test('ConnectionParameters initializing from config and config.connectionString', function () {
var subject1 = new ConnectionParameters({
connectionString: 'postgres://test@host/db',
})
var subject2 = new ConnectionParameters({
connectionString: 'postgres://test@host/db?ssl=1',
})
var subject3 = new ConnectionParameters({
connectionString: 'postgres://test@host/db',
ssl: true,
})
var subject4 = new ConnectionParameters({
connectionString: 'postgres://test@host/db?ssl=1',
ssl: false,
})
assert.equal(subject1.ssl, false)
assert.equal(subject2.ssl, true)
assert.equal(subject3.ssl, true)
assert.equal(subject4.ssl, true)
})
suite.test('escape spaces if present', function () {
var subject = new ConnectionParameters('postgres://localhost/post gres')
assert.equal(subject.database, 'post gres')
})
suite.test('do not double escape spaces', function () {
var subject = new ConnectionParameters('postgres://localhost/post%20gres')
assert.equal(subject.database, 'post gres')
})
suite.test('initializing with unix domain socket', function () {
var subject = new ConnectionParameters('/var/run/')
assert.ok(subject.isDomainSocket)
assert.equal(subject.host, '/var/run/')
assert.equal(subject.database, defaults.user)
})
suite.test('initializing with unix domain socket and a specific database, the simple way', function () {
var subject = new ConnectionParameters('/var/run/ mydb')
assert.ok(subject.isDomainSocket)
assert.equal(subject.host, '/var/run/')
assert.equal(subject.database, 'mydb')
})
suite.test('initializing with unix domain socket, the health way', function () {
var subject = new ConnectionParameters('socket:/some path/?db=my[db]&encoding=utf8')
assert.ok(subject.isDomainSocket)
assert.equal(subject.host, '/some path/')
assert.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"')
assert.equal(subject.client_encoding, 'utf8')
})
suite.test('initializing with unix domain socket, the escaped health way', function () {
var subject = new ConnectionParameters('socket:/some%20path/?db=my%2Bdb&encoding=utf8')
assert.ok(subject.isDomainSocket)
assert.equal(subject.host, '/some path/')
assert.equal(subject.database, 'my+db')
assert.equal(subject.client_encoding, 'utf8')
})
var checkForPart = function (array, part) {
assert.ok(array.indexOf(part) > -1, array.join(' ') + ' did not contain ' + part)
}
const getDNSHost = async function (host) {
return new Promise((resolve, reject) => {
dns.lookup(host, (err, addresses) => {
err ? reject(err) : resolve(addresses)
})
})
}
suite.testAsync('builds simple string', async function () {
var config = {
user: 'brian',
password: 'xyz',
host: 'localhost',
port: 888,
database: 'bam',
}
var subject = new ConnectionParameters(config)
const dnsHost = await getDNSHost(config.host)
return new Promise((resolve) => {
subject.getLibpqConnectionString(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='brian'")
checkForPart(parts, "password='xyz'")
checkForPart(parts, `hostaddr='${dnsHost}'`)
checkForPart(parts, "port='888'")
checkForPart(parts, "dbname='bam'")
resolve()
})
})
})
suite.test('builds dns string', async function () {
var config = {
user: 'brian',
password: 'asdf',
host: 'localhost',
port: 5432,
}
var subject = new ConnectionParameters(config)
const dnsHost = await getDNSHost(config.host)
return new Promise((resolve) => {
subject.getLibpqConnectionString(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='brian'")
checkForPart(parts, `hostaddr='${dnsHost}'`)
resolve()
})
})
})
suite.test('error when dns fails', function () {
var config = {
user: 'brian',
password: 'asf',
host: 'asdlfkjasldfkksfd#!$!!!!..com',
port: 5432,
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert.ok(err)
assert.isNull(constring)
})
)
})
suite.test('connecting to unix domain socket', function () {
var config = {
user: 'brian',
password: 'asf',
host: '/tmp/',
port: 5432,
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='brian'")
checkForPart(parts, "host='/tmp/'")
})
)
})
suite.test('config contains quotes and backslashes', function () {
var config = {
user: 'not\\brian',
password: "bad'chars",
host: '/tmp/',
port: 5432,
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='not\\\\brian'")
checkForPart(parts, "password='bad\\'chars'")
})
)
})
suite.test('encoding can be specified by config', function () {
var config = {
client_encoding: 'utf-8',
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "client_encoding='utf-8'")
})
)
})
suite.test('password contains < and/or > characters', function () {
var sourceConfig = {
user: 'brian',
password: 'hello<ther>e',
host: 'localhost',
port: 5432,
database: 'postgres',
}
var connectionString =
'postgres://' +
sourceConfig.user +
':' +
sourceConfig.password +
'@' +
sourceConfig.host +
':' +
sourceConfig.port +
'/' +
sourceConfig.database
var subject = new ConnectionParameters(connectionString)
assert.equal(subject.password, sourceConfig.password)
})
suite.test('username or password contains weird characters', function () {
var defaults = require('../../../lib/defaults')
defaults.ssl = true
var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000'
var subject = new ConnectionParameters(strang)
assert.equal(subject.user, 'my f%irst name')
assert.equal(subject.password, 'is&%awesome!')
assert.equal(subject.host, 'localhost')
assert.equal(subject.ssl, true)
})
suite.test('url is properly encoded', function () {
var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl'
var subject = new ConnectionParameters(encoded)
assert.equal(subject.user, 'bi%na%%ry ')
assert.equal(subject.password, 's@f#')
assert.equal(subject.host, 'localhost')
assert.equal(subject.database, ' u%20rl')
})
suite.test('ssl is set on client', function () {
var Client = require('../../../lib/client')
var defaults = require('../../../lib/defaults')
defaults.ssl = true
var c = new Client('postgres://user:password@host/database')
assert(c.ssl, 'Client should have ssl enabled via defaults')
})
suite.test('coercing string "true" to boolean', function () {
const subject = new ConnectionParameters({ ssl: 'true' })
assert.strictEqual(subject.ssl, true)
})
suite.test('ssl is set on client', function () {
var sourceConfig = {
user: 'brian',
password: 'hello<ther>e',
host: 'localhost',
port: 5432,
database: 'postgres',
ssl: {
sslmode: 'verify-ca',
sslca: '/path/ca.pem',
sslkey: '/path/cert.key',
sslcert: '/path/cert.crt',
sslrootcert: '/path/root.crt',
},
}
var Client = require('../../../lib/client')
var defaults = require('../../../lib/defaults')
defaults.ssl = true
var c = new ConnectionParameters(sourceConfig)
c.getLibpqConnectionString(
assert.calls(function (err, pgCString) {
assert(!err)
assert.equal(
pgCString.indexOf("sslrootcert='/path/root.crt'") !== -1,
true,
'libpqConnectionString should contain sslrootcert'
)
})
)
})