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
18 changes: 12 additions & 6 deletions src/security/Security.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ Security.prototype.searchProfiles = function (filters, options, cb) {
* Replace the existing profile otherwise
*
* @param {string} id - profile identifier
* @param {object} content - attribute `roles` in `content` must only contains an array of role id
* @param {array} policies - list of policies to attach to the new profile
* @param {object|responseCallback} [options] - (optional) arguments
* @param {responseCallback} [cb] - (optional) Handles the query response
*/
Security.prototype.createProfile = function (id, content, options, cb) {
Security.prototype.createProfile = function (id, policies, options, cb) {
var
self = this,
data = {},
Expand All @@ -326,7 +326,10 @@ Security.prototype.createProfile = function (id, content, options, cb) {
}

data._id = id;
data.body = content;

if (policies) {
data.body = { policies: policies };
}

if (options) {
action = options.replaceIfExist ? 'createOrReplaceProfile' : 'createProfile';
Expand All @@ -342,12 +345,12 @@ Security.prototype.createProfile = function (id, content, options, cb) {
* Update a profile in Kuzzle.
*
* @param {string} id - profile identifier
* @param {object} content - a plain javascript object representing the profile's modification
* @param {array} policies - the list of policies to apply to this profile
* @param {object|responseCallback} [options] - (optional) arguments
* @param {responseCallback} [cb] - (optional) Handles the query response
* @returns {Security} this object
*/
Security.prototype.updateProfile = function (id, content, options, cb) {
Security.prototype.updateProfile = function (id, policies, options, cb) {
var
self = this,
data = {},
Expand All @@ -363,7 +366,10 @@ Security.prototype.updateProfile = function (id, content, options, cb) {
}

data._id = id;
data.body = content;

if (policies) {
data.body = {policies: policies};
}

self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
var updatedContent = {};
Expand Down
37 changes: 17 additions & 20 deletions test/security/kuzzleSecurity/profilesMethods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ describe('Security profiles methods', function () {


describe('#createProfile', function () {
var content = {policies: [{roleId: 'myRole'}]};
var policies = [{roleId: 'myRole'}];
beforeEach(function () {
result = { result: {_id: 'foobar', _source: content} };
result = { result: {_id: 'foobar', _source: {policies: policies}} };
expectedQuery = {
action: 'createProfile',
controller: 'security'
Expand All @@ -163,38 +163,38 @@ describe('Security profiles methods', function () {
it('should send the right query to Kuzzle', function (done) {
this.timeout(50);

should(kuzzle.security.createProfile('foobar', content, function (err, res) {
should(kuzzle.security.createProfile('foobar', policies, function (err, res) {
should(err).be.null();
should(res).be.instanceof(Profile);
done();
}));

should(kuzzle.query).be.calledOnce();
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: content}, null, sinon.match.func);
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: {policies: policies}}, null, sinon.match.func);

kuzzle.query.yield(null, result);
});

it('should send the right query to Kuzzle even without callback', function () {
kuzzle.security.createProfile('foobar', content);
kuzzle.security.createProfile('foobar', policies);
should(kuzzle.query).be.calledOnce();
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: content}, undefined, undefined);
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: {policies: policies}}, undefined, undefined);
});

it('should construct a createOrReplaceProfile action if option replaceIfExist is set to true', function () {
expectedQuery.action = 'createOrReplaceProfile';

should(kuzzle.security.createProfile('foobar', content, {replaceIfExist: true}));
should(kuzzle.security.createProfile('foobar', policies, {replaceIfExist: true}));
should(kuzzle.query).be.calledOnce();
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: content});
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: {policies: policies}});
});

it('should construct a createProfile action if option replaceIfExist is set to false', function () {
expectedQuery.action = 'createProfile';

should(kuzzle.security.createProfile('foobar', content, {replaceIfExist: false}));
should(kuzzle.security.createProfile('foobar', policies, {replaceIfExist: false}));
should(kuzzle.query).be.calledOnce();
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: content});
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: {policies: policies}});
});

it('should throw an error if no id provided', function () {
Expand All @@ -216,18 +216,15 @@ describe('Security profiles methods', function () {
});

describe('#updateProfile', function () {
var content = {
policies: [{roleId: 'foo'}],
foo: 'bar'
};
var policies = [{roleId: 'foo'}];

beforeEach(function () {
result = {
result: {
_id: 'foobar',
_index: '%kuzzle',
_type: 'profiles',
_source: content
_source: {policies: policies}
}
};
expectedQuery = {
Expand All @@ -239,21 +236,21 @@ describe('Security profiles methods', function () {
it('should send the right query to Kuzzle', function (done) {
this.timeout(50);

kuzzle.security.updateProfile('foobar', content, function (err, res) {
kuzzle.security.updateProfile('foobar', policies, function (err, res) {
should(err).be.null();
should(res).be.instanceOf(Profile);
done();
});

should(kuzzle.query).be.calledOnce();
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: content}, null, sinon.match.func);
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: {policies: policies}}, null, sinon.match.func);
kuzzle.query.yield(null, result);
});

it('should send the right query to Kuzzle even without callback', function () {
kuzzle.security.updateProfile('foobar', content);
kuzzle.security.updateProfile('foobar', policies);
should(kuzzle.query).be.calledOnce();
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: content}, undefined, undefined);
should(kuzzle.query).calledWith(expectedQuery, {_id: 'foobar', body: {policies: policies}}, undefined, undefined);
});

it('should throw an error if no id provided', function () {
Expand All @@ -264,7 +261,7 @@ describe('Security profiles methods', function () {
it('should call the callback with an error if one occurs', function (done) {
this.timeout(50);

kuzzle.security.updateProfile(result.result._id, {'foo': 'bar'}, function (err, res) {
kuzzle.security.updateProfile(result.result._id, policies, function (err, res) {
should(err).be.exactly('foobar');
should(res).be.undefined();
done();
Expand Down