不要怂,就是干,撸起袖子干!

Commit 5dd5e303 by Sushant

(#4091) check if options.where is an acceptable POJO

1 parent 6f4cdcfa
...@@ -2285,6 +2285,10 @@ Model.prototype.destroy = function(options) { ...@@ -2285,6 +2285,10 @@ Model.prototype.destroy = function(options) {
throw new Error('Missing where or truncate attribute in the options parameter of model.destroy.'); throw new Error('Missing where or truncate attribute in the options parameter of model.destroy.');
} }
if (!options.truncate && !_.isPlainObject(options.where) && !_.isArray(options.where) && options.where._isSequelizeMethod !== true) {
throw new Error('Expected plain object, array or sequelize method in the options.where parameter of model.destroy.');
}
options = Utils._.extend({ options = Utils._.extend({
hooks: true, hooks: true,
individualHooks: false, individualHooks: false,
...@@ -2445,6 +2449,10 @@ Model.prototype.update = function(values, options) { ...@@ -2445,6 +2449,10 @@ Model.prototype.update = function(values, options) {
throw new Error('Missing where attribute in the options parameter passed to update.'); throw new Error('Missing where attribute in the options parameter passed to update.');
} }
if (!_.isPlainObject(options.where) && !_.isArray(options.where) && options.where._isSequelizeMethod !== true) {
throw new Error('Expected plain object, array or sequelize method in the options.where parameter of model.update.');
}
options = Utils._.extend({ options = Utils._.extend({
validate: true, validate: true,
hooks: true, hooks: true,
......
...@@ -19,37 +19,40 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -19,37 +19,40 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
before(function () { before(function () {
this.stubUpdate = sinon.stub(current.getQueryInterface(), 'bulkDelete', function () { this.stubDelete = sinon.stub(current.getQueryInterface(), 'bulkDelete', function () {
return Promise.resolve([]); return Promise.resolve([]);
}); });
}); });
beforeEach(function () { beforeEach(function () {
this.options = {where: {secretValue: '1'}} this.deloptions = {where: {secretValue: '1'}};
this.cloneOptions = _.clone(this.options); this.cloneOptions = _.clone(this.deloptions);
this.stubUpdate.reset(); this.stubDelete.reset();
}); });
afterEach(function () { afterEach(function () {
delete this.options; delete this.deloptions;
delete this.cloneOptions; delete this.cloneOptions;
}); });
after(function () { after(function () {
this.stubUpdate.restore(); this.stubDelete.restore();
}); });
it('properly clones options', function() { it('properly clones options', function() {
var self = this; var self = this;
return User.destroy(self.options).bind(this).then(function(e) { return User.destroy(self.deloptions).bind(this).then(function(e) {
expect(self.options).to.be.deep.eql(self.cloneOptions); expect(self.deloptions).to.be.deep.eql(self.cloneOptions);
}); });
}); });
it('can detect complexe objects', function() { it('can detect complexe objects', function() {
var self = this; var Where = function () { this.secretValue = '1'; };
var where = function () { this.secretValue = '1'; }
return expect(User.destroy({where:new where})).to.eventually.be.rejectedWith(Error); expect(function () {
User.destroy({where: new Where()});
}).to.throw();
}); });
}); });
}); });
...@@ -57,8 +57,12 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -57,8 +57,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('can detect complexe objects', function() { it('can detect complexe objects', function() {
var self = this; var self = this;
var where = function () { this.secretValue = '1'; } var Where = function () { this.secretValue = '1'; };
return expect(User.update(self.updates, {where:new where})).to.eventually.be.rejectedWith(Error);
expect(function () {
User.update(self.updates, {where:new Where()});
}).to.throw();
}); });
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!