Skip to content

Commit

Permalink
Retrieve a user's Guardian enrollments
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Ali committed May 30, 2017
1 parent ba4b4f9 commit 18152d5
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/management/UsersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ var UsersManager = function (options){
* @type {external:RestClient}
*/
this.identities = new RestClient(options.baseUrl + '/users/:id/identities/:provider/:user_id', clientOptions);

/**
* Provides an abstraction layer for retrieving Guardian enrollments.
*
* @type {external:RestClient}
*/
this.enrollments = new RestClient(options.baseUrl + '/users/:id/enrollments', clientOptions);
};


Expand Down Expand Up @@ -451,4 +458,25 @@ UsersManager.prototype.unlink = function (params, cb) {
};


/**
* Get a list of Guardian enrollments.
*
* @method getGuardianEnrollments
* @memberOf module:management.UsersManager.prototype
*
* @example
* management.users.getGuardianEnrollments({ id: USER_ID }, function (err, enrollments) {
* console.log(enrollments);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
UsersManager.prototype.getGuardianEnrollments = function () {
return this.enrollments.get.apply(this.enrollments, arguments);
};

module.exports = UsersManager;
20 changes: 20 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,26 @@ utils.wrapPropertyMethod(ManagementClient, 'unlinkUsers', 'users.unlink');
utils.wrapPropertyMethod(ManagementClient, 'linkUsers', 'users.link');


/**
* Get a list of a user's Guardian enrollments.
*
* @method getGuardianEnrollments
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.getGuardianEnrollments({ id: USER_ID }, function (err, enrollments) {
* console.log(enrollments);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'getGuardianEnrollments', 'users.getGuardianEnrollments');


/**
* Get all blacklisted tokens.
*
Expand Down
85 changes: 84 additions & 1 deletion test/management/users.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('UsersManager', function () {
'link',
'deleteMultifactorProvider',
'updateUserMetadata',
'updateAppMetadata'
'updateAppMetadata',
'getGuardianEnrollments'
];

methods.forEach(function (method) {
Expand Down Expand Up @@ -920,4 +921,86 @@ describe('UsersManager', function () {
});
});


describe('#getGuardianEnrollments', function () {
var data = {
id: 5,
};


beforeEach(function () {
this.request = nock(API_URL)
.get('/users/' + data.id + '/enrollments')
.reply(200);
});


it('should accept a callback', function (done) {
this
.users
.getGuardianEnrollments(data, done.bind(null, null));
});


it('should return a promise when no callback is given', function (done) {
this
.users
.getGuardianEnrollments(data)
.then(done.bind(null, null));
});


it('should perform a GET request to /api/v2/users/5/enrollments', function (done) {
var request = this.request;

this
.users
.getGuardianEnrollments(data)
.then(function () {
expect(request.isDone())
.to.be.true;

done();
});
});


it('should pass any errors to the promise catch handler', function (done) {
nock.cleanAll();

var request = nock(API_URL)
.get('/users/' + data.id + '/enrollments')
.reply(500);

this
.users
.getGuardianEnrollments(data)
.catch(function (err) {
expect(err)
.to.exist;

done();
});
});


it('should include the token in the authorization header', function (done) {
nock.cleanAll();

var request = nock(API_URL)
.get('/users/' + data.id + '/enrollments')
.matchHeader('authorization', 'Bearer ' + this.token)
.reply(200);

this
.users
.getGuardianEnrollments(data)
.then(function () {
expect(request.isDone())
.to.be.true;

done();
});
});
});
});

0 comments on commit 18152d5

Please sign in to comment.