Skip to content

Commit 43c71f5

Browse files
committed
21 Added a new replaceUser method to Security
1 parent 71bb318 commit 43c71f5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/security/Security.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,33 @@ Security.prototype.createUser = function (id, content, options, cb) {
518518
});
519519
};
520520

521+
/**
522+
* Replace an user in Kuzzle.
523+
*
524+
* @param {string} id - user identifier
525+
* @param {object} content - attribute `profileIds` in `content` must only contain an array of profile ids
526+
* @param {object|responseCallback} [options] - (optional) arguments
527+
* @param {responseCallback} [cb] - (optional) Handles the query response
528+
*/
529+
Security.prototype.replaceUser = function (id, content, options, cb) {
530+
var
531+
self = this,
532+
data = {_id: id, body: content};
533+
534+
if (!id || typeof id !== 'string') {
535+
throw new Error('Security.replaceUser: cannot replace a user without a user ID');
536+
}
537+
538+
if (!cb && typeof options === 'function') {
539+
cb = options;
540+
options = null;
541+
}
542+
543+
self.kuzzle.query(this.buildQueryArgs('replaceUser'), data, options, cb && function (err, res) {
544+
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
545+
});
546+
};
547+
521548
/**
522549
* Create a new restricted user in Kuzzle.
523550
*

test/security/kuzzleSecurity/userMethods.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,43 @@ describe('Security user methods', function () {
316316
});
317317
});
318318

319+
describe('#replaceUser', function () {
320+
beforeEach(function () {
321+
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
322+
kuzzle.query = queryStub;
323+
error = null;
324+
result = { result: {_id: 'foobar', _index: '%kuzzle', _type: 'users', _source: {profileIds: ['foobar']} } };
325+
expectedQuery = {
326+
action: 'replaceUser',
327+
controller: 'security'
328+
};
329+
});
330+
331+
it('should send the right query to Kuzzle', function(done) {
332+
expectedQuery._id = 'foobar';
333+
expectedQuery.body = {'profileIds': ['foobar']};
334+
335+
should(kuzzle.security.replaceUser(expectedQuery._id, expectedQuery.body, function (err, res) {
336+
should(err).be.null();
337+
should(res).be.instanceOf(User);
338+
should(res).containDeep(new User(kuzzle.security, result.result._id, result.result._source));
339+
done();
340+
}));
341+
});
342+
343+
it('should send the right query to Kuzzle even without callback', function (done) {
344+
expectedQuery.body = {'foo': 'bar'};
345+
expectedQuery._id = result.result._id;
346+
347+
kuzzle.security.replaceUser(result.result._id, {'foo': 'bar'});
348+
done();
349+
});
350+
351+
it('should throw an error if no id is provided', function () {
352+
should(function () { kuzzle.security.replaceUser(null, {'foo': 'bar'}); }).throw(Error);
353+
});
354+
});
355+
319356
describe('#updateUser', function () {
320357
beforeEach(function () {
321358
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});

0 commit comments

Comments
 (0)