Repository: auth0/node-auth0
Pull request #86 (2016-03-16 15:24:28)

Add support for Logs API


Patch:
diff --git a/src/management/LogsManager.js b/src/management/LogsManager.js
new file mode 100644
index 000000000..4a6dafc67
--- /dev/null
+++ b/src/management/LogsManager.js
@@ -0,0 +1,90 @@
+var RestClient = require('rest-facade').Client;
+var ArgumentError = require('../exceptions').ArgumentError;
+var utils = require('../utils');
+
+
+/**
+ * @class LogsManager
+ * Represents the relationship between Auth0 and an Identity provider.
+ * @constructor
+ * @memberOf module:management
+ *
+ * @param {Object} options            The client options.
+ * @param {String} options.baseUrl    The URL of the API.
+ * @param {Object} [options.headers]  Headers to be included in all requests.
+ */
+var LogsManager = function (options) {
+  if (options === null || typeof options !== 'object') {
+    throw new ArgumentError('Must provide client options');
+  }
+
+  if (options.baseUrl === null || options.baseUrl === undefined) {
+    throw new ArgumentError('Must provide a base URL for the API');
+  }
+
+  if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
+    throw new ArgumentError('The provided base URL is invalid');
+  }
+
+  /**
+   * Options object for the Rest Client instance.
+   *
+   * @type {Object}
+   */
+  var apiOptions = {
+    headers: options.headers,
+    query: { repeatParams: false }
+  };
+
+  /**
+   * Provides an abstraction layer for performing CRUD operations on
+   * {@link https://auth0.com/docs/api/v2#!/LogsManagers Auth0
+   *  Logs}.
+   *
+   * @type {external:RestClient}
+   */
+  this.resource = new RestClient(options.baseUrl + '/logs/:id ', apiOptions);
+};
+
+/**
+ * Get all logs.
+ *
+ * @method    getAll
+ * @memberOf  module:management.LogsManager.prototype
+ *
+ * @example
+ * management.logs.getAll(function (err, logs) {
+ *   console.log(logs.length);
+ * });
+ *
+ * @param   {Function}  [cb]    Callback function.
+ *
+ * @return  {Promise|undefined}
+ */
+utils.wrapPropertyMethod(LogsManager, 'getAll', 'resource.getAll');
+
+
+/**
+ * Get an Auth0 log.
+ *
+ * @method    get
+ * @memberOf  module:management.LogsManager.prototype
+ *
+ * @example
+ * management.logs.get({ id: EVENT_ID }, function (err, log) {
+ *   if (err) {
+ *     // Handle error.
+ *   }
+ *
+ *   console.log(log);
+ * });
+ *
+ * @param   {Object}    params          Log parameters.
+ * @param   {String}    params.id       Log ID.
+ * @param   {Function}  [cb]            Callback function.
+ *
+ * @return  {Promise|undefined}
+ */
+utils.wrapPropertyMethod(LogsManager, 'get', 'resource.get');
+
+module.exports = LogsManager;
diff --git a/src/management/index.js b/src/management/index.js
index 3dfd90af6..b9b02caf9 100644
--- a/src/management/index.js
+++ b/src/management/index.js
@@ -19,6 +19,7 @@ var StatsManager = require('./StatsManager');
 var TenantManager = require('./TenantManager');
 var JobsManager = require('./JobsManager');
 var TicketsManager = require('./TicketsManager');
+var LogsManager = require('./LogsManager');
 
 var BASE_URL_FORMAT = 'https://%s/api/v2';
 
@@ -159,6 +160,13 @@ var ManagementClient = function (options) {
    * @type {TicketsManager}
    */
   this.tickets = new TicketsManager(managerOptions);
+
+  /**
+   * Logs manager.
+   *
+   * @type {LogsManager}
+   */
+  this.logs = new LogsManager(managerOptions);
 };
 
 
@@ -1270,5 +1278,46 @@ utils.wrapPropertyMethod(ManagementClient, 'createPasswordChangeTicket', 'ticket
  */
 utils.wrapPropertyMethod(ManagementClient, 'createEmailVerificationTicket', 'tickets.verifyEmail');
 
+/**
+ * Get an Auth0 log.
+ *
+ * @method    getLog
+ * @memberOf  module:management.ManagementClient.prototype
+ *
+ * @example
+ * management.getLog({ id: EVENT_ID }, function (err, log) {
+ *   if (err) {
+ *     // Handle error.
+ *   }
+ *
+ *   console.log(log);
+ * });
+ *
+ * @param   {Object}    params          Log parameters.
+ * @param   {String}    params.id       Event ID.
+ * @param   {Function}  [cb]            Callback function.
+ *
+ * @return  {Promise|undefined}
+ */
+utils.wrapPropertyMethod(ManagementClient, 'getLog', 'logs.get');
+
+/**
+ * Get all logs.
+ *
+ * @method    getLogs
+ * @memberOf  module:management.ManagementClient.prototype
+ *
+ * @example
+ * management.getLogs(function (err, logs) {
+ *   console.log(logs.length);
+ * });
+ *
+ * @param   {Object}    data     Log data object.
+ * @param   {Function}  [cb]     Callback function.
+ *
+ * @return  {Promise|undefined}
+ */
+utils.wrapPropertyMethod(ManagementClient, 'getLogs', 'logs.getAll');
+
 
 module.exports = ManagementClient;


Test patch:
diff --git a/test/management/logs.tests.js b/test/management/logs.tests.js
new file mode 100644
index 000000000..4b3ebb9ea
--- /dev/null
+++ b/test/management/logs.tests.js
@@ -0,0 +1,315 @@
+var expect = require('chai').expect;
+var nock = require('nock');
+
+var SRC_DIR = '../../src';
+var API_URL = 'https://tenant.auth0.com';
+
+var LogsManager = require(SRC_DIR + '/management/LogsManager');
+var ArgumentError = require(SRC_DIR + '/exceptions').ArgumentError;
+
+
+describe('LogsManager', function () {
+  before(function () {
+    this.token = 'TOKEN';
+    this.logs = new LogsManager({
+      headers: { authorization: 'Bearer ' + this.token },
+      baseUrl: API_URL
+    });
+  });
+
+
+  describe('instance', function () {
+    var methods = ['getAll', 'get'];
+
+    methods.forEach(function (method) {
+      it('should have a ' + method + ' method', function () {
+        expect(this.logs[method])
+          .to.exist
+          .to.be.an.instanceOf(Function);
+      })
+    });
+  });
+
+
+  describe('#constructor', function () {
+    it('should error when no options are provided', function () {
+      expect(LogsManager)
+        .to.throw(ArgumentError, 'Must provide client options');
+    });
+
+
+    it('should throw an error when no base URL is provided', function () {
+      var client = LogsManager.bind(null, {});
+
+      expect(client)
+        .to.throw(ArgumentError, 'Must provide a base URL for the API');
+    });
+
+
+    it('should throw an error when the base URL is invalid', function () {
+      var client = LogsManager.bind(null, { baseUrl: '' });
+
+      expect(client)
+        .to.throw(ArgumentError, 'The provided base URL is invalid');
+    });
+  });
+
+
+  describe('#getAll', function () {
+    beforeEach(function () {
+      this.request = nock(API_URL)
+        .get('/logs')
+        .reply(200);
+    })
+
+
+    it('should accept a callback', function (done) {
+      this
+        .logs
+        .getAll(function () {
+        done();
+      });
+    });
+
+
+    it('should return a promise if no callback is given', function (done) {
+      this
+        .logs
+        .getAll()
+        .then(done.bind(null, null))
+        .catch(done.bind(null, null));
+    });
+
+
+    it('should pass any errors to the promise catch handler', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs')
+        .reply(500);
+
+      this
+        .logs
+        .getAll()
+        .catch(function (err) {
+          expect(err)
+            .to.exist;
+
+          done();
+        });
+    });
+
+
+    it('should pass the body of the response to the "then" handler', function (done) {
+      nock.cleanAll();
+
+      var data = [{ test: true }];
+      var request = nock(API_URL)
+        .get('/logs')
+        .reply(200, data);
+
+      this
+        .logs
+        .getAll()
+        .then(function (logs) {
+          expect(logs)
+            .to.be.an.instanceOf(Array);
+
+          expect(logs.length)
+            .to.equal(data.length);
+
+          expect(logs[0].test)
+            .to.equal(data[0].test);
+
+          done();
+        });
+    });
+
+
+    it('should perform a GET request to /api/v2/logs', function (done) {
+      var request = this.request;
+
+      this
+        .logs
+        .getAll()
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+
+
+    it('should include the token in the Authorization header', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs')
+        .matchHeader('Authorization', 'Bearer ' + this.token)
+        .reply(200)
+
+      this
+        .logs
+        .getAll()
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+          done();
+        });
+    });
+
+
+    it('should pass the parameters in the query-string', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs')
+        .query({
+          include_fields: true,
+          fields: 'test'
+        })
+        .reply(200)
+
+      this
+        .logs
+        .getAll({ include_fields: true, fields: 'test' })
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+  });
+
+
+  describe('#get', function () {
+    var params = { id: 5 };
+    var data = {
+      id: params.id,
+      name: 'Test log'
+    };
+
+    beforeEach(function () {
+      this.request = nock(API_URL)
+        .get('/logs/' + data.id)
+        .reply(200);
+    })
+
+
+    it('should accept a callback', function (done) {
+      this
+        .logs
+        .get(params, function () {
+          done();
+        });
+    });
+
+
+    it('should return a promise if no callback is given', function (done) {
+      this
+        .logs
+        .get(params)
+        .then(done.bind(null, null))
+        .catch(done.bind(null, null));
+    });
+
+
+    it('should pass any errors to the promise catch handler', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs/' + params.id)
+        .reply(500);
+
+      this
+        .logs
+        .get()
+        .catch(function (err) {
+          expect(err)
+            .to.exist;
+
+          done();
+        });
+    });
+
+
+    it('should pass the body of the response to the "then" handler', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs/' + params.id)
+        .reply(200, data);
+
+      this
+        .logs
+        .get(params)
+        .then(function (log) {
+          expect(log.id)
+            .to.equal(data.id);
+
+          done();
+        });
+    });
+
+
+    it('should perform a GET request to /api/v2/logs/:id', function (done) {
+      var request = this.request;
+
+      this
+        .logs
+        .get(params)
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+
+
+    it('should include the token in the Authorization header', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs')
+        .matchHeader('Authorization', 'Bearer ' + this.token)
+        .reply(200)
+
+      this
+        .logs
+        .getAll()
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+
+
+    it('should pass the parameters in the query-string', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .get('/logs')
+        .query({
+          include_fields: true,
+          fields: 'test'
+        })
+        .reply(200)
+
+      this
+        .logs
+        .getAll({ include_fields: true, fields: 'test' })
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+  });
+
+});