2013-12-20 22:58:29 +00:00
|
|
|
var loopback = require('../');
|
2015-12-31 23:41:18 +00:00
|
|
|
var lt = require('./helpers/loopback-testing-helper');
|
2013-12-18 05:10:05 +00:00
|
|
|
var path = require('path');
|
|
|
|
var ACCESS_CONTROL_APP = path.join(__dirname, 'fixtures', 'access-control');
|
2015-04-20 16:23:44 +00:00
|
|
|
var app = require(path.join(ACCESS_CONTROL_APP, 'server/server.js'));
|
2013-12-18 05:10:05 +00:00
|
|
|
var assert = require('assert');
|
2016-04-01 09:14:26 +00:00
|
|
|
var USER = { email: 'test@test.test', password: 'test' };
|
|
|
|
var CURRENT_USER = { email: 'current@test.test', password: 'test' };
|
2014-06-06 12:51:13 +00:00
|
|
|
var debug = require('debug')('loopback:test:access-control.integration');
|
2013-12-18 05:10:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('access control - integration', function() {
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.beforeEach.withApp(app);
|
|
|
|
|
2014-03-19 22:09:20 +00:00
|
|
|
/*
|
2013-12-18 05:10:05 +00:00
|
|
|
describe('accessToken', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
// it('should be a sublcass of AccessToken', function() {
|
2013-12-20 22:58:29 +00:00
|
|
|
// assert(app.models.accessToken.prototype instanceof loopback.AccessToken);
|
|
|
|
// });
|
2013-12-18 05:10:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should have a validate method', function() {
|
2013-12-18 05:10:05 +00:00
|
|
|
var token = new app.models.accessToken;
|
2014-11-21 01:46:21 +00:00
|
|
|
assert.equal(typeof token.validate, 'function');
|
2013-12-18 05:10:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('/accessToken', function() {
|
|
|
|
|
|
|
|
lt.beforeEach.givenModel('accessToken', {}, 'randomToken');
|
|
|
|
|
|
|
|
lt.it.shouldBeAllowedWhenCalledAnonymously('POST', '/api/accessTokens');
|
|
|
|
lt.it.shouldBeAllowedWhenCalledUnauthenticated('POST', '/api/accessTokens');
|
|
|
|
lt.it.shouldBeAllowedWhenCalledByUser(USER, 'POST', '/api/accessTokens');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/accessTokens');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', '/api/accessTokens');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(USER, 'GET', '/api/accessTokens');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('PUT', '/api/accessTokens');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('PUT', '/api/accessTokens');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(USER, 'PUT', '/api/accessTokens');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', urlForToken);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForToken);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(USER, 'GET', urlForToken);
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('PUT', urlForToken);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('PUT', urlForToken);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(USER, 'PUT', urlForToken);
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('DELETE', urlForToken);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('DELETE', urlForToken);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(USER, 'DELETE', urlForToken);
|
|
|
|
|
|
|
|
function urlForToken() {
|
|
|
|
return '/api/accessTokens/' + this.randomToken.id;
|
|
|
|
}
|
|
|
|
});
|
2014-03-21 19:53:04 +00:00
|
|
|
*/
|
2013-12-18 05:10:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/users', function() {
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.beforeEach.givenModel('user', USER, 'randomUser');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/users');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', '/api/users');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', '/api/users');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForUser);
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', urlForUser);
|
2013-12-18 05:10:05 +00:00
|
|
|
|
2014-06-09 07:53:05 +00:00
|
|
|
lt.it.shouldBeAllowedWhenCalledAnonymously(
|
|
|
|
'POST', '/api/users', newUserData());
|
|
|
|
|
|
|
|
lt.it.shouldBeAllowedWhenCalledByUser(
|
|
|
|
CURRENT_USER, 'POST', '/api/users', newUserData());
|
2013-12-18 05:10:05 +00:00
|
|
|
|
2013-12-18 05:22:05 +00:00
|
|
|
lt.it.shouldBeAllowedWhenCalledByUser(CURRENT_USER, 'POST', '/api/users/logout');
|
|
|
|
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.describe.whenCalledRemotely('DELETE', '/api/users', function() {
|
|
|
|
lt.it.shouldNotBeFound();
|
|
|
|
});
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('PUT', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('PUT', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'PUT', urlForUser);
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('PUT', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('PUT', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'PUT', urlForUser);
|
|
|
|
|
|
|
|
lt.describe.whenLoggedInAsUser(CURRENT_USER, function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.url = '/api/users/' + this.user.id + '?ok';
|
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('DELETE', '/api/users/:id', function() {
|
|
|
|
lt.it.shouldBeAllowed();
|
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/users/:id', function() {
|
|
|
|
lt.it.shouldBeAllowed();
|
2014-03-21 19:53:04 +00:00
|
|
|
it('should not include a password', function() {
|
2014-06-06 12:51:13 +00:00
|
|
|
debug('GET /api/users/:id response: %s\nheaders: %j\nbody string: %s',
|
|
|
|
this.res.statusCode,
|
|
|
|
this.res.headers,
|
|
|
|
this.res.text);
|
2014-03-21 19:53:04 +00:00
|
|
|
var user = this.res.body;
|
|
|
|
assert.equal(user.password, undefined);
|
|
|
|
});
|
2013-12-18 05:10:05 +00:00
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('PUT', '/api/users/:id', function() {
|
|
|
|
lt.it.shouldBeAllowed();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('DELETE', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('DELETE', urlForUser);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'DELETE', urlForUser);
|
|
|
|
|
|
|
|
function urlForUser() {
|
|
|
|
return '/api/users/' + this.randomUser.id;
|
|
|
|
}
|
2014-06-09 07:53:05 +00:00
|
|
|
|
|
|
|
var userCounter;
|
|
|
|
function newUserData() {
|
|
|
|
userCounter = userCounter ? ++userCounter : 1;
|
|
|
|
return {
|
|
|
|
email: 'new-' + userCounter + '@test.test',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'test',
|
2014-06-09 07:53:05 +00:00
|
|
|
};
|
|
|
|
}
|
2013-12-18 05:10:05 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/banks', function() {
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.beforeEach.givenModel('bank');
|
|
|
|
|
|
|
|
lt.it.shouldBeAllowedWhenCalledAnonymously('GET', '/api/banks');
|
|
|
|
lt.it.shouldBeAllowedWhenCalledUnauthenticated('GET', '/api/banks');
|
|
|
|
lt.it.shouldBeAllowedWhenCalledByUser(CURRENT_USER, 'GET', '/api/banks');
|
|
|
|
|
|
|
|
lt.it.shouldBeAllowedWhenCalledAnonymously('GET', urlForBank);
|
|
|
|
lt.it.shouldBeAllowedWhenCalledUnauthenticated('GET', urlForBank);
|
|
|
|
lt.it.shouldBeAllowedWhenCalledByUser(CURRENT_USER, 'GET', urlForBank);
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('POST', '/api/banks');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('POST', '/api/banks');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'POST', '/api/banks');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('PUT', urlForBank);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('PUT', urlForBank);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'PUT', urlForBank);
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('DELETE', urlForBank);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('DELETE', urlForBank);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'DELETE', urlForBank);
|
|
|
|
|
|
|
|
function urlForBank() {
|
|
|
|
return '/api/banks/' + this.bank.id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/accounts', function() {
|
2014-08-08 05:19:27 +00:00
|
|
|
var count = 0;
|
|
|
|
before(function() {
|
|
|
|
var roleModel = loopback.getModelByType(loopback.Role);
|
2014-11-21 02:35:36 +00:00
|
|
|
roleModel.registerResolver('$dummy', function(role, context, callback) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
if (context.remotingContext) {
|
2014-08-08 05:19:27 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
callback && callback(null, false); // Always true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.beforeEach.givenModel('account');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/accounts');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', '/api/accounts');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', '/api/accounts');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', urlForAccount);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForAccount);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', urlForAccount);
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('POST', '/api/accounts');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('POST', '/api/accounts');
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'POST', '/api/accounts');
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('PUT', urlForAccount);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('PUT', urlForAccount);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'PUT', urlForAccount);
|
|
|
|
|
|
|
|
lt.describe.whenLoggedInAsUser(CURRENT_USER, function() {
|
2014-03-19 22:09:20 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Create an account under the given user
|
|
|
|
app.models.account.create({
|
|
|
|
userId: self.user.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
balance: 100,
|
2014-03-19 22:09:20 +00:00
|
|
|
}, function(err, act) {
|
|
|
|
self.url = '/api/accounts/' + act.id;
|
|
|
|
done();
|
|
|
|
});
|
2013-12-18 05:10:05 +00:00
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.describe.whenCalledRemotely('PUT', '/api/accounts/:id', function() {
|
|
|
|
lt.it.shouldBeAllowed();
|
|
|
|
});
|
2014-03-19 22:09:20 +00:00
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/accounts/:id', function() {
|
|
|
|
lt.it.shouldBeAllowed();
|
|
|
|
});
|
2013-12-18 05:10:05 +00:00
|
|
|
lt.describe.whenCalledRemotely('DELETE', '/api/accounts/:id', function() {
|
|
|
|
lt.it.shouldBeDenied();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
lt.it.shouldBeDeniedWhenCalledAnonymously('DELETE', urlForAccount);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('DELETE', urlForAccount);
|
|
|
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'DELETE', urlForAccount);
|
|
|
|
|
|
|
|
function urlForAccount() {
|
|
|
|
return '/api/accounts/' + this.account.id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|