Skip to content

Commit

Permalink
Retrieve a user's Guardian enrollments (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaali authored and hzalaz committed Jun 16, 2017
1 parent ca186aa commit cdcf916
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/management/UsersManager.js
Expand Up @@ -57,8 +57,17 @@ var UsersManager = function (options){

/**
* Provides a simple abstraction layer for user logs
*
* @type {external:RestClient}
*/
this.userLogs = new RestClient(options.baseUrl + '/users/:id/logs', 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 @@ -492,4 +501,25 @@ UsersManager.prototype.logs = function (params, cb) {
return this.userLogs.get(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
Expand Up @@ -1059,6 +1059,26 @@ utils.wrapPropertyMethod(ManagementClient, 'linkUsers', 'users.link');
*/
utils.wrapPropertyMethod(ManagementClient, 'getUserLogs', 'users.logs');

/**
* 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
87 changes: 81 additions & 6 deletions test/management/users.tests.js
Expand Up @@ -31,7 +31,8 @@ describe('UsersManager', function () {
'logs',
'deleteMultifactorProvider',
'updateUserMetadata',
'updateAppMetadata'
'updateAppMetadata',
'getGuardianEnrollments'
];

methods.forEach(function (method) {
Expand Down Expand Up @@ -929,7 +930,6 @@ describe('UsersManager', function () {
'/users/' + data.id + '/logs'
);


beforeEach(function () {
this.request = nock(API_URL)
.get(url)
Expand All @@ -943,15 +943,13 @@ describe('UsersManager', function () {
.logs(data, done.bind(null, null));
});


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


it('should perform a GET request to ' + url, function (done) {
var request = this.request;

Expand All @@ -966,7 +964,6 @@ describe('UsersManager', function () {
});
});


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

Expand Down Expand Up @@ -1055,7 +1052,85 @@ describe('UsersManager', function () {
done();
});
});

});

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 cdcf916

Please sign in to comment.