2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// 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';
|
2013-11-10 06:22:16 +00:00
|
|
|
var assert = require('assert');
|
2015-03-13 15:50:30 +00:00
|
|
|
var sinon = require('sinon');
|
2013-11-10 06:22:16 +00:00
|
|
|
var loopback = require('../index');
|
2015-08-13 15:58:41 +00:00
|
|
|
var async = require('async');
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2016-04-26 04:35:54 +00:00
|
|
|
var Promise = require('bluebird');
|
2013-11-10 06:22:16 +00:00
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
function checkResult(err, result) {
|
|
|
|
assert(!err);
|
|
|
|
}
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('role model', function() {
|
2016-08-08 13:12:56 +00:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
var app, Role, RoleMapping, User, Application, ACL;
|
2014-01-23 22:26:45 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2016-08-03 13:55:29 +00:00
|
|
|
// Use local app registry to ensure models are isolated to avoid
|
2014-02-14 18:31:30 +00:00
|
|
|
// pollutions from other tests
|
2016-11-15 21:46:23 +00:00
|
|
|
app = loopback({localRegistry: true, loadBuiltinModels: true});
|
|
|
|
app.dataSource('db', {connector: 'memory'});
|
2016-08-03 13:55:29 +00:00
|
|
|
|
|
|
|
ACL = app.registry.getModel('ACL');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(ACL, {dataSource: 'db'});
|
2016-08-03 13:55:29 +00:00
|
|
|
|
|
|
|
User = app.registry.getModel('User');
|
2016-11-15 21:46:23 +00:00
|
|
|
// Speed up the password hashing algorithm for tests
|
|
|
|
User.settings.saltWorkFactor = 4;
|
|
|
|
app.model(User, {dataSource: 'db'});
|
2016-08-03 13:55:29 +00:00
|
|
|
|
|
|
|
Role = app.registry.getModel('Role');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Role, {dataSource: 'db'});
|
2016-08-03 13:55:29 +00:00
|
|
|
|
|
|
|
RoleMapping = app.registry.getModel('RoleMapping');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(RoleMapping, {dataSource: 'db'});
|
2016-08-03 13:55:29 +00:00
|
|
|
|
|
|
|
Application = app.registry.getModel('Application');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Application, {dataSource: 'db'});
|
2016-08-03 13:55:29 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
ACL.roleModel = Role;
|
|
|
|
ACL.roleMappingModel = RoleMapping;
|
|
|
|
ACL.userModel = User;
|
|
|
|
ACL.applicationModel = Application;
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
2013-11-12 18:10:32 +00:00
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
it('should define role/role relations', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'user'}, function(err, userRole) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'admin'}, function(err, adminRole) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
userRole.principals.create(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.ROLE, principalId: adminRole.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, mapping) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
function(next) {
|
|
|
|
Role.find(function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(roles.length, 2);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
RoleMapping.find(function(err, mappings) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(mappings.length, 1);
|
|
|
|
assert.equal(mappings[0].principalType, RoleMapping.ROLE);
|
|
|
|
assert.equal(mappings[0].principalId, adminRole.id);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
userRole.principals(function(err, principals) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(principals.length, 1);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
userRole.roles(function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(roles.length, 1);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-12 18:10:32 +00:00
|
|
|
});
|
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
it('should define role/user relations', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function(err, user) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
role.principals.create({principalType: RoleMapping.USER, principalId: user.id},
|
2016-04-01 09:14:26 +00:00
|
|
|
function(err, p) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
async.parallel([
|
|
|
|
function(next) {
|
|
|
|
Role.find(function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(roles.length, 1);
|
|
|
|
assert.equal(roles[0].name, 'userRole');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
role.principals(function(err, principals) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(principals.length, 1);
|
|
|
|
assert.equal(principals[0].principalType, RoleMapping.USER);
|
|
|
|
assert.equal(principals[0].principalId, user.id);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
role.users(function(err, users) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(users.length, 1);
|
|
|
|
assert.equal(users[0].id, user.id);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
|
|
|
});
|
2014-06-10 23:39:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-29 08:50:11 +00:00
|
|
|
it('should not allow duplicate role name', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
2016-04-29 08:50:11 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(err).to.exist();
|
2016-04-29 08:50:11 +00:00
|
|
|
expect(err).to.have.property('name', 'ValidationError');
|
|
|
|
expect(err).to.have.deep.property('details.codes.name');
|
|
|
|
expect(err.details.codes.name).to.contain('uniqueness');
|
|
|
|
expect(err).to.have.property('statusCode', 422);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
it('should automatically generate role id', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function(err, user) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2014-06-10 23:39:32 +00:00
|
|
|
assert(role.id);
|
2016-11-15 21:46:23 +00:00
|
|
|
role.principals.create({principalType: RoleMapping.USER, principalId: user.id},
|
2016-04-01 09:14:26 +00:00
|
|
|
function(err, p) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2014-06-10 23:39:32 +00:00
|
|
|
assert(p.id);
|
|
|
|
assert.equal(p.roleId, role.id);
|
2016-08-03 13:55:29 +00:00
|
|
|
async.parallel([
|
|
|
|
function(next) {
|
|
|
|
Role.find(function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(roles.length, 1);
|
|
|
|
assert.equal(roles[0].name, 'userRole');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
role.principals(function(err, principals) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(principals.length, 1);
|
|
|
|
assert.equal(principals[0].principalType, RoleMapping.USER);
|
|
|
|
assert.equal(principals[0].principalId, user.id);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
role.users(function(err, users) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(users.length, 1);
|
|
|
|
assert.equal(users[0].id, user.id);
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
], done);
|
2014-06-10 23:39:32 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-12 18:10:32 +00:00
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
it('should support getRoles() and isInRole()', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function(err, user) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
role.principals.create({principalType: RoleMapping.USER, principalId: user.id},
|
2016-04-01 09:14:26 +00:00
|
|
|
function(err, p) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
'userRole',
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.USER, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, inRole) {
|
|
|
|
if (err) return next(err);
|
|
|
|
// NOTE(bajtos) Apparently isRole is not a boolean,
|
|
|
|
// but the matchin role object instead
|
|
|
|
assert(!!inRole);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
'userRole',
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.APP, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, inRole) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(!inRole);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
'userRole',
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.USER, principalId: 100},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, inRole) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(!inRole);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.getRoles(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.USER, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
expect(roles).to.eql([
|
|
|
|
Role.AUTHENTICATED,
|
|
|
|
Role.EVERYONE,
|
|
|
|
role.id,
|
|
|
|
]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2016-11-23 07:51:23 +00:00
|
|
|
function(next) {
|
|
|
|
Role.getRoles(
|
|
|
|
{principalType: RoleMapping.USER, principalId: user.id},
|
|
|
|
{returnOnlyRoleNames: true},
|
|
|
|
function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
expect(roles).to.eql([
|
|
|
|
Role.AUTHENTICATED,
|
|
|
|
Role.EVERYONE,
|
|
|
|
role.name,
|
|
|
|
]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(next) {
|
|
|
|
Role.getRoles(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.APP, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
expect(roles).to.eql([
|
|
|
|
Role.AUTHENTICATED,
|
|
|
|
Role.EVERYONE,
|
|
|
|
]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.getRoles(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.USER, principalId: 100},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
expect(roles).to.eql([
|
|
|
|
Role.AUTHENTICATED,
|
|
|
|
Role.EVERYONE,
|
|
|
|
]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.getRoles(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: RoleMapping.USER, principalId: null},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
expect(roles).to.eql([
|
|
|
|
Role.UNAUTHENTICATED,
|
|
|
|
Role.EVERYONE,
|
|
|
|
]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
2013-11-12 18:47:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-27 09:51:56 +00:00
|
|
|
it('supports isInRole() returning a Promise', function(done) {
|
|
|
|
var userData = {name: 'Raymond', email: 'x@y.com', password: 'foobar'};
|
|
|
|
User.create(userData, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var principalData = {
|
|
|
|
principalType: RoleMapping.USER,
|
|
|
|
principalId: user.id,
|
|
|
|
};
|
|
|
|
role.principals.create(principalData, function(err, p) {
|
|
|
|
if (err) return done(err);
|
|
|
|
Role.isInRole('userRole', principalData)
|
|
|
|
.then(function(inRole) {
|
|
|
|
expect(inRole).to.be.true();
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports getRole() returning a Promise', function(done) {
|
|
|
|
var userData = {name: 'Raymond', email: 'x@y.com', password: 'foobar'};
|
|
|
|
User.create(userData, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var principalData = {
|
|
|
|
principalType: RoleMapping.USER,
|
|
|
|
principalId: user.id,
|
|
|
|
};
|
|
|
|
role.principals.create(principalData, function(err, p) {
|
|
|
|
if (err) return done(err);
|
|
|
|
Role.getRoles(principalData)
|
|
|
|
.then(function(roles) {
|
|
|
|
expect(roles).to.eql([
|
|
|
|
Role.AUTHENTICATED,
|
|
|
|
Role.EVERYONE,
|
|
|
|
role.id,
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
it('should support owner role resolver', function(done) {
|
2016-04-26 04:35:54 +00:00
|
|
|
Role.registerResolver('returnPromise', function(role, context) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-03 13:55:29 +00:00
|
|
|
var Album = app.registry.createModel('Album', {
|
2013-11-19 19:58:30 +00:00
|
|
|
name: String,
|
2016-04-01 09:14:26 +00:00
|
|
|
userId: Number,
|
2013-11-19 19:58:30 +00:00
|
|
|
}, {
|
|
|
|
relations: {
|
|
|
|
user: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'User',
|
2016-04-01 09:14:26 +00:00
|
|
|
foreignKey: 'userId',
|
|
|
|
},
|
|
|
|
},
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Album, {dataSource: 'db'});
|
2013-11-19 19:58:30 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function(err, user) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
async.parallel([
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
'returnPromise',
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
Role.AUTHENTICATED,
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) next(err);
|
|
|
|
assert(yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
Role.AUTHENTICATED,
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: null},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) next(err);
|
|
|
|
assert(!yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
Role.UNAUTHENTICATED,
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(!yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
Role.UNAUTHENTICATED,
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: null},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
Role.EVERYONE,
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: user.id},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Role.isInRole(
|
|
|
|
Role.EVERYONE,
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: ACL.USER, principalId: null},
|
2016-08-03 13:55:29 +00:00
|
|
|
function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
Album.create({name: 'Album 1', userId: user.id}, function(err, album1) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
var role = {
|
|
|
|
principalType: ACL.USER, principalId: user.id,
|
|
|
|
model: Album, id: album1.id,
|
|
|
|
};
|
|
|
|
Role.isInRole(Role.OWNER, role, function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(yes);
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Album.create({name: 'Album 2'}, function(err, album2) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
role = {
|
|
|
|
principalType: ACL.USER, principalId: user.id,
|
|
|
|
model: Album, id: album2.id,
|
|
|
|
};
|
|
|
|
Role.isInRole(Role.OWNER, role, function(err, yes) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(!yes);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
2016-08-03 13:55:29 +00:00
|
|
|
},
|
|
|
|
], done);
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
2015-08-13 15:58:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('isMappedToRole', function() {
|
|
|
|
var user, app, role;
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
User.create({
|
|
|
|
username: 'john',
|
|
|
|
email: 'john@gmail.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'jpass',
|
2015-08-13 15:58:41 +00:00
|
|
|
}, function(err, u) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
user = u;
|
|
|
|
User.create({
|
|
|
|
username: 'mary',
|
|
|
|
email: 'mary@gmail.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'mpass',
|
2015-08-13 15:58:41 +00:00
|
|
|
}, function(err, u) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
Application.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'demo',
|
2015-08-13 15:58:41 +00:00
|
|
|
}, function(err, a) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
app = a;
|
|
|
|
Role.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'admin',
|
2015-08-13 15:58:41 +00:00
|
|
|
}, function(err, r) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
role = r;
|
|
|
|
var principals = [
|
|
|
|
{
|
|
|
|
principalType: ACL.USER,
|
2016-04-01 09:14:26 +00:00
|
|
|
principalId: user.id,
|
2015-08-13 15:58:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
principalType: ACL.APP,
|
2016-04-01 09:14:26 +00:00
|
|
|
principalId: app.id,
|
|
|
|
},
|
2015-08-13 15:58:41 +00:00
|
|
|
];
|
|
|
|
async.each(principals, function(p, done) {
|
|
|
|
role.principals.create(p, done);
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve user by id', function(done) {
|
|
|
|
ACL.resolvePrincipal(ACL.USER, user.id, function(err, u) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(u.id).to.eql(user.id);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve user by username', function(done) {
|
|
|
|
ACL.resolvePrincipal(ACL.USER, user.username, function(err, u) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(u.username).to.eql(user.username);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve user by email', function(done) {
|
|
|
|
ACL.resolvePrincipal(ACL.USER, user.email, function(err, u) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(u.email).to.eql(user.email);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve app by id', function(done) {
|
|
|
|
ACL.resolvePrincipal(ACL.APP, app.id, function(err, a) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(a.id).to.eql(app.id);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve app by name', function(done) {
|
|
|
|
ACL.resolvePrincipal(ACL.APP, app.name, function(err, a) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(a.name).to.eql(app.name);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report isMappedToRole by user.username', function(done) {
|
|
|
|
ACL.isMappedToRole(ACL.USER, user.username, 'admin', function(err, flag) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(flag).to.eql(true);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report isMappedToRole by user.email', function(done) {
|
|
|
|
ACL.isMappedToRole(ACL.USER, user.email, 'admin', function(err, flag) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(flag).to.eql(true);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report isMappedToRole by user.username for mismatch',
|
|
|
|
function(done) {
|
|
|
|
ACL.isMappedToRole(ACL.USER, 'mary', 'admin', function(err, flag) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(flag).to.eql(false);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report isMappedToRole by app.name', function(done) {
|
|
|
|
ACL.isMappedToRole(ACL.APP, app.name, 'admin', function(err, flag) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(flag).to.eql(true);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report isMappedToRole by app.name', function(done) {
|
|
|
|
ACL.isMappedToRole(ACL.APP, app.name, 'admin', function(err, flag) {
|
|
|
|
if (err) return done(err);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
expect(flag).to.eql(true);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
|
|
|
|
2015-03-13 22:30:53 +00:00
|
|
|
describe('listByPrincipalType', function() {
|
2015-03-13 15:50:30 +00:00
|
|
|
var sandbox;
|
|
|
|
|
2015-03-13 22:30:53 +00:00
|
|
|
beforeEach(function() {
|
2015-03-13 15:50:30 +00:00
|
|
|
sandbox = sinon.sandbox.create();
|
2015-03-12 18:55:39 +00:00
|
|
|
});
|
|
|
|
|
2015-03-13 22:30:53 +00:00
|
|
|
afterEach(function() {
|
2015-03-13 15:50:30 +00:00
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch all models assigned to the role', function(done) {
|
|
|
|
var principalTypesToModels = {};
|
|
|
|
var runs = 0;
|
|
|
|
var mappings;
|
|
|
|
|
|
|
|
principalTypesToModels[RoleMapping.USER] = User;
|
|
|
|
principalTypesToModels[RoleMapping.APPLICATION] = Application;
|
|
|
|
principalTypesToModels[RoleMapping.ROLE] = Role;
|
|
|
|
|
|
|
|
mappings = Object.keys(principalTypesToModels);
|
|
|
|
|
2015-03-13 22:30:53 +00:00
|
|
|
mappings.forEach(function(principalType) {
|
2015-03-13 15:50:30 +00:00
|
|
|
var Model = principalTypesToModels[principalType];
|
2016-11-15 21:46:23 +00:00
|
|
|
Model.create({name: 'test', email: 'x@y.com', password: 'foobar'}, function(err, model) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
var uniqueRoleName = 'testRoleFor' + principalType;
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: uniqueRoleName}, function(err, role) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
role.principals.create({principalType: principalType, principalId: model.id},
|
2016-04-01 09:14:26 +00:00
|
|
|
function(err, p) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2015-03-13 15:50:30 +00:00
|
|
|
var pluralName = Model.pluralModelName.toLowerCase();
|
2015-03-13 22:30:53 +00:00
|
|
|
role[pluralName](function(err, models) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2015-03-13 15:50:30 +00:00
|
|
|
assert.equal(models.length, 1);
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-03-13 15:50:30 +00:00
|
|
|
if (++runs === mappings.length) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-03-13 22:30:53 +00:00
|
|
|
});
|
|
|
|
});
|
2015-03-12 18:55:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-13 19:43:34 +00:00
|
|
|
it('should fetch all models only assigned to the role', function(done) {
|
|
|
|
var principalTypesToModels = {};
|
|
|
|
var mappings;
|
|
|
|
|
|
|
|
principalTypesToModels[RoleMapping.USER] = User;
|
|
|
|
principalTypesToModels[RoleMapping.APPLICATION] = Application;
|
|
|
|
principalTypesToModels[RoleMapping.ROLE] = Role;
|
|
|
|
mappings = Object.keys(principalTypesToModels);
|
|
|
|
|
|
|
|
async.each(mappings, function(principalType, eachCallback) {
|
|
|
|
var Model = principalTypesToModels[principalType];
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
// Create models
|
|
|
|
function(next) {
|
|
|
|
Model.create([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'test', email: 'x@y.com', password: 'foobar'},
|
|
|
|
{name: 'test2', email: 'f@v.com', password: 'bargoo'},
|
|
|
|
{name: 'test3', email: 'd@t.com', password: 'bluegoo'}],
|
2016-09-13 19:43:34 +00:00
|
|
|
function(err, models) {
|
|
|
|
if (err) return next(err);
|
|
|
|
next(null, models);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create Roles
|
|
|
|
function(models, next) {
|
|
|
|
var uniqueRoleName = 'testRoleFor' + principalType;
|
|
|
|
var otherUniqueRoleName = 'otherTestRoleFor' + principalType;
|
|
|
|
Role.create([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: uniqueRoleName},
|
|
|
|
{name: otherUniqueRoleName}],
|
2016-09-13 19:43:34 +00:00
|
|
|
function(err, roles) {
|
|
|
|
if (err) return next(err);
|
|
|
|
next(null, models, roles);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create principles
|
|
|
|
function(models, roles, next) {
|
|
|
|
async.parallel([
|
|
|
|
function(callback) {
|
|
|
|
roles[0].principals.create(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: principalType, principalId: models[0].id},
|
2016-09-13 19:43:34 +00:00
|
|
|
function(err, p) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
callback(p);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(callback) {
|
|
|
|
roles[1].principals.create(
|
2016-11-15 21:46:23 +00:00
|
|
|
{principalType: principalType, principalId: models[1].id},
|
2016-09-13 19:43:34 +00:00
|
|
|
function(err, p) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
callback(p);
|
|
|
|
});
|
|
|
|
}],
|
|
|
|
function(err, principles) {
|
|
|
|
next(null, models, roles, principles);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Run tests against unique Role
|
|
|
|
function(models, roles, principles, next) {
|
|
|
|
var pluralName = Model.pluralModelName.toLowerCase();
|
2016-11-15 21:46:23 +00:00
|
|
|
var uniqueRole = roles[0];
|
2016-09-13 19:43:34 +00:00
|
|
|
uniqueRole[pluralName](function(err, models) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.equal(models.length, 1);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}],
|
|
|
|
eachCallback);
|
|
|
|
}, function(err) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-13 15:50:30 +00:00
|
|
|
it('should apply query', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function(err, user) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
role.principals.create({principalType: RoleMapping.USER, principalId: user.id},
|
2016-04-01 09:14:26 +00:00
|
|
|
function(err, p) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
var query = {fields: ['id', 'name']};
|
2015-03-13 15:50:30 +00:00
|
|
|
sandbox.spy(User, 'find');
|
|
|
|
role.users(query, function(err, users) {
|
2016-08-03 13:55:29 +00:00
|
|
|
if (err) return done(err);
|
2015-03-13 15:50:30 +00:00
|
|
|
assert.equal(users.length, 1);
|
|
|
|
assert.equal(users[0].id, user.id);
|
|
|
|
assert(User.find.calledWith(query));
|
2016-04-29 08:50:11 +00:00
|
|
|
|
2015-03-13 15:50:30 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-03-12 18:55:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-01-27 09:51:56 +00:00
|
|
|
|
|
|
|
it('supports Promise API', function(done) {
|
|
|
|
var userData = {name: 'Raymond', email: 'x@y.com', password: 'foobar'};
|
|
|
|
User.create(userData, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
Role.create({name: 'userRole'}, function(err, role) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var principalData = {
|
|
|
|
principalType: RoleMapping.USER,
|
|
|
|
principalId: user.id,
|
|
|
|
};
|
|
|
|
role.principals.create(principalData, function(err, p) {
|
|
|
|
if (err) return done(err);
|
|
|
|
role.users()
|
|
|
|
.then(function(users) {
|
|
|
|
var userIds = users.map(function(u) { return u.id; });
|
|
|
|
expect(userIds).to.eql([user.id]);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-03-13 22:30:53 +00:00
|
|
|
});
|
2016-05-02 12:30:11 +00:00
|
|
|
|
|
|
|
describe('isOwner', function() {
|
|
|
|
it('supports app-local model registry', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var app = loopback({localRegistry: true, loadBuiltinModels: true});
|
|
|
|
app.dataSource('db', {connector: 'memory'});
|
2016-05-02 12:30:11 +00:00
|
|
|
// attach all auth-related models to 'db' datasource
|
2016-11-15 21:46:23 +00:00
|
|
|
app.enableAuth({dataSource: 'db'});
|
2016-05-02 12:30:11 +00:00
|
|
|
|
|
|
|
var Role = app.models.Role;
|
|
|
|
var User = app.models.User;
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
// Speed up the password hashing algorithm for tests
|
|
|
|
User.settings.saltWorkFactor = 4;
|
|
|
|
|
2016-05-02 12:30:11 +00:00
|
|
|
var u = app.registry.findModel('User');
|
2016-11-15 21:46:23 +00:00
|
|
|
var credentials = {email: 'test@example.com', password: 'pass'};
|
2016-05-02 12:30:11 +00:00
|
|
|
User.create(credentials, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
Role.isOwner(User, user.id, user.id, function(err, result) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-05-02 12:30:11 +00:00
|
|
|
expect(result, 'isOwner result').to.equal(true);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-05-02 12:30:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-01-27 09:51:56 +00:00
|
|
|
|
|
|
|
it('supports Promise API', function(done) {
|
|
|
|
var credentials = {email: 'test@example.com', password: 'pass'};
|
|
|
|
User.create(credentials, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
Role.isOwner(User, user.id, user.id)
|
|
|
|
.then(function(result) {
|
|
|
|
expect(result, 'isOwner result').to.equal(true);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
2016-05-02 12:30:11 +00:00
|
|
|
});
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|