Skip to content

Commit

Permalink
Add DELETE /api/v2/users/{id}/authenticators (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Feb 9, 2023
1 parent 636deb8 commit 8a63cf9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/management/UsersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down Expand Up @@ -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.authenticators.delete(query, cb);
}

return this.authenticators.delete(query);
}

/**
* Get a list of organizations for a user.
*
Expand Down
65 changes: 65 additions & 0 deletions test/management/users.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,71 @@ 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, (err) => {
expect(err).to.be.null;
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',
Expand Down

0 comments on commit 8a63cf9

Please sign in to comment.