2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Todo do
|
2016-03-16 19:31:30 -04:00
|
|
|
let(:issue) { create(:issue) }
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
describe 'relationships' do
|
|
|
|
it { is_expected.to belong_to(:author).class_name("User") }
|
2016-02-17 14:45:32 -05:00
|
|
|
it { is_expected.to belong_to(:note) }
|
2016-02-12 08:17:42 -05:00
|
|
|
it { is_expected.to belong_to(:project) }
|
2018-07-16 09:35:19 -04:00
|
|
|
it { is_expected.to belong_to(:group) }
|
2016-02-12 08:17:42 -05:00
|
|
|
it { is_expected.to belong_to(:target).touch(true) }
|
|
|
|
it { is_expected.to belong_to(:user) }
|
|
|
|
end
|
|
|
|
|
2016-02-12 13:45:44 -05:00
|
|
|
describe 'respond to' do
|
|
|
|
it { is_expected.to respond_to(:author_name) }
|
|
|
|
it { is_expected.to respond_to(:author_email) }
|
|
|
|
end
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:action) }
|
2016-03-16 19:31:30 -04:00
|
|
|
it { is_expected.to validate_presence_of(:target_type) }
|
2016-02-12 08:17:42 -05:00
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
2018-02-04 14:46:14 -05:00
|
|
|
it { is_expected.to validate_presence_of(:author) }
|
2016-03-16 19:31:30 -04:00
|
|
|
|
|
|
|
context 'for commits' do
|
|
|
|
subject { described_class.new(target_type: 'Commit') }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:commit_id) }
|
|
|
|
it { is_expected.not_to validate_presence_of(:target_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for issuables' do
|
|
|
|
subject { described_class.new(target: issue) }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:target_id) }
|
|
|
|
it { is_expected.not_to validate_presence_of(:commit_id) }
|
|
|
|
end
|
2016-02-12 08:17:42 -05:00
|
|
|
end
|
2016-02-12 13:45:44 -05:00
|
|
|
|
2016-02-18 14:16:39 -05:00
|
|
|
describe '#body' do
|
2016-02-17 22:28:36 -05:00
|
|
|
before do
|
2016-02-18 14:16:39 -05:00
|
|
|
subject.target = build(:issue, title: 'Bugfix')
|
2016-02-17 22:28:36 -05:00
|
|
|
end
|
|
|
|
|
2016-02-18 14:16:39 -05:00
|
|
|
it 'returns target title when note is blank' do
|
2016-02-17 14:45:32 -05:00
|
|
|
subject.note = nil
|
|
|
|
|
2016-02-18 14:16:39 -05:00
|
|
|
expect(subject.body).to eq 'Bugfix'
|
2016-02-17 14:45:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns note when note is present' do
|
|
|
|
subject.note = build(:note, note: 'quick fix')
|
|
|
|
|
2016-02-18 14:16:39 -05:00
|
|
|
expect(subject.body).to eq 'quick fix'
|
2016-02-17 14:45:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 19:56:23 -04:00
|
|
|
describe '#done' do
|
2016-02-22 14:53:07 -05:00
|
|
|
it 'changes state to done' do
|
|
|
|
todo = create(:todo, state: :pending)
|
2020-05-07 08:09:46 -04:00
|
|
|
|
2016-03-16 19:56:23 -04:00
|
|
|
expect { todo.done }.to change(todo, :state).from('pending').to('done')
|
2016-02-17 22:28:36 -05:00
|
|
|
end
|
|
|
|
|
2016-02-22 14:53:07 -05:00
|
|
|
it 'does not raise error when is already done' do
|
|
|
|
todo = create(:todo, state: :done)
|
2020-05-07 08:09:46 -04:00
|
|
|
|
2016-03-16 19:56:23 -04:00
|
|
|
expect { todo.done }.not_to raise_error
|
2016-02-17 22:28:36 -05:00
|
|
|
end
|
2016-02-12 13:45:44 -05:00
|
|
|
end
|
2016-03-16 19:31:30 -04:00
|
|
|
|
|
|
|
describe '#for_commit?' do
|
|
|
|
it 'returns true when target is a commit' do
|
|
|
|
subject.target_type = 'Commit'
|
2020-05-07 08:09:46 -04:00
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
expect(subject.for_commit?).to eq true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when target is an issuable' do
|
|
|
|
subject.target_type = 'Issue'
|
2020-05-07 08:09:46 -04:00
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
expect(subject.for_commit?).to eq false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 08:09:46 -04:00
|
|
|
describe '#for_design?' do
|
|
|
|
it 'returns true when target is a Design' do
|
|
|
|
subject.target_type = 'DesignManagement::Design'
|
|
|
|
|
|
|
|
expect(subject.for_design?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when target is not a Design' do
|
|
|
|
subject.target_type = 'Issue'
|
|
|
|
|
|
|
|
expect(subject.for_design?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 14:08:19 -04:00
|
|
|
describe '#for_alert?' do
|
|
|
|
it 'returns true when target is a Alert' do
|
|
|
|
subject.target_type = 'AlertManagement::Alert'
|
|
|
|
|
|
|
|
expect(subject.for_alert?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when target is not a Alert' do
|
|
|
|
subject.target_type = 'Issue'
|
|
|
|
|
|
|
|
expect(subject.for_alert?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
describe '#target' do
|
2016-03-18 12:27:27 -04:00
|
|
|
context 'for commits' do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:commit) { project.commit }
|
|
|
|
|
2016-03-18 12:27:27 -04:00
|
|
|
it 'returns an instance of Commit when exists' do
|
|
|
|
subject.project = project
|
|
|
|
subject.target_type = 'Commit'
|
|
|
|
subject.commit_id = commit.id
|
|
|
|
|
|
|
|
expect(subject.target).to be_a(Commit)
|
|
|
|
expect(subject.target).to eq commit
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when does not exists' do
|
|
|
|
subject.project = project
|
|
|
|
subject.target_type = 'Commit'
|
|
|
|
subject.commit_id = 'xxxx'
|
|
|
|
|
|
|
|
expect(subject.target).to be_nil
|
|
|
|
end
|
2016-03-16 19:31:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the issuable for issuables' do
|
|
|
|
subject.target_id = issue.id
|
|
|
|
subject.target_type = issue.class.name
|
2020-05-07 08:09:46 -04:00
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
expect(subject.target).to eq issue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-18 12:24:47 -04:00
|
|
|
describe '#target_reference' do
|
2017-01-10 18:53:51 -05:00
|
|
|
it 'returns commit full reference with short id' do
|
2017-01-26 17:44:58 -05:00
|
|
|
project = create(:project, :repository)
|
|
|
|
commit = project.commit
|
|
|
|
|
2016-03-18 09:32:22 -04:00
|
|
|
subject.project = project
|
2016-03-16 19:31:30 -04:00
|
|
|
subject.target_type = 'Commit'
|
|
|
|
subject.commit_id = commit.id
|
2016-03-18 09:32:22 -04:00
|
|
|
|
2019-06-27 01:56:08 -04:00
|
|
|
expect(subject.target_reference).to eq commit.reference_link_text(full: false)
|
2016-03-16 19:31:30 -04:00
|
|
|
end
|
|
|
|
|
2017-01-10 18:53:51 -05:00
|
|
|
it 'returns full reference for issuables' do
|
2016-03-16 19:31:30 -04:00
|
|
|
subject.target = issue
|
2020-05-07 08:09:46 -04:00
|
|
|
|
2019-06-27 01:56:08 -04:00
|
|
|
expect(subject.target_reference).to eq issue.to_reference(full: false)
|
2016-03-16 19:31:30 -04:00
|
|
|
end
|
|
|
|
end
|
2017-04-10 11:51:15 -04:00
|
|
|
|
|
|
|
describe '#self_added?' do
|
|
|
|
let(:user_1) { build(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.user = user_1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when the user is the author' do
|
|
|
|
subject.author = user_1
|
|
|
|
|
|
|
|
expect(subject).to be_self_added
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when the user is not the author' do
|
|
|
|
subject.author = build(:user)
|
|
|
|
|
|
|
|
expect(subject).not_to be_self_added
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-12 10:06:26 -05:00
|
|
|
describe '#done?' do
|
|
|
|
let_it_be(:todo1) { create(:todo, state: :pending) }
|
|
|
|
let_it_be(:todo2) { create(:todo, state: :done) }
|
|
|
|
|
|
|
|
it 'returns true for todos with done state' do
|
|
|
|
expect(todo2.done?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for todos with state pending' do
|
|
|
|
expect(todo1.done?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-10 11:51:15 -04:00
|
|
|
describe '#self_assigned?' do
|
|
|
|
let(:user_1) { build(:user) }
|
|
|
|
|
2020-09-21 08:09:34 -04:00
|
|
|
context 'when self_added' do
|
|
|
|
before do
|
|
|
|
subject.user = user_1
|
|
|
|
subject.author = user_1
|
|
|
|
end
|
2017-04-10 11:51:15 -04:00
|
|
|
|
2020-09-21 08:09:34 -04:00
|
|
|
it 'returns true for ASSIGNED' do
|
|
|
|
subject.action = Todo::ASSIGNED
|
|
|
|
|
|
|
|
expect(subject).to be_self_assigned
|
|
|
|
end
|
2017-04-10 11:51:15 -04:00
|
|
|
|
2020-09-21 08:09:34 -04:00
|
|
|
it 'returns true for REVIEW_REQUESTED' do
|
|
|
|
subject.action = Todo::REVIEW_REQUESTED
|
2017-04-10 11:51:15 -04:00
|
|
|
|
2020-09-21 08:09:34 -04:00
|
|
|
expect(subject).to be_self_assigned
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for other action' do
|
|
|
|
subject.action = Todo::MENTIONED
|
|
|
|
|
|
|
|
expect(subject).not_to be_self_assigned
|
|
|
|
end
|
2017-04-10 11:51:15 -04:00
|
|
|
end
|
|
|
|
|
2020-09-21 08:09:34 -04:00
|
|
|
context 'when todo is not self_added' do
|
|
|
|
before do
|
|
|
|
subject.user = user_1
|
|
|
|
subject.author = build(:user)
|
|
|
|
end
|
2017-04-10 11:51:15 -04:00
|
|
|
|
2020-09-21 08:09:34 -04:00
|
|
|
it 'returns false' do
|
|
|
|
subject.action = Todo::ASSIGNED
|
|
|
|
|
|
|
|
expect(subject).not_to be_self_assigned
|
|
|
|
end
|
2017-04-10 11:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2018-09-20 09:18:04 -04:00
|
|
|
|
|
|
|
describe '.for_action' do
|
|
|
|
it 'returns the todos for a given action' do
|
|
|
|
create(:todo, action: Todo::MENTIONED)
|
|
|
|
|
|
|
|
todo = create(:todo, action: Todo::ASSIGNED)
|
|
|
|
|
|
|
|
expect(described_class.for_action(Todo::ASSIGNED)).to eq([todo])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_author' do
|
|
|
|
it 'returns the todos for a given author' do
|
|
|
|
user1 = create(:user)
|
|
|
|
user2 = create(:user)
|
|
|
|
todo = create(:todo, author: user1)
|
|
|
|
|
|
|
|
create(:todo, author: user2)
|
|
|
|
|
|
|
|
expect(described_class.for_author(user1)).to eq([todo])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_project' do
|
|
|
|
it 'returns the todos for a given project' do
|
|
|
|
project1 = create(:project)
|
|
|
|
project2 = create(:project)
|
|
|
|
todo = create(:todo, project: project1)
|
|
|
|
|
|
|
|
create(:todo, project: project2)
|
|
|
|
|
|
|
|
expect(described_class.for_project(project1)).to eq([todo])
|
|
|
|
end
|
2019-11-15 10:06:12 -05:00
|
|
|
|
|
|
|
it 'returns the todos for many projects' do
|
|
|
|
project1 = create(:project)
|
|
|
|
project2 = create(:project)
|
|
|
|
project3 = create(:project)
|
|
|
|
|
|
|
|
todo1 = create(:todo, project: project1)
|
|
|
|
todo2 = create(:todo, project: project2)
|
|
|
|
create(:todo, project: project3)
|
|
|
|
|
|
|
|
expect(described_class.for_project([project2, project1])).to contain_exactly(todo2, todo1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_undeleted_projects' do
|
|
|
|
let(:project1) { create(:project) }
|
|
|
|
let(:project2) { create(:project) }
|
|
|
|
let(:project3) { create(:project) }
|
|
|
|
|
|
|
|
let!(:todo1) { create(:todo, project: project1) }
|
|
|
|
let!(:todo2) { create(:todo, project: project2) }
|
|
|
|
let!(:todo3) { create(:todo, project: project3) }
|
|
|
|
|
|
|
|
it 'returns the todos for a given project' do
|
|
|
|
expect(described_class.for_undeleted_projects).to contain_exactly(todo1, todo2, todo3)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when todo belongs to deleted project' do
|
|
|
|
let(:project2) { create(:project, pending_delete: true) }
|
|
|
|
|
|
|
|
it 'excludes todos of deleted projects' do
|
|
|
|
expect(described_class.for_undeleted_projects).to contain_exactly(todo1, todo3)
|
|
|
|
end
|
|
|
|
end
|
2018-09-20 09:18:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_group' do
|
|
|
|
it 'returns the todos for a given group' do
|
|
|
|
group1 = create(:group)
|
|
|
|
group2 = create(:group)
|
|
|
|
todo = create(:todo, group: group1)
|
|
|
|
|
|
|
|
create(:todo, group: group2)
|
|
|
|
|
|
|
|
expect(described_class.for_group(group1)).to eq([todo])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_type' do
|
|
|
|
it 'returns the todos for a given target type' do
|
|
|
|
todo = create(:todo, target: create(:issue))
|
|
|
|
|
|
|
|
create(:todo, target: create(:merge_request))
|
|
|
|
|
2018-11-17 10:14:36 -05:00
|
|
|
expect(described_class.for_type(Issue.name)).to eq([todo])
|
2018-09-20 09:18:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 11:05:26 -04:00
|
|
|
describe '.for_target' do
|
|
|
|
it 'returns the todos for a given target' do
|
|
|
|
todo = create(:todo, target: create(:issue))
|
|
|
|
|
|
|
|
create(:todo, target: create(:merge_request))
|
|
|
|
|
2018-11-30 14:47:08 -05:00
|
|
|
expect(described_class.for_type(Issue.name).for_target(todo.target))
|
|
|
|
.to contain_exactly(todo)
|
2018-09-20 11:05:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_commit' do
|
|
|
|
it 'returns the todos for a commit ID' do
|
|
|
|
todo = create(:todo, commit_id: '123')
|
|
|
|
|
|
|
|
create(:todo, commit_id: '456')
|
|
|
|
|
|
|
|
expect(described_class.for_commit('123')).to eq([todo])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-17 11:06:17 -04:00
|
|
|
describe '.for_group_ids_and_descendants' do
|
2018-09-20 09:18:04 -04:00
|
|
|
it 'returns the todos for a group and its descendants' do
|
|
|
|
parent_group = create(:group)
|
|
|
|
child_group = create(:group, parent: parent_group)
|
|
|
|
|
|
|
|
todo1 = create(:todo, group: parent_group)
|
|
|
|
todo2 = create(:todo, group: child_group)
|
2019-10-17 11:06:17 -04:00
|
|
|
todos = described_class.for_group_ids_and_descendants([parent_group.id])
|
2018-09-20 11:05:26 -04:00
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
expect(todos).to contain_exactly(todo1, todo2)
|
2018-09-20 11:05:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-31 22:09:04 -05:00
|
|
|
describe '.for_ids' do
|
|
|
|
it 'returns the expected todos' do
|
|
|
|
todo1 = create(:todo)
|
|
|
|
todo2 = create(:todo)
|
|
|
|
todo3 = create(:todo)
|
|
|
|
create(:todo)
|
|
|
|
|
|
|
|
expect(described_class.for_ids([todo2.id, todo1.id, todo3.id])).to contain_exactly(todo1, todo2, todo3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty collection when no ids are given' do
|
|
|
|
create(:todo)
|
|
|
|
|
|
|
|
expect(described_class.for_ids([])).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_user' do
|
|
|
|
it 'returns the expected todos' do
|
|
|
|
user1 = create(:user)
|
|
|
|
user2 = create(:user)
|
|
|
|
|
|
|
|
todo1 = create(:todo, user: user1)
|
|
|
|
todo2 = create(:todo, user: user1)
|
|
|
|
create(:todo, user: user2)
|
|
|
|
|
|
|
|
expect(described_class.for_user(user1)).to contain_exactly(todo1, todo2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 11:05:26 -04:00
|
|
|
describe '.any_for_target?' do
|
|
|
|
it 'returns true if there are todos for a given target' do
|
|
|
|
todo = create(:todo)
|
|
|
|
|
|
|
|
expect(described_class.any_for_target?(todo.target)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
it 'returns true if there is at least one todo for a given target with state pending' do
|
|
|
|
issue = create(:issue)
|
|
|
|
create(:todo, state: :done, target: issue)
|
|
|
|
create(:todo, state: :pending, target: issue)
|
|
|
|
|
|
|
|
expect(described_class.any_for_target?(issue)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if there are only todos for a given target with state done while searching for pending' do
|
|
|
|
issue = create(:issue)
|
|
|
|
create(:todo, state: :done, target: issue)
|
|
|
|
create(:todo, state: :done, target: issue)
|
|
|
|
|
|
|
|
expect(described_class.any_for_target?(issue, :pending)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2018-09-20 11:05:26 -04:00
|
|
|
it 'returns false if there are no todos for a given target' do
|
|
|
|
issue = create(:issue)
|
|
|
|
|
|
|
|
expect(described_class.any_for_target?(issue)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
describe '.batch_update' do
|
2018-09-20 11:05:26 -04:00
|
|
|
it 'updates the state of todos' do
|
|
|
|
todo = create(:todo, :pending)
|
2020-05-28 14:08:37 -04:00
|
|
|
ids = described_class.batch_update(state: :done)
|
2018-09-20 11:05:26 -04:00
|
|
|
|
|
|
|
todo.reload
|
|
|
|
|
|
|
|
expect(ids).to eq([todo.id])
|
|
|
|
expect(todo.state).to eq('done')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update todos that already have the given state' do
|
|
|
|
create(:todo, :pending)
|
2018-09-20 09:18:04 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
expect(described_class.batch_update(state: :pending)).to be_empty
|
2018-09-20 09:18:04 -04:00
|
|
|
end
|
2020-04-21 20:09:24 -04:00
|
|
|
|
|
|
|
it 'updates updated_at' do
|
|
|
|
create(:todo, :pending)
|
|
|
|
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(1.day.from_now) do
|
2020-05-22 05:08:09 -04:00
|
|
|
expected_update_date = Time.current.utc
|
2020-04-21 20:09:24 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
ids = described_class.batch_update(state: :done)
|
2020-04-21 20:09:24 -04:00
|
|
|
|
|
|
|
expect(Todo.where(id: ids).map(&:updated_at)).to all(be_like_time(expected_update_date))
|
|
|
|
end
|
|
|
|
end
|
2018-09-20 09:18:04 -04:00
|
|
|
end
|
2016-02-12 08:17:42 -05:00
|
|
|
end
|