diff --git a/src/management/ConnectionsManager.js b/src/management/ConnectionsManager.js index 87ebd6e5a..f10f291c0 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,34 @@ 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) { + * if (err) { + * console.log('OFFLINE', err); + * } else { + * console.log('ONLINE', 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..6c983fdf8 100644 --- a/test/management/connections.tests.js +++ b/test/management/connections.tests.js @@ -455,6 +455,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';