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

[App Search] Credentials: Add final Logic and server routes #81519

Merged
merged 8 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
jest.mock('../../app_logic', () => ({
AppLogic: {
selectors: { myRole: jest.fn(() => ({})) },
values: { myRole: jest.fn(() => ({})) },
},
}));
import { AppLogic } from '../../app_logic';
Expand Down Expand Up @@ -1177,6 +1178,70 @@ describe('CredentialsLogic', () => {
});
});

describe('onApiTokenChange', () => {
it('calls a POST API endpoint that creates a new token', async () => {
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
const createdToken = {
name: 'new-key',
type: ApiTokenTypes.Admin,
};
mount({
activeApiToken: createdToken,
});
jest.spyOn(CredentialsLogic.actions, 'onApiTokenCreateSuccess');
const promise = Promise.resolve(createdToken);
http.post.mockReturnValue(promise);

CredentialsLogic.actions.onApiTokenChange();
expect(http.post).toHaveBeenCalledWith('/api/app_search/credentials', {
body: JSON.stringify(createdToken),
});
await promise;
expect(CredentialsLogic.actions.onApiTokenCreateSuccess).toHaveBeenCalledWith(createdToken);
expect(setSuccessMessage).toHaveBeenCalled();
});

it('calls a PUT endpoint that updates existing API tokens', async () => {
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
const updatedToken = {
name: 'test-key',
type: ApiTokenTypes.Private,
read: true,
write: false,
access_all_engines: false,
engines: ['engine1'],
};
mount({
activeApiToken: {
...updatedToken,
id: 'some-id',
},
});
jest.spyOn(CredentialsLogic.actions, 'onApiTokenUpdateSuccess');
const promise = Promise.resolve(updatedToken);
http.put.mockReturnValue(promise);

CredentialsLogic.actions.onApiTokenChange();
expect(http.put).toHaveBeenCalledWith('/api/app_search/credentials/test-key', {
body: JSON.stringify(updatedToken),
});
await promise;
expect(CredentialsLogic.actions.onApiTokenUpdateSuccess).toHaveBeenCalledWith(updatedToken);
expect(setSuccessMessage).toHaveBeenCalled();
});

it('handles errors', async () => {
mount();
const promise = Promise.reject('An error occured');
http.post.mockReturnValue(promise);

CredentialsLogic.actions.onApiTokenChange();
try {
await promise;
} catch {
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}
});
});

describe('onEngineSelect', () => {
it('calls addEngineName if the engine is not selected', () => {
mount({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { kea, MakeLogicType } from 'kea';

import { formatApiName } from '../../utils/format_api_name';
import { ApiTokenTypes, DELETE_MESSAGE } from './constants';
import { ApiTokenTypes, CREATE_MESSAGE, UPDATE_MESSAGE, DELETE_MESSAGE } from './constants';

import { HttpLogic } from '../../../shared/http';
import {
Expand Down Expand Up @@ -50,6 +50,7 @@ interface ICredentialsLogicActions {
fetchCredentials(page?: number): number;
fetchDetails(): { value: boolean };
deleteApiKey(tokenName: string): string;
onApiTokenChange(): void;
onEngineSelect(engineName: string): string;
}

Expand Down Expand Up @@ -94,6 +95,7 @@ export const CredentialsLogic = kea<
fetchCredentials: (page) => page,
fetchDetails: true,
deleteApiKey: (tokenName) => tokenName,
onApiTokenChange: () => null,
onEngineSelect: (engineName) => engineName,
}),
reducers: () => ({
Expand Down Expand Up @@ -261,7 +263,48 @@ export const CredentialsLogic = kea<
flashAPIErrors(e);
}
},
// TODO onApiTokenChange from ent-search
onApiTokenChange: async () => {
const { myRole } = AppLogic.values;
const {
id,
name,
engines,
type,
read,
write,
access_all_engines: accessAllEngines,
} = values.activeApiToken;

const data: IApiToken = {
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
name,
type,
};
if (type === ApiTokenTypes.Private) {
data.read = read;
data.write = write;
}
if (type !== ApiTokenTypes.Admin) {
data.access_all_engines = !!(accessAllEngines && myRole.canAccessAllEngines);
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
data.engines = engines;
}

try {
const { http } = HttpLogic.values;
const body = JSON.stringify(data);

if (id) {
const response = await http.put(`/api/app_search/credentials/${name}`, { body });
actions.onApiTokenUpdateSuccess(response);
setSuccessMessage(UPDATE_MESSAGE);
} else {
const response = await http.post('/api/app_search/credentials', { body });
actions.onApiTokenCreateSuccess(response);
setSuccessMessage(CREATE_MESSAGE);
}
} catch (e) {
flashAPIErrors(e);
}
},
onEngineSelect: (engineName: string) => {
if (values.activeApiToken?.engines?.includes(engineName)) {
actions.removeEngineName(engineName);
Expand Down