Skip to content

Commit 9066a3b

Browse files
scottinetGilles Ballini
authored andcommitted
Rename getProfiles to getProfileIds and add a proper getProfiles method (#225)
* update README * implement the new getProfiles method * WIP: unit tests * add unit tests
1 parent aea8fa5 commit 9066a3b

File tree

4 files changed

+114
-12
lines changed

4 files changed

+114
-12
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
Official Kuzzle Javascript SDK
55
======
66

7-
This SDK version is compatible with Kuzzle 1.0.0-RC9.5 and higher
8-
97
## About Kuzzle
108

11-
For UI and linked objects developers, Kuzzle is an open-source solution that handles all the data management (CRUD, real-time storage, search, high-level features, etc).
9+
A backend software, self-hostable and ready to use to power modern apps.
1210

1311
You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuzzle)
1412

@@ -24,11 +22,11 @@ You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuz
2422

2523
## Basic usage
2624

27-
Follow [Kuzzle Guide](http://docs.kuzzle.io/guide/#sdk-play-time)
25+
Follow [Kuzzle Guide](http://docs.kuzzle.io/guide/getting-started/#sdk-play-time)
2826

2927
## SDK Documentation
3028

31-
The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference/?javascript#kuzzle)
29+
The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference/)
3230

3331
## Report an issue
3432

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <support@kuzzle.io>",
66
"repository": {

src/security/User.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function User(Security, id, content) {
3232
return Security.kuzzle.bluebird.promisifyAll(this, {
3333
suffix: 'Promise',
3434
filter: function (name, func, target, passes) {
35-
var whitelist = ['create', 'replace', 'saveRestricted', 'update'];
35+
var whitelist = ['create', 'replace', 'saveRestricted', 'update', 'getProfiles'];
3636

3737
return passes && whitelist.indexOf(name) !== -1;
3838
}
@@ -204,10 +204,53 @@ User.prototype.creationSerialize = function () {
204204
/**
205205
* Return the associated profiles IDs
206206
*
207-
* @return {array} the associated profiles IDs
207+
* @return {array.<string>} the associated profiles IDs
208208
*/
209-
User.prototype.getProfiles = function () {
210-
return this.content.profileIds;
209+
User.prototype.getProfileIds = function () {
210+
return this.content.profileIds || [];
211+
};
212+
213+
/**
214+
* Return the associated Profile objects
215+
*
216+
* @param {object|responseCallback} [options] - Optional parameters
217+
* @param {responseCallback} cb - Handles the query response
218+
*/
219+
User.prototype.getProfiles = function (options, cb) {
220+
var
221+
self = this,
222+
fetchedProfiles = [],
223+
errored = false;
224+
225+
if (options && !cb && typeof options === 'function') {
226+
cb = options;
227+
options = null;
228+
}
229+
230+
self.Security.kuzzle.callbackRequired('User.getProfiles', cb);
231+
232+
if (!self.content.profileIds) {
233+
return cb(null, fetchedProfiles);
234+
}
235+
236+
self.content.profileIds.forEach(function (profileId) {
237+
self.Security.fetchProfile(profileId, options, function (error, profile) {
238+
if (error) {
239+
if (errored) {
240+
return;
241+
}
242+
243+
errored = true; // prevents multiple callback resolutions
244+
return cb(error);
245+
}
246+
247+
fetchedProfiles.push(profile);
248+
249+
if (fetchedProfiles.length === self.content.profileIds.length) {
250+
cb(null, fetchedProfiles);
251+
}
252+
});
253+
});
211254
};
212255

213256
module.exports = User;

test/security/kuzzleUser/methods.test.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var
33
sinon = require('sinon'),
44
Kuzzle = require('../../../src/Kuzzle'),
55
User = require('../../../src/security/User'),
6+
Profile = require('../../../src/security/Profile'),
67
sandbox = sinon.sandbox.create();
78

89
describe('User methods', function () {
@@ -312,11 +313,71 @@ describe('User methods', function () {
312313
});
313314
});
314315

315-
describe('#getProfiles', function () {
316+
describe('#getProfileIds', function () {
316317
it('should return the associated profiles', function () {
317318
var profileIds = ['profile'];
318319
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: profileIds});
319-
should(kuzzleUser.getProfiles()).be.eql(profileIds);
320+
should(kuzzleUser.getProfileIds()).be.eql(profileIds);
321+
});
322+
323+
it('should return an empty array if no profile ID is attached', function () {
324+
kuzzleUser = new User(kuzzle.security, 'foo', {});
325+
should(kuzzleUser.getProfileIds()).be.an.Array().and.be.empty();
326+
});
327+
});
328+
329+
describe('#getProfiles', function () {
330+
it('should return an empty array if no profile is attached', function (done) {
331+
kuzzleUser = new User(kuzzle.security, 'foo', {});
332+
333+
kuzzleUser.getProfiles(function (error, profiles) {
334+
should(error).be.null();
335+
should(profiles).be.an.Array().and.be.empty();
336+
done();
337+
});
338+
});
339+
340+
it('should fetch the attached profiles using the API to build Profile objects', function (done) {
341+
kuzzleUser = new User(kuzzle.security, 'foo', {profileIds: ['foo', 'bar', 'baz']});
342+
kuzzle.query.yields(null, {
343+
result: {
344+
_id: 'foobar',
345+
_source: {}
346+
}
347+
});
348+
349+
kuzzleUser.getProfiles(function (error, profiles) {
350+
should(error).be.null();
351+
should(profiles).be.an.Array().and.have.lengthOf(3);
352+
353+
profiles.forEach(function (profile) {
354+
should(profile).be.instanceof(Profile);
355+
});
356+
357+
done();
358+
});
359+
});
360+
361+
it('should not invoke the callback more than once even if multiple errors occur', function (done) {
362+
var callCount = 0;
363+
364+
kuzzleUser = new User(kuzzle.security, 'foo', {profileIds: ['foo', 'bar', 'baz']});
365+
kuzzle.query.yields(new Error('errored'));
366+
367+
kuzzleUser.getProfiles(function (error, profiles) {
368+
callCount++;
369+
should(profiles).be.undefined();
370+
should(error).be.an.Error().and.have.value('message', 'errored');
371+
should(callCount).be.eql(1);
372+
done();
373+
});
374+
});
375+
376+
it('should throw if no callback is provided', function () {
377+
kuzzleUser = new User(kuzzle.security, 'foo', {});
378+
379+
should(function () { kuzzleUser.getProfiles(); }).throw('User.getProfiles: a callback argument is required for read queries');
380+
should(function () { kuzzleUser.getProfiles({}); }).throw('User.getProfiles: a callback argument is required for read queries');
320381
});
321382
});
322383
});

0 commit comments

Comments
 (0)