2019-10-04 05:06:35 -04:00
# frozen_string_literal: true
require 'spec_helper'
2020-06-03 23:08:05 -04:00
RSpec . describe MetricsDashboard do
2019-11-12 19:06:18 -05:00
include MetricsDashboardHelpers
2019-10-04 05:06:35 -04:00
describe 'GET #metrics_dashboard' do
let_it_be ( :user ) { create ( :user ) }
2019-11-12 19:06:18 -05:00
let_it_be ( :project ) { project_with_dashboard ( '.gitlab/dashboards/test.yml' ) }
2019-10-04 05:06:35 -04:00
let_it_be ( :environment ) { create ( :environment , project : project ) }
before do
sign_in ( user )
project . add_maintainer ( user )
end
controller ( :: ApplicationController ) do
2019-12-20 04:24:38 -05:00
include MetricsDashboard
2019-10-04 05:06:35 -04:00
end
let ( :json_response ) do
routes . draw { get " metrics_dashboard " = > " anonymous # metrics_dashboard " }
response = get :metrics_dashboard , format : :json
2020-01-29 13:08:47 -05:00
response . parsed_body
2019-10-04 05:06:35 -04:00
end
context 'when no parameters are provided' do
it 'returns an error json_response' do
expect ( json_response [ 'status' ] ) . to eq ( 'error' )
end
end
context 'when params are provided' do
2019-10-31 11:06:41 -04:00
let ( :params ) { { environment : environment } }
2019-10-04 05:06:35 -04:00
before do
allow ( controller ) . to receive ( :project ) . and_return ( project )
allow ( controller )
. to receive ( :metrics_dashboard_params )
2019-10-31 11:06:41 -04:00
. and_return ( params )
2019-10-04 05:06:35 -04:00
end
it 'returns the specified dashboard' do
expect ( json_response [ 'dashboard' ] [ 'dashboard' ] ) . to eq ( 'Environment metrics' )
expect ( json_response ) . not_to have_key ( 'all_dashboards' )
2020-05-15 17:08:21 -04:00
expect ( json_response ) . to have_key ( 'metrics_data' )
2019-10-04 05:06:35 -04:00
end
2019-10-31 11:06:41 -04:00
context 'when the params are in an alternate format' do
let ( :params ) { ActionController :: Parameters . new ( { environment : environment } ) . permit! }
it 'returns the specified dashboard' do
expect ( json_response [ 'dashboard' ] [ 'dashboard' ] ) . to eq ( 'Environment metrics' )
expect ( json_response ) . not_to have_key ( 'all_dashboards' )
2020-05-15 17:08:21 -04:00
expect ( json_response ) . to have_key ( 'metrics_data' )
2020-02-14 13:08:45 -05:00
end
end
context 'when environment for dashboard is available' do
let ( :params ) { { environment : environment } }
before do
allow ( controller ) . to receive ( :project ) . and_return ( project )
allow ( controller ) . to receive ( :environment ) . and_return ( environment )
allow ( controller )
. to receive ( :metrics_dashboard_params )
. and_return ( params )
end
it 'returns the specified dashboard' do
expect ( json_response [ 'dashboard' ] [ 'dashboard' ] ) . to eq ( 'Environment metrics' )
expect ( json_response ) . not_to have_key ( 'all_dashboards' )
expect ( json_response ) . to have_key ( 'metrics_data' )
2019-10-31 11:06:41 -04:00
end
end
2020-07-02 02:09:26 -04:00
context 'when dashboard path includes encoded characters' do
let ( :params ) { { dashboard_path : 'dashboard%26copy.yml' } }
before do
allow ( controller )
. to receive ( :metrics_dashboard_params )
. and_return ( params )
end
it 'decodes dashboard path' do
expect ( :: Gitlab :: Metrics :: Dashboard :: Finder ) . to receive ( :find ) . with ( anything , anything , hash_including ( dashboard_path : 'dashboard©.yml' ) )
json_response
end
end
2019-10-04 05:06:35 -04:00
context 'when parameters are provided and the list of all dashboards is required' do
before do
allow ( controller ) . to receive ( :include_all_dashboards? ) . and_return ( true )
end
it 'returns a dashboard in addition to the list of dashboards' do
expect ( json_response [ 'dashboard' ] [ 'dashboard' ] ) . to eq ( 'Environment metrics' )
expect ( json_response ) . to have_key ( 'all_dashboards' )
end
2019-11-12 19:06:18 -05:00
context 'in all_dashboard list' do
let ( :system_dashboard ) { json_response [ 'all_dashboards' ] . find { | dashboard | dashboard [ " system_dashboard " ] == true } }
2020-07-01 05:09:30 -04:00
let ( :project_dashboard ) do
json_response [ 'all_dashboards' ] . find do | dashboard |
dashboard [ 'path' ] == '.gitlab/dashboards/test.yml'
end
end
2019-11-12 19:06:18 -05:00
it 'includes project_blob_path only for project dashboards' do
expect ( system_dashboard [ 'project_blob_path' ] ) . to be_nil
2020-01-27 07:08:35 -05:00
expect ( project_dashboard [ 'project_blob_path' ] ) . to eq ( " / #{ project . namespace . path } / #{ project . name } /-/blob/master/.gitlab/dashboards/test.yml " )
2019-11-12 19:06:18 -05:00
end
2020-07-01 05:09:30 -04:00
it 'allows editing only for project dashboards' do
expect ( system_dashboard [ 'can_edit' ] ) . to be ( false )
expect ( project_dashboard [ 'can_edit' ] ) . to be ( true )
end
it 'includes out_of_the_box_dashboard key' do
expect ( system_dashboard [ 'out_of_the_box_dashboard' ] ) . to be ( true )
expect ( project_dashboard [ 'out_of_the_box_dashboard' ] ) . to be ( false )
end
2019-11-12 19:06:18 -05:00
describe 'project permissions' do
using RSpec :: Parameterized :: TableSyntax
where ( :can_collaborate , :system_can_edit , :project_can_edit ) do
false | false | false
true | false | true
end
with_them do
before do
allow ( controller ) . to receive ( :can_collaborate_with_project? ) . and_return ( can_collaborate )
end
it " sets can_edit appropriately " do
expect ( system_dashboard [ " can_edit " ] ) . to eq ( system_can_edit )
expect ( project_dashboard [ " can_edit " ] ) . to eq ( project_can_edit )
end
end
end
2020-05-12 08:09:47 -04:00
context 'starred dashboards' do
let_it_be ( :dashboard_yml ) { fixture_file ( 'lib/gitlab/metrics/dashboard/sample_dashboard.yml' ) }
let_it_be ( :dashboards ) do
{
'.gitlab/dashboards/test.yml' = > dashboard_yml ,
'.gitlab/dashboards/anomaly.yml' = > dashboard_yml ,
'.gitlab/dashboards/errors.yml' = > dashboard_yml
}
end
2020-11-17 10:09:28 -05:00
2020-05-12 08:09:47 -04:00
let_it_be ( :project ) { create ( :project , :custom_repo , files : dashboards ) }
before do
create ( :metrics_users_starred_dashboard , user : user , project : project , dashboard_path : '.gitlab/dashboards/errors.yml' )
create ( :metrics_users_starred_dashboard , user : user , project : project , dashboard_path : '.gitlab/dashboards/test.yml' )
end
it 'adds starred dashboard information and sorts the list' do
all_dashboards = json_response [ 'all_dashboards' ] . map { | dashboard | dashboard . slice ( 'display_name' , 'starred' , 'user_starred_path' ) }
expected_response = [
2020-05-14 08:08:21 -04:00
{ " display_name " = > " anomaly.yml " , " starred " = > false , 'user_starred_path' = > api_v4_projects_metrics_user_starred_dashboards_path ( id : project . id , params : { dashboard_path : '.gitlab/dashboards/anomaly.yml' } ) } ,
{ " display_name " = > " errors.yml " , " starred " = > true , 'user_starred_path' = > api_v4_projects_metrics_user_starred_dashboards_path ( id : project . id , params : { dashboard_path : '.gitlab/dashboards/errors.yml' } ) } ,
2020-08-04 20:09:52 -04:00
{ " display_name " = > " K8s pod health " , " starred " = > false , 'user_starred_path' = > api_v4_projects_metrics_user_starred_dashboards_path ( id : project . id , params : { dashboard_path : 'config/prometheus/pod_metrics.yml' } ) } ,
{ " display_name " = > " Overview " , " starred " = > false , 'user_starred_path' = > api_v4_projects_metrics_user_starred_dashboards_path ( id : project . id , params : { dashboard_path : 'config/prometheus/common_metrics.yml' } ) } ,
2020-05-14 08:08:21 -04:00
{ " display_name " = > " test.yml " , " starred " = > true , 'user_starred_path' = > api_v4_projects_metrics_user_starred_dashboards_path ( id : project . id , params : { dashboard_path : '.gitlab/dashboards/test.yml' } ) }
2020-05-12 08:09:47 -04:00
]
2020-08-04 20:09:52 -04:00
expect ( all_dashboards ) . to eq ( expected_response )
2020-05-12 08:09:47 -04:00
end
end
2019-11-12 19:06:18 -05:00
end
2019-10-04 05:06:35 -04:00
end
end
end
end