Add test for remoting nested hidden properties

This commit is contained in:
Ritchie Martori 2014-04-11 11:40:53 -07:00
parent e9323ce664
commit 63ad8f69ae
2 changed files with 34 additions and 21 deletions

View File

@ -201,22 +201,6 @@ Model._getAccessTypeForMethod = function(method) {
} }
} }
var superToJSON = Model.prototype.toJSON;
Model.prototype.toJSON = function toJSON() {
var Model = this.constructor;
var obj = superToJSON.apply(this, arguments);
var settings = Model.definition && Model.definition.settings;
var hiddenProperties = settings && settings.hidden;
if(Array.isArray(hiddenProperties)) {
for(var i = 0; i < hiddenProperties.length; i++) {
delete obj[hiddenProperties[i]];
}
}
return obj;
}
// setup the initial model // setup the initial model
Model.setup(); Model.setup();

View File

@ -7,14 +7,28 @@ describe('hidden properties', function () {
options: {hidden: ['secret']}, options: {hidden: ['secret']},
dataSource: loopback.memory() dataSource: loopback.memory()
}); });
var Category = this.Category = this.app.model('category', {
dataSource: loopback.memory()
});
Category.hasMany(Product);
app.use(loopback.rest()); app.use(loopback.rest());
Category.create({
Product.create( name: 'my category'
{name: 'pencil', secret: 'secret'}, }, function(err, category) {
done category.products.create({
); name: 'pencil',
secret: 'a secret'
}, done);
});
}); });
afterEach(function(done) {
var Product = this.Product;
this.Category.destroyAll(function() {
Product.destroyAll(done);
});
})
it('should hide a property remotely', function (done) { it('should hide a property remotely', function (done) {
request(this.app) request(this.app)
.get('/products') .get('/products')
@ -27,4 +41,19 @@ describe('hidden properties', function () {
done(); done();
}); });
}); });
it('should hide a property of nested models', function (done) {
var app = this.app;
request(app)
.get('/categories?filter[include]=products')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res){
if(err) return done(err);
var category = res.body[0];
var product = category.products[0];
assert.equal(product.secret, undefined);
done();
});
});
}); });