summaryrefslogtreecommitdiff
path: root/spec/finders
diff options
context:
space:
mode:
Diffstat (limited to 'spec/finders')
-rw-r--r--spec/finders/alert_management/alerts_finder_spec.rb115
-rw-r--r--spec/finders/ci/pipelines_for_merge_request_finder_spec.rb2
-rw-r--r--spec/finders/environment_names_finder_spec.rb63
-rw-r--r--spec/finders/group_labels_finder_spec.rb42
-rw-r--r--spec/finders/groups_finder_spec.rb56
-rw-r--r--spec/finders/issues_finder_spec.rb10
-rw-r--r--spec/finders/labels_finder_spec.rb36
-rw-r--r--spec/finders/merge_requests/by_approvals_finder_spec.rb82
-rw-r--r--spec/finders/merge_requests_finder_spec.rb87
-rw-r--r--spec/finders/packages/generic/package_finder_spec.rb31
-rw-r--r--spec/finders/projects_finder_spec.rb32
-rw-r--r--spec/finders/releases_finder_spec.rb28
12 files changed, 470 insertions, 114 deletions
diff --git a/spec/finders/alert_management/alerts_finder_spec.rb b/spec/finders/alert_management/alerts_finder_spec.rb
index 926446b31d5..e74f3ac68ed 100644
--- a/spec/finders/alert_management/alerts_finder_spec.rb
+++ b/spec/finders/alert_management/alerts_finder_spec.rb
@@ -39,19 +39,19 @@ RSpec.describe AlertManagement::AlertsFinder, '#execute' do
end
context 'status given' do
- let(:params) { { status: AlertManagement::Alert::STATUSES[:resolved] } }
+ let(:params) { { status: :resolved } }
it { is_expected.to match_array(resolved_alert) }
context 'with an array of statuses' do
let(:triggered_alert) { create(:alert_management_alert) }
- let(:params) { { status: [AlertManagement::Alert::STATUSES[:resolved]] } }
+ let(:params) { { status: [:resolved] } }
it { is_expected.to match_array(resolved_alert) }
end
context 'with no alerts of status' do
- let(:params) { { status: AlertManagement::Alert::STATUSES[:acknowledged] } }
+ let(:params) { { status: :acknowledged } }
it { is_expected.to be_empty }
end
@@ -169,12 +169,6 @@ RSpec.describe AlertManagement::AlertsFinder, '#execute' do
end
context 'when sorting by status' do
- let(:statuses) { AlertManagement::Alert::STATUSES }
- let(:triggered) { statuses[:triggered] }
- let(:acknowledged) { statuses[:acknowledged] }
- let(:resolved) { statuses[:resolved] }
- let(:ignored) { statuses[:ignored] }
-
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) }
@@ -184,7 +178,7 @@ RSpec.describe AlertManagement::AlertsFinder, '#execute' do
let(:params) { { sort: 'status_asc' } }
it 'sorts by status: Ignored > Resolved > Acknowledged > Triggered' do
- expect(execute.map(&:status).uniq).to eq([ignored, resolved, acknowledged, triggered])
+ expect(execute.map(&:status_name).uniq).to eq([:ignored, :resolved, :acknowledged, :triggered])
end
end
@@ -192,64 +186,83 @@ RSpec.describe AlertManagement::AlertsFinder, '#execute' do
let(:params) { { sort: 'status_desc' } }
it 'sorts by status: Triggered > Acknowledged > Resolved > Ignored' do
- expect(execute.map(&:status).uniq).to eq([triggered, acknowledged, resolved, ignored])
+ expect(execute.map(&:status_name).uniq).to eq([:triggered, :acknowledged, :resolved, :ignored])
end
end
end
end
- end
- context 'search query given' do
- let_it_be(:alert) do
- create(:alert_management_alert,
- :with_fingerprint,
- title: 'Title',
- description: 'Desc',
- service: 'Service',
- monitoring_tool: 'Monitor'
- )
- end
+ 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
- before do
- alert.project.add_developer(current_user)
- end
+ context 'searching title' do
+ let(:params) { { search: alert.title } }
- subject { described_class.new(current_user, alert.project, params).execute }
+ it { is_expected.to match_array([alert]) }
+ end
- context 'searching title' do
- let(:params) { { search: alert.title } }
+ context 'searching description' do
+ let(:params) { { search: alert.description } }
- it { is_expected.to match_array([alert]) }
- end
+ it { is_expected.to match_array([alert]) }
+ end
- context 'searching description' do
- let(:params) { { search: alert.description } }
+ context 'searching service' do
+ let(:params) { { search: alert.service } }
- it { is_expected.to match_array([alert]) }
- end
+ it { is_expected.to match_array([alert]) }
+ end
- context 'searching service' do
- let(:params) { { search: alert.service } }
+ context 'searching monitoring tool' do
+ let(:params) { { search: alert.monitoring_tool } }
- it { is_expected.to match_array([alert]) }
- end
+ it { is_expected.to match_array([alert]) }
+ end
- context 'searching monitoring tool' do
- let(:params) { { search: alert.monitoring_tool } }
+ context 'searching something else' do
+ let(:params) { { search: alert.fingerprint } }
- it { is_expected.to match_array([alert]) }
- end
+ it { is_expected.to be_empty }
+ end
- context 'searching something else' do
- let(:params) { { search: alert.fingerprint } }
+ context 'empty search' do
+ let(:params) { { search: ' ' } }
- it { is_expected.to be_empty }
+ it { is_expected.not_to include(alert) }
+ end
end
- context 'empty search' do
- let(:params) { { search: ' ' } }
+ context 'assignee username given' do
+ let_it_be(:assignee) { create(:user) }
+ let_it_be(:alert) { create(:alert_management_alert, project: project, assignees: [assignee]) }
+ let(:params) { { assignee_username: username } }
+
+ context 'with valid assignee_username' do
+ let(:username) { assignee.username }
+
+ it { is_expected.to match_array([alert]) }
+ end
- it { is_expected.to match_array([alert]) }
+ context 'with invalid assignee_username' do
+ let(:username) { 'unknown username' }
+
+ it { is_expected.to be_empty }
+ end
+
+ context 'with empty assignee_username' do
+ let(:username) { ' ' }
+
+ it { is_expected.not_to include(alert) }
+ end
end
end
end
@@ -261,12 +274,12 @@ RSpec.describe AlertManagement::AlertsFinder, '#execute' do
project.add_developer(current_user)
end
- it { is_expected.to match({ 2 => 1, 3 => 1 }) } # one resolved and one ignored
+ it { is_expected.to match(resolved: 1, ignored: 1) }
context 'when filtering params are included' do
- let(:params) { { status: AlertManagement::Alert::STATUSES[:resolved] } }
+ let(:params) { { status: :resolved } }
- it { is_expected.to match({ 2 => 1 }) } # one resolved
+ it { is_expected.to match(resolved: 1) }
end
end
end
diff --git a/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb b/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb
index 196fde5efe0..65f6dc0ba74 100644
--- a/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb
+++ b/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb
@@ -122,7 +122,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do
end
context 'with unsaved merge request' do
- let(:merge_request) { build(:merge_request) }
+ let(:merge_request) { build(:merge_request, source_project: create(:project, :repository)) }
let!(:pipeline) do
create(:ci_empty_pipeline, project: project,
diff --git a/spec/finders/environment_names_finder_spec.rb b/spec/finders/environment_names_finder_spec.rb
new file mode 100644
index 00000000000..9244e4fb369
--- /dev/null
+++ b/spec/finders/environment_names_finder_spec.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe EnvironmentNamesFinder do
+ describe '#execute' do
+ let!(:group) { create(:group) }
+ let!(:project1) { create(:project, :public, namespace: group) }
+ let!(:project2) { create(:project, :private, namespace: group) }
+ let!(:user) { create(:user) }
+
+ before do
+ create(:environment, name: 'gstg', project: project1)
+ create(:environment, name: 'gprd', project: project1)
+ create(:environment, name: 'gprd', project: project2)
+ create(:environment, name: 'gcny', project: project2)
+ end
+
+ context 'using a group and a group member' do
+ it 'returns environment names for all projects' do
+ group.add_developer(user)
+
+ names = described_class.new(group, user).execute
+
+ expect(names).to eq(%w[gcny gprd gstg])
+ end
+ end
+
+ context 'using a group and a guest' do
+ it 'returns environment names for all public projects' do
+ names = described_class.new(group, user).execute
+
+ expect(names).to eq(%w[gprd gstg])
+ end
+ end
+
+ context 'using a public project and a project member' do
+ it 'returns all the unique environment names' do
+ project1.team.add_developer(user)
+
+ names = described_class.new(project1, user).execute
+
+ expect(names).to eq(%w[gprd gstg])
+ end
+ end
+
+ context 'using a public project and a guest' do
+ it 'returns all the unique environment names' do
+ names = described_class.new(project1, user).execute
+
+ expect(names).to eq(%w[gprd gstg])
+ end
+ end
+
+ context 'using a private project and a guest' do
+ it 'returns all the unique environment names' do
+ names = described_class.new(project2, user).execute
+
+ expect(names).to be_empty
+ end
+ end
+ end
+end
diff --git a/spec/finders/group_labels_finder_spec.rb b/spec/finders/group_labels_finder_spec.rb
deleted file mode 100644
index d65a8fb4fed..00000000000
--- a/spec/finders/group_labels_finder_spec.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-RSpec.describe GroupLabelsFinder, '#execute' do
- let!(:group) { create(:group) }
- let!(:user) { create(:user) }
- let!(:label1) { create(:group_label, title: 'Foo', description: 'Lorem ipsum', group: group) }
- let!(:label2) { create(:group_label, title: 'Bar', description: 'Fusce consequat', group: group) }
-
- it 'returns all group labels sorted by name if no params' do
- result = described_class.new(user, group).execute
-
- expect(result.to_a).to match_array([label2, label1])
- end
-
- it 'returns all group labels sorted by name desc' do
- result = described_class.new(user, group, sort: 'name_desc').execute
-
- expect(result.to_a).to match_array([label2, label1])
- end
-
- it 'returns group labels that match search' do
- result = described_class.new(user, group, search: 'Foo').execute
-
- expect(result.to_a).to match_array([label1])
- end
-
- it 'returns group labels user subscribed to' do
- label2.subscribe(user)
-
- result = described_class.new(user, group, subscribed: 'true').execute
-
- expect(result.to_a).to match_array([label2])
- end
-
- it 'returns second page of labels' do
- result = described_class.new(user, group, page: '2').execute
-
- expect(result.to_a).to match_array([])
- end
-end
diff --git a/spec/finders/groups_finder_spec.rb b/spec/finders/groups_finder_spec.rb
index 48e4c5dadc9..c9e9328794e 100644
--- a/spec/finders/groups_finder_spec.rb
+++ b/spec/finders/groups_finder_spec.rb
@@ -161,5 +161,61 @@ RSpec.describe GroupsFinder do
end
end
end
+
+ context 'with include parent group descendants' do
+ let_it_be(:user) { create(:user) }
+ let_it_be(:parent_group) { create(:group, :public) }
+ let_it_be(:public_subgroup) { create(:group, :public, parent: parent_group) }
+ let_it_be(:internal_sub_subgroup) { create(:group, :internal, parent: public_subgroup) }
+ let_it_be(:private_sub_subgroup) { create(:group, :private, parent: public_subgroup) }
+ let_it_be(:public_sub_subgroup) { create(:group, :public, parent: public_subgroup) }
+ let(:params) { { include_parent_descendants: true, parent: parent_group } }
+
+ context 'with nil parent' do
+ it 'returns all accessible groups' do
+ params[:parent] = nil
+ expect(described_class.new(user, params).execute).to contain_exactly(
+ parent_group,
+ public_subgroup,
+ internal_sub_subgroup,
+ public_sub_subgroup
+ )
+ end
+ end
+
+ context 'without a user' do
+ it 'only returns the group public descendants' do
+ expect(described_class.new(nil, params).execute).to contain_exactly(
+ public_subgroup,
+ public_sub_subgroup
+ )
+ end
+ end
+
+ context 'when a user is present' do
+ it 'returns the group public and internal descendants' do
+ expect(described_class.new(user, params).execute).to contain_exactly(
+ public_subgroup,
+ public_sub_subgroup,
+ internal_sub_subgroup
+ )
+ end
+ end
+
+ context 'when a parent group member is present' do
+ before do
+ parent_group.add_developer(user)
+ end
+
+ it 'returns all group descendants' do
+ expect(described_class.new(user, params).execute).to contain_exactly(
+ public_subgroup,
+ public_sub_subgroup,
+ internal_sub_subgroup,
+ private_sub_subgroup
+ )
+ end
+ end
+ end
end
end
diff --git a/spec/finders/issues_finder_spec.rb b/spec/finders/issues_finder_spec.rb
index dbf5abe64a5..21bc03011c3 100644
--- a/spec/finders/issues_finder_spec.rb
+++ b/spec/finders/issues_finder_spec.rb
@@ -843,6 +843,16 @@ RSpec.describe IssuesFinder do
expect(finder.row_count).to be_zero
end
+
+ it 'returns -1 if the query times out' do
+ finder = described_class.new(admin)
+
+ expect_next_instance_of(described_class) do |subfinder|
+ expect(subfinder).to receive(:execute).and_raise(ActiveRecord::QueryCanceled)
+ end
+
+ expect(finder.row_count).to eq(-1)
+ end
end
describe '#with_confidentiality_access_check' do
diff --git a/spec/finders/labels_finder_spec.rb b/spec/finders/labels_finder_spec.rb
index 851b9e64db6..e344591dd5d 100644
--- a/spec/finders/labels_finder_spec.rb
+++ b/spec/finders/labels_finder_spec.rb
@@ -42,7 +42,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user)
- expect(finder.execute).to eq [group_label_2, group_label_3, project_label_1, group_label_1, project_label_2, project_label_4]
+ expect(finder.execute).to match_array([group_label_2, group_label_3, project_label_1, group_label_1, project_label_2, project_label_4])
end
it 'returns labels available if nil title is supplied' do
@@ -50,7 +50,7 @@ RSpec.describe LabelsFinder do
# params[:title] will return `nil` regardless whether it is specified
finder = described_class.new(user, title: nil)
- expect(finder.execute).to eq [group_label_2, group_label_3, project_label_1, group_label_1, project_label_2, project_label_4]
+ expect(finder.execute).to match_array([group_label_2, group_label_3, project_label_1, group_label_1, project_label_2, project_label_4])
end
end
@@ -60,7 +60,7 @@ RSpec.describe LabelsFinder do
::Projects::UpdateService.new(project_1, user, archived: true).execute
finder = described_class.new(user, **group_params(group_1))
- expect(finder.execute).to eq [group_label_2, group_label_1, project_label_5]
+ expect(finder.execute).to match_array([group_label_2, group_label_1, project_label_5])
end
context 'when only_group_labels is true' do
@@ -69,7 +69,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user, only_group_labels: true, **group_params(group_1))
- expect(finder.execute).to eq [group_label_2, group_label_1]
+ expect(finder.execute).to match_array([group_label_2, group_label_1])
end
end
@@ -86,7 +86,7 @@ RSpec.describe LabelsFinder do
it 'returns group labels' do
finder = described_class.new(user, **group_params(empty_group))
- expect(finder.execute).to eq [empty_group_label_1, empty_group_label_2]
+ expect(finder.execute).to match_array([empty_group_label_1, empty_group_label_2])
end
end
end
@@ -98,7 +98,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user, **group_params(private_subgroup_1), only_group_labels: true, include_ancestor_groups: true)
- expect(finder.execute).to eq [private_group_label_1, private_subgroup_label_1]
+ expect(finder.execute).to match_array([private_group_label_1, private_subgroup_label_1])
end
it 'ignores labels from groups which user can not read' do
@@ -106,7 +106,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user, **group_params(private_subgroup_1), only_group_labels: true, include_ancestor_groups: true)
- expect(finder.execute).to eq [private_subgroup_label_1]
+ expect(finder.execute).to match_array([private_subgroup_label_1])
end
end
@@ -117,7 +117,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user, **group_params(private_group_1), only_group_labels: true, include_descendant_groups: true)
- expect(finder.execute).to eq [private_group_label_1, private_subgroup_label_1]
+ expect(finder.execute).to match_array([private_group_label_1, private_subgroup_label_1])
end
it 'ignores labels from groups which user can not read' do
@@ -125,7 +125,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user, **group_params(private_group_1), only_group_labels: true, include_descendant_groups: true)
- expect(finder.execute).to eq [private_subgroup_label_1]
+ expect(finder.execute).to match_array([private_subgroup_label_1])
end
end
@@ -140,13 +140,13 @@ RSpec.describe LabelsFinder do
shared_examples 'with full visibility' do
it 'returns all projects labels' do
- expect(finder.execute).to eq [group_label_1, limited_visibility_label, visible_label]
+ expect(finder.execute).to match_array([group_label_1, limited_visibility_label, visible_label])
end
end
shared_examples 'with limited visibility' do
it 'returns only authorized projects labels' do
- expect(finder.execute).to eq [group_label_1, visible_label]
+ expect(finder.execute).to match_array([group_label_1, visible_label])
end
end
@@ -249,7 +249,7 @@ RSpec.describe LabelsFinder do
it 'returns labels available for the project' do
finder = described_class.new(user, project_id: project_1.id)
- expect(finder.execute).to eq [group_label_2, project_label_1, group_label_1]
+ expect(finder.execute).to match_array([group_label_2, project_label_1, group_label_1])
end
context 'as an administrator' do
@@ -272,13 +272,13 @@ RSpec.describe LabelsFinder do
it 'returns label with that title' do
finder = described_class.new(user, title: 'Group Label 2')
- expect(finder.execute).to eq [group_label_2]
+ expect(finder.execute).to match_array([group_label_2])
end
it 'returns label with title alias' do
finder = described_class.new(user, name: 'Group Label 2')
- expect(finder.execute).to eq [group_label_2]
+ expect(finder.execute).to match_array([group_label_2])
end
it 'returns no labels if empty title is supplied' do
@@ -304,19 +304,19 @@ RSpec.describe LabelsFinder do
it 'returns labels with a partially matching title' do
finder = described_class.new(user, search: '(group)')
- expect(finder.execute).to eq [group_label_1]
+ expect(finder.execute).to match_array([group_label_1])
end
it 'returns labels with a partially matching description' do
finder = described_class.new(user, search: 'awesome')
- expect(finder.execute).to eq [project_label_1]
+ expect(finder.execute).to match_array([project_label_1])
end
it 'returns labels matching a single character' do
finder = described_class.new(user, search: '(')
- expect(finder.execute).to eq [group_label_1]
+ expect(finder.execute).to match_array([group_label_1])
end
end
@@ -326,7 +326,7 @@ RSpec.describe LabelsFinder do
finder = described_class.new(user, subscribed: 'true')
- expect(finder.execute).to eq [project_label_1]
+ expect(finder.execute).to match_array([project_label_1])
end
end
diff --git a/spec/finders/merge_requests/by_approvals_finder_spec.rb b/spec/finders/merge_requests/by_approvals_finder_spec.rb
new file mode 100644
index 00000000000..0e1856879f1
--- /dev/null
+++ b/spec/finders/merge_requests/by_approvals_finder_spec.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe MergeRequests::ByApprovalsFinder do
+ let_it_be(:first_user) { create(:user) }
+ let_it_be(:second_user) { create(:user) }
+ let(:third_user) { create(:user) }
+
+ let_it_be(:merge_request_without_approvals) { create(:merge_request) }
+ let_it_be(:merge_request_with_first_user_approval) do
+ create(:merge_request).tap do |mr|
+ create(:approval, merge_request: mr, user: first_user)
+ end
+ end
+ let_it_be(:merge_request_with_both_approvals) do
+ create(:merge_request).tap do |mr|
+ create(:approval, merge_request: mr, user: first_user)
+ create(:approval, merge_request: mr, user: second_user)
+ end
+ end
+
+ def merge_requests(ids: nil, names: [])
+ described_class.new(names, ids).execute(MergeRequest.all)
+ end
+
+ context 'filter by no approvals' do
+ it 'returns merge requests without approvals' do
+ expected_result = [merge_request_without_approvals]
+
+ expect(merge_requests(ids: 'None')).to match_array(expected_result)
+ expect(merge_requests(names: ['None'])).to match_array(expected_result)
+ end
+ end
+
+ context 'filter by any approvals' do
+ it 'returns merge requests approved by at least one user' do
+ expected_result = [merge_request_with_first_user_approval, merge_request_with_both_approvals]
+
+ expect(merge_requests(ids: 'Any')).to match_array(expected_result)
+ expect(merge_requests(names: ['Any'])).to match_array(expected_result)
+ end
+ end
+
+ context 'filter by specific user approval' do
+ it 'returns merge requests approved by specific user' do
+ expected_result = [merge_request_with_first_user_approval, merge_request_with_both_approvals]
+
+ expect(merge_requests(ids: [first_user.id])).to match_array(expected_result)
+ expect(merge_requests(names: [first_user.username])).to match_array(expected_result)
+ end
+ end
+
+ context 'filter by multiple user approval' do
+ it 'returns merge requests approved by both users' do
+ expected_result = [merge_request_with_both_approvals]
+
+ expect(merge_requests(ids: [first_user.id, second_user.id])).to match_array(expected_result)
+ expect(merge_requests(names: [first_user.username, second_user.username])).to match_array(expected_result)
+ end
+
+ context 'limiting max conditional elements' do
+ it 'returns merge requests approved by both users, considering limit of 2 being defined' do
+ stub_const('MergeRequests::ByApprovalsFinder::MAX_FILTER_ELEMENTS', 2)
+
+ expected_result = [merge_request_with_both_approvals]
+
+ expect(merge_requests(ids: [first_user.id, second_user.id, third_user.id])).to match_array(expected_result)
+ expect(merge_requests(names: [first_user.username, second_user.username, third_user.username])).to match_array(expected_result)
+ end
+ end
+ end
+
+ context 'with empty params' do
+ it 'returns all merge requests' do
+ expected_result = [merge_request_without_approvals, merge_request_with_first_user_approval, merge_request_with_both_approvals]
+
+ expect(merge_requests(ids: [])).to match_array(expected_result)
+ expect(merge_requests(names: [])).to match_array(expected_result)
+ end
+ end
+end
diff --git a/spec/finders/merge_requests_finder_spec.rb b/spec/finders/merge_requests_finder_spec.rb
index 4f86323c7c6..68958e37001 100644
--- a/spec/finders/merge_requests_finder_spec.rb
+++ b/spec/finders/merge_requests_finder_spec.rb
@@ -486,6 +486,83 @@ RSpec.describe MergeRequestsFinder do
expect(merge_requests).to contain_exactly(old_merge_request, new_merge_request)
end
end
+
+ context 'filtering by the merge request deployments' do
+ let(:gstg) { create(:environment, project: project4, name: 'gstg') }
+ let(:gprd) { create(:environment, project: project4, name: 'gprd') }
+
+ let(:mr1) do
+ create(
+ :merge_request,
+ :simple,
+ :merged,
+ author: user,
+ source_project: project4,
+ target_project: project4
+ )
+ end
+
+ let(:mr2) do
+ create(
+ :merge_request,
+ :simple,
+ :merged,
+ author: user,
+ source_project: project4,
+ target_project: project4
+ )
+ end
+
+ let(:deploy1) do
+ create(
+ :deployment,
+ :success,
+ deployable: nil,
+ environment: gstg,
+ project: project4,
+ sha: mr1.diff_head_sha,
+ finished_at: Time.utc(2020, 10, 1, 12, 0)
+ )
+ end
+
+ let(:deploy2) do
+ create(
+ :deployment,
+ :success,
+ deployable: nil,
+ environment: gprd,
+ project: project4,
+ sha: mr2.diff_head_sha,
+ finished_at: Time.utc(2020, 10, 2, 15, 0)
+ )
+ end
+
+ before do
+ deploy1.link_merge_requests(MergeRequest.where(id: mr1.id))
+ deploy2.link_merge_requests(MergeRequest.where(id: mr2.id))
+ end
+
+ it 'filters merge requests deployed to a given environment' do
+ mrs = described_class.new(user, environment: 'gstg').execute
+
+ expect(mrs).to eq([mr1])
+ end
+
+ it 'filters merge requests deployed before a given date' do
+ mrs =
+ described_class.new(user, deployed_before: '2020-10-02').execute
+
+ expect(mrs).to eq([mr1])
+ end
+
+ it 'filters merge requests deployed after a given date' do
+ mrs = described_class
+ .new(user, deployed_after: '2020-10-01 12:00')
+ .execute
+
+ expect(mrs).to eq([mr2])
+ end
+ end
end
describe '#row_count', :request_store do
@@ -500,6 +577,16 @@ RSpec.describe MergeRequestsFinder do
expect(finder.row_count).to eq(1)
end
+
+ it 'returns -1 if the query times out' do
+ finder = described_class.new(user)
+
+ expect_next_instance_of(described_class) do |subfinder|
+ expect(subfinder).to receive(:execute).and_raise(ActiveRecord::QueryCanceled)
+ end
+
+ expect(finder.row_count).to eq(-1)
+ end
end
context 'external authorization' do
diff --git a/spec/finders/packages/generic/package_finder_spec.rb b/spec/finders/packages/generic/package_finder_spec.rb
new file mode 100644
index 00000000000..ed34268e7a9
--- /dev/null
+++ b/spec/finders/packages/generic/package_finder_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe ::Packages::Generic::PackageFinder do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:package) { create(:generic_package, project: project) }
+
+ describe '#execute!' do
+ subject(:finder) { described_class.new(project) }
+
+ it 'finds package by name and version' do
+ found_package = finder.execute!(package.name, package.version)
+
+ expect(found_package).to eq(package)
+ end
+
+ it 'ignores packages with same name but different version' do
+ create(:generic_package, project: project, name: package.name, version: '3.1.4')
+
+ found_package = finder.execute!(package.name, package.version)
+
+ expect(found_package).to eq(package)
+ end
+
+ it 'raises ActiveRecord::RecordNotFound if package is not found' do
+ expect { finder.execute!(package.name, '3.1.4') }
+ .to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+end
diff --git a/spec/finders/projects_finder_spec.rb b/spec/finders/projects_finder_spec.rb
index 8ae19757c25..2d712bd44ce 100644
--- a/spec/finders/projects_finder_spec.rb
+++ b/spec/finders/projects_finder_spec.rb
@@ -31,6 +31,10 @@ RSpec.describe ProjectsFinder, :do_not_mock_admin_mode do
let(:use_cte) { true }
let(:finder) { described_class.new(params: params.merge(use_cte: use_cte), current_user: current_user, project_ids_relation: project_ids_relation) }
+ before do
+ stub_feature_flags(project_finder_similarity_sort: false)
+ end
+
subject { finder.execute }
shared_examples 'ProjectFinder#execute examples' do
@@ -304,9 +308,33 @@ RSpec.describe ProjectsFinder, :do_not_mock_admin_mode do
end
describe 'sorting' do
- let(:params) { { sort: 'name_asc' } }
+ context 'when sorting by a field' do
+ let(:params) { { sort: 'name_asc' } }
+
+ it { is_expected.to eq([internal_project, public_project]) }
+ end
- it { is_expected.to eq([internal_project, public_project]) }
+ context 'when sorting by similarity' do
+ let(:params) { { sort: 'similarity', search: 'pro' } }
+
+ let_it_be(:internal_project2) do
+ create(:project, :internal, group: group, name: 'projA', path: 'projA')
+ end
+
+ let_it_be(:internal_project3) do
+ create(:project, :internal, group: group, name: 'projABC', path: 'projABC')
+ end
+
+ let_it_be(:internal_project4) do
+ create(:project, :internal, group: group, name: 'projAB', path: 'projAB')
+ end
+
+ before do
+ stub_feature_flags(project_finder_similarity_sort: true)
+ end
+
+ it { is_expected.to eq([internal_project2, internal_project4, internal_project3]) }
+ end
end
describe 'with admin user' do
diff --git a/spec/finders/releases_finder_spec.rb b/spec/finders/releases_finder_spec.rb
index e8049a9eb81..94b6fe53daa 100644
--- a/spec/finders/releases_finder_spec.rb
+++ b/spec/finders/releases_finder_spec.rb
@@ -77,6 +77,34 @@ RSpec.describe ReleasesFinder do
expect(subject).to eq([v1_1_0, v1_0_0])
end
+ context 'with sorting parameters' do
+ before do
+ v1_1_0.update_attribute(:created_at, 3.days.ago)
+ end
+
+ context 'by default is released_at in descending order' do
+ it { is_expected.to eq([v1_1_0, v1_0_0]) }
+ end
+
+ context 'released_at in ascending order' do
+ let(:params) { { sort: 'asc' } }
+
+ it { is_expected.to eq([v1_0_0, v1_1_0]) }
+ end
+
+ context 'order by created_at in descending order' do
+ let(:params) { { order_by: 'created_at' } }
+
+ it { is_expected.to eq([v1_0_0, v1_1_0]) }
+ end
+
+ context 'order by created_at in ascending order' do
+ let(:params) { { order_by: 'created_at', sort: 'asc' } }
+
+ it { is_expected.to eq([v1_1_0, v1_0_0]) }
+ end
+ end
+
it_behaves_like 'preload'
it_behaves_like 'when tag is nil'
it_behaves_like 'when a tag parameter is passed'