2019-10-16 05:07:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe TodosFinder do
|
2016-07-26 17:21:20 -04:00
|
|
|
describe '#execute' do
|
2020-08-19 17:10:32 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, :repository, namespace: group) }
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
|
|
|
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
|
2018-03-05 11:41:48 -05:00
|
|
|
let(:finder) { described_class }
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2020-08-19 17:10:32 -04:00
|
|
|
before_all do
|
2018-03-05 11:41:48 -05:00
|
|
|
group.add_developer(user)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2018-07-16 09:35:19 -04:00
|
|
|
describe '#execute' do
|
2019-10-17 11:06:17 -04:00
|
|
|
it 'returns no todos if user is nil' do
|
|
|
|
expect(described_class.new(nil, {}).execute).to be_empty
|
|
|
|
end
|
|
|
|
|
2018-07-16 09:35:19 -04:00
|
|
|
context 'filtering' do
|
|
|
|
let!(:todo1) { create(:todo, user: user, project: project, target: issue) }
|
|
|
|
let!(:todo2) { create(:todo, user: user, group: group, target: merge_request) }
|
|
|
|
|
|
|
|
it 'returns correct todos when filtered by a project' do
|
|
|
|
todos = finder.new(user, { project_id: project.id }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct todos when filtered by a group' do
|
|
|
|
todos = finder.new(user, { group_id: group.id }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1, todo2])
|
|
|
|
end
|
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
context 'when filtering by type' do
|
2020-06-24 14:09:03 -04:00
|
|
|
it 'returns todos by type when filtered by a single type' do
|
2019-10-21 14:06:29 -04:00
|
|
|
todos = finder.new(user, { type: 'Issue' }).execute
|
2018-07-16 09:35:19 -04:00
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
expect(todos).to match_array([todo1])
|
|
|
|
end
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
it 'returns todos by type when filtered by multiple types' do
|
|
|
|
design_todo = create(:todo, user: user, group: group, target: create(:design))
|
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
todos = finder.new(user, { type: %w[Issue MergeRequest] }).execute
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
|
|
|
expect(todos).not_to include(design_todo)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all todos when type is nil' do
|
|
|
|
todos = finder.new(user, { type: nil }).execute
|
|
|
|
|
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all todos when type is an empty collection' do
|
|
|
|
todos = finder.new(user, { type: [] }).execute
|
|
|
|
|
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all todos when type is blank' do
|
|
|
|
todos = finder.new(user, { type: '' }).execute
|
|
|
|
|
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns todos by type when blank type is in type collection' do
|
|
|
|
todos = finder.new(user, { type: ['', 'MergeRequest'] }).execute
|
|
|
|
|
|
|
|
expect(todos).to contain_exactly(todo2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns todos of all types when only blanks are in a collection' do
|
|
|
|
todos = finder.new(user, { type: ['', ''] }).execute
|
|
|
|
|
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all todos when no type param' do
|
|
|
|
todos = finder.new(user).execute
|
|
|
|
|
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an argument error when invalid type is passed' do
|
|
|
|
todos_finder = finder.new(user, { type: %w[Issue MergeRequest NotAValidType] })
|
|
|
|
|
|
|
|
expect { todos_finder.execute }.to raise_error(ArgumentError)
|
2019-10-21 14:06:29 -04:00
|
|
|
end
|
2018-07-16 09:35:19 -04:00
|
|
|
end
|
|
|
|
|
2019-10-14 14:06:24 -04:00
|
|
|
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
|
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
it 'returns the expected todos when filtering for multiple action ids' do
|
|
|
|
todos = finder.new(user, { action_id: [Todo::DIRECTLY_ADDRESSED, Todo::ASSIGNED] }).execute
|
2019-10-14 14:06:24 -04:00
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
expect(todos).to match_array([todo2, todo1])
|
2019-10-14 14:06:24 -04:00
|
|
|
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
|
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
it 'returns the expected todos when filtering for multiple action names' do
|
|
|
|
todos = finder.new(user, { action: [:directly_addressed, :assigned] }).execute
|
2019-10-14 14:06:24 -04:00
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
expect(todos).to match_array([todo2, todo1])
|
2019-10-14 14:06:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-15 14:06:01 -04:00
|
|
|
context 'when filtering by author' do
|
2020-08-19 17:10:32 -04:00
|
|
|
let_it_be(:author1) { create(:user) }
|
|
|
|
let_it_be(:author2) { create(:user) }
|
2019-10-15 14:06:01 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-17 11:06:17 -04:00
|
|
|
context 'by groups' do
|
|
|
|
context 'with subgroups' do
|
2020-08-19 17:10:32 -04:00
|
|
|
let_it_be(:subgroup) { create(:group, parent: group) }
|
2019-10-17 11:06:17 -04:00
|
|
|
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) }
|
2020-08-19 17:10:32 -04:00
|
|
|
let_it_be(:subgroup1) { create(:group, parent: group) }
|
|
|
|
let_it_be(:subgroup2) { create(:group, parent: group2) }
|
2019-10-17 11:06:17 -04:00
|
|
|
|
|
|
|
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!(:todo4) { create(:todo, user: user, group: subgroup1, target: issue) }
|
|
|
|
let!(:todo5) { create(:todo, user: user, group: subgroup2, target: issue) }
|
|
|
|
let!(:todo6) { create(:todo, user: user, group: group3, target: issue) }
|
|
|
|
|
|
|
|
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, todo4, todo5])
|
|
|
|
end
|
2018-07-16 09:35:19 -04:00
|
|
|
end
|
|
|
|
end
|
2019-10-28 05:06:04 -04:00
|
|
|
|
|
|
|
context 'by state' do
|
|
|
|
let!(:todo1) { create(:todo, user: user, group: group, target: issue, state: :done) }
|
|
|
|
let!(:todo2) { create(:todo, user: user, group: group, target: issue, state: :pending) }
|
|
|
|
|
|
|
|
it 'returns the expected items when no state is provided' do
|
|
|
|
todos = finder.new(user, {}).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo2])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected items when a state is provided' do
|
|
|
|
todos = finder.new(user, { state: :done }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected items when multiple states are provided' do
|
|
|
|
todos = finder.new(user, { state: [:pending, :done] }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1, todo2])
|
|
|
|
end
|
|
|
|
end
|
2019-11-15 10:06:12 -05:00
|
|
|
|
|
|
|
context 'by project' do
|
|
|
|
let_it_be(:project1) { create(:project) }
|
|
|
|
let_it_be(:project2) { create(:project) }
|
|
|
|
let_it_be(:project3) { create(:project) }
|
|
|
|
|
|
|
|
let!(:todo1) { create(:todo, user: user, project: project1, state: :pending) }
|
|
|
|
let!(:todo2) { create(:todo, user: user, project: project2, state: :pending) }
|
|
|
|
let!(:todo3) { create(:todo, user: user, project: project3, state: :pending) }
|
|
|
|
|
|
|
|
it 'returns the expected todos for one project' do
|
|
|
|
todos = finder.new(user, { project_id: project2.id }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo2])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected todos for many projects' do
|
|
|
|
todos = finder.new(user, { project_id: [project2.id, project1.id] }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo2, todo1])
|
|
|
|
end
|
|
|
|
end
|
2020-08-03 11:09:44 -04:00
|
|
|
|
|
|
|
context 'when filtering by target id' do
|
|
|
|
it 'returns the expected todos for the target' do
|
2020-08-13 08:09:50 -04:00
|
|
|
todos = finder.new(user, { type: 'Issue', target_id: issue.id }).execute
|
2020-08-03 11:09:44 -04:00
|
|
|
|
|
|
|
expect(todos).to match_array([todo1])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected todos for multiple target ids' do
|
2020-08-13 08:09:50 -04:00
|
|
|
another_issue = create(:issue, project: project)
|
|
|
|
todo3 = create(:todo, user: user, project: project, target: another_issue)
|
2020-08-03 11:09:44 -04:00
|
|
|
|
2020-08-13 08:09:50 -04:00
|
|
|
todos = finder.new(user, { type: 'Issue', target_id: [issue.id, another_issue.id] }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1, todo3])
|
2020-08-03 11:09:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected todos for empty target id collection' do
|
|
|
|
todos = finder.new(user, { target_id: [] }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1, todo2])
|
|
|
|
end
|
|
|
|
end
|
2018-07-16 09:35:19 -04:00
|
|
|
end
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
context 'external authorization' do
|
|
|
|
it_behaves_like 'a finder with external authorization service' do
|
|
|
|
let!(:subject) { create(:todo, project: project, user: user) }
|
|
|
|
let(:project_params) { { project_id: project.id } }
|
|
|
|
end
|
|
|
|
end
|
2018-07-16 09:35:19 -04:00
|
|
|
end
|
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
describe '#sort' do
|
|
|
|
context 'by date' do
|
|
|
|
let!(:todo1) { create(:todo, user: user, project: project) }
|
|
|
|
let!(:todo2) { create(:todo, user: user, project: project) }
|
|
|
|
let!(:todo3) { create(:todo, user: user, project: project) }
|
|
|
|
|
|
|
|
it 'sorts with oldest created first' do
|
|
|
|
todos = finder.new(user, { sort: 'id_asc' }).execute
|
|
|
|
|
|
|
|
expect(todos.first).to eq(todo1)
|
|
|
|
expect(todos.second).to eq(todo2)
|
|
|
|
expect(todos.third).to eq(todo3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sorts with newest created first' do
|
|
|
|
todos = finder.new(user, { sort: 'id_desc' }).execute
|
|
|
|
|
|
|
|
expect(todos.first).to eq(todo3)
|
|
|
|
expect(todos.second).to eq(todo2)
|
|
|
|
expect(todos.third).to eq(todo1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sorts by priority" do
|
2019-12-20 04:24:38 -05:00
|
|
|
project_2 = create(:project)
|
2018-03-05 11:41:48 -05:00
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
label_1 = create(:label, title: 'label_1', project: project, priority: 1)
|
|
|
|
label_2 = create(:label, title: 'label_2', project: project, priority: 2)
|
|
|
|
label_3 = create(:label, title: 'label_3', project: project, priority: 3)
|
2018-03-05 11:41:48 -05:00
|
|
|
label_1_2 = create(:label, title: 'label_1', project: project_2, priority: 1)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
issue_1 = create(:issue, title: 'issue_1', project: project)
|
|
|
|
issue_2 = create(:issue, title: 'issue_2', project: project)
|
|
|
|
issue_3 = create(:issue, title: 'issue_3', project: project)
|
|
|
|
issue_4 = create(:issue, title: 'issue_4', project: project)
|
2018-03-05 11:41:48 -05:00
|
|
|
merge_request_1 = create(:merge_request, source_project: project_2)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2018-03-05 11:41:48 -05:00
|
|
|
merge_request_1.labels << label_1_2
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
# Covers the case where Todo has more than one label
|
|
|
|
issue_3.labels << label_1
|
|
|
|
issue_3.labels << label_3
|
|
|
|
|
|
|
|
issue_2.labels << label_3
|
|
|
|
issue_1.labels << label_2
|
|
|
|
|
|
|
|
todo_1 = create(:todo, user: user, project: project, target: issue_4)
|
|
|
|
todo_2 = create(:todo, user: user, project: project, target: issue_2)
|
|
|
|
todo_3 = create(:todo, user: user, project: project, target: issue_3, created_at: 2.hours.ago)
|
|
|
|
todo_4 = create(:todo, user: user, project: project, target: issue_1)
|
2018-03-05 11:41:48 -05:00
|
|
|
todo_5 = create(:todo, user: user, project: project_2, target: merge_request_1, created_at: 1.hour.ago)
|
|
|
|
|
|
|
|
project_2.add_developer(user)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
todos = finder.new(user, { sort: 'priority' }).execute
|
|
|
|
|
2018-03-05 11:41:48 -05:00
|
|
|
expect(todos).to eq([todo_3, todo_5, todo_4, todo_2, todo_1])
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-20 11:05:26 -04:00
|
|
|
|
2019-11-11 16:06:20 -05:00
|
|
|
describe '.todo_types' do
|
|
|
|
it 'returns the expected types' do
|
|
|
|
expected_result =
|
|
|
|
if Gitlab.ee?
|
2020-08-03 11:09:44 -04:00
|
|
|
%w[Epic Issue MergeRequest DesignManagement::Design AlertManagement::Alert]
|
2019-11-11 16:06:20 -05:00
|
|
|
else
|
2020-08-03 11:09:44 -04:00
|
|
|
%w[Issue MergeRequest DesignManagement::Design AlertManagement::Alert]
|
2019-11-11 16:06:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(described_class.todo_types).to contain_exactly(*expected_result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 11:05:26 -04:00
|
|
|
describe '#any_for_target?' do
|
|
|
|
it 'returns true if there are any todos for the given target' do
|
|
|
|
todo = create(:todo, :pending)
|
|
|
|
finder = described_class.new(todo.user)
|
|
|
|
|
|
|
|
expect(finder.any_for_target?(todo.target)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if there are no todos for the given target' do
|
|
|
|
issue = create(:issue)
|
|
|
|
finder = described_class.new(issue.author)
|
|
|
|
|
|
|
|
expect(finder.any_for_target?(issue)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|