diff options
Diffstat (limited to 'spec/finders')
42 files changed, 305 insertions, 43 deletions
diff --git a/spec/finders/access_requests_finder_spec.rb b/spec/finders/access_requests_finder_spec.rb index 605777462bb..fbfc8035bcc 100644 --- a/spec/finders/access_requests_finder_spec.rb +++ b/spec/finders/access_requests_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe AccessRequestsFinder do @@ -5,13 +7,13 @@ describe AccessRequestsFinder do let(:access_requester) { create(:user) } let(:project) do - create(:project, :public, :access_requestable) do |project| + create(:project, :public) do |project| project.request_access(access_requester) end end let(:group) do - create(:group, :public, :access_requestable) do |group| + create(:group, :public) do |group| group.request_access(access_requester) end end diff --git a/spec/finders/admin/projects_finder_spec.rb b/spec/finders/admin/projects_finder_spec.rb index 44cc8debd04..eb5d0bba183 100644 --- a/spec/finders/admin/projects_finder_spec.rb +++ b/spec/finders/admin/projects_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Admin::ProjectsFinder do diff --git a/spec/finders/artifacts_finder_spec.rb b/spec/finders/artifacts_finder_spec.rb new file mode 100644 index 00000000000..b956e2c9515 --- /dev/null +++ b/spec/finders/artifacts_finder_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ArtifactsFinder do + let(:project) { create(:project) } + + describe '#execute' do + before do + create(:ci_build, :artifacts, project: project) + end + + subject { described_class.new(project, params).execute } + + context 'with empty params' do + let(:params) { {} } + + it 'returns all artifacts belonging to the project' do + expect(subject).to contain_exactly(*project.job_artifacts) + end + end + + context 'with sort param' do + let(:params) { { sort: 'size_desc' } } + + it 'sorts the artifacts' do + expect(subject).to eq(project.job_artifacts.order_by('size_desc')) + end + end + end +end diff --git a/spec/finders/autocomplete/move_to_project_finder_spec.rb b/spec/finders/autocomplete/move_to_project_finder_spec.rb index 4a87b47bd08..f997dd32c40 100644 --- a/spec/finders/autocomplete/move_to_project_finder_spec.rb +++ b/spec/finders/autocomplete/move_to_project_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Autocomplete::MoveToProjectFinder do diff --git a/spec/finders/autocomplete/users_finder_spec.rb b/spec/finders/autocomplete/users_finder_spec.rb index f3b54ca0461..5d340c46114 100644 --- a/spec/finders/autocomplete/users_finder_spec.rb +++ b/spec/finders/autocomplete/users_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Autocomplete::UsersFinder do diff --git a/spec/finders/boards/visits_finder_spec.rb b/spec/finders/boards/visits_finder_spec.rb index 4d40f4826f8..7e3ad8aa9f0 100644 --- a/spec/finders/boards/visits_finder_spec.rb +++ b/spec/finders/boards/visits_finder_spec.rb @@ -10,7 +10,7 @@ describe Boards::VisitsFinder do let(:project) { create(:project) } let(:project_board) { create(:board, project: project) } - subject(:finder) { described_class.new(project_board.parent, user) } + subject(:finder) { described_class.new(project_board.resource_parent, user) } it 'returns nil when there is no user' do finder.current_user = nil @@ -27,7 +27,7 @@ describe Boards::VisitsFinder do it 'queries for last N visits' do expect(BoardProjectRecentVisit).to receive(:latest).with(user, project, count: 5).once - described_class.new(project_board.parent, user).latest(5) + described_class.new(project_board.resource_parent, user).latest(5) end end @@ -35,7 +35,7 @@ describe Boards::VisitsFinder do let(:group) { create(:group) } let(:group_board) { create(:board, group: group) } - subject(:finder) { described_class.new(group_board.parent, user) } + subject(:finder) { described_class.new(group_board.resource_parent, user) } it 'returns nil when there is no user' do finder.current_user = nil @@ -52,7 +52,7 @@ describe Boards::VisitsFinder do it 'queries for last N visits' do expect(BoardGroupRecentVisit).to receive(:latest).with(user, group, count: 5).once - described_class.new(group_board.parent, user).latest(5) + described_class.new(group_board.resource_parent, user).latest(5) end end end diff --git a/spec/finders/branches_finder_spec.rb b/spec/finders/branches_finder_spec.rb index 3fc86f3e408..1a33bdf11d7 100644 --- a/spec/finders/branches_finder_spec.rb +++ b/spec/finders/branches_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe BranchesFinder do diff --git a/spec/finders/clusters/knative_services_finder_spec.rb b/spec/finders/clusters/knative_services_finder_spec.rb index 159724b3c1f..7ad64cc3bca 100644 --- a/spec/finders/clusters/knative_services_finder_spec.rb +++ b/spec/finders/clusters/knative_services_finder_spec.rb @@ -77,6 +77,7 @@ describe Clusters::KnativeServicesFinder do describe '#knative_detected' do subject { finder.knative_detected } + before do synchronous_reactive_cache(finder) end diff --git a/spec/finders/clusters/kubernetes_namespace_finder_spec.rb b/spec/finders/clusters/kubernetes_namespace_finder_spec.rb index 8beba0b99a4..7d9c4daa0fe 100644 --- a/spec/finders/clusters/kubernetes_namespace_finder_spec.rb +++ b/spec/finders/clusters/kubernetes_namespace_finder_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Clusters::KubernetesNamespaceFinder do described_class.new( cluster, project: project, - environment_slug: 'production', + environment_name: 'production', allow_blank_token: allow_blank_token ) end @@ -22,8 +22,8 @@ RSpec.describe Clusters::KubernetesNamespaceFinder do end describe '#execute' do - let(:production) { create(:environment, project: project, slug: 'production') } - let(:staging) { create(:environment, project: project, slug: 'staging') } + let(:production) { create(:environment, project: project, name: 'production') } + let(:staging) { create(:environment, project: project, name: 'staging') } let(:cluster) { create(:cluster, :group, :provided_by_user) } let(:project) { create(:project) } diff --git a/spec/finders/clusters_finder_spec.rb b/spec/finders/clusters_finder_spec.rb index da529e0670f..f6ea8347f67 100644 --- a/spec/finders/clusters_finder_spec.rb +++ b/spec/finders/clusters_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe ClustersFinder do diff --git a/spec/finders/concerns/finder_methods_spec.rb b/spec/finders/concerns/finder_methods_spec.rb index e074e53c2c5..2e44df8b044 100644 --- a/spec/finders/concerns/finder_methods_spec.rb +++ b/spec/finders/concerns/finder_methods_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe FinderMethods do 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 f29acb521a8..6ba98b79176 100644 --- a/spec/finders/concerns/finder_with_cross_project_access_spec.rb +++ b/spec/finders/concerns/finder_with_cross_project_access_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe FinderWithCrossProjectAccess do @@ -22,6 +24,7 @@ describe FinderWithCrossProjectAccess do let(:user) { create(:user) } subject(:finder) { finder_class.new(user) } + let!(:result) { create(:issue) } before do diff --git a/spec/finders/contributed_projects_finder_spec.rb b/spec/finders/contributed_projects_finder_spec.rb index ee84fd067d4..1d907261fe9 100644 --- a/spec/finders/contributed_projects_finder_spec.rb +++ b/spec/finders/contributed_projects_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe ContributedProjectsFinder do diff --git a/spec/finders/environments_finder_spec.rb b/spec/finders/environments_finder_spec.rb index 25835bb4d94..69687eaa99f 100644 --- a/spec/finders/environments_finder_spec.rb +++ b/spec/finders/environments_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe EnvironmentsFinder do diff --git a/spec/finders/events_finder_spec.rb b/spec/finders/events_finder_spec.rb index 3bce46cc4d1..848030262cd 100644 --- a/spec/finders/events_finder_spec.rb +++ b/spec/finders/events_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe EventsFinder do diff --git a/spec/finders/fork_projects_finder_spec.rb b/spec/finders/fork_projects_finder_spec.rb index 98cff37205e..2fba53a74a0 100644 --- a/spec/finders/fork_projects_finder_spec.rb +++ b/spec/finders/fork_projects_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe ForkProjectsFinder do diff --git a/spec/finders/group_descendants_finder_spec.rb b/spec/finders/group_descendants_finder_spec.rb index 5fb6739d6e2..17875a9b9ab 100644 --- a/spec/finders/group_descendants_finder_spec.rb +++ b/spec/finders/group_descendants_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe GroupDescendantsFinder do diff --git a/spec/finders/group_members_finder_spec.rb b/spec/finders/group_members_finder_spec.rb index 49b0e14241e..08f3b4024b3 100644 --- a/spec/finders/group_members_finder_spec.rb +++ b/spec/finders/group_members_finder_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require 'spec_helper' describe GroupMembersFinder, '#execute' do let(:group) { create(:group) } - let(:nested_group) { create(:group, :access_requestable, parent: group) } + let(:nested_group) { create(:group, parent: group) } let(:user1) { create(:user) } let(:user2) { create(:user) } let(:user3) { create(:user) } diff --git a/spec/finders/group_projects_finder_spec.rb b/spec/finders/group_projects_finder_spec.rb index f4bd8a3f6ba..b291b5d4b90 100644 --- a/spec/finders/group_projects_finder_spec.rb +++ b/spec/finders/group_projects_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe GroupProjectsFinder do diff --git a/spec/finders/groups_finder_spec.rb b/spec/finders/groups_finder_spec.rb index c8875d1f92d..741a89a270b 100644 --- a/spec/finders/groups_finder_spec.rb +++ b/spec/finders/groups_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe GroupsFinder do diff --git a/spec/finders/issues_finder_spec.rb b/spec/finders/issues_finder_spec.rb index a17ff1ad50d..c27ce263bf0 100644 --- a/spec/finders/issues_finder_spec.rb +++ b/spec/finders/issues_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe IssuesFinder do diff --git a/spec/finders/joined_groups_finder_spec.rb b/spec/finders/joined_groups_finder_spec.rb index ae3e55f90f1..b01bd44470a 100644 --- a/spec/finders/joined_groups_finder_spec.rb +++ b/spec/finders/joined_groups_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe JoinedGroupsFinder do diff --git a/spec/finders/labels_finder_spec.rb b/spec/finders/labels_finder_spec.rb index ba41ded112a..2681f098fec 100644 --- a/spec/finders/labels_finder_spec.rb +++ b/spec/finders/labels_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe LabelsFinder do diff --git a/spec/finders/license_template_finder_spec.rb b/spec/finders/license_template_finder_spec.rb index f6f40bf33cc..183ee67d801 100644 --- a/spec/finders/license_template_finder_spec.rb +++ b/spec/finders/license_template_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe LicenseTemplateFinder do diff --git a/spec/finders/members_finder_spec.rb b/spec/finders/members_finder_spec.rb index 6920fb4e572..f9b8fee6f2d 100644 --- a/spec/finders/members_finder_spec.rb +++ b/spec/finders/members_finder_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require 'spec_helper' describe MembersFinder, '#execute' do set(:group) { create(:group) } - set(:nested_group) { create(:group, :access_requestable, parent: group) } + set(:nested_group) { create(:group, parent: group) } set(:project) { create(:project, namespace: nested_group) } set(:user1) { create(:user) } set(:user2) { create(:user) } @@ -55,7 +57,7 @@ describe MembersFinder, '#execute' do context 'when include_invited_groups_members == true' do subject { described_class.new(project, user2).execute(include_invited_groups_members: true) } - set(:linked_group) { create(:group, :public, :access_requestable) } + set(:linked_group) { create(:group, :public) } set(:nested_linked_group) { create(:group, parent: linked_group) } set(:linked_group_member) { linked_group.add_guest(user1) } set(:nested_linked_group_member) { nested_linked_group.add_guest(user2) } @@ -86,10 +88,8 @@ describe MembersFinder, '#execute' do create(:project_group_link, project: project, group: nested_linked_group, group_access: Gitlab::Access::REPORTER) nested_linked_group.add_developer(user1) - result = subject - - expect(result).to contain_exactly(linked_group_member, nested_linked_group_member) - expect(result.first.access_level).to eq(Gitlab::Access::REPORTER) + expect(subject.map(&:user)).to contain_exactly(user1, user2) + expect(subject.max_by(&:access_level).access_level).to eq(Gitlab::Access::REPORTER) end end end diff --git a/spec/finders/merge_request_target_project_finder_spec.rb b/spec/finders/merge_request_target_project_finder_spec.rb index d26a75179de..1d78b7ba4e3 100644 --- a/spec/finders/merge_request_target_project_finder_spec.rb +++ b/spec/finders/merge_request_target_project_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe MergeRequestTargetProjectFinder do diff --git a/spec/finders/merge_requests_finder_spec.rb b/spec/finders/merge_requests_finder_spec.rb index 6c0bbeff4f4..a396284f1e9 100644 --- a/spec/finders/merge_requests_finder_spec.rb +++ b/spec/finders/merge_requests_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe MergeRequestsFinder do diff --git a/spec/finders/milestones_finder_spec.rb b/spec/finders/milestones_finder_spec.rb index 34c7b508c56..3545ff35ed8 100644 --- a/spec/finders/milestones_finder_spec.rb +++ b/spec/finders/milestones_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe MilestonesFinder do diff --git a/spec/finders/notes_finder_spec.rb b/spec/finders/notes_finder_spec.rb index 88906adfeeb..44636a22ef9 100644 --- a/spec/finders/notes_finder_spec.rb +++ b/spec/finders/notes_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe NotesFinder do diff --git a/spec/finders/personal_access_tokens_finder_spec.rb b/spec/finders/personal_access_tokens_finder_spec.rb index 3e849c9a644..a44daf585ba 100644 --- a/spec/finders/personal_access_tokens_finder_spec.rb +++ b/spec/finders/personal_access_tokens_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe PersonalAccessTokensFinder do diff --git a/spec/finders/personal_projects_finder_spec.rb b/spec/finders/personal_projects_finder_spec.rb index ef7dd0cd4a8..7686dd3dc9d 100644 --- a/spec/finders/personal_projects_finder_spec.rb +++ b/spec/finders/personal_projects_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe PersonalProjectsFinder do diff --git a/spec/finders/pipeline_schedules_finder_spec.rb b/spec/finders/pipeline_schedules_finder_spec.rb index 2fefa0280d1..8d0bde15e03 100644 --- a/spec/finders/pipeline_schedules_finder_spec.rb +++ b/spec/finders/pipeline_schedules_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe PipelineSchedulesFinder do diff --git a/spec/finders/pipelines_finder_spec.rb b/spec/finders/pipelines_finder_spec.rb index b23fd8ccdc6..05d13a76e0e 100644 --- a/spec/finders/pipelines_finder_spec.rb +++ b/spec/finders/pipelines_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe PipelinesFinder do diff --git a/spec/finders/projects_finder_spec.rb b/spec/finders/projects_finder_spec.rb index ac866e49fcd..4ec12b5a7f7 100644 --- a/spec/finders/projects_finder_spec.rb +++ b/spec/finders/projects_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe ProjectsFinder do diff --git a/spec/finders/runner_jobs_finder_spec.rb b/spec/finders/runner_jobs_finder_spec.rb index 01f45a37ba8..c11f9182036 100644 --- a/spec/finders/runner_jobs_finder_spec.rb +++ b/spec/finders/runner_jobs_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe RunnerJobsFinder do diff --git a/spec/finders/snippets_finder_spec.rb b/spec/finders/snippets_finder_spec.rb index d367f9015c7..bcb762664f7 100644 --- a/spec/finders/snippets_finder_spec.rb +++ b/spec/finders/snippets_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe SnippetsFinder do @@ -15,16 +17,27 @@ describe SnippetsFinder do end describe '#execute' do - set(:user) { create(:user) } - set(:private_personal_snippet) { create(:personal_snippet, :private, author: user) } - set(:internal_personal_snippet) { create(:personal_snippet, :internal, author: user) } - set(:public_personal_snippet) { create(:personal_snippet, :public, author: user) } + let_it_be(:user) { create(:user) } + let_it_be(:admin) { create(:admin) } + let_it_be(:group) { create(:group, :public) } + let_it_be(:project) { create(:project, :public, group: group) } + + let_it_be(:private_personal_snippet) { create(:personal_snippet, :private, author: user) } + let_it_be(:internal_personal_snippet) { create(:personal_snippet, :internal, author: user) } + let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, author: user) } + + let_it_be(:private_project_snippet) { create(:project_snippet, :private, project: project) } + let_it_be(:internal_project_snippet) { create(:project_snippet, :internal, project: project) } + let_it_be(:public_project_snippet) { create(:project_snippet, :public, project: project) } context 'filter by scope' do it "returns all snippets for 'all' scope" do snippets = described_class.new(user, scope: :all).execute - expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet) + expect(snippets).to contain_exactly( + private_personal_snippet, internal_personal_snippet, public_personal_snippet, + internal_project_snippet, public_project_snippet + ) end it "returns all snippets for 'are_private' scope" do @@ -36,13 +49,13 @@ describe SnippetsFinder do it "returns all snippets for 'are_internal' scope" do snippets = described_class.new(user, scope: :are_internal).execute - expect(snippets).to contain_exactly(internal_personal_snippet) + expect(snippets).to contain_exactly(internal_personal_snippet, internal_project_snippet) end - it "returns all snippets for 'are_private' scope" do + it "returns all snippets for 'are_public' scope" do snippets = described_class.new(user, scope: :are_public).execute - expect(snippets).to contain_exactly(public_personal_snippet) + expect(snippets).to contain_exactly(public_personal_snippet, public_project_snippet) end end @@ -84,7 +97,6 @@ describe SnippetsFinder do end it 'returns all snippets for an admin' do - admin = create(:user, :admin) snippets = described_class.new(admin, author: user).execute expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet) @@ -92,12 +104,6 @@ describe SnippetsFinder do end context 'project snippets' do - let(:group) { create(:group, :public) } - let(:project) { create(:project, :public, group: group) } - let!(:private_project_snippet) { create(:project_snippet, :private, project: project) } - let!(:internal_project_snippet) { create(:project_snippet, :internal, project: project) } - let!(:public_project_snippet) { create(:project_snippet, :public, project: project) } - it 'returns public personal and project snippets for unauthorized user' do snippets = described_class.new(nil, project: project).execute @@ -145,11 +151,54 @@ describe SnippetsFinder do end it 'returns all snippets for an admin' do - admin = create(:user, :admin) snippets = described_class.new(admin, project: project).execute expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet) end + + context 'filter by author' do + let!(:other_user) { create(:user) } + let!(:other_private_project_snippet) { create(:project_snippet, :private, project: project, author: other_user) } + let!(:other_internal_project_snippet) { create(:project_snippet, :internal, project: project, author: other_user) } + let!(:other_public_project_snippet) { create(:project_snippet, :public, project: project, author: other_user) } + + it 'returns all snippets for project members' do + project.add_developer(user) + + snippets = described_class.new(user, author: other_user).execute + + expect(snippets) + .to contain_exactly( + other_private_project_snippet, + other_internal_project_snippet, + other_public_project_snippet + ) + end + end + end + + context 'explore snippets' do + it 'returns only public personal snippets for unauthenticated users' do + snippets = described_class.new(nil, explore: true).execute + + expect(snippets).to contain_exactly(public_personal_snippet) + end + + it 'also returns internal personal snippets for authenticated users' do + snippets = described_class.new(user, explore: true).execute + + expect(snippets).to contain_exactly( + internal_personal_snippet, public_personal_snippet + ) + end + + it 'returns all personal snippets for admins' do + snippets = described_class.new(admin, explore: true).execute + + expect(snippets).to contain_exactly( + private_personal_snippet, internal_personal_snippet, public_personal_snippet + ) + end end context 'when the user cannot read cross project' do diff --git a/spec/finders/tags_finder_spec.rb b/spec/finders/tags_finder_spec.rb index 460e278e2d3..85f970b71c4 100644 --- a/spec/finders/tags_finder_spec.rb +++ b/spec/finders/tags_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe TagsFinder do diff --git a/spec/finders/template_finder_spec.rb b/spec/finders/template_finder_spec.rb index 114af9461e0..ed47752cf60 100644 --- a/spec/finders/template_finder_spec.rb +++ b/spec/finders/template_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe TemplateFinder do diff --git a/spec/finders/todos_finder_spec.rb b/spec/finders/todos_finder_spec.rb index f7b35e76925..044e135fa0b 100644 --- a/spec/finders/todos_finder_spec.rb +++ b/spec/finders/todos_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe TodosFinder do @@ -14,6 +16,10 @@ describe TodosFinder do end describe '#execute' do + it 'returns no todos if user is nil' do + expect(described_class.new(nil, {}).execute).to be_empty + end + context 'filtering' do let!(:todo1) { create(:todo, user: user, project: project, target: issue) } let!(:todo2) { create(:todo, user: user, group: group, target: merge_request) } @@ -36,14 +42,98 @@ describe TodosFinder do expect(todos).to match_array([todo1]) end - context 'with subgroups' do - let(:subgroup) { create(:group, parent: group) } - let!(:todo3) { create(:todo, user: user, group: subgroup, target: issue) } + context 'when filtering for actions' do + let!(:todo1) { create(:todo, user: user, project: project, target: issue, action: Todo::ASSIGNED) } + let!(:todo2) { create(:todo, user: user, group: group, target: merge_request, action: Todo::DIRECTLY_ADDRESSED) } + + context 'by action ids' do + it 'returns the expected todos' do + todos = finder.new(user, { action_id: Todo::DIRECTLY_ADDRESSED }).execute + + expect(todos).to match_array([todo2]) + end + + context 'multiple actions' do + it 'returns the expected todos' do + todos = finder.new(user, { action_id: [Todo::DIRECTLY_ADDRESSED, Todo::ASSIGNED] }).execute + + expect(todos).to match_array([todo2, todo1]) + end + end + end + + context 'by action names' do + it 'returns the expected todos' do + todos = finder.new(user, { action: :directly_addressed }).execute + + expect(todos).to match_array([todo2]) + end + + context 'multiple actions' do + it 'returns the expected todos' do + todos = finder.new(user, { action: [:directly_addressed, :assigned] }).execute + + expect(todos).to match_array([todo2, todo1]) + end + end + end + end + + context 'when filtering by author' do + let(:author1) { create(:user) } + let(:author2) { create(:user) } + + let!(:todo1) { create(:todo, user: user, author: author1) } + let!(:todo2) { create(:todo, user: user, author: author2) } + + it 'returns correct todos when filtering by an author' do + todos = finder.new(user, { author_id: author1.id }).execute + + expect(todos).to match_array([todo1]) + end + + context 'querying for multiple authors' do + it 'returns the correct todo items' do + todos = finder.new(user, { author_id: [author2.id, author1.id] }).execute + + expect(todos).to match_array([todo2, todo1]) + end + end + end + + context 'by groups' do + context 'with subgroups' do + let(:subgroup) { create(:group, parent: group) } + let!(:todo3) { create(:todo, user: user, group: subgroup, target: issue) } + + it 'returns todos from subgroups when filtered by a group' do + todos = finder.new(user, { group_id: group.id }).execute + + expect(todos).to match_array([todo1, todo2, todo3]) + end + end + + context 'filtering for multiple groups' do + let_it_be(:group2) { create(:group) } + let_it_be(:group3) { create(:group) } + + let!(:todo1) { create(:todo, user: user, project: project, target: issue) } + let!(:todo2) { create(:todo, user: user, group: group, target: merge_request) } + let!(:todo3) { create(:todo, user: user, group: group2, target: merge_request) } + + let(:subgroup1) { create(:group, parent: group) } + let!(:todo4) { create(:todo, user: user, group: subgroup1, target: issue) } + + let(:subgroup2) { create(:group, parent: group2) } + let!(:todo5) { create(:todo, user: user, group: subgroup2, target: issue) } + + let!(:todo6) { create(:todo, user: user, group: group3, target: issue) } - it 'returns todos from subgroups when filtered by a group' do - todos = finder.new(user, { group_id: group.id }).execute + it 'returns the expected groups' do + todos = finder.new(user, { group_id: [group.id, group2.id] }).execute - expect(todos).to match_array([todo1, todo2, todo3]) + expect(todos).to match_array([todo1, todo2, todo3, todo4, todo5]) + end end end end diff --git a/spec/finders/user_finder_spec.rb b/spec/finders/user_finder_spec.rb index 4771b878b8e..c20d7850d68 100644 --- a/spec/finders/user_finder_spec.rb +++ b/spec/finders/user_finder_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe UserFinder do - set(:user) { create(:user) } + let_it_be(:user) { create(:user) } describe '#find_by_id' do context 'when the user exists' do @@ -24,7 +24,7 @@ describe UserFinder do context 'when the user does not exist' do it 'returns nil' do - found = described_class.new(1).find_by_id + found = described_class.new(-1).find_by_id expect(found).to be_nil end @@ -84,7 +84,7 @@ describe UserFinder do context 'when the user does not exist' do it 'returns nil' do - found = described_class.new(1).find_by_id_or_username + found = described_class.new(-1).find_by_id_or_username expect(found).to be_nil end @@ -110,7 +110,7 @@ describe UserFinder do context 'when the user does not exist' do it 'raises ActiveRecord::RecordNotFound' do - finder = described_class.new(1) + finder = described_class.new(-1) expect { finder.find_by_id! }.to raise_error(ActiveRecord::RecordNotFound) end @@ -170,10 +170,32 @@ describe UserFinder do context 'when the user does not exist' do it 'raises ActiveRecord::RecordNotFound' do - finder = described_class.new(1) + finder = described_class.new(-1) expect { finder.find_by_id_or_username! }.to raise_error(ActiveRecord::RecordNotFound) end end end + + describe '#find_by_ssh_key_id' do + let_it_be(:ssh_key) { create(:key, user: user) } + + it 'returns the user when passing the ssh key id' do + found = described_class.new(ssh_key.id).find_by_ssh_key_id + + expect(found).to eq(user) + end + + it 'returns the user when passing the ssh key id (string)' do + found = described_class.new(ssh_key.id.to_s).find_by_ssh_key_id + + expect(found).to eq(user) + end + + it 'returns nil when the id does not exist' do + found = described_class.new(-1).find_by_ssh_key_id + + expect(found).to be_nil + end + end end diff --git a/spec/finders/user_recent_events_finder_spec.rb b/spec/finders/user_recent_events_finder_spec.rb index 5ebceeb7586..eef6448a4a2 100644 --- a/spec/finders/user_recent_events_finder_spec.rb +++ b/spec/finders/user_recent_events_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe UserRecentEventsFinder do diff --git a/spec/finders/users_finder_spec.rb b/spec/finders/users_finder_spec.rb index d71d3c99272..7f1fc1cc1c5 100644 --- a/spec/finders/users_finder_spec.rb +++ b/spec/finders/users_finder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe UsersFinder do |