2020-04-28 11:09:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe AlertManagement::AlertsFinder, '#execute' do
|
2020-04-28 11:09:29 -04:00
|
|
|
let_it_be(:current_user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
2020-07-21 08:09:30 -04:00
|
|
|
let_it_be(:resolved_alert) { create(:alert_management_alert, :all_fields, :resolved, project: project, ended_at: 1.year.ago, events: 2, severity: :high) }
|
|
|
|
let_it_be(:ignored_alert) { create(:alert_management_alert, :all_fields, :ignored, project: project, events: 1, severity: :critical) }
|
|
|
|
let_it_be(:triggered_alert) { create(:alert_management_alert, :all_fields) }
|
2020-12-10 13:10:16 -05:00
|
|
|
let_it_be(:threat_monitroing_alert) { create(:alert_management_alert, domain: 'threat_monitoring') }
|
|
|
|
|
2020-04-28 11:09:29 -04:00
|
|
|
let(:params) { {} }
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
describe '#execute' do
|
2020-07-03 11:09:13 -04:00
|
|
|
subject(:execute) { described_class.new(current_user, project, params).execute }
|
2020-04-28 11:09:29 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'user is not a developer or above' do
|
|
|
|
it { is_expected.to be_empty }
|
2020-04-28 11:09:29 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'user is developer' do
|
|
|
|
before do
|
|
|
|
project.add_developer(current_user)
|
|
|
|
end
|
2020-04-28 11:09:29 -04:00
|
|
|
|
2020-12-10 13:10:16 -05:00
|
|
|
context 'domain' do
|
|
|
|
context 'domain is threat management' do
|
|
|
|
let(:params) { { domain: 'threat_management' } }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'domain is unknown' do
|
|
|
|
let(:params) { { domain: 'unkown' } }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'domain is missing' do
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
|
|
|
|
end
|
2020-12-21 10:10:05 -05:00
|
|
|
|
|
|
|
context 'skips domain if iid is given' do
|
|
|
|
let(:params) { { iid: resolved_alert.iid, domain: 'threat_monitoring' } }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(resolved_alert) }
|
|
|
|
end
|
2020-12-10 13:10:16 -05:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'empty params' do
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
|
2020-04-28 11:09:29 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'iid given' do
|
2020-07-21 08:09:30 -04:00
|
|
|
let(:params) { { iid: resolved_alert.iid } }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to match_array(resolved_alert) }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'unknown iid' do
|
|
|
|
let(:params) { { iid: 'unknown' } }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
2020-05-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'status given' do
|
2020-10-08 11:08:17 -04:00
|
|
|
let(:params) { { status: :resolved } }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to match_array(resolved_alert) }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'with an array of statuses' do
|
2020-07-21 08:09:30 -04:00
|
|
|
let(:triggered_alert) { create(:alert_management_alert) }
|
2020-10-08 11:08:17 -04:00
|
|
|
let(:params) { { status: [:resolved] } }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to match_array(resolved_alert) }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'with no alerts of status' do
|
2020-10-08 11:08:17 -04:00
|
|
|
let(:params) { { status: :acknowledged } }
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'with an empty status array' do
|
|
|
|
let(:params) { { status: [] } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to match_array([resolved_alert, ignored_alert]) }
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'with an nil status' do
|
|
|
|
let(:params) { { status: nil } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to match_array([resolved_alert, ignored_alert]) }
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
describe 'sorting' do
|
|
|
|
context 'when sorting by created' do
|
|
|
|
context 'sorts alerts ascending' do
|
|
|
|
let(:params) { { sort: 'created_asc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [resolved_alert, ignored_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'sorts alerts descending' do
|
|
|
|
let(:params) { { sort: 'created_desc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [ignored_alert, resolved_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'when sorting by updated' do
|
|
|
|
context 'sorts alerts ascending' do
|
|
|
|
let(:params) { { sort: 'updated_asc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [resolved_alert, ignored_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'sorts alerts descending' do
|
|
|
|
let(:params) { { sort: 'updated_desc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [ignored_alert, resolved_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'when sorting by start time' do
|
|
|
|
context 'sorts alerts ascending' do
|
2020-05-26 20:08:11 -04:00
|
|
|
let(:params) { { sort: 'started_at_asc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [resolved_alert, ignored_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'sorts alerts descending' do
|
2020-05-26 20:08:11 -04:00
|
|
|
let(:params) { { sort: 'started_at_desc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [ignored_alert, resolved_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'when sorting by end time' do
|
|
|
|
context 'sorts alerts ascending' do
|
2020-05-26 20:08:11 -04:00
|
|
|
let(:params) { { sort: 'ended_at_asc' } }
|
2020-05-19 20:08:20 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [resolved_alert, ignored_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'sorts alerts descending' do
|
2020-05-26 20:08:11 -04:00
|
|
|
let(:params) { { sort: 'ended_at_desc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [ignored_alert, resolved_alert] }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'when sorting by events count' do
|
|
|
|
let_it_be(:alert_count_6) { create(:alert_management_alert, project: project, events: 6) }
|
|
|
|
let_it_be(:alert_count_3) { create(:alert_management_alert, project: project, events: 3) }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'sorts alerts ascending' do
|
2020-05-26 20:08:11 -04:00
|
|
|
let(:params) { { sort: 'event_count_asc' } }
|
2020-05-07 02:09:38 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [ignored_alert, resolved_alert, alert_count_3, alert_count_6] }
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'sorts alerts descending' do
|
2020-05-26 20:08:11 -04:00
|
|
|
let(:params) { { sort: 'event_count_desc' } }
|
2020-05-19 20:08:20 -04:00
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it { is_expected.to eq [alert_count_6, alert_count_3, resolved_alert, ignored_alert] }
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'when sorting by severity' do
|
2020-07-03 11:09:13 -04:00
|
|
|
let_it_be(:alert_critical) { create(:alert_management_alert, :critical, project: project) }
|
|
|
|
let_it_be(:alert_high) { create(:alert_management_alert, :high, project: project) }
|
|
|
|
let_it_be(:alert_medium) { create(:alert_management_alert, :medium, project: project) }
|
|
|
|
let_it_be(:alert_low) { create(:alert_management_alert, :low, project: project) }
|
|
|
|
let_it_be(:alert_info) { create(:alert_management_alert, :info, project: project) }
|
|
|
|
let_it_be(:alert_unknown) { create(:alert_management_alert, :unknown, project: project) }
|
|
|
|
|
|
|
|
context 'with ascending sort order' do
|
2020-05-19 20:08:20 -04:00
|
|
|
let(:params) { { sort: 'severity_asc' } }
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
it 'sorts alerts by severity from less critical to more critical' do
|
|
|
|
expect(execute.pluck(:severity).uniq).to eq(%w(unknown info low medium high critical))
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
context 'with descending sort order' do
|
2020-05-19 20:08:20 -04:00
|
|
|
let(:params) { { sort: 'severity_desc' } }
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
it 'sorts alerts by severity from more critical to less critical' do
|
|
|
|
expect(execute.pluck(:severity).uniq).to eq(%w(critical high medium low info unknown))
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-19 20:08:20 -04:00
|
|
|
context 'when sorting by status' do
|
|
|
|
let_it_be(:alert_triggered) { create(:alert_management_alert, project: project) }
|
|
|
|
let_it_be(:alert_acknowledged) { create(:alert_management_alert, :acknowledged, project: project) }
|
|
|
|
let_it_be(:alert_resolved) { create(:alert_management_alert, :resolved, project: project) }
|
|
|
|
let_it_be(:alert_ignored) { create(:alert_management_alert, :ignored, project: project) }
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
context 'with ascending sort order' do
|
2020-05-19 20:08:20 -04:00
|
|
|
let(:params) { { sort: 'status_asc' } }
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
it 'sorts by status: Ignored > Resolved > Acknowledged > Triggered' do
|
2020-10-08 11:08:17 -04:00
|
|
|
expect(execute.map(&:status_name).uniq).to eq([:ignored, :resolved, :acknowledged, :triggered])
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
context 'with descending sort order' do
|
2020-05-19 20:08:20 -04:00
|
|
|
let(:params) { { sort: 'status_desc' } }
|
|
|
|
|
2020-07-03 11:09:13 -04:00
|
|
|
it 'sorts by status: Triggered > Acknowledged > Resolved > Ignored' do
|
2020-10-08 11:08:17 -04:00
|
|
|
expect(execute.map(&:status_name).uniq).to eq([:triggered, :acknowledged, :resolved, :ignored])
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
2020-05-07 02:09:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'search query given' do
|
|
|
|
let_it_be(:alert) do
|
|
|
|
create(:alert_management_alert,
|
|
|
|
:with_fingerprint,
|
|
|
|
project: project,
|
|
|
|
title: 'Title',
|
|
|
|
description: 'Desc',
|
|
|
|
service: 'Service',
|
|
|
|
monitoring_tool: 'Monitor'
|
|
|
|
)
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'searching title' do
|
|
|
|
let(:params) { { search: alert.title } }
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
it { is_expected.to match_array([alert]) }
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'searching description' do
|
|
|
|
let(:params) { { search: alert.description } }
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
it { is_expected.to match_array([alert]) }
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'searching service' do
|
|
|
|
let(:params) { { search: alert.service } }
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
it { is_expected.to match_array([alert]) }
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'searching monitoring tool' do
|
|
|
|
let(:params) { { search: alert.monitoring_tool } }
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
it { is_expected.to match_array([alert]) }
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'searching something else' do
|
|
|
|
let(:params) { { search: alert.fingerprint } }
|
2020-05-18 05:08:12 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
end
|
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
context 'assignee username given' do
|
|
|
|
let_it_be(:assignee) { create(:user) }
|
|
|
|
let_it_be(:alert) { create(:alert_management_alert, project: project, assignees: [assignee]) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
let(:params) { { assignee_username: username } }
|
|
|
|
|
|
|
|
context 'with valid assignee_username' do
|
|
|
|
let(:username) { assignee.username }
|
|
|
|
|
|
|
|
it { is_expected.to match_array([alert]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid assignee_username' do
|
|
|
|
let(:username) { 'unknown username' }
|
|
|
|
|
|
|
|
it { is_expected.to be_empty }
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-28 11:09:29 -04:00
|
|
|
end
|
2020-05-19 20:08:20 -04:00
|
|
|
|
|
|
|
describe '.counts_by_status' do
|
|
|
|
subject { described_class.counts_by_status(current_user, project, params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(current_user)
|
|
|
|
end
|
|
|
|
|
2020-10-08 11:08:17 -04:00
|
|
|
it { is_expected.to match(resolved: 1, ignored: 1) }
|
2020-05-19 20:08:20 -04:00
|
|
|
|
|
|
|
context 'when filtering params are included' do
|
2020-10-08 11:08:17 -04:00
|
|
|
let(:params) { { status: :resolved } }
|
2020-05-19 20:08:20 -04:00
|
|
|
|
2020-10-08 11:08:17 -04:00
|
|
|
it { is_expected.to match(resolved: 1) }
|
2020-05-19 20:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-28 11:09:29 -04:00
|
|
|
end
|