Skip to content

Commit

Permalink
NEW unit test for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreclr committed Jan 26, 2017
1 parent 719945d commit 199fa03
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/role.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,88 @@ describe('role model', function() {
.then(_ => done());
});

it('should resolve owner when no flag on userId property', function(done) {
var Message = app.registry.createModel('message',
{ content: String, userId: Number },
{
relations: {
// It shouldn't resolve the `sender` role
sender: {
type: 'belongsTo',
model: 'User',
foreignKey: 'senderId',
},
}
});

app.model(Message, {dataSource: 'db'});

var password = 'pass';
new Promise((resolve, reject) => {
app.models.User.create(
[
{email: 'user@user.com', password},
{email: 'sender@sender.com', password}
], (err, users) => {
if (err) return reject(err);
resolve(users);
});
}).then(users => {
var user = users[0];
var sender = users[1];
var userId = user.id;
var senderId = sender.id;
return Promise.props({
sender, user,
messages: new Promise((resolve, reject) => {
Message.create([
{userId}, {userId, senderId}, {}
], (err, messages) => {
if (err) return reject(err);
resolve(messages);
})
})
});
}).then(data => {
var {sender, user, messages} = data;
var userRole = {
principalType: ACL.USER, principalId: user.id,
model: Message, id: messages[0].id,
};
var senderRole = {
principalType: ACL.USER, principalId: user.id,
model: Message, id: messages[1].id,
};
var noOwnerRole = {
principalType: ACL.USER, principalId: sender.id,
model: Message, id: messages[2].id,
};

return Promise.all([
new Promise((resolve, reject) => {
Role.isInRole(Role.OWNER, userRole, (err, yes) => {
if (err) return reject(err);
resolve(yes);
})
}),
new Promise((resolve, reject) => {
Role.isInRole(Role.OWNER, senderRole, (err, yes) => {
if (err) return reject(err);
resolve(yes);
});
}),
new Promise((resolve, reject) => {
Role.isInRole(Role.OWNER, noOwnerRole, (err, yes) => {
if (err) return reject(err);
resolve(!yes);
});
}),
]);
}).catch(done)
.map(bool => assert(bool))
.then(_ => done());
});

describe('isMappedToRole', function() {
var user, app, role;

Expand Down

0 comments on commit 199fa03

Please sign in to comment.