Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename getProfiles to getProfileIds and add a proper getProfiles method #225

Merged
merged 4 commits into from
Jun 1, 2017
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
Official Kuzzle Javascript SDK
======

This SDK version is compatible with Kuzzle 1.0.0-RC9.5 and higher

## About Kuzzle

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).
A backend software, self-hostable and ready to use to power modern apps.

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

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

## Basic usage

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

## SDK Documentation

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

## Report an issue

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "5.0.0",
"version": "5.0.1",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
51 changes: 47 additions & 4 deletions src/security/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function User(Security, id, content) {
return Security.kuzzle.bluebird.promisifyAll(this, {
suffix: 'Promise',
filter: function (name, func, target, passes) {
var whitelist = ['create', 'replace', 'saveRestricted', 'update'];
var whitelist = ['create', 'replace', 'saveRestricted', 'update', 'getProfiles'];

return passes && whitelist.indexOf(name) !== -1;
}
Expand Down Expand Up @@ -204,10 +204,53 @@ User.prototype.creationSerialize = function () {
/**
* Return the associated profiles IDs
*
* @return {array} the associated profiles IDs
* @return {array.<string>} the associated profiles IDs
*/
User.prototype.getProfiles = function () {
return this.content.profileIds;
User.prototype.getProfileIds = function () {
return this.content.profileIds || [];
};

/**
* Return the associated Profile objects
*
* @param {object|responseCallback} [options] - Optional parameters
* @param {responseCallback} cb - Handles the query response
*/
User.prototype.getProfiles = function (options, cb) {
var
self = this,
fetchedProfiles = [],
errored = false;

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

self.Security.kuzzle.callbackRequired('User.getProfiles', cb);

if (!self.content.profileIds) {
return cb(null, fetchedProfiles);
}

self.content.profileIds.forEach(function (profileId) {
self.Security.fetchProfile(profileId, options, function (error, profile) {
if (error) {
if (errored) {
return;
}

errored = true; // prevents multiple callback resolutions
return cb(error);
}

fetchedProfiles.push(profile);

if (fetchedProfiles.length === self.content.profileIds.length) {
cb(null, fetchedProfiles);
}
});
});
};

module.exports = User;
65 changes: 63 additions & 2 deletions test/security/kuzzleUser/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var
sinon = require('sinon'),
Kuzzle = require('../../../src/Kuzzle'),
User = require('../../../src/security/User'),
Profile = require('../../../src/security/Profile'),
sandbox = sinon.sandbox.create();

describe('User methods', function () {
Expand Down Expand Up @@ -312,11 +313,71 @@ describe('User methods', function () {
});
});

describe('#getProfiles', function () {
describe('#getProfileIds', function () {
it('should return the associated profiles', function () {
var profileIds = ['profile'];
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: profileIds});
should(kuzzleUser.getProfiles()).be.eql(profileIds);
should(kuzzleUser.getProfileIds()).be.eql(profileIds);
});

it('should return an empty array if no profile ID is attached', function () {
kuzzleUser = new User(kuzzle.security, 'foo', {});
should(kuzzleUser.getProfileIds()).be.an.Array().and.be.empty();
});
});

describe('#getProfiles', function () {
it('should return an empty array if no profile is attached', function (done) {
kuzzleUser = new User(kuzzle.security, 'foo', {});

kuzzleUser.getProfiles(function (error, profiles) {
should(error).be.null();
should(profiles).be.an.Array().and.be.empty();
done();
});
});

it('should fetch the attached profiles using the API to build Profile objects', function (done) {
kuzzleUser = new User(kuzzle.security, 'foo', {profileIds: ['foo', 'bar', 'baz']});
kuzzle.query.yields(null, {
result: {
_id: 'foobar',
_source: {}
}
});

kuzzleUser.getProfiles(function (error, profiles) {
should(error).be.null();
should(profiles).be.an.Array().and.have.lengthOf(3);

profiles.forEach(function (profile) {
should(profile).be.instanceof(Profile);
});

done();
});
});

it('should not invoke the callback more than once even if multiple errors occur', function (done) {
var callCount = 0;

kuzzleUser = new User(kuzzle.security, 'foo', {profileIds: ['foo', 'bar', 'baz']});
kuzzle.query.yields(new Error('errored'));

kuzzleUser.getProfiles(function (error, profiles) {
callCount++;
should(profiles).be.undefined();
should(error).be.an.Error().and.have.value('message', 'errored');
should(callCount).be.eql(1);
done();
});
});

it('should throw if no callback is provided', function () {
kuzzleUser = new User(kuzzle.security, 'foo', {});

should(function () { kuzzleUser.getProfiles(); }).throw('User.getProfiles: a callback argument is required for read queries');
should(function () { kuzzleUser.getProfiles({}); }).throw('User.getProfiles: a callback argument is required for read queries');
});
});
});