Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fixes #1489] Add meta flag for 'unsafe' queries #1519

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions test/unit/query/query.find.unsafe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var assert = require('assert');
var _ = require('@sailshq/lodash');
var Waterline = require('../../../lib/waterline');

describe('Collection Query ::', function() {
describe('.find()', function() {
describe('with meta unsafe flag', function(){
var query;

before(function(done) {
var waterline = new Waterline();
var Model = Waterline.Model.extend({
identity: 'user',
connection: 'foo',
primaryKey: 'id',
schema: false,
attributes: {
id: {
type: 'number'
},
name: {
type: 'string',
defaultsTo: 'Foo Bar'
},
nested: {
type: 'json'
}
}
});

waterline.registerModel(Model);

// Fixture Adapter Def
var adapterDef = { find: function(con, query, cb) { return cb(null, [{id: 1, criteria: query.criteria}]); }};

var connections = {
'foo': {
adapter: 'foobar'
}
};

waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
if(err) {
return done(err);
}
query = orm.collections.user;
return done();
});
});

it('should allow meta to be optional', function(done) {
query.find({}, function(err) {
if(err) {
return done(err);
}

return done();
});
});

it('should block a query not explicitly unsafe', function(done) {
query.find({
'nested.key': 'foobar'
}, function(err) {
if(err) {
return done();
}

return done(new Error('Unsafe query allowed through by default!'));
});
});

it('should allow a mongo-style query to pass through to the adapter with meta.unsafe', function(done) {
query.find()
.where({ 'nested.key': 'Foo Bar' })
.sort([{ 'nested.key': 'desc' }])
.meta({unsafe: true})
.exec(function(err, results) {
if (err) {
return done(err);
}

assert(_.isArray(results));
assert.equal(results[0].criteria.where['nested.key'], 'Foo Bar');
assert.equal(results[0].criteria.sort[0]['nested.key'], 'DESC');

return done();
});
});

it('should still normalize mongo-style waterline queries with meta.unsafe', function(done) {
query.find()
.where({ 'nested.key': ['Foo Bar'] })
.sort([{ 'nested.key': 'desc' }])
.meta({unsafe: true})
.exec(function(err, results) {
if (err) {
return done(err);
}

assert(_.isArray(results));
assert.equal(results[0].criteria.where['nested.key'].in[0], 'Foo Bar');
assert.equal(results[0].criteria.sort[0]['nested.key'], 'DESC');

return done();
});
});
});
});
});
107 changes: 107 additions & 0 deletions test/unit/query/query.findOne.unsafe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var assert = require('assert');
var _ = require('@sailshq/lodash');
var Waterline = require('../../../lib/waterline');

describe('Collection Query ::', function() {
describe('.findOne()', function() {
describe('with meta unsafe flag', function() {
var query;

before(function(done) {
var waterline = new Waterline();
var Model = Waterline.Model.extend({
identity: 'user',
connection: 'foo',
primaryKey: 'id',
schema: false,
attributes: {
id: {
type: 'number'
},
name: {
type: 'string',
defaultsTo: 'Foo Bar'
},
nested: {
type: 'json'
}
}
});

waterline.registerModel(Model);

// Fixture Adapter Def
var adapterDef = { find: function(con, query, cb) { return cb(null, [{id: 1, criteria: query.criteria}]); }};

var connections = {
'foo': {
adapter: 'foobar'
}
};

waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
if(err) {
return done(err);
}
query = orm.collections.user;
return done();
});
});

it('should allow meta to be optional', function(done) {
query.findOne({
foo: 'bar'
})
.exec(function(err) {
if(err) {
return done(err);
}

return done();
});
});

it('should block a query not explicitly unsafe', function(done) {
query.findOne({
'nested.key': 'foobar'
}, function(err) {
if(err) {
return done();
}

return done(new Error('Unsafe query allowed through by default!'));
});
});

it('should allow a mongo-style query to pass through to the adapter with meta.unsafe', function(done) {
query.findOne({ 'nested.key': 'Foo Bar' })
.meta({unsafe: true})
.exec(function(err, results) {
if (err) {
return done(err);
}

assert(_.isPlainObject(results));
assert.equal(results.criteria.where['nested.key'], 'Foo Bar');

return done();
});
});

it('should still normalize mongo-style waterline queries with meta.unsafe', function(done) {
query.findOne({ 'nested.key': ['Foo Bar'] })
.meta({unsafe: true})
.exec(function(err, results) {
if (err) {
return done(err);
}

assert(_.isPlainObject(results));
assert.equal(results.criteria.where['nested.key'].in[0], 'Foo Bar');

return done();
});
});
});
});
});