summaryrefslogtreecommitdiff
path: root/spec/finders
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-06 21:08:48 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-06 21:08:48 +0000
commita89cb5cbdd832d4d9e80517973aceda6bc0a3856 (patch)
tree574475bd0901a2f8906d36a4728b8bbb95b41e1c /spec/finders
parent0d6fa033121a9bef708b8f2de186c4034c61d4a3 (diff)
downloadgitlab-ce-a89cb5cbdd832d4d9e80517973aceda6bc0a3856.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/finders')
-rw-r--r--spec/finders/concerns/finder_with_cross_project_access_spec.rb2
-rw-r--r--spec/finders/projects/prometheus/alerts_finder_spec.rb169
-rw-r--r--spec/finders/protected_branches_finder_spec.rb37
3 files changed, 207 insertions, 1 deletions
diff --git a/spec/finders/concerns/finder_with_cross_project_access_spec.rb b/spec/finders/concerns/finder_with_cross_project_access_spec.rb
index 6ba98b79176..f3365309b05 100644
--- a/spec/finders/concerns/finder_with_cross_project_access_spec.rb
+++ b/spec/finders/concerns/finder_with_cross_project_access_spec.rb
@@ -128,7 +128,7 @@ describe FinderWithCrossProjectAccess do
end
end
- context '.finder_model' do
+ describe '.finder_model' do
it 'is set correctly' do
expect(finder_class.finder_model).to eq(Project)
end
diff --git a/spec/finders/projects/prometheus/alerts_finder_spec.rb b/spec/finders/projects/prometheus/alerts_finder_spec.rb
new file mode 100644
index 00000000000..bb59e77cca8
--- /dev/null
+++ b/spec/finders/projects/prometheus/alerts_finder_spec.rb
@@ -0,0 +1,169 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Projects::Prometheus::AlertsFinder do
+ let(:finder) { described_class.new(params) }
+ let(:params) { {} }
+
+ describe 'with params' do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:other_project) { create(:project) }
+ let_it_be(:other_env) { create(:environment, project: other_project) }
+ let_it_be(:production) { create(:environment, project: project) }
+ let_it_be(:staging) { create(:environment, project: project) }
+ let_it_be(:alert) { create_alert(project, production) }
+ let_it_be(:alert2) { create_alert(project, production) }
+ let_it_be(:stg_alert) { create_alert(project, staging) }
+ let_it_be(:other_alert) { create_alert(other_project, other_env) }
+
+ describe '#execute' do
+ subject { finder.execute }
+
+ context 'with project' do
+ before do
+ params[:project] = project
+ end
+
+ it { is_expected.to eq([alert, alert2, stg_alert]) }
+
+ context 'with matching metric' do
+ before do
+ params[:metric] = alert.prometheus_metric
+ end
+
+ it { is_expected.to eq([alert]) }
+ end
+
+ context 'with matching metric id' do
+ before do
+ params[:metric] = alert.prometheus_metric_id
+ end
+
+ it { is_expected.to eq([alert]) }
+ end
+
+ context 'with project non-specific metric' do
+ before do
+ params[:metric] = other_alert.prometheus_metric
+ end
+
+ it { is_expected.to be_empty }
+ end
+ end
+
+ context 'with environment' do
+ before do
+ params[:environment] = production
+ end
+
+ it { is_expected.to eq([alert, alert2]) }
+
+ context 'with matching metric' do
+ before do
+ params[:metric] = alert.prometheus_metric
+ end
+
+ it { is_expected.to eq([alert]) }
+ end
+
+ context 'with environment non-specific metric' do
+ before do
+ params[:metric] = stg_alert.prometheus_metric
+ end
+
+ it { is_expected.to be_empty }
+ end
+ end
+
+ context 'with matching project and environment' do
+ before do
+ params[:project] = project
+ params[:environment] = production
+ end
+
+ it { is_expected.to eq([alert, alert2]) }
+
+ context 'with matching metric' do
+ before do
+ params[:metric] = alert.prometheus_metric
+ end
+
+ it { is_expected.to eq([alert]) }
+ end
+
+ context 'with environment non-specific metric' do
+ before do
+ params[:metric] = stg_alert.prometheus_metric
+ end
+
+ it { is_expected.to be_empty }
+ end
+
+ context 'with matching id' do
+ before do
+ params[:id] = alert.id
+ end
+
+ it { is_expected.to eq([alert]) }
+ end
+
+ context 'with a nil id' do
+ before do
+ params[:id] = nil
+ end
+
+ it { is_expected.to eq([alert, alert2]) }
+ end
+ end
+
+ context 'with non-matching project-environment pair' do
+ before do
+ params[:project] = project
+ params[:environment] = other_env
+ end
+
+ it { is_expected.to be_empty }
+ end
+
+ context 'with id' do
+ before do
+ params[:id] = alert.id
+ end
+
+ it { is_expected.to eq([alert]) }
+ end
+
+ context 'with multiple ids' do
+ before do
+ params[:id] = [alert.id, other_alert.id]
+ end
+
+ it { is_expected.to eq([alert, other_alert]) }
+ end
+
+ context 'with non-matching id' do
+ before do
+ params[:id] = -5
+ end
+
+ it { is_expected.to be_empty }
+ end
+ end
+
+ private
+
+ def create_alert(project, environment)
+ create(:prometheus_alert, project: project, environment: environment)
+ end
+ end
+
+ describe 'without params' do
+ subject { finder }
+
+ it 'raises an error' do
+ expect { subject }
+ .to raise_error(ArgumentError, 'Please provide one or more of the following params: :project, :environment, :id')
+ end
+ end
+end
diff --git a/spec/finders/protected_branches_finder_spec.rb b/spec/finders/protected_branches_finder_spec.rb
new file mode 100644
index 00000000000..e6a2cf4577c
--- /dev/null
+++ b/spec/finders/protected_branches_finder_spec.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe ProtectedBranchesFinder do
+ let(:project) { create(:project) }
+ let!(:protected_branch) { create(:protected_branch, project: project) }
+ let!(:another_protected_branch) { create(:protected_branch, project: project) }
+ let!(:other_protected_branch) { create(:protected_branch) }
+ let(:params) { {} }
+
+ describe '#execute' do
+ subject { described_class.new(project, params).execute }
+
+ it 'returns all protected branches of project by default' do
+ expect(subject).to match_array([protected_branch, another_protected_branch])
+ end
+
+ context 'when search param is present' do
+ let(:params) { { search: protected_branch.name } }
+
+ it 'filters by search param' do
+ expect(subject).to eq([protected_branch])
+ end
+ end
+
+ context 'when there are more protected branches than the limit' do
+ before do
+ stub_const("#{described_class}::LIMIT", 1)
+ end
+
+ it 'returns limited protected branches of project' do
+ expect(subject).to eq([another_protected_branch])
+ end
+ end
+ end
+end