2013-07-17 00:53:52 +00:00
|
|
|
var should = require('./init.js');
|
2013-10-11 18:50:00 +00:00
|
|
|
var utils = require('../lib/utils');
|
|
|
|
var fieldsToArray = utils.fieldsToArray;
|
|
|
|
var removeUndefined = utils.removeUndefined;
|
2013-12-17 01:14:56 +00:00
|
|
|
var mergeSettings = utils.mergeSettings;
|
2014-08-15 17:39:18 +00:00
|
|
|
var sortObjectsByIds = utils.sortObjectsByIds;
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('util.fieldsToArray', function () {
|
|
|
|
it('Turn objects and strings into an array of fields to include when finding models', function () {
|
2013-07-17 00:53:52 +00:00
|
|
|
|
|
|
|
function sample(fields) {
|
|
|
|
var properties = ['foo', 'bar', 'bat', 'baz'];
|
|
|
|
return {
|
|
|
|
expect: function (arr) {
|
|
|
|
should.deepEqual(fieldsToArray(fields, properties), arr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-07-17 00:53:52 +00:00
|
|
|
sample(false).expect(undefined);
|
|
|
|
sample(null).expect(undefined);
|
|
|
|
sample({}).expect(undefined);
|
|
|
|
sample('foo').expect(['foo']);
|
|
|
|
sample(['foo']).expect(['foo']);
|
|
|
|
sample({'foo': 1}).expect(['foo']);
|
|
|
|
sample({'bat': true}).expect(['bat']);
|
|
|
|
sample({'bat': 0}).expect(['foo', 'bar', 'baz']);
|
|
|
|
sample({'bat': false}).expect(['foo', 'bar', 'baz']);
|
|
|
|
});
|
2013-10-11 18:50:00 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('util.removeUndefined', function () {
|
|
|
|
it('Remove undefined values from the query object', function () {
|
|
|
|
var q1 = {where: {x: 1, y: undefined}};
|
|
|
|
should.deepEqual(removeUndefined(q1), {where: {x: 1}});
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var q2 = {where: {x: 1, y: 2}};
|
|
|
|
should.deepEqual(removeUndefined(q2), {where: {x: 1, y: 2}});
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var q3 = {where: {x: 1, y: {in: [2, undefined]}}};
|
|
|
|
should.deepEqual(removeUndefined(q3), {where: {x: 1, y: {in: [2]}}});
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
should.equal(removeUndefined(null), null);
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
should.equal(removeUndefined(undefined), undefined);
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
should.equal(removeUndefined('x'), 'x');
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var date = new Date();
|
|
|
|
var q4 = {where: {x: 1, y: date}};
|
|
|
|
should.deepEqual(removeUndefined(q4), {where: {x: 1, y: date}});
|
2013-10-30 05:16:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-10-25 23:18:02 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('util.parseSettings', function () {
|
|
|
|
it('Parse a full url into a settings object', function () {
|
|
|
|
var url = 'mongodb://x:y@localhost:27017/mydb?w=2';
|
|
|
|
var settings = utils.parseSettings(url);
|
|
|
|
should.equal(settings.hostname, 'localhost');
|
|
|
|
should.equal(settings.port, 27017);
|
|
|
|
should.equal(settings.host, 'localhost');
|
|
|
|
should.equal(settings.user, 'x');
|
|
|
|
should.equal(settings.password, 'y');
|
|
|
|
should.equal(settings.database, 'mydb');
|
|
|
|
should.equal(settings.connector, 'mongodb');
|
|
|
|
should.equal(settings.w, '2');
|
|
|
|
should.equal(settings.url, 'mongodb://x:y@localhost:27017/mydb?w=2');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Parse a url without auth into a settings object', function () {
|
|
|
|
var url = 'mongodb://localhost:27017/mydb/abc?w=2';
|
|
|
|
var settings = utils.parseSettings(url);
|
|
|
|
should.equal(settings.hostname, 'localhost');
|
|
|
|
should.equal(settings.port, 27017);
|
|
|
|
should.equal(settings.host, 'localhost');
|
|
|
|
should.equal(settings.user, undefined);
|
|
|
|
should.equal(settings.password, undefined);
|
|
|
|
should.equal(settings.database, 'mydb');
|
|
|
|
should.equal(settings.connector, 'mongodb');
|
|
|
|
should.equal(settings.w, '2');
|
|
|
|
should.equal(settings.url, 'mongodb://localhost:27017/mydb/abc?w=2');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Parse a url with complex query into a settings object', function () {
|
|
|
|
var url = 'mysql://127.0.0.1:3306/mydb?x[a]=1&x[b]=2&engine=InnoDB';
|
|
|
|
var settings = utils.parseSettings(url);
|
|
|
|
should.equal(settings.hostname, '127.0.0.1');
|
|
|
|
should.equal(settings.port, 3306);
|
|
|
|
should.equal(settings.host, '127.0.0.1');
|
|
|
|
should.equal(settings.user, undefined);
|
|
|
|
should.equal(settings.password, undefined);
|
|
|
|
should.equal(settings.database, 'mydb');
|
|
|
|
should.equal(settings.connector, 'mysql');
|
|
|
|
should.equal(settings.x.a, '1');
|
|
|
|
should.equal(settings.x.b, '2');
|
|
|
|
should.equal(settings.engine, 'InnoDB');
|
|
|
|
should.equal(settings.url, 'mysql://127.0.0.1:3306/mydb?x[a]=1&x[b]=2&engine=InnoDB');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Parse a url without auth into a settings object', function () {
|
|
|
|
var url = 'memory://?x=1';
|
|
|
|
var settings = utils.parseSettings(url);
|
|
|
|
should.equal(settings.hostname, '');
|
|
|
|
should.equal(settings.user, undefined);
|
|
|
|
should.equal(settings.password, undefined);
|
|
|
|
should.equal(settings.database, undefined);
|
|
|
|
should.equal(settings.connector, 'memory');
|
|
|
|
should.equal(settings.x, '1');
|
|
|
|
should.equal(settings.url, 'memory://?x=1');
|
|
|
|
|
|
|
|
});
|
2013-10-25 23:18:02 +00:00
|
|
|
|
2013-12-17 01:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('mergeSettings', function () {
|
|
|
|
it('should merge settings correctly', function () {
|
|
|
|
var src = { base: 'User',
|
|
|
|
relations: { accessTokens: { model: 'accessToken', type: 'hasMany',
|
|
|
|
foreignKey: 'userId' },
|
|
|
|
account: { model: 'account', type: 'belongsTo' } },
|
|
|
|
acls: [
|
|
|
|
{ accessType: '*',
|
|
|
|
permission: 'DENY',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone' },
|
|
|
|
{ accessType: '*',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
property: 'login',
|
|
|
|
principalId: '$everyone' },
|
|
|
|
{ permission: 'ALLOW',
|
|
|
|
property: 'findById',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$owner' }
|
|
|
|
] };
|
|
|
|
var tgt = { strict: false,
|
|
|
|
acls: [
|
|
|
|
{ principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
property: 'create' },
|
|
|
|
{ principalType: 'ROLE',
|
|
|
|
principalId: '$owner',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
property: 'removeById' }
|
|
|
|
],
|
|
|
|
maxTTL: 31556926,
|
|
|
|
ttl: 1209600 };
|
|
|
|
|
|
|
|
var dst = mergeSettings(tgt, src);
|
|
|
|
|
|
|
|
var expected = { strict: false,
|
|
|
|
acls: [
|
|
|
|
{ principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
property: 'create' },
|
|
|
|
{ principalType: 'ROLE',
|
|
|
|
principalId: '$owner',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
property: 'removeById' },
|
|
|
|
{ accessType: '*',
|
|
|
|
permission: 'DENY',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone' },
|
|
|
|
{ accessType: '*',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
property: 'login',
|
|
|
|
principalId: '$everyone' },
|
|
|
|
{ permission: 'ALLOW',
|
|
|
|
property: 'findById',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$owner' }
|
|
|
|
],
|
|
|
|
maxTTL: 31556926,
|
|
|
|
ttl: 1209600,
|
|
|
|
base: 'User',
|
|
|
|
relations: { accessTokens: { model: 'accessToken', type: 'hasMany',
|
|
|
|
foreignKey: 'userId' },
|
|
|
|
account: { model: 'account', type: 'belongsTo' } } };
|
|
|
|
|
|
|
|
should.deepEqual(dst.acls, expected.acls, 'Merged settings should match the expectation');
|
|
|
|
});
|
2014-08-15 17:39:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('sortObjectsByIds', function () {
|
|
|
|
|
|
|
|
var items = [
|
|
|
|
{ id: 1, name: 'a' },
|
|
|
|
{ id: 2, name: 'b' },
|
|
|
|
{ id: 3, name: 'c' },
|
|
|
|
{ id: 4, name: 'd' },
|
|
|
|
{ id: 5, name: 'e' },
|
|
|
|
{ id: 6, name: 'f' }
|
|
|
|
];
|
|
|
|
|
|
|
|
it('should sort', function() {
|
|
|
|
var sorted = sortObjectsByIds('id', [6, 5, 4, 3, 2, 1], items);
|
|
|
|
var names = sorted.map(function(u) { return u.name; });
|
|
|
|
should.deepEqual(names, ['f', 'e', 'd', 'c', 'b', 'a']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should sort - partial ids', function() {
|
2014-08-15 17:47:12 +00:00
|
|
|
var sorted = sortObjectsByIds('id', [5, 3, 2], items);
|
2014-08-15 17:39:18 +00:00
|
|
|
var names = sorted.map(function(u) { return u.name; });
|
|
|
|
should.deepEqual(names, ['e', 'c', 'b', 'a', 'd', 'f']);
|
|
|
|
});
|
2014-08-15 17:47:12 +00:00
|
|
|
|
|
|
|
it('should sort - strict', function() {
|
|
|
|
var sorted = sortObjectsByIds('id', [5, 3, 2], items, true);
|
|
|
|
var names = sorted.map(function(u) { return u.name; });
|
|
|
|
should.deepEqual(names, ['e', 'c', 'b']);
|
|
|
|
});
|
2014-08-15 17:39:18 +00:00
|
|
|
|
2014-08-15 17:47:12 +00:00
|
|
|
});
|