-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apikeys): implement the base paths for the api keys resource (#22)
feat(apikeys): implement the base paths for the api keys resource
- Loading branch information
1 parent
e3a7123
commit 8796b09
Showing
7 changed files
with
181 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import API from '../../APICore'; | ||
import {New} from '../BaseInterfaces'; | ||
import Resource from '../Resource'; | ||
import {ApiKeyModel, CreateApiKeyOptions} from './ApiKeysInterfaces'; | ||
|
||
export default class ApiKey extends Resource { | ||
static baseUrl = `/rest/organizations/${API.orgPlaceholder}/apikeys`; | ||
|
||
list() { | ||
return this.api.get<ApiKeyModel[]>(ApiKey.baseUrl); | ||
} | ||
|
||
create(apiKey: New<ApiKeyModel, 'resourceId'>, options?: CreateApiKeyOptions) { | ||
return this.api.post<ApiKeyModel>(this.buildPath(ApiKey.baseUrl, options), apiKey); | ||
} | ||
|
||
get(apiKeyId: string) { | ||
return this.api.get<ApiKeyModel>(`${ApiKey.baseUrl}/${apiKeyId}`); | ||
} | ||
|
||
update(apiKey: ApiKeyModel) { | ||
return this.api.put(`${ApiKey.baseUrl}/${apiKey.id}`, apiKey); | ||
} | ||
|
||
toggle(apiKey: ApiKeyModel) { | ||
const path = `${ApiKey.baseUrl}/${apiKey.id}/${apiKey.enabled ? 'disable' : 'activate'}`; | ||
|
||
return this.api.put(path, apiKey); | ||
} | ||
|
||
delete(apiKeyId: string) { | ||
return this.api.delete(`${ApiKey.baseUrl}/${apiKeyId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {IdAndDisplayNameModel, PrivilegeModel} from '../BaseInterfaces'; | ||
|
||
export interface ApiKeyModel { | ||
organizationId?: string; | ||
id?: string; | ||
enabled?: boolean; | ||
value?: string; | ||
displayName?: string; | ||
description?: string; | ||
createdBy?: {}; | ||
createdDate?: number; | ||
allowedIps?: string[]; | ||
apiKeysThatCanEdit?: IdAndDisplayNameModel[]; | ||
deniedIps?: string[]; | ||
groupsThatCanEdit?: IdAndDisplayNameModel[]; | ||
privileges?: PrivilegeModel[]; | ||
resourceId?: string; | ||
} | ||
|
||
export interface CreateApiKeyOptions { | ||
apiKeyTemplateId?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ApiKeys'; | ||
export * from './ApiKeysInterfaces'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import API from '../../../APICore'; | ||
import {New} from '../../BaseInterfaces'; | ||
import ApiKey from '../ApiKeys'; | ||
import {ApiKeyModel} from '../ApiKeysInterfaces'; | ||
|
||
jest.mock('../../../APICore'); | ||
|
||
const APIMock: jest.Mock<API> = API as any; | ||
|
||
describe('ApiKey', () => { | ||
let apiKey: ApiKey; | ||
const api = new APIMock() as jest.Mocked<API>; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
apiKey = new ApiKey(api); | ||
}); | ||
|
||
describe('list', () => { | ||
it('should make a GET call to the ApiKeys base url', () => { | ||
apiKey.list(); | ||
expect(api.get).toHaveBeenCalledTimes(1); | ||
expect(api.get).toHaveBeenCalledWith(ApiKey.baseUrl); | ||
}); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should make a POST call to the ApiKeys base url', () => { | ||
const apiKeyModel: New<ApiKeyModel> = { | ||
organizationId: 'a-smol-org', | ||
value: '', | ||
}; | ||
|
||
apiKey.create(apiKeyModel); | ||
expect(api.post).toHaveBeenCalledTimes(1); | ||
expect(api.post).toHaveBeenCalledWith(ApiKey.baseUrl, apiKeyModel); | ||
}); | ||
}); | ||
|
||
describe('get', () => { | ||
it('should make a GET call to the specific ApiKey url', () => { | ||
const apiKeyToGetId = 'ApiKey-to-be-fetched'; | ||
|
||
apiKey.get(apiKeyToGetId); | ||
expect(api.get).toHaveBeenCalledTimes(1); | ||
expect(api.get).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyToGetId}`); | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('should make a PUT call to the specific ApiKey url', () => { | ||
const apiKeyModel: ApiKeyModel = { | ||
organizationId: 'a-smol-org', | ||
id: 'a-specific-id', | ||
value: '', | ||
}; | ||
|
||
apiKey.update(apiKeyModel); | ||
expect(api.put).toHaveBeenCalledTimes(1); | ||
expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyModel.id}`, apiKeyModel); | ||
}); | ||
}); | ||
|
||
describe('delete', () => { | ||
it('should make a DELETE call to the specific ApiKey url', () => { | ||
const apiKeyToDeleteId = 'ApiKey-to-be-deleted'; | ||
|
||
apiKey.delete(apiKeyToDeleteId); | ||
expect(api.delete).toHaveBeenCalledTimes(1); | ||
expect(api.delete).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyToDeleteId}`); | ||
}); | ||
}); | ||
|
||
describe('toggle', () => { | ||
const apiKeyModel: ApiKeyModel = { | ||
id: 'ApiKey-to-be-toggled', | ||
organizationId: 'a-smol-org', | ||
enabled: null, | ||
}; | ||
|
||
it('should make a PUT call to disable the specific apiKey', () => { | ||
apiKeyModel.enabled = true; | ||
|
||
apiKey.toggle(apiKeyModel); | ||
expect(api.put).toHaveBeenCalledTimes(1); | ||
expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyModel.id}/disable`, apiKeyModel); | ||
}); | ||
|
||
it('should make a PUT call to activate the specific apiKey', () => { | ||
apiKeyModel.enabled = false; | ||
|
||
apiKey.toggle(apiKeyModel); | ||
expect(api.put).toHaveBeenCalledTimes(1); | ||
expect(api.put).toHaveBeenCalledWith(`${ApiKey.baseUrl}/${apiKeyModel.id}/activate`, apiKeyModel); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters