node-postgres/test/unit/connection-parameters/environment-variable-tests.js

115 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-01-18 08:27:48 +08:00
var helper = require(__dirname + '/../test-helper');
var assert = require('assert');
2013-01-18 08:14:13 +08:00
var ConnectionParameters = require(__dirname + '/../../../lib/connection-parameters');
var defaults = require(__dirname + '/../../../lib').defaults;
//clear process.env
var realEnv = {};
for(var key in process.env) {
realEnv[key] = process.env[key];
delete process.env[key];
}
test('ConnectionParameters initialized from environment variables', function(t) {
process.env['PGHOST'] = 'local';
process.env['PGUSER'] = 'bmc2';
process.env['PGPORT'] = 7890;
process.env['PGDATABASE'] = 'allyerbase';
process.env['PGPASSWORD'] = 'open';
var subject = new ConnectionParameters();
2013-01-18 08:27:48 +08:00
assert.equal(subject.host, 'local', 'env host');
assert.equal(subject.user, 'bmc2', 'env user');
assert.equal(subject.port, 7890, 'env port');
assert.equal(subject.database, 'allyerbase', 'env database');
assert.equal(subject.password, 'open', 'env password');
});
2013-01-18 08:14:13 +08:00
test('ConnectionParameters initialized from mix', function(t) {
delete process.env['PGPASSWORD'];
delete process.env['PGDATABASE'];
var subject = new ConnectionParameters({
user: 'testing',
database: 'zugzug'
2013-01-18 08:27:48 +08:00
});
assert.equal(subject.host, 'local', 'env host');
assert.equal(subject.user, 'testing', 'config user');
assert.equal(subject.port, 7890, 'env port');
assert.equal(subject.database, 'zugzug', 'config database');
assert.equal(subject.password, defaults.password, 'defaults password');
});
2013-01-18 08:14:13 +08:00
//clear process.env
for(var key in process.env) {
delete process.env[key];
}
test('connection string parsing', function(t) {
var string = 'postgres://brian:pw@boom:381/lala';
var subject = new ConnectionParameters(string);
2013-01-18 08:27:48 +08:00
assert.equal(subject.host, 'boom', 'string host');
assert.equal(subject.user, 'brian', 'string user');
assert.equal(subject.password, 'pw', 'string password');
assert.equal(subject.port, 381, 'string port');
assert.equal(subject.database, 'lala', 'string database');
});
2013-01-18 08:14:13 +08:00
test('connection string parsing - ssl', function(t) {
var string = 'postgres://brian:pw@boom:381/lala?ssl=true';
var subject = new ConnectionParameters(string);
assert.equal(subject.ssl, true, 'ssl');
string = 'postgres://brian:pw@boom:381/lala?ssl=1';
subject = new ConnectionParameters(string);
assert.equal(subject.ssl, true, 'ssl');
string = 'postgres://brian:pw@boom:381/lala?other&ssl=true';
subject = new ConnectionParameters(string);
assert.equal(subject.ssl, true, 'ssl');
string = 'postgres://brian:pw@boom:381/lala?ssl=0';
subject = new ConnectionParameters(string);
assert.equal(!!subject.ssl, false, 'ssl');
string = 'postgres://brian:pw@boom:381/lala';
subject = new ConnectionParameters(string);
assert.equal(!!subject.ssl, false, 'ssl');
});
//clear process.env
for(var key in process.env) {
delete process.env[key];
}
test('ssl is false by default', function() {
var subject = new ConnectionParameters()
assert.equal(subject.ssl, false)
})
var testVal = function(mode, expected) {
//clear process.env
for(var key in process.env) {
delete process.env[key];
}
process.env.PGSSLMODE = mode;
test('ssl is ' + expected + ' when $PGSSLMODE=' + mode, function() {
var subject = new ConnectionParameters();
assert.equal(subject.ssl, expected);
});
};
testVal('', false);
testVal('disable', false);
testVal('allow', false);
testVal('prefer', true);
testVal('require', true);
testVal('verify-ca', true);
testVal('verify-full', true);
2013-01-18 08:14:13 +08:00
//restore process.env
for(var key in realEnv) {
process.env[key] = realEnv[key];
}