Skip to content

Commit

Permalink
Merge pull request strongloop#3681 from zipitwireless/master
Browse files Browse the repository at this point in the history
Update nestRemoting to pass optionsFromContext
  • Loading branch information
bajtos authored Dec 12, 2017
2 parents fdb4539 + 317e00d commit b045e4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
35 changes: 21 additions & 14 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,38 +947,45 @@ module.exports = function(registry) {
}
});

const lastArg = opts.accepts[opts.accepts.length - 1] || {};
const hasOptionsFromContext =
(lastArg.arg || lastArg.name) === 'options' &&
lastArg.type === 'object' && lastArg.http;

if (relation.multiple) {
sharedClass.defineMethod(methodName, opts, function(fkId) {
var args = Array.prototype.slice.call(arguments, 1);
var last = args[args.length - 1];
var cb = typeof last === 'function' ? last : null;
this[getterName](fkId, function(err, inst) {
if (err && cb) return cb(err);
const args = Array.prototype.slice.call(arguments, 1);
const cb = args[args.length - 1];
const contextOptions =
hasOptionsFromContext && args[args.length - 2] || {};
this[getterName](fkId, contextOptions, function(err, inst) {
if (err) return cb(err);
if (inst instanceof relation.modelTo) {
try {
nestedFn.apply(inst, args);
} catch (err) {
if (cb) return cb(err);
return cb(err);
}
} else if (cb) {
} else {
cb(err, null);
}
});
}, method.isStatic);
} else {
sharedClass.defineMethod(methodName, opts, function() {
var args = Array.prototype.slice.call(arguments);
var last = args[args.length - 1];
var cb = typeof last === 'function' ? last : null;
this[getterName](function(err, inst) {
if (err && cb) return cb(err);
const args = Array.prototype.slice.call(arguments);
const cb = args[args.length - 1];
const contextOptions =
hasOptionsFromContext && args[args.length - 2] || {};
this[getterName](contextOptions, function(err, inst) {
if (err) return cb(err);
if (inst instanceof relation.modelTo) {
try {
nestedFn.apply(inst, args);
} catch (err) {
if (cb) return cb(err);
return cb(err);
}
} else if (cb) {
} else {
cb(err, null);
}
});
Expand Down
1 change: 0 additions & 1 deletion test/multiple-user-principal-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ describe('Multiple users with custom principalType', function() {
});

it('fails when the access token belongs to a different user mode', () => {
debugger;
logServerErrorsOtherThan(403, app);
return supertest(app)
.post('/AnotherUsers/change-password')
Expand Down
23 changes: 22 additions & 1 deletion test/relations.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,8 @@ describe('relations - integration', function() {
});

describe('nested relations', function() {
let accessOptions;

before(function defineModels() {
var Book = app.registry.createModel(
'Book',
Expand Down Expand Up @@ -1486,7 +1488,8 @@ describe('relations - integration', function() {
throw new Error('This should not crash the app');
};

Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'}});
Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'},
accepts: [{arg: 'options', type: 'object', http: 'optionsFromRequest'}]});

// Now `pages` has nestRemoting set to true and no need to call nestRemoting()
// Book.nestRemoting('pages');
Expand All @@ -1507,6 +1510,15 @@ describe('relations - integration', function() {

next();
});

Page.observe('access', function(ctx, next) {
accessOptions = ctx.options;
next();
});
});

beforeEach(function resetAccessOptions() {
accessOptions = 'access hook not triggered';
});

before(function createBook(done) {
Expand Down Expand Up @@ -1619,6 +1631,7 @@ describe('relations - integration', function() {
it('enables nested relationship routes - belongsTo findById', function(done) {
var test = this;
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
.expect(200)
.end(function(err, res) {
if (err) return done(err);

Expand Down Expand Up @@ -1658,6 +1671,14 @@ describe('relations - integration', function() {
});
});

it('passes options to nested relationship routes', function() {
return this.get(`/api/books/${this.book.id}/pages/${this.page.id}/notes/${this.note.id}`)
.expect(200)
.then(res => {
expect(accessOptions).to.have.property('accessToken');
});
});

it('should nest remote hooks of ModelTo - hasMany findById', function(done) {
var test = this;
this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id)
Expand Down

0 comments on commit b045e4a

Please sign in to comment.