Skip to content

Commit

Permalink
feat(apikeys): implement the base paths for the api keys resource (#22)
Browse files Browse the repository at this point in the history
feat(apikeys): implement the base paths for the api keys resource
  • Loading branch information
rosalie-liu authored Oct 17, 2019
1 parent e3a7123 commit 8796b09
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 21 deletions.
34 changes: 34 additions & 0 deletions src/resources/ApiKeys/ApiKeys.ts
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}`);
}
}
22 changes: 22 additions & 0 deletions src/resources/ApiKeys/ApiKeysInterfaces.ts
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;
}
2 changes: 2 additions & 0 deletions src/resources/ApiKeys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ApiKeys';
export * from './ApiKeysInterfaces';
97 changes: 97 additions & 0 deletions src/resources/ApiKeys/test/ApiKeys.spec.ts
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);
});
});
});
21 changes: 21 additions & 0 deletions src/resources/BaseInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,24 @@ export interface PageModel<T = any> {
}

export type New<T, K extends string | number | symbol = null> = Omit<T, 'id' | K>;

export enum AuthProvider {
SALESFORCE = 'SALESFORCE',
SALESFORCE_SANDBOX = 'SALESFORCE_SANDBOX',
GOOGLE = 'GOOGLE',
OFFICE365 = 'OFFICE365',
SAML = 'SAML',
EMAIL = 'EMAIL',
}

export interface IdAndDisplayNameModel {
id: string;
displayName?: string;
}

export interface PrivilegeModel {
owner: string;
targetDomain: string;
targetId: string;
type?: string;
}
23 changes: 2 additions & 21 deletions src/resources/Groups/GroupsInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {AuthProvider, IdAndDisplayNameModel, PrivilegeModel} from '../BaseInterfaces';

export interface GroupModel {
id: string;
displayName: string;
Expand All @@ -11,11 +13,6 @@ export interface GroupModel {
apiKeysThatCanEdit?: IdAndDisplayNameModel[];
}

export interface IdAndDisplayNameModel {
id: string;
displayName?: string;
}

export interface InviteByEmail {
id: string;
email: string;
Expand Down Expand Up @@ -60,29 +57,13 @@ export interface MemberModel {
providerUsername?: string;
}

export interface PrivilegeModel {
owner: string;
targetDomain: string;
targetId?: string;
type?: string;
}

export interface RealmModel {
id: string;
provider: AuthProvider;
displayName: string;
samlIdentityProviderId?: string;
}

export enum AuthProvider {
SALESFORCE = 'SALESFORCE',
SALESFORCE_SANDBOX = 'SALESFORCE_SANDBOX',
GOOGLE = 'GOOGLE',
OFFICE365 = 'OFFICE365',
SAML = 'SAML',
EMAIL = 'EMAIL',
}

export interface CreateGroupOptions {
canEditItself?: boolean;
sendEmailToInviteUsers?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/resources/PlatformResources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import API from '../APICore';
import ApiKey from './ApiKeys/ApiKeys';
import Catalog from './Catalogs/Catalog';
import Group from './Groups/Groups';
import Organization from './Organizations/Organization';
Expand All @@ -8,6 +9,7 @@ const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'catalog', resource: Catalog},
{key: 'group', resource: Group},
{key: 'organization', resource: Organization},
{key: 'apiKey', resource: ApiKey},
];

class PlatformResources {
Expand All @@ -16,6 +18,7 @@ class PlatformResources {
catalog: Catalog;
group: Group;
organization: Organization;
apiKey: ApiKey;

registerAll() {
resourcesMap.forEach(({key, resource}) => {
Expand Down

0 comments on commit 8796b09

Please sign in to comment.