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
Model.setup();

View File

@ -7,14 +7,28 @@ describe('hidden properties', function () {
options: {hidden: ['secret']},
dataSource: loopback.memory()
});
var Category = this.Category = this.app.model('category', {
dataSource: loopback.memory()
});
Category.hasMany(Product);
app.use(loopback.rest());
Product.create(
{name: 'pencil', secret: 'secret'},
done
);
Category.create({
name: 'my category'
}, function(err, category) {
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) {
request(this.app)
.get('/products')
@ -27,4 +41,19 @@ describe('hidden properties', function () {
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();
});
});
});