Skip to content

Commit

Permalink
feat(api-keys): add endpoints for bulk operations (#882)
Browse files Browse the repository at this point in the history
J:ADUI-10308
Add endpoints for bulk delete, bulk activate, and bulk disable on API keys resource.
  • Loading branch information
gdostie authored Nov 5, 2024
1 parent cf4c206 commit ed6394f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/resources/ApiKeys/ApiKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(`${ApiKey.baseUrl}/delete/bulk`, apiKeyIds);
}
return this.api.delete<void>(`${ApiKey.baseUrl}/${Array.isArray(apiKeyIds) ? apiKeyIds[0] : apiKeyIds}`);
}

extend(apiKeyId: string) {
Expand All @@ -38,4 +41,18 @@ export default class ApiKey extends Resource {
duplicate(apiKeyId: string, options: DuplicateApiKeyOptions) {
return this.api.put<ApiKeyModel>(this.buildPath(`${ApiKey.baseUrl}/${apiKeyId}/duplicate`), options);
}

activate(apiKeyIds: string | string[]) {
if (Array.isArray(apiKeyIds) && apiKeyIds.length > 1) {
return this.api.put<void>(`${ApiKey.baseUrl}/activate/bulk`, apiKeyIds);
}
return this.api.put<void>(`${ApiKey.baseUrl}/${Array.isArray(apiKeyIds) ? apiKeyIds[0] : apiKeyIds}/activate`);
}

disable(apiKeyIds: string | string[]) {
if (Array.isArray(apiKeyIds) && apiKeyIds.length > 1) {
return this.api.put<void>(`${ApiKey.baseUrl}/disable/bulk`, apiKeyIds);
}
return this.api.put<void>(`${ApiKey.baseUrl}/${Array.isArray(apiKeyIds) ? apiKeyIds[0] : apiKeyIds}/disable`);
}
}
62 changes: 61 additions & 1 deletion src/resources/ApiKeys/test/ApiKeys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
]);
});
});
});

0 comments on commit ed6394f

Please sign in to comment.