Skip to content

Commit

Permalink
[Management] Relationships API test coverage (#19737)
Browse files Browse the repository at this point in the history
* Added coverage around search and dashboard tests.

* Added tests to check for whether the resource is available. If not, return 404.

* Skipped two tests due to #19713. Added error handling for relationships API for when no result is found. Return 404.

* Applied patch file per PR.

* Applied Chris patch and tested locally. No failures.

* Removed ajv and utilised joi for schema validation.

* Fixed package.json.

* Copied package.json description from master.

* Reverted package.json and made proper edit.
  • Loading branch information
cuff-links committed Jun 20, 2018
1 parent 036d046 commit fea9c42
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/core_plugins/kibana/server/lib/__tests__/relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('findRelationships', () => {
const size = 10;

const savedObjectsClient = {
get: () => {},
find: () => ({
saved_objects: [
{
Expand Down Expand Up @@ -202,6 +203,7 @@ describe('findRelationships', () => {
const size = 10;

const savedObjectsClient = {
get: () => {},
find: options => {
if (options.type === 'visualization') {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,34 @@ async function findDashboardRelationships(id, size, savedObjectsClient) {
const panelsJSON = JSON.parse(dashboard.attributes.panelsJSON);
if (panelsJSON) {
const visualizationIds = panelsJSON.map(panel => panel.id);
const visualizationResponse = await savedObjectsClient.bulkGet(visualizationIds.slice(0, size).map(id => ({
id,
type: 'visualization',
})));

visualizations.push(...visualizationResponse.saved_objects.reduce((accum, object) => {
if (!object.error) {
accum.push({
id: object.id,
title: object.attributes.title,
});
}
return accum;
}, []));
const visualizationResponse = await savedObjectsClient.bulkGet(
visualizationIds.slice(0, size).map(id => ({
id,
type: 'visualization',
}))
);

visualizations.push(
...visualizationResponse.saved_objects.reduce((accum, object) => {
if (!object.error) {
accum.push({
id: object.id,
title: object.attributes.title,
});
}
return accum;
}, [])
);
}

return { visualizations };
}

async function findVisualizationRelationships(id, size, savedObjectsClient) {
await savedObjectsClient.get('visualization', id);
const allDashboardsResponse = await savedObjectsClient.find({
type: 'dashboard',
fields: ['title', 'panelsJSON']
fields: ['title', 'panelsJSON'],
});

const dashboards = [];
Expand All @@ -71,7 +76,6 @@ async function findVisualizationRelationships(id, size, savedObjectsClient) {
break;
}
}

return { dashboards };
}

Expand All @@ -92,7 +96,7 @@ async function findSavedSearchRelationships(id, size, savedObjectsClient) {
type: 'visualization',
searchFields: ['savedSearchId'],
search: id,
fields: ['title']
fields: ['title'],
});

const visualizations = allVisualizationsResponse.saved_objects.reduce((accum, object) => {
Expand All @@ -109,6 +113,7 @@ async function findSavedSearchRelationships(id, size, savedObjectsClient) {
}

async function findIndexPatternRelationships(id, size, savedObjectsClient) {
await savedObjectsClient.get('index-pattern', id);
const [allVisualizationsResponse, savedSearchResponse] = await Promise.all([
savedObjectsClient.find({
type: 'visualization',
Expand Down Expand Up @@ -159,7 +164,6 @@ async function findIndexPatternRelationships(id, size, savedObjectsClient) {
break;
}
}

return { visualizations, searches };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import Boom from 'boom';
import Joi from 'joi';
import { findRelationships } from '../../../../lib/management/saved_objects/relationships';
import { isNotFoundError } from '../../../../../../../server/saved_objects/service/lib/errors';

export function registerRelationships(server) {
server.route({
Expand All @@ -33,7 +34,7 @@ export function registerRelationships(server) {
}),
query: Joi.object().keys({
size: Joi.number(),
})
}),
},
},

Expand All @@ -43,18 +44,15 @@ export function registerRelationships(server) {
const size = req.query.size || 10;

try {
const response = await findRelationships(
type,
id,
size,
req.getSavedObjectsClient(),
);

const response = await findRelationships(type, id, size, req.getSavedObjectsClient());
reply(response);
}
catch (err) {
} catch (err) {
if (isNotFoundError(err)) {
reply(Boom.boomify(new Error('Resource not found'), { statusCode: 404 }));
return;
}
reply(Boom.boomify(err, { statusCode: 500 }));
}
}
},
});
}
Loading

0 comments on commit fea9c42

Please sign in to comment.