Skip to content

Commit

Permalink
Added hidden fields support to autocrud.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Rademacher committed Sep 9, 2013
1 parent 5504e96 commit 499c7e1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 68 deletions.
10 changes: 7 additions & 3 deletions lib/autocrud.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var sys = require('sys'),
events = require('events'),
ObjectID = require('mongodb').ObjectID,
jsonSchema = require('json-schema');
jsonSchema = require('json-schema'),
schemaTools = require('./schema-tools');

function Autocrud(options) {
if (!(this instanceof Autocrud)) return new Autocrud(options);
Expand All @@ -15,6 +16,9 @@ function Autocrud(options) {
path = options.path,
schema = options.schema;

// Generate alternate schemas and mongo projections
var projection = schemaTools.getMongoProjection(schema);

// Selection of which routes to create
var getCreate = (options.getCreate) ? options.getCreate : true,
postCreate = (options.postCreate) ? options.postCreate : true,
Expand Down Expand Up @@ -66,7 +70,7 @@ function Autocrud(options) {
// GET

this.getRouteFn = function (req, res) {
var cursor = collection.find(createQuery(req)),
var cursor = collection.find(createQuery(req), projection),
sort = req.param('sort'),
limit = req.param('limit'),
skip = req.param('skip');
Expand Down Expand Up @@ -100,7 +104,7 @@ function Autocrud(options) {
this.getIdRouteFn = function (req, res) {
try {
var _id = new ObjectID(req.params.id);
collection.findOne(createQuery(req, {_id: _id}), function (err, document) {
collection.findOne(createQuery(req, {_id: _id}), projection, function (err, document) {
if (err) return res.json(500, err);
if (!document) return res.send(404);
res.json(document);
Expand Down
File renamed without changes.
71 changes: 45 additions & 26 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ function defineAPI(done) {
ownerSelf: true
});

autocrud({
app: app,
collection: mongo.schema,
name: 'schema',
path: '/api',
schema: {
type: 'object',
properties: {
username: {type:'string', required:true},
password: {type:'string', required:true, hidden:true}
},
additionalProperties: false
}
});

app.post('/login', passport.authenticate('local'), function (req, res) {
res.json(200, {success: true});
});
Expand Down Expand Up @@ -214,35 +229,39 @@ before(function (done) {
conn.collection('owned', function(err, owned) {
if (err) return done(err);
mongo.owned = owned;

// Insert valid test data to mongo
widget.insert(validPool, function (err, result) {
if (err) return console.log(err);
result.forEach(function (resObj) {
resObj._id = resObj._id.toString();
committedPool.push(resObj);
});
committedPool = _.sortBy(committedPool, '_id');
conn.collection('schema', function(err, schema) {
if (err) return done(err);
mongo.schema = schema;

defineAPI(done);
});
// Insert valid test data to mongo
widget.insert(validPool, function (err, result) {
if (err) return console.log(err);
result.forEach(function (resObj) {
resObj._id = resObj._id.toString();
committedPool.push(resObj);
});
committedPool = _.sortBy(committedPool, '_id');

defineAPI(done);
});

// Insert valid users to mongo
user.insert({
username: 'andrew',
password: '12345',
roles: ['customer']
}, function (err, result) {
if (err) return console.log(err);
});
// Insert valid users to mongo
user.insert({
username: 'andrew',
password: '12345',
roles: ['customer']
}, function (err, result) {
if (err) return console.log(err);
});

user.insert({
username: 'root',
password: '12345',
roles: ['administrator']
}, function (err, result) {
if (err) return console.log(err);
});
user.insert({
username: 'root',
password: '12345',
roles: ['administrator']
}, function (err, result) {
if (err) return console.log(err);
});
});
});
});
});
Expand Down
42 changes: 42 additions & 0 deletions test/schema-tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var _ = require('underscore'),
assert = require('assert'),
getMongoProjection = require('../lib/schema-tools').getMongoProjection;

var sampleSchema = {
type: 'object',
additionalProperties: false,
properties: {
username: {type:'string', required: true},
password: {type:'string', required: true, hidden: true},
stripe: {
type: 'object',
additionalProperties: false,
properties: {
stripeId: {type:'string', required: true},
stripeKey: {type:'string', required: true, hidden: true}
}
},
subUsers: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
username: {type:'string', required: true},
password: {type:'string', required: true, hidden: true}
}
}
}
}
};

describe('Schema Manipulation', function() {
it('should generate a mongo projection', function() {
var projection = getMongoProjection(sampleSchema);
assert(_.isEqual(projection, {
'password': 0,
'stripe.stripeKey': 0,
'subUsers.password': 0
}));
});
});
60 changes: 21 additions & 39 deletions test/schema.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
var _ = require('underscore'),
assert = require('assert'),
getMongoProjection = require('../lib/schema').getMongoProjection;
var assert = require('assert'),
rest = require('restler'),
_ = require('underscore');

var sampleSchema = {
type: 'object',
additionalProperties: false,
properties: {
username: {type:'string', required: true},
password: {type:'string', required: true, hidden: true},
stripe: {
type: 'object',
additionalProperties: false,
properties: {
stripeId: {type:'string', required: true},
stripeKey: {type:'string', required: true, hidden: true}
}
},
subUsers: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
username: {type:'string', required: true},
password: {type:'string', required: true, hidden: true}
}
}
}
}
};

describe('Schema Manipulation', function() {
it('should generate a mongo projection', function() {
var projection = getMongoProjection(sampleSchema);
assert(_.isEqual(projection, {
'password': 0,
'stripe.stripeKey': 0,
'subUsers.password': 0
}));
describe('Schema Modification', function() {
describe('Projection', function() {
it('should not return hidden fields in GET calls', function(done) {
rest.json(callPrefix + '/schema', {
username: 'testuser',
password: 'pass'
}, null, 'POST')
.on('complete', function(data, res) {
assert(res.statusCode === 200);

rest.json(callPrefix + '/schema/' + data._id, null, null, 'GET')
.on('complete', function(data, res) {
assert(data.username);
assert(!data.password);
done();
});
});
});
});
});

0 comments on commit 499c7e1

Please sign in to comment.