diff options
Diffstat (limited to 'spec/graphql/mutations/alert_management')
8 files changed, 421 insertions, 2 deletions
diff --git a/spec/graphql/mutations/alert_management/http_integration/create_spec.rb b/spec/graphql/mutations/alert_management/http_integration/create_spec.rb new file mode 100644 index 00000000000..9aa89761aaf --- /dev/null +++ b/spec/graphql/mutations/alert_management/http_integration/create_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::HttpIntegration::Create do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let(:args) { { project_path: project.full_path, active: true, name: 'HTTP Integration' } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_operations) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when HttpIntegrations::CreateService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: ::AlertManagement::HttpIntegration.last!, + errors: [] + ) + end + end + + context 'when HttpIntegrations::CreateService responds with an error' do + before do + allow_any_instance_of(::AlertManagement::HttpIntegrations::CreateService) + .to receive(:execute) + .and_return(ServiceResponse.error(payload: { integration: nil }, message: 'An integration already exists')) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: nil, + errors: ['An integration already exists'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/http_integration/destroy_spec.rb b/spec/graphql/mutations/alert_management/http_integration/destroy_spec.rb new file mode 100644 index 00000000000..f74f9186743 --- /dev/null +++ b/spec/graphql/mutations/alert_management/http_integration/destroy_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::HttpIntegration::Destroy do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let(:integration) { create(:alert_management_http_integration, project: project) } + let(:args) { { id: GitlabSchema.id_from_object(integration) } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_operations) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when HttpIntegrations::DestroyService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: integration, + errors: [] + ) + end + end + + context 'when HttpIntegrations::DestroyService responds with an error' do + before do + allow_any_instance_of(::AlertManagement::HttpIntegrations::DestroyService) + .to receive(:execute) + .and_return(ServiceResponse.error(payload: { integration: nil }, message: 'An error has occurred')) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: nil, + errors: ['An error has occurred'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/http_integration/reset_token_spec.rb b/spec/graphql/mutations/alert_management/http_integration/reset_token_spec.rb new file mode 100644 index 00000000000..d3ffb2abb47 --- /dev/null +++ b/spec/graphql/mutations/alert_management/http_integration/reset_token_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::HttpIntegration::ResetToken do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:integration) { create(:alert_management_http_integration, project: project) } + let(:args) { { id: GitlabSchema.id_from_object(integration) } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_operations) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has sufficient access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when HttpIntegrations::UpdateService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: integration, + errors: [] + ) + end + end + + context 'when HttpIntegrations::UpdateService responds with an error' do + before do + allow_any_instance_of(::AlertManagement::HttpIntegrations::UpdateService) + .to receive(:execute) + .and_return(ServiceResponse.error(payload: { integration: nil }, message: 'Token cannot be reset')) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: nil, + errors: ['Token cannot be reset'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/http_integration/update_spec.rb b/spec/graphql/mutations/alert_management/http_integration/update_spec.rb new file mode 100644 index 00000000000..d6318e3161d --- /dev/null +++ b/spec/graphql/mutations/alert_management/http_integration/update_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::HttpIntegration::Update do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:integration) { create(:alert_management_http_integration, project: project) } + let(:args) { { id: GitlabSchema.id_from_object(integration), active: false, name: 'New Name' } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_operations) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has sufficient access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when HttpIntegrations::UpdateService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: integration, + errors: [] + ) + end + end + + context 'when HttpIntegrations::UpdateService responds with an error' do + before do + allow_any_instance_of(::AlertManagement::HttpIntegrations::UpdateService) + .to receive(:execute) + .and_return(ServiceResponse.error(payload: { integration: nil }, message: 'Failed to update')) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: nil, + errors: ['Failed to update'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/prometheus_integration/create_spec.rb b/spec/graphql/mutations/alert_management/prometheus_integration/create_spec.rb new file mode 100644 index 00000000000..02a5e2e74e2 --- /dev/null +++ b/spec/graphql/mutations/alert_management/prometheus_integration/create_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::PrometheusIntegration::Create do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let(:args) { { project_path: project.full_path, active: true, api_url: 'http://prometheus.com/' } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_project) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when Prometheus Integration already exists' do + let_it_be(:existing_integration) { create(:prometheus_service, project: project) } + + it 'returns errors' do + expect(resolve).to eq( + integration: nil, + errors: ['Multiple Prometheus integrations are not supported'] + ) + end + end + + context 'when UpdateService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: ::PrometheusService.last!, + errors: [] + ) + end + + it 'creates a corresponding token' do + expect { resolve }.to change(::Alerting::ProjectAlertingSetting, :count).by(1) + end + end + + context 'when UpdateService responds with an error' do + before do + allow_any_instance_of(::Projects::Operations::UpdateService) + .to receive(:execute) + .and_return({ status: :error, message: 'An error occurred' }) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: nil, + errors: ['An error occurred'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/prometheus_integration/reset_token_spec.rb b/spec/graphql/mutations/alert_management/prometheus_integration/reset_token_spec.rb new file mode 100644 index 00000000000..45d92695e06 --- /dev/null +++ b/spec/graphql/mutations/alert_management/prometheus_integration/reset_token_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::PrometheusIntegration::ResetToken do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:integration) { create(:prometheus_service, project: project) } + let(:args) { { id: GitlabSchema.id_from_object(integration) } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_project) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has sufficient access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when ::Projects::Operations::UpdateService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: integration, + errors: [] + ) + end + end + + context 'when ::Projects::Operations::UpdateService responds with an error' do + before do + allow_any_instance_of(::Projects::Operations::UpdateService) + .to receive(:execute) + .and_return({ status: :error, message: 'An error occurred' }) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: integration, + errors: ['An error occurred'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/prometheus_integration/update_spec.rb b/spec/graphql/mutations/alert_management/prometheus_integration/update_spec.rb new file mode 100644 index 00000000000..eab4474d827 --- /dev/null +++ b/spec/graphql/mutations/alert_management/prometheus_integration/update_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::PrometheusIntegration::Update do + let_it_be(:current_user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:integration) { create(:prometheus_service, project: project) } + let(:args) { { id: GitlabSchema.id_from_object(integration), active: false, api_url: 'http://new-url.com' } } + + specify { expect(described_class).to require_graphql_authorizations(:admin_project) } + + describe '#resolve' do + subject(:resolve) { mutation_for(project, current_user).resolve(args) } + + context 'user has sufficient access to project' do + before do + project.add_maintainer(current_user) + end + + context 'when ::Projects::Operations::UpdateService responds with success' do + it 'returns the integration with no errors' do + expect(resolve).to eq( + integration: integration, + errors: [] + ) + end + end + + context 'when ::Projects::Operations::UpdateService responds with an error' do + before do + allow_any_instance_of(::Projects::Operations::UpdateService) + .to receive(:execute) + .and_return({ status: :error, message: 'An error occurred' }) + end + + it 'returns errors' do + expect(resolve).to eq( + integration: integration, + errors: ['An error occurred'] + ) + end + end + end + + context 'when resource is not accessible to the user' do + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + + private + + def mutation_for(project, user) + described_class.new(object: project, context: { current_user: user }, field: nil) + end +end diff --git a/spec/graphql/mutations/alert_management/update_alert_status_spec.rb b/spec/graphql/mutations/alert_management/update_alert_status_spec.rb index ab98088ebcd..08761ce64c2 100644 --- a/spec/graphql/mutations/alert_management/update_alert_status_spec.rb +++ b/spec/graphql/mutations/alert_management/update_alert_status_spec.rb @@ -37,8 +37,8 @@ RSpec.describe Mutations::AlertManagement::UpdateAlertStatus do context 'error occurs when updating' do it 'returns the alert with errors' do # Stub an error on the alert - allow_next_instance_of(Resolvers::AlertManagement::AlertResolver) do |resolver| - allow(resolver).to receive(:resolve).and_return(alert) + allow_next_instance_of(::AlertManagement::AlertsFinder) do |finder| + allow(finder).to receive(:execute).and_return([alert]) end allow(alert).to receive(:save).and_return(false) |