Files
kami-parse-server/spec/PostgresConfigParser.spec.js

103 lines
2.9 KiB
JavaScript

const parser = require('../lib/Adapters/Storage/Postgres/PostgresConfigParser');
const fs = require('fs');
const queryParamTests = {
'a=1&b=2': { a: '1', b: '2' },
'a=abcd%20efgh&b=abcd%3Defgh': { a: 'abcd efgh', b: 'abcd=efgh' },
'a=1&b&c=true': { a: '1', b: '', c: 'true' },
};
describe('PostgresConfigParser.parseQueryParams', () => {
it('creates a map from a query string', () => {
for (const key in queryParamTests) {
const result = parser.parseQueryParams(key);
const testObj = queryParamTests[key];
expect(Object.keys(result).length).toEqual(Object.keys(testObj).length);
for (const k in result) {
expect(result[k]).toEqual(testObj[k]);
}
}
});
});
const baseURI = 'postgres://username:password@localhost:5432/db-name';
const testfile = fs.readFileSync('./Dockerfile').toString();
const dbOptionsTest = {};
dbOptionsTest[
`${baseURI}?ssl=true&binary=true&application_name=app_name&fallback_application_name=f_app_name&poolSize=12`
] = {
ssl: true,
binary: true,
application_name: 'app_name',
fallback_application_name: 'f_app_name',
max: 12,
};
dbOptionsTest[`${baseURI}?ssl=&binary=aa`] = {
binary: false,
};
dbOptionsTest[
`${baseURI}?ssl=true&ca=./Dockerfile&pfx=./Dockerfile&cert=./Dockerfile&key=./Dockerfile&binary=aa&passphrase=word&secureOptions=20`
] = {
ssl: {
ca: testfile,
pfx: testfile,
cert: testfile,
key: testfile,
passphrase: 'word',
secureOptions: 20,
},
binary: false,
};
dbOptionsTest[
`${baseURI}?ssl=false&ca=./Dockerfile&pfx=./Dockerfile&cert=./Dockerfile&key=./Dockerfile&binary=aa`
] = {
ssl: { ca: testfile, pfx: testfile, cert: testfile, key: testfile },
binary: false,
};
dbOptionsTest[`${baseURI}?rejectUnauthorized=true`] = {
ssl: { rejectUnauthorized: true },
};
dbOptionsTest[`${baseURI}?max=5&query_timeout=100&idleTimeoutMillis=1000&keepAlive=true`] = {
max: 5,
query_timeout: 100,
idleTimeoutMillis: 1000,
keepAlive: true,
};
describe('PostgresConfigParser.getDatabaseOptionsFromURI', () => {
it('creates a db options map from a query string', () => {
for (const key in dbOptionsTest) {
const result = parser.getDatabaseOptionsFromURI(key);
const testObj = dbOptionsTest[key];
for (const k in testObj) {
expect(result[k]).toEqual(testObj[k]);
}
}
});
it('sets the poolSize to 10 if the it is not a number', () => {
const result = parser.getDatabaseOptionsFromURI(`${baseURI}?poolSize=sdf`);
expect(result.max).toEqual(10);
});
it('sets the max to 10 if the it is not a number', () => {
const result = parser.getDatabaseOptionsFromURI(`${baseURI}?&max=sdf`);
expect(result.poolSize).toBeUndefined();
expect(result.max).toEqual(10);
});
it('max should take precedence over poolSize', () => {
const result = parser.getDatabaseOptionsFromURI(`${baseURI}?poolSize=20&max=12`);
expect(result.poolSize).toBeUndefined();
expect(result.max).toEqual(12);
});
});