Repository: auth0/node-auth0
Pull request #181 (2017-05-30 06:34:22)

Retrieve a user's Guardian enrollments

coveralls: 
[![Coverage Status](https://coveralls.io/builds/11739685/badge)](https://coveralls.io/builds/11739685)

Coverage increased (+0.02%) to 95.281% when pulling **18152d557b4f6ddca84408783e09e330bce2574a on joshuaali:get-guardian-enrollments** into **ba4b4f9427435156fecaf629818eb11be7885533 on auth0:master**.

coveralls: 
[![Coverage Status](https://:/builds/11997644/badge)](https://:/builds/11997644)

Coverage increased (+0.02%) to 95.22% when pulling **8e30e3fe4dbd4160800db65659f018c6a5e84c09 on joshuaali:get-guardian-enrollments** into **ca186aa44cf853499e9421cfc73011469892170e on auth0:master**.


Patch:
diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js
index 9447ed427..ea8367bec 100644
--- a/src/management/UsersManager.js
+++ b/src/management/UsersManager.js
@@ -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);
 };
 
 
@@ -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;
diff --git a/src/management/index.js b/src/management/index.js
index bf6f60ac8..a5b382a1f 100644
--- a/src/management/index.js
+++ b/src/management/index.js
@@ -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.
  *


Test patch:
diff --git a/test/management/users.tests.js b/test/management/users.tests.js
index f781ff859..7f039ddc3 100644
--- a/test/management/users.tests.js
+++ b/test/management/users.tests.js
@@ -31,7 +31,8 @@ describe('UsersManager', function () {
       'logs',
       'deleteMultifactorProvider',
       'updateUserMetadata',
-      'updateAppMetadata'
+      'updateAppMetadata',
+      'getGuardianEnrollments'
     ];
 
     methods.forEach(function (method) {
@@ -929,7 +930,6 @@ describe('UsersManager', function () {
       '/users/' + data.id + '/logs'
     );
 
-
     beforeEach(function () {
       this.request = nock(API_URL)
         .get(url)
@@ -943,7 +943,6 @@ describe('UsersManager', function () {
         .logs(data, done.bind(null, null));
     });
 
-
     it('should return a promise when no callback is given', function (done) {
       this
         .users
@@ -951,7 +950,6 @@ describe('UsersManager', function () {
         .then(done.bind(null, null));
     });
 
-
     it('should perform a GET request to ' + url, function (done) {
       var request = this.request;
 
@@ -966,7 +964,6 @@ describe('UsersManager', function () {
         });
     });
 
-
     it('should pass any errors to the promise catch handler', function (done) {
       nock.cleanAll();
 
@@ -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();
+        });
+    });
+  });
 });