diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /spec/services/metrics | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'spec/services/metrics')
5 files changed, 122 insertions, 5 deletions
diff --git a/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb b/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb index b386159541a..3d26ab2ede5 100644 --- a/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb +++ b/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb @@ -5,8 +5,6 @@ require 'spec_helper' describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_store_caching do include MetricsDashboardHelpers - STAGES = ::Gitlab::Metrics::Dashboard::Stages - let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project, :repository) } let_it_be(:environment) { create(:environment, project: project) } @@ -83,7 +81,7 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor allow(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).and_return(double(process: file_content_hash)) end - it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, [STAGES::CommonMetricsInserter, STAGES::CustomMetricsInserter, STAGES::Sorter] + it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, [::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter, ::Gitlab::Metrics::Dashboard::Stages::CustomMetricsInserter, ::Gitlab::Metrics::Dashboard::Stages::Sorter] context 'selected branch already exists' do let(:branch) { 'existing_branch' } diff --git a/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb b/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb index 034d6aba5d6..3547e1f0f8c 100644 --- a/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb @@ -154,7 +154,7 @@ describe Metrics::Dashboard::GrafanaMetricEmbedService do context 'when value not present in cache' do it 'returns nil' do - expect(ReactiveCachingWorker) + expect(ExternalServiceReactiveCachingWorker) .to receive(:perform_async) .with(service.class, service.id, *cache_params) @@ -217,7 +217,7 @@ describe Metrics::Dashboard::DatasourceNameParser do include GrafanaApiHelpers let(:grafana_url) { valid_grafana_dashboard_link('https://gitlab.grafana.net') } - let(:grafana_dashboard) { JSON.parse(fixture_file('grafana/dashboard_response.json'), symbolize_names: true) } + let(:grafana_dashboard) { Gitlab::Json.parse(fixture_file('grafana/dashboard_response.json'), symbolize_names: true) } subject { described_class.new(grafana_url, grafana_dashboard).parse } diff --git a/spec/services/metrics/dashboard/transient_embed_service_spec.rb b/spec/services/metrics/dashboard/transient_embed_service_spec.rb index 4982f56cddc..125fff7c23c 100644 --- a/spec/services/metrics/dashboard/transient_embed_service_spec.rb +++ b/spec/services/metrics/dashboard/transient_embed_service_spec.rb @@ -67,6 +67,12 @@ describe Metrics::Dashboard::TransientEmbedService, :use_clean_rails_memory_stor expect(get_type_for_embed(alt_embed)).to eq('area-chart') end + context 'when embed_json cannot be parsed as json' do + let(:embed_json) { '' } + + it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity + end + private def get_embed_json(type = 'line-graph') diff --git a/spec/services/metrics/users_starred_dashboards/create_service_spec.rb b/spec/services/metrics/users_starred_dashboards/create_service_spec.rb new file mode 100644 index 00000000000..eac4965ba44 --- /dev/null +++ b/spec/services/metrics/users_starred_dashboards/create_service_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::UsersStarredDashboards::CreateService do + let_it_be(:user) { create(:user) } + let(:dashboard_path) { 'config/prometheus/common_metrics.yml' } + let(:service_instance) { described_class.new(user, project, dashboard_path) } + let(:project) { create(:project) } + let(:starred_dashboard_params) do + { + user: user, + project: project, + dashboard_path: dashboard_path + } + end + + shared_examples 'prevented starred dashboard creation' do |message| + it 'returns error response', :aggregate_failures do + expect(Metrics::UsersStarredDashboard).not_to receive(:new) + + response = service_instance.execute + + expect(response.status).to be :error + expect(response.message).to eql message + end + end + + describe '.execute' do + context 'with anonymous user' do + it_behaves_like 'prevented starred dashboard creation', 'You are not authorized to add star to this dashboard' + end + + context 'with reporter user' do + before do + project.add_reporter(user) + end + + context 'incorrect dashboard_path' do + let(:dashboard_path) { 'something_incorrect.yml' } + + it_behaves_like 'prevented starred dashboard creation', 'Dashboard with requested path can not be found' + end + + context 'with valid dashboard path' do + it 'creates starred dashboard and returns success response', :aggregate_failures do + expect_next_instance_of(Metrics::UsersStarredDashboard, starred_dashboard_params) do |starred_dashboard| + expect(starred_dashboard).to receive(:save).and_return true + end + + response = service_instance.execute + + expect(response.status).to be :success + end + + context 'Metrics::UsersStarredDashboard has validation errors' do + it 'returns error response', :aggregate_failures do + expect_next_instance_of(Metrics::UsersStarredDashboard, starred_dashboard_params) do |starred_dashboard| + expect(starred_dashboard).to receive(:save).and_return(false) + expect(starred_dashboard).to receive(:errors).and_return(double(messages: { base: ['Model validation error'] })) + end + + response = service_instance.execute + + expect(response.status).to be :error + expect(response.message).to eql(base: ['Model validation error']) + end + end + end + end + end +end diff --git a/spec/services/metrics/users_starred_dashboards/delete_service_spec.rb b/spec/services/metrics/users_starred_dashboards/delete_service_spec.rb new file mode 100644 index 00000000000..68a2fef5931 --- /dev/null +++ b/spec/services/metrics/users_starred_dashboards/delete_service_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::UsersStarredDashboards::DeleteService do + subject(:service_instance) { described_class.new(user, project, dashboard_path) } + + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + + describe '#execute' do + let_it_be(:user_starred_dashboard_1) { create(:metrics_users_starred_dashboard, user: user, project: project, dashboard_path: 'dashboard_1') } + let_it_be(:user_starred_dashboard_2) { create(:metrics_users_starred_dashboard, user: user, project: project) } + let_it_be(:other_user_starred_dashboard) { create(:metrics_users_starred_dashboard, project: project) } + let_it_be(:other_project_starred_dashboard) { create(:metrics_users_starred_dashboard, user: user) } + + context 'without dashboard_path' do + let(:dashboard_path) { nil } + + it 'does not scope user starred dashboards by dashboard path' do + result = service_instance.execute + + expect(result.success?).to be_truthy + expect(result.payload[:deleted_rows]).to be(2) + expect(Metrics::UsersStarredDashboard.all).to contain_exactly(other_user_starred_dashboard, other_project_starred_dashboard) + end + end + + context 'with dashboard_path' do + let(:dashboard_path) { 'dashboard_1' } + + it 'does scope user starred dashboards by dashboard path' do + result = service_instance.execute + + expect(result.success?).to be_truthy + expect(result.payload[:deleted_rows]).to be(1) + expect(Metrics::UsersStarredDashboard.all).to contain_exactly(user_starred_dashboard_2, other_user_starred_dashboard, other_project_starred_dashboard) + end + end + end +end |