2020-01-21 19:19:18 +00:00
|
|
|
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
2017-01-31 22:07:24 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const expect = require('./helpers/expect');
|
|
|
|
const loopback = require('../');
|
|
|
|
const Promise = require('bluebird');
|
2017-01-31 22:07:24 +00:00
|
|
|
|
|
|
|
describe('role-mapping model', function() {
|
|
|
|
this.timeout(10000);
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
let app, oneUser, anApp, aRole;
|
|
|
|
const models = {};
|
2017-01-31 22:07:24 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
app = loopback({localRegistry: true, loadBuiltinModels: true});
|
|
|
|
app.dataSource('db', {connector: 'memory'});
|
|
|
|
|
|
|
|
// setup models
|
|
|
|
['User', 'Role', 'RoleMapping', 'Application'].map(setupModel);
|
|
|
|
|
|
|
|
// create generic instances
|
|
|
|
return Promise.all([
|
|
|
|
models.User.create({
|
|
|
|
username: 'oneUser',
|
|
|
|
email: 'user@email.com',
|
|
|
|
password: 'password',
|
|
|
|
}),
|
|
|
|
models.Application.create({name: 'anApp'}),
|
|
|
|
models.Role.create({name: 'aRole'}),
|
|
|
|
])
|
2017-12-12 08:33:15 +00:00
|
|
|
.spread(function(u, a, r) {
|
|
|
|
oneUser = u;
|
|
|
|
anApp = a;
|
|
|
|
aRole = r;
|
|
|
|
});
|
2017-01-31 22:07:24 +00:00
|
|
|
|
|
|
|
// helper
|
|
|
|
function setupModel(modelName) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const model = app.registry.getModel(modelName);
|
2017-01-31 22:07:24 +00:00
|
|
|
app.model(model, {dataSource: 'db'});
|
|
|
|
models[modelName] = model;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports .user() with a callback', function(done) {
|
|
|
|
models.RoleMapping.create(
|
|
|
|
{principalType: 'USER', principalId: oneUser.id},
|
|
|
|
function(err, mapping) {
|
|
|
|
if (err) done(err);
|
|
|
|
mapping.user(function(err, user) {
|
|
|
|
if (err) done(err);
|
|
|
|
expect(user.id).to.equal(oneUser.id);
|
|
|
|
done();
|
|
|
|
});
|
2019-11-17 19:04:57 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
);
|
2017-01-31 22:07:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('supports .user() returning a promise', function() {
|
|
|
|
return models.RoleMapping.create({principalType: 'USER', principalId: oneUser.id})
|
|
|
|
.then(function(mapping) {
|
|
|
|
return mapping.user();
|
|
|
|
})
|
|
|
|
.then(function(user) {
|
|
|
|
expect(user.id).to.equal(oneUser.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports .application() with a callback', function(done) {
|
|
|
|
models.RoleMapping.create(
|
|
|
|
{principalType: 'APP', principalId: anApp.id},
|
|
|
|
function(err, mapping) {
|
|
|
|
if (err) done(err);
|
|
|
|
mapping.application(function(err, app) {
|
|
|
|
if (err) done(err);
|
|
|
|
expect(app.id).to.equal(anApp.id);
|
|
|
|
done();
|
|
|
|
});
|
2019-11-17 19:04:57 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
);
|
2017-01-31 22:07:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('supports .application() returning a promise', function() {
|
|
|
|
return models.RoleMapping.create({principalType: 'APP', principalId: anApp.id})
|
|
|
|
.then(function(mapping) {
|
|
|
|
return mapping.application();
|
|
|
|
})
|
|
|
|
.then(function(app) {
|
|
|
|
expect(app.id).to.equal(anApp.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports .childRole() with a callback', function(done) {
|
|
|
|
models.RoleMapping.create(
|
|
|
|
{principalType: 'ROLE', principalId: aRole.id},
|
|
|
|
function(err, mapping) {
|
|
|
|
if (err) done(err);
|
|
|
|
mapping.childRole(function(err, role) {
|
|
|
|
if (err) done(err);
|
|
|
|
expect(role.id).to.equal(aRole.id);
|
|
|
|
done();
|
|
|
|
});
|
2019-11-17 19:04:57 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
);
|
2017-01-31 22:07:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('supports .childRole() returning a promise', function() {
|
|
|
|
return models.RoleMapping.create({principalType: 'ROLE', principalId: aRole.id})
|
|
|
|
.then(function(mapping) {
|
|
|
|
return mapping.childRole();
|
|
|
|
})
|
|
|
|
.then(function(role) {
|
|
|
|
expect(role.id).to.equal(aRole.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|