From 6dec442356e0a4a86f2e49caf2209529d4e0a36e Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 19 Oct 2012 11:25:34 -0700 Subject: [PATCH] Changed root behavior --- lib/resources/collection/index.js | 24 +++---- lib/script.js | 7 +- test-app/public/test/collection.test.js | 85 ++++++++++++++++++++++++- test-app/resources/todos/post.js | 4 ++ 4 files changed, 100 insertions(+), 20 deletions(-) diff --git a/lib/resources/collection/index.js b/lib/resources/collection/index.js index 8af0b2a..988de7a 100644 --- a/lib/resources/collection/index.js +++ b/lib/resources/collection/index.js @@ -143,8 +143,9 @@ Collection.prototype.sanitizeQuery = function (query) { // skip properties that do not exist, but allow $ queries and id if(!prop && key.indexOf('$') !== 0 && key !== 'id') return; - // hack - $limitRecursion is not a mongo property so we'll get rid of it, too + // hack - $limitRecursion and $skipEvents are not mongo properties so we'll get rid of them, too if (key === '$limitRecursion') return; + if (key === '$skipEvents') return; if(expected == 'number' && actual == 'string') { sanitized[key] = parseFloat(val); @@ -316,9 +317,7 @@ Collection.prototype.find = function (ctx, fn) { errors[key] = val || true; }, hide: function(property) { - if (!session.isRoot) { - delete data[property]; - } + delete data[property]; }, 'this': data, data: data @@ -350,9 +349,7 @@ Collection.prototype.find = function (ctx, fn) { errors[key] = val || true; }, hide: function(property) { - if (!session.isRoot) { - delete data[property]; - } + delete data[property]; }, 'this': data, data: data @@ -453,6 +450,7 @@ Collection.prototype.save = function (ctx, fn) { function done(err, item) { errors = errors && {errors: errors}; + debug('errors: %j', err); fn(errors || err, item); } @@ -463,14 +461,10 @@ Collection.prototype.save = function (ctx, fn) { errors[key] = val || true; }, hide: function(property) { - if (!session.isRoot) { - delete item[property]; - } + delete item[property]; }, protect: function(property) { - if (!session.isRoot) { - delete item[property]; - } + delete item[property]; }, 'this': item, data: item @@ -671,8 +665,8 @@ Collection.prototype.execCommands = function (type, obj, commands) { }; Collection.prototype.shouldRunEvent = function(ev, ctx) { - var runEvents = ctx && ((ctx.body && ctx.body.$runEvents) || (ctx.query && ctx.query.$runEevents)) - , rootPrevent = ctx && ctx.session && ctx.session.isRoot && !runEvents; + var skipEvents = ctx && ((ctx.body && ctx.body.$skipEvents) || (ctx.query && ctx.query.$skipEvents)) + , rootPrevent = ctx && ctx.session && ctx.session.isRoot && skipEvents; return !rootPrevent && ev; }; diff --git a/lib/script.js b/lib/script.js index f52a260..4140edb 100644 --- a/lib/script.js +++ b/lib/script.js @@ -37,15 +37,14 @@ Script.prototype.run = function (ctx, domain, fn) { var scriptContext = { 'this': {}, cancel: function(msg, status) { - if (!req.isRoot) { - var err = {message: msg, statusCode: status}; - throw err; - } + var err = {message: msg, statusCode: status}; + throw err; }, me: session && session.user, console: console, query: ctx.query, internal: req && req.internal, + isRoot: req && req.session && req.session.isRoot, emit: function(collection, query, event, data) { if(arguments.length === 4) { session.emitToUsers(collection, query, event, data); diff --git a/test-app/public/test/collection.test.js b/test-app/public/test/collection.test.js index 79142a8..ddf7a26 100644 --- a/test-app/public/test/collection.test.js +++ b/test-app/public/test/collection.test.js @@ -1,3 +1,4 @@ +/*global _dpd:false */ describe('Collection', function() { describe('dpd.todos', function() { it('should exist', function() { @@ -67,6 +68,18 @@ describe('Collection', function() { done(); }); }); + it('should create a todo that exists in the store', function(done) { + dpd.todos.post({title: 'faux'}, function (todo, err) { + expect(todo.id.length).to.equal(16); + expect(todo.title).to.equal('faux'); + expect(err).to.not.exist; + dpd.todos.get(todo.id, function(res, err) { + if (err) return done(err); + expect(res.title).to.equal('faux'); + done(); + }); + }); + }); }); describe('.post({title: "notvalid"}, fn)', function() { @@ -381,7 +394,6 @@ describe('Collection', function() { todoId = res.id; dpd.todos.put(todoId, {message: "notvalidput"}, next); }).chain(function(next, res, err) { - console.log(res, err); expect(err).to.exist; expect(err.errors).to.exist; expect(err.errors.message).to.equal("message should not be notvalidput"); @@ -592,6 +604,77 @@ describe('Collection', function() { }); }); + describe('root', function() { + afterEach(function(done) { + _dpd.ajax.headers = {}; + cleanCollection(dpd.todos, done); + }); + + describe('dpd-ssh-key', function() { + beforeEach(function() { + _dpd.ajax.headers = { + 'dpd-ssh-key': true + }; + }); + + it('should detect root', function(done) { + chain(function(next) { + dpd.todos.post({title: 'valid'}, next); + }).chain(function(next, res, err) { + if (err) return done(err); + expect(res.isRoot).to.equal(true); + done(); + }); + }); + + it('should allow skipping events', function(done) { + chain(function(next) { + dpd.todos.post({title: 'notvalid', $skipEvents: true}, next); + }).chain(function(next, res, err) { + if (err) return done(err); + expect(res.title).to.equal('notvalid'); + done(); + }); + }); + + it('should allow skipping events on get', function(done) { + var id; + chain(function(next) { + dpd.todos.post({title: '$GET_CANCEL'}, next); + }).chain(function(next, res, err) { + if (err) return done(err); + id = res.id; + dpd.todos.get(id, {$skipEvents: true}, next); + }).chain(function(next, res, err) { + if (err) return done(err); + expect(res.title).to.equal("$GET_CANCEL"); + done(); + }); + }); + }); + + it('should not allow skipping events', function(done) { + chain(function(next) { + dpd.todos.post({title: 'notvalid', $skipEvents: true}, next); + }).chain(function(next, res, err) { + expect(err).to.exist; + expect(err.errors).to.exist; + done(); + }); + }); + + it('should not detect root', function(done) { + chain(function(next) { + dpd.todos.post({title: 'valid'}, next); + }).chain(function(next, res, err) { + if (err) return done(err); + expect(res.isRoot).to.not.exist; + done(); + }); + }); + + }); + describe('dpd.recursive', function() { beforeEach(function(done) { dpd.recursive.post({name: "dataception"}, function(res) { diff --git a/test-app/resources/todos/post.js b/test-app/resources/todos/post.js index d3db4f6..f864703 100644 --- a/test-app/resources/todos/post.js +++ b/test-app/resources/todos/post.js @@ -35,4 +35,8 @@ if (this.title === "$CANCEL_TEST") { if (this.title === "$INTERNAL_CANCEL_TEST") { if (!internal) cancel('internal cancel'); +} + +if (isRoot) { + this.isRoot = true; } \ No newline at end of file