|  | 
| 3 | 3 |   sinon = require('sinon'), | 
| 4 | 4 |   Kuzzle = require('../../../src/Kuzzle'), | 
| 5 | 5 |   User = require('../../../src/security/User'), | 
|  | 6 | +  Profile = require('../../../src/security/Profile'), | 
| 6 | 7 |   sandbox = sinon.sandbox.create(); | 
| 7 | 8 | 
 | 
| 8 | 9 | describe('User methods', function () { | 
| @@ -312,11 +313,71 @@ describe('User methods', function () { | 
| 312 | 313 |     }); | 
| 313 | 314 |   }); | 
| 314 | 315 | 
 | 
| 315 |  | -  describe('#getProfiles', function () { | 
|  | 316 | +  describe('#getProfileIds', function () { | 
| 316 | 317 |     it('should return the associated profiles', function () { | 
| 317 | 318 |       var profileIds = ['profile']; | 
| 318 | 319 |       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'); | 
| 320 | 381 |     }); | 
| 321 | 382 |   }); | 
| 322 | 383 | }); | 
0 commit comments