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

[Management] Relationships API test coverage #19737

Merged

Conversation

cuff-links
Copy link
Contributor

@cuff-links cuff-links commented Jun 7, 2018

Added some tests around the relationships endpoints for management to validate responses and check error handling.

@elasticmachine
Copy link
Contributor

💔 Build Failed

@epixa
Copy link
Contributor

epixa commented Jun 7, 2018

Just to clarify: these are management APIs, not our saved object APIs. The saved object apis are documented here: https://www.elastic.co/guide/en/kibana/6.3/saved-objects-api.html

@cuff-links cuff-links changed the title Saved objects integration test coverage [Management] Relationships API test coverage Jun 7, 2018
@cuff-links
Copy link
Contributor Author

@epixa Thanks for the reminder. Keep getting confused because of the directory structure. Noted. Does this title and description work better?

…tionships API for when no result is found. Return 404.
@jen-huang jen-huang requested review from jen-huang and removed request for timroes June 7, 2018 21:31
@cuff-links
Copy link
Contributor Author

Fixes #19713

@elasticmachine
Copy link
Contributor

💔 Build Failed

@cuff-links
Copy link
Contributor Author

jenkins test this

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@chrisronline
Copy link
Contributor

Looks great @silne30!

One thing we might want to do, per our conversation on zoom, is to leave the lib file alone and capture all 404 errors at the route level, then return an explicit 404.

Something like this diff:

diff --git a/src/core_plugins/kibana/server/lib/management/saved_objects/relationships.js b/src/core_plugins/kibana/server/lib/management/saved_objects/relationships.js
index 61e204b8bf..e98bebe4c4 100644
--- a/src/core_plugins/kibana/server/lib/management/saved_objects/relationships.js
+++ b/src/core_plugins/kibana/server/lib/management/saved_objects/relationships.js
@@ -45,11 +45,11 @@ async function findDashboardRelationships(id, size, savedObjectsClient) {
     );
   }
 
-  visualizations.length > 0  ? { visualizations } : 404;
   return { visualizations };
 }
 
 async function findVisualizationRelationships(id, size, savedObjectsClient) {
+  await savedObjectsClient.get('visualization', id);
   const allDashboardsResponse = await savedObjectsClient.find({
     type: 'dashboard',
     fields: ['title', 'panelsJSON'],
@@ -76,7 +76,7 @@ async function findVisualizationRelationships(id, size, savedObjectsClient) {
       break;
     }
   }
-  return dashboards.length > 0 ? { dashboards } : 404;
+  return { dashboards };
 }
 
 async function findSavedSearchRelationships(id, size, savedObjectsClient) {
@@ -109,10 +109,11 @@ async function findSavedSearchRelationships(id, size, savedObjectsClient) {
     return accum;
   }, []);
 
-  return visualizations.length > 0 && indexPatterns.length > 0 ? { visualizations, indexPatterns } : 404;
+  return { visualizations, indexPatterns };
 }
 
 async function findIndexPatternRelationships(id, size, savedObjectsClient) {
+  await savedObjectsClient.get('index-pattern', id);
   const [allVisualizationsResponse, savedSearchResponse] = await Promise.all([
     savedObjectsClient.find({
       type: 'visualization',
@@ -163,7 +164,7 @@ async function findIndexPatternRelationships(id, size, savedObjectsClient) {
       break;
     }
   }
-  return visualizations.length > 0 && searches.length > 0 ? { visualizations, searches } : 404;
+  return { visualizations, searches };
 }
 
 export async function findRelationships(type, id, size, savedObjectsClient) {
diff --git a/src/core_plugins/kibana/server/routes/api/management/saved_objects/relationships.js b/src/core_plugins/kibana/server/routes/api/management/saved_objects/relationships.js
index 015606725d..34eca71cc2 100644
--- a/src/core_plugins/kibana/server/routes/api/management/saved_objects/relationships.js
+++ b/src/core_plugins/kibana/server/routes/api/management/saved_objects/relationships.js
@@ -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({
@@ -44,13 +45,12 @@ export function registerRelationships(server) {
 
       try {
         const response = await findRelationships(type, id, size, req.getSavedObjectsClient());
-
-        if (typeof response === 'number') {
-          reply(Boom.boomify(new Error('Resource not found'), { statusCode: response }));
-        }
-
         reply(response);
       } catch (err) {
+        if (isNotFoundError(err)) {
+          reply(Boom.boomify(new Error('Resource not found'), { statusCode: 404 }));
+          return;
+        }
         reply(Boom.boomify(err, { statusCode: 500 }));
       }
     },

@elasticmachine
Copy link
Contributor

💔 Build Failed

@cuff-links
Copy link
Contributor Author

jenkins test this

@elasticmachine
Copy link
Contributor

💔 Build Failed

@cuff-links
Copy link
Contributor Author

jenkins test this again

@elasticmachine
Copy link
Contributor

💔 Build Failed

@chrisronline
Copy link
Contributor

Hey @silne30

Sorry for leaving you hanging for a few days about these tests.

This patch should fix the build errors:

--- a/src/core_plugins/kibana/server/lib/__tests__/relationships.js
+++ b/src/core_plugins/kibana/server/lib/__tests__/relationships.js
@@ -77,6 +77,7 @@ describe('findRelationships', () => {
     const size = 10;
 
     const savedObjectsClient = {
+      get: () => {},
       find: () => ({
         saved_objects: [
           {
@@ -202,6 +203,7 @@ describe('findRelationships', () => {
     const size = 10;
 
     const savedObjectsClient = {
+      get: () => {},
       find: options => {
         if (options.type === 'visualization') {
           return {

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@cuff-links
Copy link
Contributor Author

@chrisronline @jen-huang Whichever one of you wanna merge this bad boy, feel free.

@chrisronline
Copy link
Contributor

@silne30 We currently have https://github.com/hapijs/joi for schema validation. Is it possible to use that instead of ajv?

@cjcenizal cjcenizal self-requested a review June 18, 2018 17:51
@cuff-links
Copy link
Contributor Author

@chrisronline Yeah. I just throw code on the wall for you guys to tell me all the things I need to change. Working on that now.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Contributor

@chrisronline chrisronline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! One request

package.json Outdated
@@ -1,15 +1,8 @@
{
"name": "kibana",
"description": "Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elasticsearch.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can revert these changes? Doesn't seem necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. Not even sure when that happened.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still seeing changes here. Try just copying the file from master (https://raw.githubusercontent.com/elastic/kibana/master/package.json) into your copy and commit/push that up

Copy link
Contributor

@chrisronline chrisronline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@cuff-links cuff-links merged commit fea9c42 into elastic:master Jun 20, 2018
@cuff-links cuff-links deleted the Saved_Objects_Integration_Test_Coverage branch June 20, 2018 20:07
cuff-links added a commit to cuff-links/kibana that referenced this pull request Jul 13, 2018
* 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 elastic#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.
cuff-links added a commit to cuff-links/kibana that referenced this pull request Jul 13, 2018
* 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 elastic#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.
cuff-links pushed a commit that referenced this pull request Jul 13, 2018
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants