diff options
Diffstat (limited to 'spec/services/alert_management/http_integrations')
3 files changed, 206 insertions, 0 deletions
diff --git a/spec/services/alert_management/http_integrations/create_service_spec.rb b/spec/services/alert_management/http_integrations/create_service_spec.rb new file mode 100644 index 00000000000..ac5c62caf84 --- /dev/null +++ b/spec/services/alert_management/http_integrations/create_service_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AlertManagement::HttpIntegrations::CreateService do + let_it_be(:user_with_permissions) { create(:user) } + let_it_be(:user_without_permissions) { create(:user) } + let_it_be_with_reload(:project) { create(:project) } + + let(:current_user) { user_with_permissions } + let(:params) { {} } + + let(:service) { described_class.new(project, current_user, params) } + + before_all do + project.add_maintainer(user_with_permissions) + end + + describe '#execute' do + shared_examples 'error response' do |message| + it 'has an informative message' do + expect(response).to be_error + expect(response.message).to eq(message) + end + end + + subject(:response) { service.execute } + + context 'when the current_user is anonymous' do + let(:current_user) { nil } + + it_behaves_like 'error response', 'You have insufficient permissions to create an HTTP integration for this project' + end + + context 'when current_user does not have permission to create integrations' do + let(:current_user) { user_without_permissions } + + it_behaves_like 'error response', 'You have insufficient permissions to create an HTTP integration for this project' + end + + context 'when an integration already exists' do + let_it_be(:existing_integration) { create(:alert_management_http_integration, project: project) } + + it_behaves_like 'error response', 'Multiple HTTP integrations are not supported for this project' + end + + context 'when an error occurs during update' do + it_behaves_like 'error response', "Name can't be blank" + end + + context 'with valid params' do + let(:params) { { name: 'New HTTP Integration', active: true } } + + it 'successfully creates an integration' do + expect(response).to be_success + + integration = response.payload[:integration] + expect(integration).to be_a(::AlertManagement::HttpIntegration) + expect(integration.name).to eq('New HTTP Integration') + expect(integration).to be_active + expect(integration.token).to be_present + expect(integration.endpoint_identifier).to be_present + end + end + end +end diff --git a/spec/services/alert_management/http_integrations/destroy_service_spec.rb b/spec/services/alert_management/http_integrations/destroy_service_spec.rb new file mode 100644 index 00000000000..cd949d728de --- /dev/null +++ b/spec/services/alert_management/http_integrations/destroy_service_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AlertManagement::HttpIntegrations::DestroyService do + let_it_be(:user_with_permissions) { create(:user) } + let_it_be(:user_without_permissions) { create(:user) } + let_it_be(:project) { create(:project) } + + let!(:integration) { create(:alert_management_http_integration, project: project) } + let(:current_user) { user_with_permissions } + let(:params) { {} } + let(:service) { described_class.new(integration, current_user) } + + before_all do + project.add_maintainer(user_with_permissions) + end + + describe '#execute' do + shared_examples 'error response' do |message| + it 'has an informative message' do + expect(response).to be_error + expect(response.message).to eq(message) + end + end + + subject(:response) { service.execute } + + context 'when the current_user is anonymous' do + let(:current_user) { nil } + + it_behaves_like 'error response', 'You have insufficient permissions to remove this HTTP integration' + end + + context 'when current_user does not have permission to create integrations' do + let(:current_user) { user_without_permissions } + + it_behaves_like 'error response', 'You have insufficient permissions to remove this HTTP integration' + end + + context 'when an error occurs during removal' do + before do + allow(integration).to receive(:destroy).and_return(false) + integration.errors.add(:name, 'cannot be removed') + end + + it_behaves_like 'error response', 'Name cannot be removed' + end + + it 'successfully returns the integration' do + expect(response).to be_success + + integration_result = response.payload[:integration] + expect(integration_result).to be_a(::AlertManagement::HttpIntegration) + expect(integration_result.name).to eq(integration.name) + expect(integration_result.active).to eq(integration.active) + expect(integration_result.token).to eq(integration.token) + expect(integration_result.endpoint_identifier).to eq(integration.endpoint_identifier) + + expect { integration.reload }.to raise_error ActiveRecord::RecordNotFound + end + end +end diff --git a/spec/services/alert_management/http_integrations/update_service_spec.rb b/spec/services/alert_management/http_integrations/update_service_spec.rb new file mode 100644 index 00000000000..94c34d9a29c --- /dev/null +++ b/spec/services/alert_management/http_integrations/update_service_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AlertManagement::HttpIntegrations::UpdateService do + let_it_be(:user_with_permissions) { create(:user) } + let_it_be(:user_without_permissions) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be_with_reload(:integration) { create(:alert_management_http_integration, :inactive, project: project, name: 'Old Name') } + + let(:current_user) { user_with_permissions } + let(:params) { {} } + + let(:service) { described_class.new(integration, current_user, params) } + + before_all do + project.add_maintainer(user_with_permissions) + end + + describe '#execute' do + shared_examples 'error response' do |message| + it 'has an informative message' do + expect(response).to be_error + expect(response.message).to eq(message) + end + end + + subject(:response) { service.execute } + + context 'when the current_user is anonymous' do + let(:current_user) { nil } + + it_behaves_like 'error response', 'You have insufficient permissions to update this HTTP integration' + end + + context 'when current_user does not have permission to create integrations' do + let(:current_user) { user_without_permissions } + + it_behaves_like 'error response', 'You have insufficient permissions to update this HTTP integration' + end + + context 'when an error occurs during update' do + let(:params) { { name: '' } } + + it_behaves_like 'error response', "Name can't be blank" + end + + context 'with name param' do + let(:params) { { name: 'New Name' } } + + it 'successfully updates the integration' do + expect(response).to be_success + expect(response.payload[:integration].name).to eq('New Name') + end + end + + context 'with active param' do + let(:params) { { active: true } } + + it 'successfully updates the integration' do + expect(response).to be_success + expect(response.payload[:integration]).to be_active + end + end + + context 'with regenerate_token flag' do + let(:params) { { regenerate_token: true } } + + it 'successfully updates the integration' do + previous_token = integration.token + + expect(response).to be_success + expect(response.payload[:integration].token).not_to eq(previous_token) + end + end + end +end |