diff options
Diffstat (limited to 'spec/services/alert_management')
6 files changed, 312 insertions, 6 deletions
diff --git a/spec/services/alert_management/create_alert_issue_service_spec.rb b/spec/services/alert_management/create_alert_issue_service_spec.rb index f2be317a13d..2834322be7b 100644 --- a/spec/services/alert_management/create_alert_issue_service_spec.rb +++ b/spec/services/alert_management/create_alert_issue_service_spec.rb @@ -12,6 +12,7 @@ RSpec.describe AlertManagement::CreateAlertIssueService do 'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1' } end + let_it_be(:generic_alert, reload: true) { create(:alert_management_alert, :triggered, project: project, payload: payload) } let_it_be(:prometheus_alert, reload: true) { create(:alert_management_alert, :triggered, :prometheus, project: project, payload: payload) } let(:alert) { generic_alert } 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 diff --git a/spec/services/alert_management/process_prometheus_alert_service_spec.rb b/spec/services/alert_management/process_prometheus_alert_service_spec.rb index ae0b8d6d7ac..2f920de7fc7 100644 --- a/spec/services/alert_management/process_prometheus_alert_service_spec.rb +++ b/spec/services/alert_management/process_prometheus_alert_service_spec.rb @@ -11,9 +11,16 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do describe '#execute' do let(:service) { described_class.new(project, nil, payload) } - let(:incident_management_setting) { double(auto_close_incident?: auto_close_incident, create_issue?: create_issue) } let(:auto_close_incident) { true } let(:create_issue) { true } + let(:send_email) { true } + let(:incident_management_setting) do + double( + auto_close_incident?: auto_close_incident, + create_issue?: create_issue, + send_email?: send_email + ) + end before do allow(service) @@ -55,6 +62,7 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do it_behaves_like 'adds an alert management alert event' it_behaves_like 'processes incident issues' + it_behaves_like 'Alert Notification Service sends notification email' context 'existing alert is resolved' do let!(:alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: fingerprint) } @@ -92,28 +100,48 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do end end - context 'when auto-alert creation is disabled' do + context 'when auto-creation of issues is disabled' do let(:create_issue) { false } it_behaves_like 'does not process incident issues' end + + context 'when emails are disabled' do + let(:send_email) { false } + + it 'does not send notification' do + expect(NotificationService).not_to receive(:new) + + expect(subject).to be_success + end + end end context 'when alert does not exist' do context 'when alert can be created' do it_behaves_like 'creates an alert management alert' + it_behaves_like 'Alert Notification Service sends notification email' + it_behaves_like 'processes incident issues' it 'creates a system note corresponding to alert creation' do expect { subject }.to change(Note, :count).by(1) end - it_behaves_like 'processes incident issues' - context 'when auto-alert creation is disabled' do let(:create_issue) { false } it_behaves_like 'does not process incident issues' end + + context 'when emails are disabled' do + let(:send_email) { false } + + it 'does not send notification' do + expect(NotificationService).not_to receive(:new) + + expect(subject).to be_success + end + end end context 'when alert cannot be created' do @@ -125,6 +153,9 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do allow(service).to receive_message_chain(:alert, :errors).and_return(errors) end + it_behaves_like 'Alert Notification Service sends no notifications', http_status: :bad_request + it_behaves_like 'does not process incident issues due to error', http_status: :bad_request + it 'writes a warning to the log' do expect(Gitlab::AppLogger).to receive(:warn).with( message: 'Unable to create AlertManagement::Alert', @@ -134,8 +165,6 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do execute end - - it_behaves_like 'does not process incident issues' end it { is_expected.to be_success } @@ -148,6 +177,9 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do context 'when auto_resolve_incident set to true' do context 'when status can be changed' do + it_behaves_like 'Alert Notification Service sends notification email' + it_behaves_like 'does not process incident issues' + it 'resolves an existing alert' do expect { execute }.to change { alert.reload.resolved? }.to(true) end @@ -185,6 +217,8 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do execute end + + it_behaves_like 'Alert Notification Service sends notification email' end it { is_expected.to be_success } @@ -197,6 +231,16 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do expect { execute }.not_to change { alert.reload.resolved? } end end + + context 'when emails are disabled' do + let(:send_email) { false } + + it 'does not send notification' do + expect(NotificationService).not_to receive(:new) + + expect(subject).to be_success + end + end end context 'environment given' do diff --git a/spec/services/alert_management/sync_alert_service_data_service_spec.rb b/spec/services/alert_management/sync_alert_service_data_service_spec.rb new file mode 100644 index 00000000000..ecec60011db --- /dev/null +++ b/spec/services/alert_management/sync_alert_service_data_service_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AlertManagement::SyncAlertServiceDataService do + let_it_be(:alerts_service) do + AlertsService.skip_callback(:save, :after, :update_http_integration) + service = create(:alerts_service, :active) + AlertsService.set_callback(:save, :after, :update_http_integration) + + service + end + + describe '#execute' do + subject(:execute) { described_class.new(alerts_service).execute } + + context 'without http integration' do + it 'creates the integration' do + expect { execute } + .to change { AlertManagement::HttpIntegration.count }.by(1) + end + + it 'returns a success' do + expect(subject.success?).to eq(true) + end + end + + context 'existing legacy http integration' do + let_it_be(:integration) { create(:alert_management_http_integration, :legacy, project: alerts_service.project) } + + it 'updates the integration' do + expect { execute } + .to change { integration.reload.encrypted_token }.to(alerts_service.data.encrypted_token) + .and change { integration.encrypted_token_iv }.to(alerts_service.data.encrypted_token_iv) + end + + it 'returns a success' do + expect(subject.success?).to eq(true) + end + end + + context 'existing other http integration' do + let_it_be(:integration) { create(:alert_management_http_integration, project: alerts_service.project) } + + it 'creates the integration' do + expect { execute } + .to change { AlertManagement::HttpIntegration.count }.by(1) + end + + it 'returns a success' do + expect(subject.success?).to eq(true) + end + end + end +end |