From 06c0b871adcbdd33724fa757c35f7b3b95053226 Mon Sep 17 00:00:00 2001 From: Frederik Date: Fri, 1 Oct 2021 16:30:29 +0200 Subject: [PATCH 1/4] Add support for checking connection status --- src/management/ConnectionsManager.js | 31 ++++++++++++++++ test/management/connections.tests.js | 55 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/management/ConnectionsManager.js b/src/management/ConnectionsManager.js index 87ebd6e5a..b68085321 100644 --- a/src/management/ConnectionsManager.js +++ b/src/management/ConnectionsManager.js @@ -51,6 +51,13 @@ var ConnectionsManager = function(options) { ); this.resource = new RetryRestClient(auth0RestClient, options.retry); + var statusClient = new Auth0RestClient( + options.baseUrl + '/connections/:id/status', + clientOptions, + options.tokenProvider + ); + this.status = new RetryRestClient(statusClient, options.retry); + /** * Provides an abstraction layer for consuming the * {@link https://auth0.com/docs/api/management/v2#!/Connections/delete_users_by_email @@ -192,6 +199,30 @@ utils.wrapPropertyMethod(ConnectionsManager, 'update', 'resource.patch'); */ utils.wrapPropertyMethod(ConnectionsManager, 'delete', 'resource.delete'); +/** + * Checks the status of an ad/ldap connection referenced by its ID. + * + * @method checkStatus + * @memberOf module:management.OrganizationsManager.prototype + * + * @example + * var params = {id : 'CONNECTION_ID'} + * @example + * This methods takes the connection ID and returns the status when online, or an error when offline. + * + * + * management.connections.checkStatus( {id : 'CONNECTION_ID'}, function (err, status) { + * console.log(status); + * }); + * + * @param {Object} params Connection parameters + * @param {String} params.id ID of the Connection. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(ConnectionsManager, 'checkStatus', 'status.get'); + /** * Delete a connection user by email. * diff --git a/test/management/connections.tests.js b/test/management/connections.tests.js index 828356252..7c18a61e0 100644 --- a/test/management/connections.tests.js +++ b/test/management/connections.tests.js @@ -5,6 +5,7 @@ var SRC_DIR = '../../src'; var API_URL = 'https://tenant.auth0.com'; var ConnectionsManager = require(SRC_DIR + '/management/ConnectionsManager'); +var ManagementClient = require(SRC_DIR + '/management/index'); var ArgumentError = require('rest-facade').ArgumentError; describe('ConnectionsManager', function() { @@ -455,6 +456,60 @@ describe('ConnectionsManager', function() { }); }); + describe('#checkStatus', function() { + var params = { id: 5 }; + var data = { + id: params.id, + name: 'Test connection' + }; + + beforeEach(function() { + this.request = nock(API_URL) + .get('/connections/' + data.id + '/status') + .reply(200); + }); + + it('should accept a callback', function(done) { + this.connections.checkStatus(params, function() { + done(); + }); + }); + + it('should return a promise if no callback is given', function(done) { + this.connections + .checkStatus(params) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should report success', function(done) { + nock.cleanAll(); + + var request = nock(API_URL) + .get('/connections/' + params.id + '/status') + .reply(200); + + this.connections.checkStatus(params).then(function(response) { + expect(response).to.exist; + done(); + }); + }); + + it('should report failure', function(done) { + nock.cleanAll(); + + var request = nock(API_URL) + .get('/connections/' + params.id + '/status') + .reply(500); + + this.connections.checkStatus(params).catch(function(err) { + expect(err).to.exist; + + done(); + }); + }); + }); + describe('#delete user', function() { var id = 5; var email = 'user@domain.com'; From 8d3387ffc1682d8651b1342bfd7f49ed58daeb28 Mon Sep 17 00:00:00 2001 From: Frederik Date: Fri, 1 Oct 2021 16:38:23 +0200 Subject: [PATCH 2/4] update example --- src/management/ConnectionsManager.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/management/ConnectionsManager.js b/src/management/ConnectionsManager.js index b68085321..cf43077e4 100644 --- a/src/management/ConnectionsManager.js +++ b/src/management/ConnectionsManager.js @@ -211,8 +211,12 @@ utils.wrapPropertyMethod(ConnectionsManager, 'delete', 'resource.delete'); * This methods takes the connection ID and returns the status when online, or an error when offline. * * - * management.connections.checkStatus( {id : 'CONNECTION_ID'}, function (err, status) { - * console.log(status); + * management.connections.checkStatus( {id : 'CONNECTION_ID'}, function (err) { + * if (err) { + * console.log('OFFLINE'); + * } else { + * console.log('ONLINE'); + * } * }); * * @param {Object} params Connection parameters From 5e52e8f7051898356ce6c7c4e3f172acba7c8eb2 Mon Sep 17 00:00:00 2001 From: Frederik Date: Fri, 1 Oct 2021 16:39:11 +0200 Subject: [PATCH 3/4] Update example --- src/management/ConnectionsManager.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/management/ConnectionsManager.js b/src/management/ConnectionsManager.js index cf43077e4..f10f291c0 100644 --- a/src/management/ConnectionsManager.js +++ b/src/management/ConnectionsManager.js @@ -211,11 +211,11 @@ utils.wrapPropertyMethod(ConnectionsManager, 'delete', 'resource.delete'); * This methods takes the connection ID and returns the status when online, or an error when offline. * * - * management.connections.checkStatus( {id : 'CONNECTION_ID'}, function (err) { + * management.connections.checkStatus( {id : 'CONNECTION_ID'}, function (err, status) { * if (err) { - * console.log('OFFLINE'); + * console.log('OFFLINE', err); * } else { - * console.log('ONLINE'); + * console.log('ONLINE', status); * } * }); * From c52cde5aa6f74aea7ac70025a4bdeb5d2df3421c Mon Sep 17 00:00:00 2001 From: Frederik Date: Fri, 1 Oct 2021 16:39:56 +0200 Subject: [PATCH 4/4] Remove unused import --- test/management/connections.tests.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/management/connections.tests.js b/test/management/connections.tests.js index 7c18a61e0..6c983fdf8 100644 --- a/test/management/connections.tests.js +++ b/test/management/connections.tests.js @@ -5,7 +5,6 @@ var SRC_DIR = '../../src'; var API_URL = 'https://tenant.auth0.com'; var ConnectionsManager = require(SRC_DIR + '/management/ConnectionsManager'); -var ManagementClient = require(SRC_DIR + '/management/index'); var ArgumentError = require('rest-facade').ArgumentError; describe('ConnectionsManager', function() {