Add test for remoting nested hidden properties
This commit is contained in:
parent
e9323ce664
commit
63ad8f69ae
|
@ -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();
|
||||
|
||||
|
|
|
@ -7,13 +7,27 @@ describe('hidden properties', function () {
|
|||
options: {hidden: ['secret']},
|
||||
dataSource: loopback.memory()
|
||||
});
|
||||
app.use(loopback.rest());
|
||||
|
||||
Product.create(
|
||||
{name: 'pencil', secret: 'secret'},
|
||||
done
|
||||
);
|
||||
var Category = this.Category = this.app.model('category', {
|
||||
dataSource: loopback.memory()
|
||||
});
|
||||
Category.hasMany(Product);
|
||||
app.use(loopback.rest());
|
||||
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)
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue