From 18152d557b4f6ddca84408783e09e330bce2574a Mon Sep 17 00:00:00 2001 From: Joshua Ali Date: Tue, 30 May 2017 16:33:23 +1000 Subject: [PATCH] Retrieve a user's Guardian enrollments --- src/management/UsersManager.js | 28 +++++++++++ src/management/index.js | 20 ++++++++ test/management/users.tests.js | 85 +++++++++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js index 15c6ed305..45b639585 100644 --- a/src/management/UsersManager.js +++ b/src/management/UsersManager.js @@ -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); }; @@ -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; diff --git a/src/management/index.js b/src/management/index.js index 2721bc02f..739846a5b 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -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. * diff --git a/test/management/users.tests.js b/test/management/users.tests.js index 695fd097c..72933af88 100644 --- a/test/management/users.tests.js +++ b/test/management/users.tests.js @@ -30,7 +30,8 @@ describe('UsersManager', function () { 'link', 'deleteMultifactorProvider', 'updateUserMetadata', - 'updateAppMetadata' + 'updateAppMetadata', + 'getGuardianEnrollments' ]; methods.forEach(function (method) { @@ -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(); + }); + }); + }); });