diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 21:09:19 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 21:09:19 +0300 |
commit | 3290d46655f07d7ae3dca788d6df9f326972ffd8 (patch) | |
tree | 0d24713e1592cdd3583257f14a52d46a22539ed1 /spec/services/metrics | |
parent | c6b3ec3f56fa32a0e0ed3de0d0878d25f1adaddf (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services/metrics')
-rw-r--r-- | spec/services/metrics/dashboard/annotations/create_service_spec.rb | 160 | ||||
-rw-r--r-- | spec/services/metrics/dashboard/annotations/delete_service_spec.rb | 93 |
2 files changed, 253 insertions, 0 deletions
diff --git a/spec/services/metrics/dashboard/annotations/create_service_spec.rb b/spec/services/metrics/dashboard/annotations/create_service_spec.rb new file mode 100644 index 00000000000..7dabca3c860 --- /dev/null +++ b/spec/services/metrics/dashboard/annotations/create_service_spec.rb @@ -0,0 +1,160 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::Dashboard::Annotations::CreateService do + let_it_be(:user) { create(:user) } + let(:description) { 'test annotation' } + let(:dashboard_path) { 'config/prometheus/common_metrics.yml' } + let(:starting_at) { 15.minutes.ago } + let(:ending_at) { nil } + let(:service_instance) { described_class.new(user, annotation_params) } + let(:annotation_params) do + { + environment: environment, + cluster: cluster, + description: description, + dashboard_path: dashboard_path, + starting_at: starting_at, + ending_at: ending_at + } + end + + shared_examples 'executed annotation creation' do + it 'returns success response', :aggregate_failures do + annotation = instance_double(::Metrics::Dashboard::Annotation) + allow(::Metrics::Dashboard::Annotation).to receive(:new).and_return(annotation) + allow(annotation).to receive(:save).and_return(true) + + response = service_instance.execute + + expect(response[:status]).to be :success + expect(response[:annotation]).to be annotation + end + + it 'creates annotation', :aggregate_failures do + annotation = instance_double(::Metrics::Dashboard::Annotation) + + expect(::Metrics::Dashboard::Annotation) + .to receive(:new).with(annotation_params).and_return(annotation) + expect(annotation).to receive(:save).and_return(true) + + service_instance.execute + end + end + + shared_examples 'prevented annotation creation' do |message| + it 'returns error response', :aggregate_failures do + response = service_instance.execute + + expect(response[:status]).to be :error + expect(response[:message]).to eql message + end + + it 'does not change db state' do + expect(::Metrics::Dashboard::Annotation).not_to receive(:new) + + service_instance.execute + end + end + + shared_examples 'annotation creation failure' do + it 'returns error response', :aggregate_failures do + annotation = instance_double(::Metrics::Dashboard::Annotation) + + expect(annotation).to receive(:errors).and_return('Model validation error') + expect(::Metrics::Dashboard::Annotation) + .to receive(:new).with(annotation_params).and_return(annotation) + expect(annotation).to receive(:save).and_return(false) + + response = service_instance.execute + + expect(response[:status]).to be :error + expect(response[:message]).to eql 'Model validation error' + end + end + + describe '.execute' do + context 'with environment' do + let(:environment) { create(:environment) } + let(:cluster) { nil } + + context 'with anonymous user' do + it_behaves_like 'prevented annotation creation', 'You are not authorized to create annotation for selected environment' + end + + context 'with maintainer user' do + before do + environment.project.add_maintainer(user) + end + + it_behaves_like 'executed annotation creation' + end + end + + context 'with cluster' do + let(:environment) { nil } + + context 'with anonymous user' do + let(:cluster) { create(:cluster, :project) } + + it_behaves_like 'prevented annotation creation', 'You are not authorized to create annotation for selected cluster' + end + + context 'with maintainer user' do + let(:cluster) { create(:cluster, :project) } + + before do + cluster.project.add_maintainer(user) + end + + it_behaves_like 'executed annotation creation' + end + + context 'with owner user' do + let(:cluster) { create(:cluster, :group) } + + before do + cluster.group.add_owner(user) + end + + it_behaves_like 'executed annotation creation' + end + end + + context 'non cluster nor environment is supplied' do + let(:environment) { nil } + let(:cluster) { nil } + + it_behaves_like 'annotation creation failure' + end + + context 'missing dashboard_path' do + let(:cluster) { create(:cluster, :project) } + let(:environment) { nil } + let(:dashboard_path) { nil } + + context 'with maintainer user' do + before do + cluster.project.add_maintainer(user) + end + + it_behaves_like 'annotation creation failure' + end + end + + context 'incorrect dashboard_path' do + let(:cluster) { create(:cluster, :project) } + let(:environment) { nil } + let(:dashboard_path) { 'something_incorrect.yml' } + + context 'with maintainer user' do + before do + cluster.project.add_maintainer(user) + end + + it_behaves_like 'prevented annotation creation', 'Dashboard with requested path can not be found' + end + end + end +end diff --git a/spec/services/metrics/dashboard/annotations/delete_service_spec.rb b/spec/services/metrics/dashboard/annotations/delete_service_spec.rb new file mode 100644 index 00000000000..95825db6902 --- /dev/null +++ b/spec/services/metrics/dashboard/annotations/delete_service_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::Dashboard::Annotations::DeleteService do + let(:user) { create(:user) } + let(:service_instance) { described_class.new(user, annotation) } + + shared_examples 'executed annotation deletion' do + it 'returns success response', :aggregate_failures do + expect(annotation).to receive(:destroy).and_return(true) + + response = service_instance.execute + + expect(response[:status]).to be :success + end + end + + shared_examples 'prevented annotation deletion' do |message| + it 'returns error response', :aggregate_failures do + response = service_instance.execute + + expect(response[:status]).to be :error + expect(response[:message]).to eql message + end + + it 'does not change db state' do + expect(annotation).not_to receive(:destroy) + + service_instance.execute + end + end + + describe '.execute' do + context 'with specific environment' do + let(:annotation) { create(:metrics_dashboard_annotation, environment: environment) } + let(:environment) { create(:environment) } + + context 'with anonymous user' do + it_behaves_like 'prevented annotation deletion', 'You are not authorized to delete this annotation' + end + + context 'with maintainer user' do + before do + environment.project.add_maintainer(user) + end + + it_behaves_like 'executed annotation deletion' + + context 'annotation failed to delete' do + it 'returns error response', :aggregate_failures do + allow(annotation).to receive(:destroy).and_return(false) + + response = service_instance.execute + + expect(response[:status]).to be :error + expect(response[:message]).to eql 'Annotation has not been deleted' + end + end + end + end + + context 'with specific cluster' do + let(:annotation) { create(:metrics_dashboard_annotation, cluster: cluster, environment: nil) } + + context 'with anonymous user' do + let(:cluster) { create(:cluster, :project) } + + it_behaves_like 'prevented annotation deletion', 'You are not authorized to delete this annotation' + end + + context 'with maintainer user' do + let(:cluster) { create(:cluster, :project) } + + before do + cluster.project.add_maintainer(user) + end + + it_behaves_like 'executed annotation deletion' + end + + context 'with owner user' do + let(:cluster) { create(:cluster, :group) } + + before do + cluster.group.add_owner(user) + end + + it_behaves_like 'executed annotation deletion' + end + end + end +end |