From dff7419ff447f9077533d46b29680180af5817c2 Mon Sep 17 00:00:00 2001 From: adamjmcgrath Date: Thu, 9 Feb 2023 06:29:16 -0400 Subject: [PATCH 1/3] Add DELETE /api/v2/users/{id}/authenticators --- src/management/UsersManager.js | 25 +++++++++++++ test/management/users.tests.js | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js index 0d7e22383..69e651052 100644 --- a/src/management/UsersManager.js +++ b/src/management/UsersManager.js @@ -85,6 +85,13 @@ class UsersManager extends BaseManager { */ this.permissions = this._getRestClient('/users/:id/permissions'); + /** + * For CRUD on user's authenticators + * + * @type {external:RestClient} + */ + this.authenticators = this._getRestClient('/users/:id/authenticators'); + this.organizations = this._getRestClient('/users/:id/organizations'); } @@ -742,6 +749,24 @@ class UsersManager extends BaseManager { return this.permissions.delete(query, data); } + deleteAllAuthenticators(params, cb) { + const query = params || {}; + + // Require a user ID. + if (!query.id) { + throw new ArgumentError('The user_id cannot be null or undefined'); + } + if (typeof query.id !== 'string') { + throw new ArgumentError('The user_id has to be a string'); + } + + if (cb && cb instanceof Function) { + return this.permissions.delete(query, cb); + } + + return this.authenticators.delete(query); + } + /** * Get a list of organizations for a user. * diff --git a/test/management/users.tests.js b/test/management/users.tests.js index 9c0ab1dea..80cfd9439 100644 --- a/test/management/users.tests.js +++ b/test/management/users.tests.js @@ -1489,6 +1489,70 @@ describe('UsersManager', () => { }); }); + describe('#deleteAllAuthenticators', () => { + let params; + let scope; + + beforeEach(() => { + params = { + id: 'user_id', + }; + + scope = nock(API_URL).delete(`/users/${params.id}/authenticators`).reply(200); + }); + + it('should validate empty user_id', () => { + expect(() => { + usersManager.deleteAllAuthenticators({ id: null }, () => {}); + }).to.throw('The user_id cannot be null or undefined'); + }); + + it('should validate non-string user_id', () => { + expect(() => { + usersManager.deleteAllAuthenticators({ id: 123 }, () => {}); + }).to.throw('The user_id has to be a string'); + }); + + it('should accept a callback', (done) => { + usersManager.deleteAllAuthenticators(params, () => { + done(); + }); + }); + + it('should return a promise if no callback is given', () => { + expect(usersManager.deleteAllAuthenticators(params, {})).instanceOf(Promise); + }); + + it('should pass any errors to the promise catch handler', async () => { + nock.cleanAll(); + + nock(API_URL).post(`/users/${params.id}/authenticators`).reply(500); + + try { + await usersManager.deleteAllAuthenticators(params, {}); + } catch (err) { + expect(err).to.exist; + } + }); + + it('should perform a DELETE request to /api/v2/users/user_id/authenticators', async () => { + await usersManager.deleteAllAuthenticators(params, {}); + expect(scope.isDone()).to.be.true; + }); + + it('should include the token in the Authorization header', async () => { + nock.cleanAll(); + + const request = nock(API_URL) + .delete(`/users/${params.id}/authenticators`) + .matchHeader('Authorization', `Bearer ${token}`) + .reply(200); + + await usersManager.deleteAllAuthenticators(params, {}); + expect(request.isDone()).to.be.true; + }); + }); + describe('#getUserOrganizations', () => { const data = { id: 'user_id', From 34c05c2a5dbd6e860a454d9951dfa395709360c5 Mon Sep 17 00:00:00 2001 From: adamjmcgrath Date: Thu, 9 Feb 2023 06:56:47 -0400 Subject: [PATCH 2/3] fix typo --- src/management/UsersManager.js | 2 +- test/management/users.tests.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js index 69e651052..ee247787a 100644 --- a/src/management/UsersManager.js +++ b/src/management/UsersManager.js @@ -761,7 +761,7 @@ class UsersManager extends BaseManager { } if (cb && cb instanceof Function) { - return this.permissions.delete(query, cb); + return this.authenticators.delete(query, cb); } return this.authenticators.delete(query); diff --git a/test/management/users.tests.js b/test/management/users.tests.js index 80cfd9439..c1f37534e 100644 --- a/test/management/users.tests.js +++ b/test/management/users.tests.js @@ -1515,6 +1515,7 @@ describe('UsersManager', () => { it('should accept a callback', (done) => { usersManager.deleteAllAuthenticators(params, () => { + expect(scope.isDone()).to.be.true; done(); }); }); From a3b7959cc86fbc1737c10c68325b8b704f7fdc68 Mon Sep 17 00:00:00 2001 From: adamjmcgrath Date: Thu, 9 Feb 2023 07:05:48 -0400 Subject: [PATCH 3/3] fix test --- test/management/users.tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/management/users.tests.js b/test/management/users.tests.js index c1f37534e..55a3664a2 100644 --- a/test/management/users.tests.js +++ b/test/management/users.tests.js @@ -1514,8 +1514,8 @@ describe('UsersManager', () => { }); it('should accept a callback', (done) => { - usersManager.deleteAllAuthenticators(params, () => { - expect(scope.isDone()).to.be.true; + usersManager.deleteAllAuthenticators(params, (err) => { + expect(err).to.be.null; done(); }); });