From ed6394fd641a2f64422390491fed87ce3d6d5fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Dostie?= <35579930+gdostie@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:01:06 -0500 Subject: [PATCH] feat(api-keys): add endpoints for bulk operations (#882) J:ADUI-10308 Add endpoints for bulk delete, bulk activate, and bulk disable on API keys resource. --- src/resources/ApiKeys/ApiKeys.ts | 21 +++++++- src/resources/ApiKeys/test/ApiKeys.spec.ts | 62 +++++++++++++++++++++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/resources/ApiKeys/ApiKeys.ts b/src/resources/ApiKeys/ApiKeys.ts index b354cb5e..2b5495c4 100644 --- a/src/resources/ApiKeys/ApiKeys.ts +++ b/src/resources/ApiKeys/ApiKeys.ts @@ -27,8 +27,11 @@ export default class ApiKey extends Resource { return this.api.put(path, apiKey); } - delete(apiKeyId: string) { - return this.api.delete(`${ApiKey.baseUrl}/${apiKeyId}`); + delete(apiKeyIds: string | string[]) { + if (Array.isArray(apiKeyIds) && apiKeyIds.length > 1) { + return this.api.post(`${ApiKey.baseUrl}/delete/bulk`, apiKeyIds); + } + return this.api.delete(`${ApiKey.baseUrl}/${Array.isArray(apiKeyIds) ? apiKeyIds[0] : apiKeyIds}`); } extend(apiKeyId: string) { @@ -38,4 +41,18 @@ export default class ApiKey extends Resource { duplicate(apiKeyId: string, options: DuplicateApiKeyOptions) { return this.api.put(this.buildPath(`${ApiKey.baseUrl}/${apiKeyId}/duplicate`), options); } + + activate(apiKeyIds: string | string[]) { + if (Array.isArray(apiKeyIds) && apiKeyIds.length > 1) { + return this.api.put(`${ApiKey.baseUrl}/activate/bulk`, apiKeyIds); + } + return this.api.put(`${ApiKey.baseUrl}/${Array.isArray(apiKeyIds) ? apiKeyIds[0] : apiKeyIds}/activate`); + } + + disable(apiKeyIds: string | string[]) { + if (Array.isArray(apiKeyIds) && apiKeyIds.length > 1) { + return this.api.put(`${ApiKey.baseUrl}/disable/bulk`, apiKeyIds); + } + return this.api.put(`${ApiKey.baseUrl}/${Array.isArray(apiKeyIds) ? apiKeyIds[0] : apiKeyIds}/disable`); + } } diff --git a/src/resources/ApiKeys/test/ApiKeys.spec.ts b/src/resources/ApiKeys/test/ApiKeys.spec.ts index bccb9fca..6823e706 100644 --- a/src/resources/ApiKeys/test/ApiKeys.spec.ts +++ b/src/resources/ApiKeys/test/ApiKeys.spec.ts @@ -62,13 +62,27 @@ describe('ApiKey', () => { }); describe('delete', () => { - it('should make a DELETE call to the specific ApiKey url', async () => { + it('makes a DELETE call to DELETE call to /rest/organizations/:orgId/apikeys/:id when only one id is specified', async () => { const apiKeyToDeleteId = 'ApiKey-to-be-deleted'; await apiKey.delete(apiKeyToDeleteId); expect(api.delete).toHaveBeenCalledTimes(1); expect(api.delete).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyToDeleteId}`); }); + + it('makes a DELETE call to /rest/organizations/:orgId/apikeys/:id when an array of one id is provided', async () => { + const apiKeyToDeleteId = 'ApiKey-to-be-deleted'; + await apiKey.delete([apiKeyToDeleteId]); + expect(api.delete).toHaveBeenCalledTimes(1); + expect(api.delete).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyToDeleteId}`); + }); + + it('makes a POST call to /rest/organizations/:orgId/apikeys/delete/bulk when multiple ids are provided', async () => { + const apiKeysToDelete = ['api-key-id-one', 'api-key-id-two']; + await apiKey.delete(apiKeysToDelete); + expect(api.post).toHaveBeenCalledTimes(1); + expect(api.post).toHaveBeenCalledWith(`${ApiKey.baseUrl}/delete/bulk`, apiKeysToDelete); + }); }); describe('toggle', () => { @@ -120,4 +134,50 @@ describe('ApiKey', () => { expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyModel.id}/activation/extend`); }); }); + + describe('activate', () => { + it('makes a PUT call to /rest/organizations/:orgId/apikeys/:id/activate when only one id is provided', async () => { + await apiKey.activate('api-key-id'); + expect(api.put).toHaveBeenCalledTimes(1); + expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/api-key-id/activate`); + }); + + it('makes a PUT call to /rest/organizations/:orgId/apikeys/:id/activate when an array of one id is provided', async () => { + await apiKey.activate(['api-key-id']); + expect(api.put).toHaveBeenCalledTimes(1); + expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/api-key-id/activate`); + }); + + it('makes a PUT call to /rest/organizations/:orgId/apikeys/activate/bulk when multiple ids are provided', async () => { + await apiKey.activate(['api-key-id-one', 'api-key-id-two']); + expect(api.put).toHaveBeenCalledTimes(1); + expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/activate/bulk`, [ + 'api-key-id-one', + 'api-key-id-two', + ]); + }); + }); + + describe('disable', () => { + it('makes a PUT call to /rest/organizations/:orgId/apikeys/:id/disable when only one id is provided', async () => { + await apiKey.disable('api-key-id'); + expect(api.put).toHaveBeenCalledTimes(1); + expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/api-key-id/disable`); + }); + + it('makes a PUT call to /rest/organizations/:orgId/apikeys/:id/disable when an array of one id is provided', async () => { + await apiKey.disable(['api-key-id']); + expect(api.put).toHaveBeenCalledTimes(1); + expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/api-key-id/disable`); + }); + + it('makes a PUT call to /rest/organizations/:orgId/apikeys/disable/bulk when multiple ids are provided', async () => { + await apiKey.disable(['api-key-id-one', 'api-key-id-two']); + expect(api.put).toHaveBeenCalledTimes(1); + expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/disable/bulk`, [ + 'api-key-id-one', + 'api-key-id-two', + ]); + }); + }); });