Repository: auth0/node-auth0
Pull request #791 (2023-03-13 13:51:22)

[SDK-4013] Add API2 Factor Management Endpoints
### Changes

Method | Endpoint | Description
-- | -- | --
GET | /api/v2/users/{user}/authentication-methods | Gets a list of authentication methods
GET | /api/v2/users/{user}/authentication-methods/{authentication_method_id} | Gets an authentication method by ID.
POST | /api/v2/users/{id}/authentication-methods | Creates an authentication method for a given user.
PUT | /api/v2/users/{user}/authentication-methods | Updates all authentication methods for a user by replacing them with the given ones.
PATCH | /api/v2/users/{user}/authentication-methods/{authentication_method_id} | Updates an authentication method.
DELETE | /api/v2/users/{user}/authentication-methods | Deletes all authentication methods for the given user.
DELETE | /api/v2/users/{user}/authentication-methods/{authentication_method_id} | Deletes an authentication method by ID



Patch:
diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js
index ee247787a..1a07f21d5 100644
--- a/src/management/UsersManager.js
+++ b/src/management/UsersManager.js
@@ -93,6 +93,10 @@ class UsersManager extends BaseManager {
     this.authenticators = this._getRestClient('/users/:id/authenticators');
 
     this.organizations = this._getRestClient('/users/:id/organizations');
+
+    this.authenticationMethods = this._getRestClient(
+      '/users/:id/authentication-methods/:authentication_method_id'
+    );
   }
 
   /**
@@ -782,6 +786,97 @@ class UsersManager extends BaseManager {
   getUserOrganizations(...args) {
     return this.organizations.getAll(...args);
   }
+
+  /**
+   * Gets a list of authentication methods.
+   *
+   * @param   {object}    params      The user data object.
+   * @param   {string}    params.id   The user id.
+   * @param   {Function}  [cb]        Callback function.
+   * @returns  {Promise|undefined}
+   */
+  getAuthenticationMethods(...args) {
+    return this.authenticationMethods.getAll(...args);
+  }
+
+  /**
+   * Gets an authentication method by ID.
+   *
+   * @param   {object}    params                          The user data object.
+   * @param   {string}    params.id                       The user id.
+   * @param   {string}    params.authentication_method_id The authentication method id.
+   * @param   {Function}  [cb]                            Callback function.
+   * @returns  {Promise|undefined}
+   */
+  getAuthenticationMethodById(...args) {
+    return this.authenticationMethods.get(...args);
+  }
+
+  /**
+   * Creates an authentication method for a given user.
+   *
+   * @param   {object}    params                          The user data object.
+   * @param   {string}    params.id                       The user id.
+   * @param   {object}    data                            Authentication method data.
+   * @param   {Function}  [cb]                            Callback function.
+   * @returns  {Promise|undefined}
+   */
+  createAuthenticationMethod(...args) {
+    return this.authenticationMethods.create(...args);
+  }
+
+  /**
+   * Updates all authentication methods for a user by replacing them with the given ones.
+   *
+   * @param   {object}    params                          The user data object.
+   * @param   {string}    params.id                       The user id.
+   * @param   {object}    data                            Updated authentication method data.
+   * @param   {Function}  [cb]                            Callback function.
+   * @returns  {Promise|undefined}
+   */
+  updateAuthenticationMethods(...args) {
+    return this.authenticationMethods.update(...args);
+  }
+
+  /**
+   * Updates an authentication method.
+   *
+   * @param   {object}    params                          The user data object.
+   * @param   {string}    params.id                       The user id.
+   * @param   {string}    params.authentication_method_id The authentication method id.
+   * @param   {object}    data                            Updated authentication method data.
+   * @param   {Function}  [cb]                            Callback function.
+   * @returns  {Promise|undefined}
+   */
+  updateAuthenticationMethodById(...args) {
+    return this.authenticationMethods.patch(...args);
+  }
+
+  /**
+   * Deletes all authentication methods for the given user.
+   *
+   * @param   {object}    params                          The user data object.
+   * @param   {string}    params.id                       The user id.
+   * @param   {Function}  [cb]                            Callback function.
+   * @returns  {Promise|undefined}
+   */
+  deleteAuthenticationMethods(...args) {
+    return this.authenticationMethods.delete(...args);
+  }
+
+  /**
+   * Deletes an authentication method by ID.
+   *
+   * @param   {object}    params                          The user data object.
+   * @param   {string}    params.id                       The user id.
+   * @param   {string}    params.authentication_method_id The authentication method id.
+   * @param   {object}    data                            Updated authentication method data.
+   * @param   {Function}  [cb]                            Callback function.
+   * @returns  {Promise|undefined}
+   */
+  deleteAuthenticationMethodById(...args) {
+    return this.authenticationMethods.delete(...args);
+  }
 }
 
 module.exports = UsersManager;


Test patch:
diff --git a/test/management/users.tests.js b/test/management/users.tests.js
index 55a3664a2..079b447cf 100644
--- a/test/management/users.tests.js
+++ b/test/management/users.tests.js
@@ -1605,4 +1605,153 @@ describe('UsersManager', () => {
       expect(request.isDone()).to.be.true;
     });
   });
+
+  describe('#getAuthenticationMethods', () => {
+    const params = {
+      id: 'user_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL).get(`/users/${params.id}/authentication-methods`).reply(200);
+    });
+
+    it('should perform a GET request to /api/v2/users/{user}/authentication-methods', async () => {
+      await usersManager.getAuthenticationMethods(params);
+      expect(scope.isDone()).to.be.true;
+    });
+  });
+
+  describe('#getAuthenticationMethodById', () => {
+    const params = {
+      id: 'user_id',
+      authentication_method_id: 'authentication_method_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL)
+        .get(`/users/${params.id}/authentication-methods/${params.authentication_method_id}`)
+        .reply(200);
+    });
+
+    it('should perform a GET request to /api/v2/users/{user}/authentication-methods/{authentication_method_id}', async () => {
+      await usersManager.getAuthenticationMethodById(params);
+      expect(scope.isDone()).to.be.true;
+    });
+  });
+
+  describe('#createAuthenticationMethod', () => {
+    const params = {
+      id: 'user_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL).post(`/users/${params.id}/authentication-methods`).reply(200);
+    });
+
+    it('should perform a POST request to /api/v2/users/{id}/authentication-methods', async () => {
+      await usersManager.createAuthenticationMethod(params, {});
+      expect(scope.isDone()).to.be.true;
+    });
+  });
+
+  describe('#updateAuthenticationMethods', () => {
+    const params = {
+      id: 'user_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL).put(`/users/${params.id}/authentication-methods`).reply(200);
+    });
+
+    it('should perform a PUT request to /api/v2/users/{user}/authentication-methods', async () => {
+      await usersManager.updateAuthenticationMethods(params, {});
+      expect(scope.isDone()).to.be.true;
+    });
+  });
+
+  describe('#updateAuthenticationMethodById', () => {
+    const params = {
+      id: 'user_id',
+      authentication_method_id: 'authentication_method_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL)
+        .patch(`/users/${params.id}/authentication-methods/${params.authentication_method_id}`)
+        .reply(200);
+    });
+
+    it('should perform a PATCH request to /api/v2/users/{user}/authentication-methods/{authentication_method_id}', async () => {
+      await usersManager.updateAuthenticationMethodById(params, {});
+      expect(scope.isDone()).to.be.true;
+    });
+  });
+
+  describe('#deleteAuthenticationMethods', () => {
+    const params = {
+      id: 'user_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL).delete(`/users/${params.id}/authentication-methods`).reply(200);
+    });
+
+    it('should perform a DELETE request to /api/v2/users/{user}/authentication-methods', async () => {
+      await usersManager.deleteAuthenticationMethods(params);
+      expect(scope.isDone()).to.be.true;
+    });
+  });
+
+  describe('#deleteAuthenticationMethodById', () => {
+    const params = {
+      id: 'user_id',
+      authentication_method_id: 'authentication_method_id',
+    };
+
+    /**
+     * @type {nock}
+     */
+    let scope;
+
+    beforeEach(() => {
+      scope = nock(API_URL)
+        .delete(`/users/${params.id}/authentication-methods/${params.authentication_method_id}`)
+        .reply(200);
+    });
+
+    it('should perform a DELETE request to /api/v2/users/{user}/authentication-methods/{authentication_method_id}', async () => {
+      await usersManager.deleteAuthenticationMethodById(params);
+      expect(scope.isDone()).to.be.true;
+    });
+  });
 });