2020-03-19 20:09:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'email_spec'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Emails::Projects do
|
2020-03-19 20:09:29 -04:00
|
|
|
include EmailSpec::Matchers
|
|
|
|
include_context 'gitlab email notification'
|
|
|
|
|
|
|
|
shared_examples 'no email' do
|
|
|
|
it 'does not send mail' do
|
|
|
|
expect(subject.message).to be_a_kind_of(ActionMailer::Base::NullMail)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'shows the incident issues url' do
|
|
|
|
context 'create issue setting enabled' do
|
|
|
|
before do
|
|
|
|
create(:project_incident_management_setting, project: project, create_issue: true)
|
|
|
|
end
|
|
|
|
|
2020-12-08 04:09:41 -05:00
|
|
|
let(:incidents_url) { project_incidents_url(project) }
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-12-08 04:09:41 -05:00
|
|
|
it { is_expected.to have_body_text(incidents_url) }
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe '#prometheus_alert_fired_email' do
|
2020-09-22 08:09:39 -04:00
|
|
|
let(:default_title) { Gitlab::AlertManagement::Payload::Generic::DEFAULT_TITLE }
|
|
|
|
let(:payload) { { 'startsAt' => Time.now.rfc3339 } }
|
2020-11-03 13:09:22 -05:00
|
|
|
let(:alert) { create(:alert_management_alert, :from_payload, payload: payload, project: project) }
|
2020-09-22 08:09:39 -04:00
|
|
|
|
2020-03-19 20:09:29 -04:00
|
|
|
subject do
|
2020-11-03 13:09:22 -05:00
|
|
|
Notify.prometheus_alert_fired_email(project, user, alert)
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
2021-07-29 17:10:10 -04:00
|
|
|
it_behaves_like 'an email with X-GitLab headers containing project details'
|
|
|
|
|
|
|
|
it 'has expected X-GitLab alert headers', :aggregate_failures do
|
|
|
|
is_expected.to have_header('X-GitLab-Alert-ID', /#{alert.id}/)
|
|
|
|
is_expected.to have_header('X-GitLab-Alert-IID', /#{alert.iid}/)
|
|
|
|
is_expected.to have_header('X-GitLab-NotificationReason', "alert_#{alert.state}")
|
|
|
|
|
|
|
|
is_expected.not_to have_header('X-GitLab-Incident-ID', /.+/)
|
|
|
|
is_expected.not_to have_header('X-GitLab-Incident-IID', /.+/)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with incident' do
|
2021-08-02 08:09:59 -04:00
|
|
|
let(:alert) { create(:alert_management_alert, :with_incident, :from_payload, payload: payload, project: project) }
|
2021-07-29 17:10:10 -04:00
|
|
|
let(:incident) { alert.issue }
|
|
|
|
|
|
|
|
it 'has expected X-GitLab incident headers', :aggregate_failures do
|
|
|
|
is_expected.to have_header('X-GitLab-Incident-ID', /#{incident.id}/)
|
|
|
|
is_expected.to have_header('X-GitLab-Incident-IID', /#{incident.iid}/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 13:09:22 -05:00
|
|
|
context 'with empty payload' do
|
2020-09-22 08:09:39 -04:00
|
|
|
let(:payload) { {} }
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
it_behaves_like 'an email sent from GitLab'
|
|
|
|
it_behaves_like 'it should not have Gmail Actions links'
|
|
|
|
it_behaves_like 'a user cannot unsubscribe through footer link'
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
it 'has expected subject' do
|
|
|
|
is_expected.to have_subject("#{project.name} | Alert: #{default_title}")
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
it 'has expected content' do
|
|
|
|
is_expected.to have_body_text('An alert has been triggered')
|
|
|
|
is_expected.to have_body_text(project.full_path)
|
2020-11-03 13:09:22 -05:00
|
|
|
is_expected.to have_body_text(alert.details_url)
|
2020-09-22 08:09:39 -04:00
|
|
|
is_expected.not_to have_body_text('Description:')
|
|
|
|
is_expected.not_to have_body_text('Environment:')
|
|
|
|
is_expected.not_to have_body_text('Metric:')
|
|
|
|
end
|
|
|
|
end
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
context 'with description' do
|
|
|
|
let(:payload) { { 'description' => 'alert description' } }
|
2020-03-19 20:09:29 -04:00
|
|
|
|
|
|
|
it_behaves_like 'an email sent from GitLab'
|
|
|
|
it_behaves_like 'it should not have Gmail Actions links'
|
|
|
|
it_behaves_like 'a user cannot unsubscribe through footer link'
|
|
|
|
|
|
|
|
it 'has expected subject' do
|
2020-09-22 08:09:39 -04:00
|
|
|
is_expected.to have_subject("#{project.name} | Alert: #{default_title}")
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has expected content' do
|
|
|
|
is_expected.to have_body_text('An alert has been triggered')
|
|
|
|
is_expected.to have_body_text(project.full_path)
|
2020-11-03 13:09:22 -05:00
|
|
|
is_expected.to have_body_text(alert.details_url)
|
2020-09-22 08:09:39 -04:00
|
|
|
is_expected.to have_body_text('Description:')
|
|
|
|
is_expected.to have_body_text('alert description')
|
|
|
|
is_expected.not_to have_body_text('Environment:')
|
|
|
|
is_expected.not_to have_body_text('Metric:')
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
context 'with environment' do
|
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
2021-04-08 23:09:05 -04:00
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
let(:payload) { { 'gitlab_environment_name' => environment.name } }
|
|
|
|
let(:metrics_url) { metrics_project_environment_url(project, environment) }
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
it_behaves_like 'an email sent from GitLab'
|
|
|
|
it_behaves_like 'it should not have Gmail Actions links'
|
|
|
|
it_behaves_like 'a user cannot unsubscribe through footer link'
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
it 'has expected subject' do
|
|
|
|
is_expected.to have_subject("#{project.name} | Alert: #{environment.name}: #{default_title}")
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
it 'has expected content' do
|
|
|
|
is_expected.to have_body_text('An alert has been triggered')
|
|
|
|
is_expected.to have_body_text(project.full_path)
|
2020-11-03 13:09:22 -05:00
|
|
|
is_expected.to have_body_text(alert.details_url)
|
2020-09-22 08:09:39 -04:00
|
|
|
is_expected.to have_body_text('Environment:')
|
|
|
|
is_expected.to have_body_text(environment.name)
|
|
|
|
is_expected.not_to have_body_text('Description:')
|
|
|
|
is_expected.not_to have_body_text('Metric:')
|
|
|
|
end
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
context 'with gitlab alerting rule' do
|
|
|
|
let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) }
|
|
|
|
let_it_be(:environment) { prometheus_alert.environment }
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-11-03 13:09:22 -05:00
|
|
|
let(:alert) { create(:alert_management_alert, :prometheus, :from_payload, payload: payload, project: project) }
|
2020-09-22 08:09:39 -04:00
|
|
|
let(:title) { "#{prometheus_alert.title} #{prometheus_alert.computed_operator} #{prometheus_alert.threshold}" }
|
|
|
|
let(:metrics_url) { metrics_project_environment_url(project, environment) }
|
2020-03-19 20:09:29 -04:00
|
|
|
|
|
|
|
before do
|
2020-09-22 08:09:39 -04:00
|
|
|
payload['labels'] = {
|
|
|
|
'gitlab_alert_id' => prometheus_alert.prometheus_metric_id,
|
|
|
|
'alertname' => prometheus_alert.title
|
|
|
|
}
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an email sent from GitLab'
|
|
|
|
it_behaves_like 'it should not have Gmail Actions links'
|
|
|
|
it_behaves_like 'a user cannot unsubscribe through footer link'
|
2020-09-22 08:09:39 -04:00
|
|
|
it_behaves_like 'shows the incident issues url'
|
2020-03-19 20:09:29 -04:00
|
|
|
|
|
|
|
it 'has expected subject' do
|
2020-09-22 08:09:39 -04:00
|
|
|
is_expected.to have_subject("#{project.name} | Alert: #{environment.name}: #{title} for 5 minutes")
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has expected content' do
|
|
|
|
is_expected.to have_body_text('An alert has been triggered')
|
|
|
|
is_expected.to have_body_text(project.full_path)
|
2020-11-03 13:09:22 -05:00
|
|
|
is_expected.to have_body_text(alert.details_url)
|
2020-09-22 08:09:39 -04:00
|
|
|
is_expected.to have_body_text('Environment:')
|
|
|
|
is_expected.to have_body_text(environment.name)
|
|
|
|
is_expected.to have_body_text('Metric:')
|
|
|
|
is_expected.to have_body_text(prometheus_alert.full_query)
|
|
|
|
is_expected.to have_body_text(metrics_url)
|
2020-03-19 20:09:29 -04:00
|
|
|
is_expected.not_to have_body_text('Description:')
|
|
|
|
end
|
|
|
|
end
|
2020-11-03 13:09:22 -05:00
|
|
|
|
|
|
|
context 'resolved' do
|
|
|
|
let_it_be(:alert) { create(:alert_management_alert, :resolved, project: project) }
|
|
|
|
|
|
|
|
it_behaves_like 'an email sent from GitLab'
|
|
|
|
it_behaves_like 'it should not have Gmail Actions links'
|
|
|
|
it_behaves_like 'a user cannot unsubscribe through footer link'
|
|
|
|
|
|
|
|
it 'has expected subject' do
|
|
|
|
is_expected.to have_subject("#{project.name} | Alert: #{alert.title}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has expected content' do
|
|
|
|
is_expected.to have_body_text('An alert has been resolved')
|
|
|
|
is_expected.to have_body_text(project.full_path)
|
|
|
|
is_expected.to have_body_text(alert.details_url)
|
|
|
|
end
|
|
|
|
end
|
2020-03-19 20:09:29 -04:00
|
|
|
end
|
|
|
|
end
|