Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-4013] Add API2 Factor Management Endpoints #791

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/management/UsersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

/**
Expand Down Expand Up @@ -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;
149 changes: 149 additions & 0 deletions test/management/users.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});