Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,8 @@ module.exports = function(User) {
// add principalType in AccessToken.query if using polymorphic relations
// between AccessToken and User
var relatedUser = AccessToken.relations.user;
var isRelationPolymorphic = relatedUser.polymorphic && !relatedUser.modelTo;
var isRelationPolymorphic = relatedUser && relatedUser.polymorphic &&
!relatedUser.modelTo;
if (isRelationPolymorphic) {
query.principalType = this.modelName;
}
Expand Down
22 changes: 22 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,28 @@ describe('User', function() {
});
});

// See https://github.com/strongloop/loopback/issues/3215
it('handles subclassed user with no accessToken relation', () => {
// setup a new LoopBack app, we don't want to use shared models
app = loopback({localRegistry: true, loadBuiltinModels: true});
app.set('remoting', {errorHandler: {debug: true, log: false}});
app.dataSource('db', {connector: 'memory'});
const User = app.registry.createModel({
name: 'user',
base: 'User',
});
app.model(User, {dataSource: 'db'});
app.enableAuth({dataSource: 'db'});
expect(app.models.User.modelName).to.eql('user');

return User.create(validCredentials)
.then(u => {
u.email = 'updated@example.com';
return u.save();
// the test passes when save() does not throw any error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@superkhau superkhau Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You wrote the style guide entry though so you would know better. ;) What I'm suggesting is we should have something that catches and calls expect.fail() instead of the comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm suggesting is we should have something that catches and calls expect.fail() instead of the comment.

By calling expect.fail, the test would hide the original error and report an unhelpful failure.

Note that we are asserting a resolved promise here, not a rejected promise as is described in the style guide link you pasted.

I suppose I could rewrite this test as follows, would you find it easier to read?

.then(u => {
  u.email = 'updated@example.com';
  return u.save();
})
.then(u => {
  // the test passes when save() does not throw any error
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit better, but not worth the effort. LGTM. ;)

});
});

function assertPreservedTokens(done) {
AccessToken.find({where: {userId: user.pk}}, function(err, tokens) {
if (err) return done(err);
Expand Down