2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const loopback = require('../');
|
|
|
|
const request = require('supertest');
|
2014-03-21 19:18:00 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('hidden properties', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = this.app = loopback();
|
|
|
|
const Product = this.Product = loopback.PersistedModel.extend(
|
2018-08-08 15:22:20 +00:00
|
|
|
'product',
|
2014-05-03 04:19:14 +00:00
|
|
|
{},
|
2019-11-17 19:04:57 +00:00
|
|
|
{hidden: ['secret']},
|
2014-05-03 04:19:14 +00:00
|
|
|
);
|
|
|
|
Product.attachTo(loopback.memory());
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const Category = this.Category = loopback.PersistedModel.extend('category');
|
2014-05-03 04:19:14 +00:00
|
|
|
Category.attachTo(loopback.memory());
|
2014-04-11 18:40:53 +00:00
|
|
|
Category.hasMany(Product);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 04:19:14 +00:00
|
|
|
app.model(Product);
|
|
|
|
app.model(Category);
|
2014-03-21 19:53:04 +00:00
|
|
|
app.use(loopback.rest());
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-04-11 18:40:53 +00:00
|
|
|
Category.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'my category',
|
2014-04-11 18:40:53 +00:00
|
|
|
}, function(err, category) {
|
|
|
|
category.products.create({
|
|
|
|
name: 'pencil',
|
2016-04-01 09:14:26 +00:00
|
|
|
secret: 'a secret',
|
2014-04-11 18:40:53 +00:00
|
|
|
}, done);
|
|
|
|
});
|
2014-03-21 19:18:00 +00:00
|
|
|
});
|
|
|
|
|
2014-04-11 18:40:53 +00:00
|
|
|
afterEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const Product = this.Product;
|
2014-04-11 18:40:53 +00:00
|
|
|
this.Category.destroyAll(function() {
|
|
|
|
Product.destroyAll(done);
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|
2014-04-11 18:40:53 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should hide a property remotely', function(done) {
|
|
|
|
request(this.app)
|
2017-12-12 08:33:15 +00:00
|
|
|
.get('/products')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const product = res.body[0];
|
2017-12-12 08:33:15 +00:00
|
|
|
assert.equal(product.secret, undefined);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-03-21 19:18:00 +00:00
|
|
|
});
|
2014-04-11 18:40:53 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should hide a property of nested models', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = this.app;
|
2014-04-11 18:40:53 +00:00
|
|
|
request(app)
|
|
|
|
.get('/categories?filter[include]=products')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const category = res.body[0];
|
|
|
|
const product = category.products[0];
|
2014-04-11 18:40:53 +00:00
|
|
|
assert.equal(product.secret, undefined);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-04-11 18:40:53 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-03-21 19:18:00 +00:00
|
|
|
});
|