2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
var path = require('path');
|
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
describe('loopback.rest', function() {
|
2016-05-02 12:53:21 +00:00
|
|
|
var app, MyModel;
|
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
beforeEach(function() {
|
2016-05-02 12:53:21 +00:00
|
|
|
// override the global app object provided by test/support.js
|
|
|
|
// and create a local one that does not share state with other tests
|
|
|
|
app = loopback({ localRegistry: true, loadBuiltinModels: true });
|
|
|
|
var db = app.dataSource('db', { connector: 'memory' });
|
|
|
|
MyModel = app.registry.createModel('MyModel');
|
|
|
|
MyModel.attachTo(db);
|
2014-02-04 15:17:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works out-of-the-box', function(done) {
|
2014-07-31 04:57:45 +00:00
|
|
|
app.model(MyModel);
|
2014-02-04 15:17:32 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/mymodels')
|
|
|
|
.expect(200)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
it('should report 200 for DELETE /:id found', function(done) {
|
|
|
|
app.set('legacyExplorer', false);
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
2016-04-01 09:14:26 +00:00
|
|
|
MyModel.create({ name: 'm1' }, function(err, inst) {
|
2015-09-19 02:13:30 +00:00
|
|
|
request(app)
|
|
|
|
.del('/mymodels/' + inst.id)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
expect(res.body.count).to.equal(1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-31 04:57:45 +00:00
|
|
|
it('should report 404 for GET /:id not found', function(done) {
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/mymodels/1')
|
|
|
|
.expect(404)
|
2014-12-18 20:26:27 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert(errorResponse);
|
|
|
|
assert.equal(errorResponse.code, 'MODEL_NOT_FOUND');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-07-31 04:57:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should report 404 for HEAD /:id not found', function(done) {
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).head('/mymodels/1')
|
|
|
|
.expect(404)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
2014-10-22 21:39:39 +00:00
|
|
|
it('should report 200 for GET /:id/exists not found', function(done) {
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/mymodels/1/exists')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
expect(res.body).to.eql({ exists: false });
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-22 21:39:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-31 04:57:45 +00:00
|
|
|
it('should report 200 for GET /:id found', function(done) {
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
2016-04-01 09:14:26 +00:00
|
|
|
MyModel.create({ name: 'm1' }, function(err, inst) {
|
2014-07-31 04:57:45 +00:00
|
|
|
request(app).get('/mymodels/' + inst.id)
|
|
|
|
.expect(200)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report 200 for HEAD /:id found', function(done) {
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
2016-04-01 09:14:26 +00:00
|
|
|
MyModel.create({ name: 'm2' }, function(err, inst) {
|
2014-07-31 04:57:45 +00:00
|
|
|
request(app).head('/mymodels/' + inst.id)
|
|
|
|
.expect(200)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-22 21:39:39 +00:00
|
|
|
it('should report 200 for GET /:id/exists found', function(done) {
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
2016-04-01 09:14:26 +00:00
|
|
|
MyModel.create({ name: 'm2' }, function(err, inst) {
|
2014-10-22 21:39:39 +00:00
|
|
|
request(app).get('/mymodels/' + inst.id + '/exists')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
expect(res.body).to.eql({ exists: true });
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-22 21:39:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-31 09:36:59 +00:00
|
|
|
it('should honour `remoting.rest.supportedTypes`', function(done) {
|
2016-05-02 12:53:21 +00:00
|
|
|
var app = loopback({ localRegistry: true });
|
2014-10-31 09:36:59 +00:00
|
|
|
|
|
|
|
// NOTE it is crucial to set `remoting` before creating any models
|
|
|
|
var supportedTypes = ['json', 'application/javascript', 'text/javascript'];
|
2016-04-01 09:14:26 +00:00
|
|
|
app.set('remoting', { rest: { supportedTypes: supportedTypes }});
|
2014-10-31 09:36:59 +00:00
|
|
|
|
|
|
|
app.model(MyModel);
|
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
request(app).get('/mymodels')
|
2014-11-21 02:35:36 +00:00
|
|
|
.set('Accept', 'text/html,application/xml;q= 0.9,*/*;q= 0.8')
|
2014-10-31 09:36:59 +00:00
|
|
|
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
|
2015-01-22 07:02:43 +00:00
|
|
|
it('allows models to provide a custom HTTP path', function(done) {
|
2016-05-02 12:53:21 +00:00
|
|
|
var CustomModel = app.registry.createModel('CustomModel',
|
2015-01-22 07:02:43 +00:00
|
|
|
{ name: String },
|
2016-04-01 09:14:26 +00:00
|
|
|
{ http: { 'path': 'domain1/CustomModelPath' },
|
2015-01-22 07:02:43 +00:00
|
|
|
});
|
|
|
|
|
2016-05-02 12:53:21 +00:00
|
|
|
app.model(CustomModel, { dataSource: 'db' });
|
2015-01-22 07:02:43 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
request(app).get('/domain1/CustomModelPath').expect(200).end(done);
|
|
|
|
});
|
|
|
|
|
2015-01-23 13:04:13 +00:00
|
|
|
it('should report 200 for url-encoded HTTP path', function(done) {
|
2016-05-02 12:53:21 +00:00
|
|
|
var CustomModel = app.registry.createModel('CustomModel',
|
2015-01-23 13:04:13 +00:00
|
|
|
{ name: String },
|
2016-04-01 09:14:26 +00:00
|
|
|
{ http: { path: 'domain%20one/CustomModelPath' },
|
2015-01-23 13:04:13 +00:00
|
|
|
});
|
|
|
|
|
2016-05-02 12:53:21 +00:00
|
|
|
app.model(CustomModel, { dataSource: 'db' });
|
2015-01-23 13:04:13 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
request(app).get('/domain%20one/CustomModelPath').expect(200).end(done);
|
|
|
|
});
|
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
it('includes loopback.token when necessary', function(done) {
|
|
|
|
givenUserModelWithAuth();
|
2016-05-02 12:53:21 +00:00
|
|
|
app.enableAuth({ dataSource: 'db' });
|
2014-02-04 15:17:32 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
givenLoggedInUser(function(err, token) {
|
|
|
|
if (err) return done(err);
|
2016-05-02 12:53:21 +00:00
|
|
|
expect(token).instanceOf(app.models.AccessToken);
|
2014-02-04 15:17:32 +00:00
|
|
|
request(app).get('/users/' + token.userId)
|
|
|
|
.set('Authorization', token.id)
|
|
|
|
.expect(200)
|
|
|
|
.end(done);
|
2014-11-21 01:52:11 +00:00
|
|
|
}, done);
|
2014-02-04 15:17:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not include loopback.token when auth not enabled', function(done) {
|
|
|
|
var User = givenUserModelWithAuth();
|
|
|
|
User.getToken = function(req, cb) {
|
|
|
|
cb(null, req.accessToken ? req.accessToken.id : null);
|
|
|
|
};
|
|
|
|
loopback.remoteMethod(User.getToken, {
|
2016-04-01 09:14:26 +00:00
|
|
|
accepts: [{ type: 'object', http: { source: 'req' }}],
|
|
|
|
returns: [{ type: 'object', name: 'id' }],
|
2014-02-04 15:17:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.use(loopback.rest());
|
|
|
|
givenLoggedInUser(function(err, token) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
request(app).get('/users/getToken')
|
|
|
|
.set('Authorization', token.id)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
expect(res.body.id).to.equal(null);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-11-21 01:52:11 +00:00
|
|
|
}, done);
|
2014-02-04 15:17:32 +00:00
|
|
|
});
|
|
|
|
|
2015-02-25 19:48:08 +00:00
|
|
|
it('should report 200 for legacy explorer route /routes', function(done) {
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/routes')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-25 19:48:08 +00:00
|
|
|
expect(res.body).to.eql([]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-25 19:48:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report 200 for legacy explorer route /models', function(done) {
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/models')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-25 19:48:08 +00:00
|
|
|
expect(res.body).to.eql({});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-25 19:48:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report 404 for disabled legacy explorer route /routes', function(done) {
|
|
|
|
app.set('legacyExplorer', false);
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/routes')
|
|
|
|
.expect(404)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report 404 for disabled legacy explorer route /models', function(done) {
|
|
|
|
app.set('legacyExplorer', false);
|
|
|
|
app.use(loopback.rest());
|
|
|
|
request(app).get('/models')
|
|
|
|
.expect(404)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
describe('context propagation', function() {
|
|
|
|
var User;
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
User = givenUserModelWithAuth();
|
|
|
|
User.getToken = function(cb) {
|
|
|
|
var context = loopback.getCurrentContext();
|
|
|
|
var req = context.get('http').req;
|
|
|
|
expect(req).to.have.property('accessToken');
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
var juggler = require('loopback-datasource-juggler');
|
|
|
|
expect(juggler.getCurrentContext().get('http').req)
|
|
|
|
.to.have.property('accessToken');
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
var remoting = require('strong-remoting');
|
|
|
|
expect(remoting.getCurrentContext().get('http').req)
|
|
|
|
.to.have.property('accessToken');
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
cb(null, req && req.accessToken ? req.accessToken.id : null);
|
|
|
|
};
|
|
|
|
// Set up the ACL
|
2016-04-01 09:14:26 +00:00
|
|
|
User.settings.acls.push({ principalType: 'ROLE',
|
2014-10-22 21:29:56 +00:00
|
|
|
principalId: '$authenticated', permission: 'ALLOW',
|
2016-04-01 09:14:26 +00:00
|
|
|
property: 'getToken' });
|
2014-10-22 21:29:56 +00:00
|
|
|
|
|
|
|
loopback.remoteMethod(User.getToken, {
|
|
|
|
accepts: [],
|
|
|
|
returns: [
|
2016-04-01 09:14:26 +00:00
|
|
|
{ type: 'object', name: 'id' },
|
|
|
|
],
|
2014-10-22 21:29:56 +00:00
|
|
|
});
|
2014-06-17 17:35:19 +00:00
|
|
|
});
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
function invokeGetToken(done) {
|
|
|
|
givenLoggedInUser(function(err, token) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
request(app).get('/users/getToken')
|
|
|
|
.set('Authorization', token.id)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
expect(res.body.id).to.equal(token.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
it('should enable context using loopback.context', function(done) {
|
2014-11-04 10:27:49 +00:00
|
|
|
app.use(loopback.context({ enableHttpContext: true }));
|
2016-05-02 12:53:21 +00:00
|
|
|
app.enableAuth({ dataSource: 'db' });
|
2014-10-22 21:29:56 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
invokeGetToken(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should enable context with loopback.rest', function(done) {
|
2016-05-02 12:53:21 +00:00
|
|
|
app.enableAuth({ dataSource: 'db' });
|
2016-04-01 09:14:26 +00:00
|
|
|
app.set('remoting', { context: { enableHttpContext: true }});
|
2014-11-04 10:27:49 +00:00
|
|
|
app.use(loopback.rest());
|
2014-10-22 21:29:56 +00:00
|
|
|
|
|
|
|
invokeGetToken(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support explicit context', function(done) {
|
2016-05-02 12:53:21 +00:00
|
|
|
app.enableAuth({ dataSource: 'db' });
|
2014-10-22 21:29:56 +00:00
|
|
|
app.use(loopback.context());
|
|
|
|
app.use(loopback.token(
|
2016-05-02 12:53:21 +00:00
|
|
|
{ model: app.registry.getModelByType('AccessToken') }));
|
2014-10-22 21:29:56 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
loopback.getCurrentContext().set('accessToken', req.accessToken);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
User.getToken = function(cb) {
|
|
|
|
var context = loopback.getCurrentContext();
|
|
|
|
var accessToken = context.get('accessToken');
|
|
|
|
expect(context.get('accessToken')).to.have.property('id');
|
|
|
|
|
|
|
|
var juggler = require('loopback-datasource-juggler');
|
|
|
|
context = juggler.getCurrentContext();
|
|
|
|
expect(context.get('accessToken')).to.have.property('id');
|
|
|
|
|
|
|
|
var remoting = require('strong-remoting');
|
|
|
|
context = remoting.getCurrentContext();
|
|
|
|
expect(context.get('accessToken')).to.have.property('id');
|
|
|
|
|
|
|
|
cb(null, accessToken ? accessToken.id : null);
|
|
|
|
};
|
|
|
|
|
|
|
|
loopback.remoteMethod(User.getToken, {
|
|
|
|
accepts: [],
|
|
|
|
returns: [
|
2016-04-01 09:14:26 +00:00
|
|
|
{ type: 'object', name: 'id' },
|
|
|
|
],
|
2014-10-22 21:29:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
invokeGetToken(done);
|
2014-06-17 17:35:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
function givenUserModelWithAuth() {
|
2016-05-02 12:53:21 +00:00
|
|
|
var AccessToken = app.registry.getModel('AccessToken');
|
|
|
|
app.model(AccessToken, { dataSource: 'db' });
|
|
|
|
var User = app.registry.getModel('User');
|
|
|
|
app.model(User, { dataSource: 'db' });
|
|
|
|
|
|
|
|
// NOTE(bajtos) This is puzzling to me. The built-in User & AccessToken
|
|
|
|
// models should come with both relations already set up, i.e. the
|
|
|
|
// following two lines should not be neccessary.
|
|
|
|
// And it does behave that way when only tests in this file are run.
|
|
|
|
// However, when I run the full test suite (all files), the relations
|
|
|
|
// get broken.
|
|
|
|
AccessToken.belongsTo(User, { as: 'user', foreignKey: 'userId' });
|
|
|
|
User.hasMany(AccessToken, { as: 'accessTokens', foreignKey: 'userId' });
|
|
|
|
|
|
|
|
return User;
|
2014-02-04 15:17:32 +00:00
|
|
|
}
|
2016-05-02 12:53:21 +00:00
|
|
|
|
2014-11-21 01:52:11 +00:00
|
|
|
function givenLoggedInUser(cb, done) {
|
2014-02-04 15:17:32 +00:00
|
|
|
var credentials = { email: 'user@example.com', password: 'pwd' };
|
2016-05-02 12:53:21 +00:00
|
|
|
var User = app.models.User;
|
2014-02-04 15:17:32 +00:00
|
|
|
User.create(credentials,
|
|
|
|
function(err, user) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-04 15:17:32 +00:00
|
|
|
User.login(credentials, cb);
|
|
|
|
});
|
|
|
|
}
|
2015-09-19 02:13:30 +00:00
|
|
|
|
|
|
|
describe('shared methods', function() {
|
|
|
|
function getFixturePath(dirName) {
|
|
|
|
return path.join(__dirname, 'fixtures/shared-methods/' + dirName +
|
|
|
|
'/server/server.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('with specific definitions in model-config.json', function() {
|
|
|
|
it('should not be exposed when the definition value is false',
|
|
|
|
function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var app = require(getFixturePath('model-config-defined-false'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.get('/todos')
|
|
|
|
.expect(404, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
|
|
|
|
it('should be exposed when the definition value is true', function(done) {
|
|
|
|
var app = require(getFixturePath('model-config-defined-true'));
|
|
|
|
request(app)
|
|
|
|
.get('/todos')
|
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with default definitions in model-config.json', function() {
|
|
|
|
it('should not be exposed when the definition value is false',
|
|
|
|
function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var app = require(getFixturePath('model-config-default-false'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.get('/todos')
|
|
|
|
.expect(404, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
|
|
|
|
it('should be exposed when the definition value is true', function(done) {
|
|
|
|
var app = require(getFixturePath('model-config-default-true'));
|
|
|
|
app.models.Todo.create([
|
2016-04-01 09:14:26 +00:00
|
|
|
{ content: 'a' },
|
|
|
|
{ content: 'b' },
|
|
|
|
{ content: 'c' },
|
2015-09-19 02:13:30 +00:00
|
|
|
], function() {
|
|
|
|
request(app)
|
|
|
|
.del('/todos')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
expect(res.body.count).to.equal(3);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with specific definitions in config.json', function() {
|
|
|
|
it('should not be exposed when the definition value is false',
|
|
|
|
function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var app = require(getFixturePath('config-defined-false'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.get('/todos')
|
|
|
|
.expect(404, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
|
|
|
|
it('should be exposed when the definition value is true',
|
|
|
|
function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var app = require(getFixturePath('config-defined-true'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.get('/todos')
|
|
|
|
.expect(200, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('with default definitions in config.json', function() {
|
|
|
|
it('should not be exposed when the definition value is false',
|
|
|
|
function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var app = require(getFixturePath('config-default-false'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.get('/todos')
|
|
|
|
.expect(404, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
|
|
|
|
it('should be exposed when the definition value is true', function(done) {
|
|
|
|
var app = require(getFixturePath('config-default-true'));
|
|
|
|
app.models.Todo.create([
|
2016-04-01 09:14:26 +00:00
|
|
|
{ content: 'a' },
|
|
|
|
{ content: 'b' },
|
|
|
|
{ content: 'c' },
|
2015-09-19 02:13:30 +00:00
|
|
|
], function() {
|
|
|
|
request(app)
|
|
|
|
.del('/todos')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
expect(res.body.count).to.equal(3);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-19 02:13:30 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// The fixture in `shared-method/both-configs-set/config.json` has `*:false`
|
|
|
|
// set which disables the REST endpoints for built-in models such as User as
|
|
|
|
// a side effect since tests share the same loopback instance. As a
|
|
|
|
// consequence, this causes the tests in user.integration to fail.
|
|
|
|
describe.skip('with definitions in both config.json and model-config.json',
|
|
|
|
function() {
|
2016-04-01 09:14:26 +00:00
|
|
|
it('should prioritize the settings in model-config.json', function(done) {
|
|
|
|
var app = require(getFixturePath('both-configs-set'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.del('/todos')
|
|
|
|
.expect(404, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('should fall back to config.json settings if setting is not found in' +
|
2015-09-19 02:13:30 +00:00
|
|
|
'model-config.json', function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var app = require(getFixturePath('both-configs-set'));
|
|
|
|
request(app)
|
2015-09-19 02:13:30 +00:00
|
|
|
.get('/todos')
|
|
|
|
.expect(404, done);
|
2016-04-01 09:14:26 +00:00
|
|
|
});
|
|
|
|
});
|
2015-09-19 02:13:30 +00:00
|
|
|
});
|
2014-02-04 15:17:32 +00:00
|
|
|
});
|