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
34 changes: 34 additions & 0 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,40 @@ Kuzzle.prototype.whoAmI = function (callback) {
return self;
};


/**
* Update current user in Kuzzle.
*
* @param {object} content - a plain javascript object representing the user's modification
* @param {object} [options] - (optional) arguments
* @param {responseCallback} [cb] - (optional) Handles the query response
*/
Kuzzle.prototype.updateSelf = function (content, options, cb) {
var
self = this,
data = {},
queryArgs = {controller: 'auth', action: 'updateSelf'};

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}

data.body = content;

if (cb) {
self.query(queryArgs, data, options, function (err, res) {
if (err) {
return cb(err);
}

cb(null, res.result);
});
} else {
self.query(queryArgs, data, options);
}
};

/**
* Clean up the queue, ensuring the queryTTL and queryMaxSize properties are respected
*/
Expand Down
4 changes: 2 additions & 2 deletions src/security/kuzzleSecurity.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,10 @@ KuzzleSecurity.prototype.updateUser = function (id, content, options, cb) {
return cb(err);
}

cb(null, res.result._id);
cb(null, new KuzzleUser(self, res.result._id, res.result._source));
});
} else {
self.kuzzle.query(this.buildQueryArgs(action), data);
self.kuzzle.query(this.buildQueryArgs(action), data, options);
}
};

Expand Down
50 changes: 49 additions & 1 deletion test/kuzzle/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ describe('Kuzzle methods', function () {
should(Object.keys(query).length).be.exactly(0);
}

cb(error, result);
if (cb) {
if (error) {
return cb(error);
}

cb(error, result);
}
},
emitted,
kuzzle;
Expand Down Expand Up @@ -637,6 +643,48 @@ describe('Kuzzle methods', function () {
});
});

describe('#updateSelf', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
kuzzle.query = queryStub;
error = null;
result = { result: {_id: 'foobar', _index: '%kuzzle', _type: 'users'} };
expectedQuery = {
action: 'updateSelf',
controller: 'auth'
};
});

it('should send the right query to Kuzzle', function (done) {
expectedQuery.body = {'foo': 'bar'};

should(kuzzle.updateSelf({'foo': 'bar'}, function (err, res) {
should(err).be.null();
should(res).be.exactly(result.result);
done();
}));
});

it('should send the right query to Kuzzle even without callback', function (done) {
expectedQuery.body = {'foo': 'bar'};

kuzzle.updateSelf({'foo': 'bar'});
done();
});

it('should call the callback with an error if one occurs', function (done) {
expectedQuery.body = {'foo': 'bar'};
error = 'foobar';
this.timeout(50);

kuzzle.updateSelf({'foo': 'bar'}, function (err, res) {
should(err).be.exactly('foobar');
should(res).be.undefined();
done();
});
});
});

describe('#security', function () {
it('should be an instance of KuzzleSecurity', function () {
var kuzzle;
Expand Down
6 changes: 3 additions & 3 deletions test/security/kuzzleSecurity/userMethods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ describe('KuzzleSecurity user methods', function () {
});
});


describe('#createUser', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
Expand Down Expand Up @@ -331,7 +330,7 @@ describe('KuzzleSecurity user methods', function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
kuzzle.query = queryStub;
error = null;
result = { result: {_id: 'foobar', _index: '%kuzzle', _type: 'users'} };
result = { result: {_id: 'foobar', _index: '%kuzzle', _type: 'users', _source: {profile: 'foobar'} } };
expectedQuery = {
action: 'updateUser',
controller: 'security'
Expand All @@ -344,7 +343,8 @@ describe('KuzzleSecurity user methods', function () {

should(kuzzle.security.updateUser(result.result._id, {'foo': 'bar'}, function (err, res) {
should(err).be.null();
should(res).be.exactly(result.result._id);
should(res).be.an.instanceOf(KuzzleUser);
should(res).containDeep(new KuzzleUser(kuzzle.security, result.result._id, result.result._source));
done();
}));
});
Expand Down