Repository: auth0/node-auth0
Pull request #176 (2017-05-23 20:23:50)

Add get user logs
https://auth0.com/docs/api/management/v2#!/Users/get_logs_by_user
https://github.com/auth0/node-auth0/issues/175


Patch:
diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js
index 15c6ed305..9447ed427 100644
--- a/src/management/UsersManager.js
+++ b/src/management/UsersManager.js
@@ -54,6 +54,11 @@ var UsersManager = function (options){
    * @type {external:RestClient}
    */
   this.identities = new RestClient(options.baseUrl + '/users/:id/identities/:provider/:user_id', clientOptions);
+
+  /**
+   * Provides a simple abstraction layer for user logs
+   */
+  this.userLogs = new RestClient(options.baseUrl + '/users/:id/logs', clientOptions);
 };
 
 
@@ -450,5 +455,41 @@ UsersManager.prototype.unlink = function (params, cb) {
   return this.identities.delete(params);
 };
 
+/**
+ * Get user's log events.
+ *
+ * @method    logs
+ * @memberOf  module:management.UsersManager.prototype
+ *
+ * @example
+ * var params = { id: USER_ID, page: 0, per_page: 50, sort: 'date:-1', include_totals: true };
+ *
+ * management.users.logs(params, function (err, logs) {
+ *   if (err) {
+ *     // Handle error.
+ *   }
+ *
+ *   console.log(logs);
+ * });
+ *
+ * @param   {Object}    params                Get logs data.
+ * @param   {String}    params.id             User id.
+ * @param   {Number}    params.per_page       Number of logs per page.
+ * @param   {Number}    params.page           Page number.
+ * @param   {String}    params.sort           The field to use for sorting. Use field:order where order is 1 for ascending and -1 for descending. For example date:-1.
+ * @param   {Boolean}   params.include_totals true if a query summary must be included in the result, false otherwise. Default false;
+ * @param   {Function}  [cb]                  Callback function.
+ *
+ * @return {Promise|undefined}
+ */
+UsersManager.prototype.logs = function (params, cb) {
+  params = params || {};
+
+  if (!params.id || typeof params.id !== 'string') {
+    throw new ArgumentError('id field is required');
+  }
+
+  return this.userLogs.get(params, cb);
+};
 
 module.exports = UsersManager;
diff --git a/src/management/index.js b/src/management/index.js
index 2721bc02f..bf6f60ac8 100644
--- a/src/management/index.js
+++ b/src/management/index.js
@@ -1030,6 +1030,34 @@ utils.wrapPropertyMethod(ManagementClient, 'unlinkUsers', 'users.unlink');
  */
 utils.wrapPropertyMethod(ManagementClient, 'linkUsers', 'users.link');
 
+/**
+ * Get user's log events.
+ *
+ * @method    getUserLogs
+ * @memberOf  module:management.ManagementClient.prototype
+ *
+ * @example
+ * var params = { id: USER_ID, page: 0, per_page: 50, sort: 'date:-1', include_totals: true };
+ *
+ * management.getUserLogs(params, function (err, logs) {
+ *   if (err) {
+ *     // Handle error.
+ *   }
+ *
+ *   console.log(logs);
+ * });
+ *
+ * @param   {Object}    params                Get logs data.
+ * @param   {String}    params.id             User id.
+ * @param   {Number}    params.per_page       Number of logs per page.
+ * @param   {Number}    params.page           Page number.
+ * @param   {String}    params.sort           The field to use for sorting. Use field:order where order is 1 for ascending and -1 for descending. For example date:-1.
+ * @param   {Boolean}   params.include_totals true if a query summary must be included in the result, false otherwise. Default false;
+ * @param   {Function}  [cb]                  Callback function.
+ *
+ * @return {Promise|undefined}
+ */
+utils.wrapPropertyMethod(ManagementClient, 'getUserLogs', 'users.logs');
 
 /**
  * Get all blacklisted tokens.


Test patch:
diff --git a/test/management/users.tests.js b/test/management/users.tests.js
index 695fd097c..f781ff859 100644
--- a/test/management/users.tests.js
+++ b/test/management/users.tests.js
@@ -28,6 +28,7 @@ describe('UsersManager', function () {
       'deleteAll',
       'unlink',
       'link',
+      'logs',
       'deleteMultifactorProvider',
       'updateUserMetadata',
       'updateAppMetadata'
@@ -920,4 +921,141 @@ describe('UsersManager', function () {
     });
   });
 
+  describe('#logs', function () {
+    var data = {
+      id: 'user_id'
+    };
+    var url = (
+      '/users/' + data.id + '/logs'
+    );
+
+
+    beforeEach(function () {
+      this.request = nock(API_URL)
+        .get(url)
+        .reply(200);
+    });
+
+
+    it('should accept a callback', function (done) {
+      this
+        .users
+        .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;
+
+      this
+        .users
+        .logs(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(url)
+        .reply(500);
+
+      this
+        .users
+        .logs(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(url)
+        .matchHeader('authorization', 'Bearer ' + this.token)
+        .reply(200);
+
+      this
+        .users
+        .logs(data)
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+
+    it('should pass the body of the response to the "then" handler', function (done) {
+      nock.cleanAll();
+
+      var response = [{ test: true }];
+      var request = nock(API_URL)
+        .get(url)
+        .reply(200, response);
+
+      this
+        .users
+        .logs(data)
+        .then(function (logs) {
+          expect(logs)
+            .to.be.an.instanceOf(Array);
+
+          expect(logs.length)
+            .to.equal(response.length);
+
+          expect(logs[0].test)
+            .to.equal(response[0].test);
+
+          done();
+        });
+    });
+
+    it('should pass the parameters in the query-string', function (done) {
+      nock.cleanAll();
+
+      var params = {
+        page: 0,
+        per_page: 30
+      };
+      var request = nock(API_URL)
+        .get(url)
+        .query(params)
+        .reply(200);
+
+      data.page = params.page;
+      data.per_page = params.per_page;
+
+      this
+        .users
+        .logs(data)
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+
+  });
+
 });