Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apikeys): implement the base paths for the api keys resource #22

Merged
merged 5 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

This comment was marked as resolved.

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}`);
}
}
42 changes: 42 additions & 0 deletions src/resources/ApiKeys/ApiKeysInterfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export interface ApiKeyModel {
rosalie-liu marked this conversation as resolved.
Show resolved Hide resolved
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 enum Provider {

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

SALESFORCE = 'SALESFORCE',
SALESFORCE_SANDBOX = 'SALESFORCE_SANDBOX',
GOOGLE = 'GOOGLE',
OFFICE365 = 'OFFICE365',
SAML = 'SAML',
EMAIL = 'EMAIL',
OTHER = 'OTHER',
}

export interface IdAndDisplayNameModel {

This comment was marked as resolved.

This comment was marked as resolved.

id: string;
displayName?: string;
}

export interface PrivilegeModel {
rosalie-liu marked this conversation as resolved.
Show resolved Hide resolved

This comment was marked as resolved.

This comment was marked as resolved.

owner: string;
targetDomain: string;
targetId: string;
type?: 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);
});
});
});
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