debian-mirror-gitlab/spec/services/todo_service_spec.rb

987 lines
50 KiB
Ruby
Raw Normal View History

2016-04-02 18:10:28 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe TodoService do
2016-04-02 18:10:28 +05:30
let(:author) { create(:user) }
2016-06-02 11:05:42 +05:30
let(:assignee) { create(:user) }
let(:non_member) { create(:user) }
let(:member) { create(:user) }
let(:guest) { create(:user) }
2016-06-02 11:05:42 +05:30
let(:admin) { create(:admin) }
let(:john_doe) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:skipped) { create(:user) }
let(:skip_users) { [skipped] }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2017-08-17 22:00:37 +05:30
let(:mentions) { 'FYI: ' + [author, assignee, john_doe, member, guest, non_member, admin, skipped].map(&:to_reference).join(' ') }
let(:directly_addressed) { [author, assignee, john_doe, member, guest, non_member, admin, skipped].map(&:to_reference).join(' ') }
let(:directly_addressed_and_mentioned) { member.to_reference + ", what do you think? cc: " + [guest, admin, skipped].map(&:to_reference).join(' ') }
2016-04-02 18:10:28 +05:30
let(:service) { described_class.new }
before do
2018-03-17 18:26:18 +05:30
project.add_guest(guest)
project.add_developer(author)
project.add_developer(member)
project.add_developer(john_doe)
project.add_developer(skipped)
2016-04-02 18:10:28 +05:30
end
describe 'Issues' do
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, project: project, assignees: [john_doe], author: author, description: "- [ ] Task 1\n- [ ] Task 2 #{mentions}") }
let(:addressed_issue) { create(:issue, project: project, assignees: [john_doe], author: author, description: "#{directly_addressed}\n- [ ] Task 1\n- [ ] Task 2") }
let(:unassigned_issue) { create(:issue, project: project, assignees: []) }
let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee], description: mentions) }
let(:addressed_confident_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee], description: directly_addressed) }
2016-04-02 18:10:28 +05:30
describe '#new_issue' do
it 'creates a todo if assigned' do
service.new_issue(issue, author)
should_create_todo(user: john_doe, target: issue, action: Todo::ASSIGNED)
end
it 'does not create a todo if unassigned' do
should_not_create_any_todo { service.new_issue(unassigned_issue, author) }
end
2016-08-24 12:49:21 +05:30
it 'creates a todo if assignee is the current user' do
2017-08-17 22:00:37 +05:30
unassigned_issue.assignees = [john_doe]
2016-08-24 12:49:21 +05:30
service.new_issue(unassigned_issue, john_doe)
should_create_todo(user: john_doe, target: unassigned_issue, author: john_doe, action: Todo::ASSIGNED)
2016-04-02 18:10:28 +05:30
end
it 'creates a todo for each valid mentioned user' do
service.new_issue(issue, author)
2016-06-02 11:05:42 +05:30
should_create_todo(user: member, target: issue, action: Todo::MENTIONED)
should_create_todo(user: guest, target: issue, action: Todo::MENTIONED)
2016-08-24 12:49:21 +05:30
should_create_todo(user: author, target: issue, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
should_not_create_todo(user: john_doe, target: issue, action: Todo::MENTIONED)
2016-06-02 11:05:42 +05:30
should_not_create_todo(user: non_member, target: issue, action: Todo::MENTIONED)
end
2017-08-17 22:00:37 +05:30
it 'creates a directly addressed todo for each valid addressed user' do
service.new_issue(addressed_issue, author)
should_create_todo(user: member, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: guest, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: author, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: john_doe, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
end
it 'creates correct todos for each valid user based on the type of mention' do
issue.update(description: directly_addressed_and_mentioned)
service.new_issue(issue, author)
should_create_todo(user: member, target: issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: admin, target: issue, action: Todo::MENTIONED)
should_create_todo(user: guest, target: issue, action: Todo::MENTIONED)
end
it 'does not create todo if user can not see the issue when issue is confidential' do
2016-06-02 11:05:42 +05:30
service.new_issue(confidential_issue, john_doe)
should_create_todo(user: assignee, target: confidential_issue, author: john_doe, action: Todo::ASSIGNED)
should_create_todo(user: author, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_create_todo(user: member, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_create_todo(user: admin, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_not_create_todo(user: guest, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
2016-08-24 12:49:21 +05:30
should_create_todo(user: john_doe, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not create directly addressed todo if user cannot see the issue when issue is confidential' do
service.new_issue(addressed_confident_issue, john_doe)
should_create_todo(user: assignee, target: addressed_confident_issue, author: john_doe, action: Todo::ASSIGNED)
should_create_todo(user: author, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: member, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: admin, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: guest, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: john_doe, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
end
2016-06-02 11:05:42 +05:30
context 'when a private group is mentioned' do
2017-08-17 22:00:37 +05:30
let(:group) { create(:group, :private) }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private, group: group) }
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, author: author, project: project, description: group.to_reference) }
2016-06-02 11:05:42 +05:30
before do
group.add_owner(author)
group.add_user(member, Gitlab::Access::DEVELOPER)
group.add_user(john_doe, Gitlab::Access::DEVELOPER)
service.new_issue(issue, author)
end
it 'creates a todo for group members' do
should_create_todo(user: member, target: issue)
should_create_todo(user: john_doe, target: issue)
end
2016-04-02 18:10:28 +05:30
end
end
describe '#update_issue' do
2017-08-17 22:00:37 +05:30
it 'creates a todo for each valid mentioned user not included in skip_users' do
service.update_issue(issue, author, skip_users)
2016-04-02 18:10:28 +05:30
2016-06-02 11:05:42 +05:30
should_create_todo(user: member, target: issue, action: Todo::MENTIONED)
should_create_todo(user: guest, target: issue, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
should_create_todo(user: john_doe, target: issue, action: Todo::MENTIONED)
2016-08-24 12:49:21 +05:30
should_create_todo(user: author, target: issue, action: Todo::MENTIONED)
2016-06-02 11:05:42 +05:30
should_not_create_todo(user: non_member, target: issue, action: Todo::MENTIONED)
2017-08-17 22:00:37 +05:30
should_not_create_todo(user: skipped, target: issue, action: Todo::MENTIONED)
end
it 'creates a todo for each valid user not included in skip_users based on the type of mention' do
issue.update(description: directly_addressed_and_mentioned)
service.update_issue(issue, author, skip_users)
should_create_todo(user: member, target: issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: guest, target: issue, action: Todo::MENTIONED)
should_create_todo(user: admin, target: issue, action: Todo::MENTIONED)
should_not_create_todo(user: skipped, target: issue)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'creates a directly addressed todo for each valid addressed user not included in skip_users' do
service.update_issue(addressed_issue, author, skip_users)
should_create_todo(user: member, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: guest, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: john_doe, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: author, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: skipped, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
end
it 'does not create a todo if user was already mentioned and todo is pending' do
2016-06-02 11:05:42 +05:30
create(:todo, :mentioned, user: member, project: project, target: issue, author: author)
2017-08-17 22:00:37 +05:30
expect { service.update_issue(issue, author, skip_users) }.not_to change(member.todos, :count)
end
it 'does not create a todo if user was already mentioned and todo is done' do
create(:todo, :mentioned, :done, user: skipped, project: project, target: issue, author: author)
expect { service.update_issue(issue, author, skip_users) }.not_to change(skipped.todos, :count)
end
it 'does not create a directly addressed todo if user was already mentioned or addressed and todo is pending' do
create(:todo, :directly_addressed, user: member, project: project, target: addressed_issue, author: author)
expect { service.update_issue(addressed_issue, author, skip_users) }.not_to change(member.todos, :count)
end
it 'does not create a directly addressed todo if user was already mentioned or addressed and todo is done' do
create(:todo, :directly_addressed, :done, user: skipped, project: project, target: addressed_issue, author: author)
expect { service.update_issue(addressed_issue, author, skip_users) }.not_to change(skipped.todos, :count)
2016-06-02 11:05:42 +05:30
end
2016-04-02 18:10:28 +05:30
it 'does not create todo if user can not see the issue when issue is confidential' do
2016-06-02 11:05:42 +05:30
service.update_issue(confidential_issue, john_doe)
should_create_todo(user: author, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_create_todo(user: assignee, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_create_todo(user: member, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_create_todo(user: admin, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
should_not_create_todo(user: guest, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
2016-08-24 12:49:21 +05:30
should_create_todo(user: john_doe, target: confidential_issue, author: john_doe, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not create a directly addressed todo if user can not see the issue when issue is confidential' do
service.update_issue(addressed_confident_issue, john_doe)
should_create_todo(user: author, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: assignee, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: member, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: admin, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: guest, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: john_doe, target: addressed_confident_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED)
end
2016-06-22 15:30:34 +05:30
context 'issues with a task list' do
it 'does not create todo when tasks are marked as completed' do
issue.update(description: "- [x] Task 1\n- [X] Task 2 #{mentions}")
service.update_issue(issue, author)
should_not_create_todo(user: admin, target: issue, action: Todo::MENTIONED)
should_not_create_todo(user: assignee, target: issue, action: Todo::MENTIONED)
should_not_create_todo(user: author, target: issue, action: Todo::MENTIONED)
should_not_create_todo(user: john_doe, target: issue, action: Todo::MENTIONED)
should_not_create_todo(user: member, target: issue, action: Todo::MENTIONED)
should_not_create_todo(user: non_member, target: issue, action: Todo::MENTIONED)
end
2017-08-17 22:00:37 +05:30
it 'does not create directly addressed todo when tasks are marked as completed' do
addressed_issue.update(description: "#{directly_addressed}\n- [x] Task 1\n- [x] Task 2\n")
service.update_issue(addressed_issue, author)
should_not_create_todo(user: admin, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: assignee, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: author, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: john_doe, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: member, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: addressed_issue, action: Todo::DIRECTLY_ADDRESSED)
end
2016-06-22 15:30:34 +05:30
it 'does not raise an error when description not change' do
issue.update(title: 'Sample')
2016-06-22 15:30:34 +05:30
expect { service.update_issue(issue, author) }.not_to raise_error
end
end
2016-04-02 18:10:28 +05:30
end
describe '#close_issue' do
it 'marks related pending todos to the target for the user as done' do
first_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
second_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
service.close_issue(issue, john_doe)
expect(first_todo.reload).to be_done
expect(second_todo.reload).to be_done
end
end
2018-03-17 18:26:18 +05:30
describe '#destroy_target' do
it 'refreshes the todos count cache for users with todos on the target' do
create(:todo, target: issue, user: john_doe, author: john_doe, project: issue.project)
2016-09-29 09:46:39 +05:30
2018-03-17 18:26:18 +05:30
expect_any_instance_of(User).to receive(:update_todos_count_cache).and_call_original
service.destroy_target(issue) { }
end
it 'does not refresh the todos count cache for users with only done todos on the target' do
create(:todo, :done, target: issue, user: john_doe, author: john_doe, project: issue.project)
expect_any_instance_of(User).not_to receive(:update_todos_count_cache)
service.destroy_target(issue) { }
end
it 'yields the target to the caller' do
expect { |b| service.destroy_target(issue, &b) }
.to yield_with_args(issue)
2016-09-29 09:46:39 +05:30
end
end
2016-04-02 18:10:28 +05:30
describe '#reassigned_issue' do
it 'creates a pending todo for new assignee' do
2017-08-17 22:00:37 +05:30
unassigned_issue.assignees << john_doe
2016-04-02 18:10:28 +05:30
service.reassigned_issue(unassigned_issue, author)
should_create_todo(user: john_doe, target: unassigned_issue, action: Todo::ASSIGNED)
end
it 'does not create a todo if unassigned' do
2017-08-17 22:00:37 +05:30
issue.assignees.destroy_all
2016-04-02 18:10:28 +05:30
should_not_create_any_todo { service.reassigned_issue(issue, author) }
end
2016-08-24 12:49:21 +05:30
it 'creates a todo if new assignee is the current user' do
2017-08-17 22:00:37 +05:30
unassigned_issue.assignees << john_doe
2016-08-24 12:49:21 +05:30
service.reassigned_issue(unassigned_issue, john_doe)
2016-04-02 18:10:28 +05:30
2016-08-24 12:49:21 +05:30
should_create_todo(user: john_doe, target: unassigned_issue, author: john_doe, action: Todo::ASSIGNED)
2016-04-02 18:10:28 +05:30
end
end
describe '#mark_pending_todos_as_done' do
it 'marks related pending todos to the target for the user as done' do
first_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
second_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
service.mark_pending_todos_as_done(issue, john_doe)
expect(first_todo.reload).to be_done
expect(second_todo.reload).to be_done
end
2016-06-22 15:30:34 +05:30
describe 'cached counts' do
it 'updates when todos change' do
create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
expect(john_doe.todos_done_count).to eq(0)
expect(john_doe.todos_pending_count).to eq(1)
expect(john_doe).to receive(:update_todos_count_cache).and_call_original
service.mark_pending_todos_as_done(issue, john_doe)
expect(john_doe.todos_done_count).to eq(1)
expect(john_doe.todos_pending_count).to eq(0)
end
end
end
2017-08-17 22:00:37 +05:30
shared_examples 'updating todos state' do |meth, state, new_state|
let!(:first_todo) { create(:todo, state, user: john_doe, project: project, target: issue, author: author) }
let!(:second_todo) { create(:todo, state, user: john_doe, project: project, target: issue, author: author) }
2016-06-22 15:30:34 +05:30
2017-08-17 22:00:37 +05:30
it 'updates related todos for the user with the new_state' do
2016-09-13 17:45:13 +05:30
service.send(meth, collection, john_doe)
2016-06-22 15:30:34 +05:30
2017-08-17 22:00:37 +05:30
expect(first_todo.reload.state?(new_state)).to be true
expect(second_todo.reload.state?(new_state)).to be true
end
it 'returns the updated ids' do
expect(service.send(meth, collection, john_doe)).to match_array([first_todo.id, second_todo.id])
2016-06-22 15:30:34 +05:30
end
describe 'cached counts' do
it 'updates when todos change' do
2017-08-17 22:00:37 +05:30
expect(john_doe.todos.where(state: new_state).count).to eq(0)
expect(john_doe.todos.where(state: state).count).to eq(2)
2016-06-22 15:30:34 +05:30
expect(john_doe).to receive(:update_todos_count_cache).and_call_original
2016-09-13 17:45:13 +05:30
service.send(meth, collection, john_doe)
2016-06-22 15:30:34 +05:30
2017-08-17 22:00:37 +05:30
expect(john_doe.todos.where(state: new_state).count).to eq(2)
expect(john_doe.todos.where(state: state).count).to eq(0)
2016-06-22 15:30:34 +05:30
end
end
2016-04-02 18:10:28 +05:30
end
2016-09-13 17:45:13 +05:30
describe '#mark_todos_as_done' do
2017-08-17 22:00:37 +05:30
it_behaves_like 'updating todos state', :mark_todos_as_done, :pending, :done do
2017-09-10 17:25:29 +05:30
let(:collection) { Todo.all }
2016-09-13 17:45:13 +05:30
end
end
describe '#mark_todos_as_done_by_ids' do
2017-08-17 22:00:37 +05:30
it_behaves_like 'updating todos state', :mark_todos_as_done_by_ids, :pending, :done do
let(:collection) { [first_todo, second_todo].map(&:id) }
end
end
describe '#mark_todos_as_pending' do
it_behaves_like 'updating todos state', :mark_todos_as_pending, :done, :pending do
2017-09-10 17:25:29 +05:30
let(:collection) { Todo.all }
2017-08-17 22:00:37 +05:30
end
end
describe '#mark_todos_as_pending_by_ids' do
it_behaves_like 'updating todos state', :mark_todos_as_pending_by_ids, :done, :pending do
2016-09-13 17:45:13 +05:30
let(:collection) { [first_todo, second_todo].map(&:id) }
end
end
2016-04-02 18:10:28 +05:30
describe '#new_note' do
let!(:first_todo) { create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) }
let!(:second_todo) { create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) }
2017-08-17 22:00:37 +05:30
let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee]) }
2016-04-02 18:10:28 +05:30
let(:note) { create(:note, project: project, noteable: issue, author: john_doe, note: mentions) }
2017-08-17 22:00:37 +05:30
let(:addressed_note) { create(:note, project: project, noteable: issue, author: john_doe, note: directly_addressed) }
2016-04-02 18:10:28 +05:30
let(:note_on_commit) { create(:note_on_commit, project: project, author: john_doe, note: mentions) }
2017-08-17 22:00:37 +05:30
let(:addressed_note_on_commit) { create(:note_on_commit, project: project, author: john_doe, note: directly_addressed) }
2016-06-02 11:05:42 +05:30
let(:note_on_confidential_issue) { create(:note_on_issue, noteable: confidential_issue, project: project, note: mentions) }
2017-08-17 22:00:37 +05:30
let(:addressed_note_on_confidential_issue) { create(:note_on_issue, noteable: confidential_issue, project: project, note: directly_addressed) }
2016-04-02 18:10:28 +05:30
let(:note_on_project_snippet) { create(:note_on_project_snippet, project: project, author: john_doe, note: mentions) }
let(:system_note) { create(:system_note, project: project, noteable: issue) }
it 'mark related pending todos to the noteable for the note author as done' do
first_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
second_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
service.new_note(note, john_doe)
expect(first_todo.reload).to be_done
expect(second_todo.reload).to be_done
end
it 'does not mark related pending todos it is a system note' do
service.new_note(system_note, john_doe)
expect(first_todo.reload).to be_pending
expect(second_todo.reload).to be_pending
end
it 'creates a todo for each valid mentioned user' do
service.new_note(note, john_doe)
2016-06-02 11:05:42 +05:30
should_create_todo(user: member, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
should_create_todo(user: guest, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
2016-04-02 18:10:28 +05:30
should_create_todo(user: author, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
2016-08-24 12:49:21 +05:30
should_create_todo(user: john_doe, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
2016-06-02 11:05:42 +05:30
should_not_create_todo(user: non_member, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'creates a todo for each valid user based on the type of mention' do
note.update(note: directly_addressed_and_mentioned)
service.new_note(note, john_doe)
should_create_todo(user: member, target: issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: note)
should_create_todo(user: admin, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
should_create_todo(user: guest, target: issue, author: john_doe, action: Todo::MENTIONED, note: note)
end
it 'creates a directly addressed todo for each valid addressed user' do
service.new_note(addressed_note, john_doe)
should_create_todo(user: member, target: issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note)
should_create_todo(user: guest, target: issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note)
should_create_todo(user: author, target: issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note)
should_create_todo(user: john_doe, target: issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note)
should_not_create_todo(user: non_member, target: issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note)
end
it 'does not create todo if user can not see the issue when leaving a note on a confidential issue' do
2016-06-02 11:05:42 +05:30
service.new_note(note_on_confidential_issue, john_doe)
should_create_todo(user: author, target: confidential_issue, author: john_doe, action: Todo::MENTIONED, note: note_on_confidential_issue)
should_create_todo(user: assignee, target: confidential_issue, author: john_doe, action: Todo::MENTIONED, note: note_on_confidential_issue)
should_create_todo(user: member, target: confidential_issue, author: john_doe, action: Todo::MENTIONED, note: note_on_confidential_issue)
should_create_todo(user: admin, target: confidential_issue, author: john_doe, action: Todo::MENTIONED, note: note_on_confidential_issue)
should_not_create_todo(user: guest, target: confidential_issue, author: john_doe, action: Todo::MENTIONED, note: note_on_confidential_issue)
2016-08-24 12:49:21 +05:30
should_create_todo(user: john_doe, target: confidential_issue, author: john_doe, action: Todo::MENTIONED, note: note_on_confidential_issue)
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not create a directly addressed todo if user can not see the issue when leaving a note on a confidential issue' do
service.new_note(addressed_note_on_confidential_issue, john_doe)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
should_create_todo(user: author, target: confidential_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_confidential_issue)
should_create_todo(user: assignee, target: confidential_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_confidential_issue)
should_create_todo(user: member, target: confidential_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_confidential_issue)
should_create_todo(user: admin, target: confidential_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_confidential_issue)
should_not_create_todo(user: guest, target: confidential_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_confidential_issue)
should_create_todo(user: john_doe, target: confidential_issue, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_confidential_issue)
end
context 'on commit' do
let(:project) { create(:project, :repository) }
it 'creates a todo for each valid mentioned user when leaving a note on commit' do
service.new_note(note_on_commit, john_doe)
should_create_todo(user: member, target_id: nil, target_type: 'Commit', commit_id: note_on_commit.commit_id, author: john_doe, action: Todo::MENTIONED, note: note_on_commit)
should_create_todo(user: author, target_id: nil, target_type: 'Commit', commit_id: note_on_commit.commit_id, author: john_doe, action: Todo::MENTIONED, note: note_on_commit)
should_create_todo(user: john_doe, target_id: nil, target_type: 'Commit', commit_id: note_on_commit.commit_id, author: john_doe, action: Todo::MENTIONED, note: note_on_commit)
should_not_create_todo(user: non_member, target_id: nil, target_type: 'Commit', commit_id: note_on_commit.commit_id, author: john_doe, action: Todo::MENTIONED, note: note_on_commit)
end
it 'creates a directly addressed todo for each valid mentioned user when leaving a note on commit' do
service.new_note(addressed_note_on_commit, john_doe)
should_create_todo(user: member, target_id: nil, target_type: 'Commit', commit_id: addressed_note_on_commit.commit_id, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_commit)
should_create_todo(user: author, target_id: nil, target_type: 'Commit', commit_id: addressed_note_on_commit.commit_id, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_commit)
should_create_todo(user: john_doe, target_id: nil, target_type: 'Commit', commit_id: addressed_note_on_commit.commit_id, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_commit)
should_not_create_todo(user: non_member, target_id: nil, target_type: 'Commit', commit_id: addressed_note_on_commit.commit_id, author: john_doe, action: Todo::DIRECTLY_ADDRESSED, note: addressed_note_on_commit)
end
2016-04-02 18:10:28 +05:30
end
it 'does not create todo when leaving a note on snippet' do
should_not_create_any_todo { service.new_note(note_on_project_snippet, john_doe) }
end
end
describe '#mark_todo' do
it 'creates a todo from a issue' do
service.mark_todo(unassigned_issue, author)
should_create_todo(user: author, target: unassigned_issue, action: Todo::MARKED)
end
end
2016-09-13 17:45:13 +05:30
describe '#todo_exists?' do
it 'returns false when no todo exist for the given issuable' do
expect(service.todo_exist?(unassigned_issue, author)).to be_falsy
end
it 'returns true when a todo exist for the given issuable' do
service.mark_todo(unassigned_issue, author)
expect(service.todo_exist?(unassigned_issue, author)).to be_truthy
end
end
2016-04-02 18:10:28 +05:30
end
describe 'Merge Requests' do
let(:mr_assigned) { create(:merge_request, source_project: project, author: author, assignee: john_doe, description: "- [ ] Task 1\n- [ ] Task 2 #{mentions}") }
2017-08-17 22:00:37 +05:30
let(:addressed_mr_assigned) { create(:merge_request, source_project: project, author: author, assignee: john_doe, description: "#{directly_addressed}\n- [ ] Task 1\n- [ ] Task 2") }
2016-04-02 18:10:28 +05:30
let(:mr_unassigned) { create(:merge_request, source_project: project, author: author, assignee: nil) }
describe '#new_merge_request' do
it 'creates a pending todo if assigned' do
service.new_merge_request(mr_assigned, author)
should_create_todo(user: john_doe, target: mr_assigned, action: Todo::ASSIGNED)
end
it 'does not create a todo if unassigned' do
should_not_create_any_todo { service.new_merge_request(mr_unassigned, author) }
end
it 'does not create a todo if assignee is the current user' do
should_not_create_any_todo { service.new_merge_request(mr_unassigned, john_doe) }
end
it 'creates a todo for each valid mentioned user' do
service.new_merge_request(mr_assigned, author)
2016-06-02 11:05:42 +05:30
should_create_todo(user: member, target: mr_assigned, action: Todo::MENTIONED)
2016-11-03 12:29:30 +05:30
should_not_create_todo(user: guest, target: mr_assigned, action: Todo::MENTIONED)
2016-08-24 12:49:21 +05:30
should_create_todo(user: author, target: mr_assigned, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
should_not_create_todo(user: john_doe, target: mr_assigned, action: Todo::MENTIONED)
2016-06-02 11:05:42 +05:30
should_not_create_todo(user: non_member, target: mr_assigned, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'creates a todo for each valid user based on the type of mention' do
mr_assigned.update(description: directly_addressed_and_mentioned)
service.new_merge_request(mr_assigned, author)
should_create_todo(user: member, target: mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: admin, target: mr_assigned, action: Todo::MENTIONED)
end
it 'creates a directly addressed todo for each valid addressed user' do
service.new_merge_request(addressed_mr_assigned, author)
should_create_todo(user: member, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: guest, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: author, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: john_doe, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
end
2016-04-02 18:10:28 +05:30
end
describe '#update_merge_request' do
2017-08-17 22:00:37 +05:30
it 'creates a todo for each valid mentioned user not included in skip_users' do
service.update_merge_request(mr_assigned, author, skip_users)
2016-04-02 18:10:28 +05:30
2016-06-02 11:05:42 +05:30
should_create_todo(user: member, target: mr_assigned, action: Todo::MENTIONED)
2016-11-03 12:29:30 +05:30
should_not_create_todo(user: guest, target: mr_assigned, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
should_create_todo(user: john_doe, target: mr_assigned, action: Todo::MENTIONED)
2016-08-24 12:49:21 +05:30
should_create_todo(user: author, target: mr_assigned, action: Todo::MENTIONED)
2016-06-02 11:05:42 +05:30
should_not_create_todo(user: non_member, target: mr_assigned, action: Todo::MENTIONED)
2017-08-17 22:00:37 +05:30
should_not_create_todo(user: skipped, target: mr_assigned, action: Todo::MENTIONED)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'creates a todo for each valid user not included in skip_users based on the type of mention' do
mr_assigned.update(description: directly_addressed_and_mentioned)
service.update_merge_request(mr_assigned, author, skip_users)
should_create_todo(user: member, target: mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: admin, target: mr_assigned, action: Todo::MENTIONED)
should_not_create_todo(user: skipped, target: mr_assigned)
end
it 'creates a directly addressed todo for each valid addressed user not included in skip_users' do
service.update_merge_request(addressed_mr_assigned, author, skip_users)
should_create_todo(user: member, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: guest, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: john_doe, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: author, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: skipped, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
end
it 'does not create a todo if user was already mentioned and todo is pending' do
2016-06-02 11:05:42 +05:30
create(:todo, :mentioned, user: member, project: project, target: mr_assigned, author: author)
2016-04-02 18:10:28 +05:30
2016-06-02 11:05:42 +05:30
expect { service.update_merge_request(mr_assigned, author) }.not_to change(member.todos, :count)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not create a todo if user was already mentioned and todo is done' do
create(:todo, :mentioned, :done, user: skipped, project: project, target: mr_assigned, author: author)
expect { service.update_merge_request(mr_assigned, author, skip_users) }.not_to change(skipped.todos, :count)
end
it 'does not create a directly addressed todo if user was already mentioned or addressed and todo is pending' do
create(:todo, :directly_addressed, user: member, project: project, target: addressed_mr_assigned, author: author)
2018-03-17 18:26:18 +05:30
expect { service.update_merge_request(addressed_mr_assigned, author) }.not_to change(member.todos, :count)
2017-08-17 22:00:37 +05:30
end
it 'does not create a directly addressed todo if user was already mentioned or addressed and todo is done' do
create(:todo, :directly_addressed, user: skipped, project: project, target: addressed_mr_assigned, author: author)
2018-03-17 18:26:18 +05:30
expect { service.update_merge_request(addressed_mr_assigned, author, skip_users) }.not_to change(skipped.todos, :count)
2017-08-17 22:00:37 +05:30
end
2016-06-22 15:30:34 +05:30
context 'with a task list' do
it 'does not create todo when tasks are marked as completed' do
mr_assigned.update(description: "- [x] Task 1\n- [X] Task 2 #{mentions}")
2016-06-22 15:30:34 +05:30
service.update_merge_request(mr_assigned, author)
2016-06-22 15:30:34 +05:30
should_not_create_todo(user: admin, target: mr_assigned, action: Todo::MENTIONED)
should_not_create_todo(user: assignee, target: mr_assigned, action: Todo::MENTIONED)
should_not_create_todo(user: author, target: mr_assigned, action: Todo::MENTIONED)
should_not_create_todo(user: john_doe, target: mr_assigned, action: Todo::MENTIONED)
should_not_create_todo(user: member, target: mr_assigned, action: Todo::MENTIONED)
should_not_create_todo(user: non_member, target: mr_assigned, action: Todo::MENTIONED)
2016-11-03 12:29:30 +05:30
should_not_create_todo(user: guest, target: mr_assigned, action: Todo::MENTIONED)
2016-06-22 15:30:34 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not create directly addressed todo when tasks are marked as completed' do
addressed_mr_assigned.update(description: "#{directly_addressed}\n- [x] Task 1\n- [X] Task 2")
service.update_merge_request(addressed_mr_assigned, author)
should_not_create_todo(user: admin, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: assignee, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: author, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: john_doe, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: member, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: guest, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
end
2016-06-22 15:30:34 +05:30
it 'does not raise an error when description not change' do
mr_assigned.update(title: 'Sample')
expect { service.update_merge_request(mr_assigned, author) }.not_to raise_error
end
end
2016-04-02 18:10:28 +05:30
end
describe '#close_merge_request' do
it 'marks related pending todos to the target for the user as done' do
first_todo = create(:todo, :assigned, user: john_doe, project: project, target: mr_assigned, author: author)
second_todo = create(:todo, :assigned, user: john_doe, project: project, target: mr_assigned, author: author)
service.close_merge_request(mr_assigned, john_doe)
expect(first_todo.reload).to be_done
expect(second_todo.reload).to be_done
end
end
describe '#reassigned_merge_request' do
it 'creates a pending todo for new assignee' do
mr_unassigned.update_attribute(:assignee, john_doe)
service.reassigned_merge_request(mr_unassigned, author)
should_create_todo(user: john_doe, target: mr_unassigned, action: Todo::ASSIGNED)
end
it 'does not create a todo if unassigned' do
mr_assigned.update_attribute(:assignee, nil)
should_not_create_any_todo { service.reassigned_merge_request(mr_assigned, author) }
end
2016-08-24 12:49:21 +05:30
it 'creates a todo if new assignee is the current user' do
2016-04-02 18:10:28 +05:30
mr_assigned.update_attribute(:assignee, john_doe)
2016-08-24 12:49:21 +05:30
service.reassigned_merge_request(mr_assigned, john_doe)
2016-04-02 18:10:28 +05:30
2016-08-24 12:49:21 +05:30
should_create_todo(user: john_doe, target: mr_assigned, author: john_doe, action: Todo::ASSIGNED)
2016-04-02 18:10:28 +05:30
end
2016-11-03 12:29:30 +05:30
it 'does not create a todo for guests' do
service.reassigned_merge_request(mr_assigned, author)
should_not_create_todo(user: guest, target: mr_assigned, action: Todo::MENTIONED)
end
2017-08-17 22:00:37 +05:30
it 'does not create a directly addressed todo for guests' do
service.reassigned_merge_request(addressed_mr_assigned, author)
should_not_create_todo(user: guest, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
end
2016-04-02 18:10:28 +05:30
end
describe '#merge_merge_request' do
it 'marks related pending todos to the target for the user as done' do
first_todo = create(:todo, :assigned, user: john_doe, project: project, target: mr_assigned, author: author)
second_todo = create(:todo, :assigned, user: john_doe, project: project, target: mr_assigned, author: author)
service.merge_merge_request(mr_assigned, john_doe)
expect(first_todo.reload).to be_done
expect(second_todo.reload).to be_done
end
2016-11-03 12:29:30 +05:30
it 'does not create todo for guests' do
service.merge_merge_request(mr_assigned, john_doe)
should_not_create_todo(user: guest, target: mr_assigned, action: Todo::MENTIONED)
end
2017-08-17 22:00:37 +05:30
it 'does not create directly addressed todo for guests' do
service.merge_merge_request(addressed_mr_assigned, john_doe)
should_not_create_todo(user: guest, target: addressed_mr_assigned, action: Todo::DIRECTLY_ADDRESSED)
end
2016-04-02 18:10:28 +05:30
end
2016-06-02 11:05:42 +05:30
describe '#new_award_emoji' do
it 'marks related pending todos to the target for the user as done' do
todo = create(:todo, user: john_doe, project: project, target: mr_assigned, author: author)
service.new_award_emoji(mr_assigned, john_doe)
expect(todo.reload).to be_done
end
end
2016-06-02 11:05:42 +05:30
describe '#merge_request_build_failed' do
2018-11-08 19:23:39 +05:30
let(:merge_participants) { [mr_unassigned.author, admin] }
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
before do
allow(mr_unassigned).to receive(:merge_participants).and_return(merge_participants)
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
it 'creates a pending todo for each merge_participant' do
2017-08-17 22:00:37 +05:30
service.merge_request_build_failed(mr_unassigned)
2018-11-08 19:23:39 +05:30
merge_participants.each do |participant|
should_create_todo(user: participant, author: participant, target: mr_unassigned, action: Todo::BUILD_FAILED)
end
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
end
describe '#merge_request_push' do
it 'marks related pending todos to the target for the user as done' do
first_todo = create(:todo, :build_failed, user: author, project: project, target: mr_assigned, author: john_doe)
second_todo = create(:todo, :build_failed, user: john_doe, project: project, target: mr_assigned, author: john_doe)
service.merge_request_push(mr_assigned, author)
expect(first_todo.reload).to be_done
expect(second_todo.reload).not_to be_done
end
end
2017-08-17 22:00:37 +05:30
describe '#merge_request_became_unmergeable' do
2018-11-08 19:23:39 +05:30
let(:merge_participants) { [admin, create(:user)] }
before do
allow(mr_unassigned).to receive(:merge_participants).and_return(merge_participants)
end
it 'creates a pending todo for each merge_participant' do
2017-08-17 22:00:37 +05:30
mr_unassigned.update(merge_when_pipeline_succeeds: true, merge_user: admin)
service.merge_request_became_unmergeable(mr_unassigned)
2018-11-08 19:23:39 +05:30
merge_participants.each do |participant|
should_create_todo(user: participant, author: participant, target: mr_unassigned, action: Todo::UNMERGEABLE)
end
2017-08-17 22:00:37 +05:30
end
end
describe '#mark_todo' do
it 'creates a todo from a merge request' do
service.mark_todo(mr_unassigned, author)
should_create_todo(user: author, target: mr_unassigned, action: Todo::MARKED)
end
end
2016-08-24 12:49:21 +05:30
describe '#new_note' do
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2016-08-24 12:49:21 +05:30
let(:mention) { john_doe.to_reference }
let(:diff_note_on_merge_request) { create(:diff_note_on_merge_request, project: project, noteable: mr_unassigned, author: author, note: "Hey #{mention}") }
2017-08-17 22:00:37 +05:30
let(:addressed_diff_note_on_merge_request) { create(:diff_note_on_merge_request, project: project, noteable: mr_unassigned, author: author, note: "#{mention}, hey!") }
2016-08-24 12:49:21 +05:30
let(:legacy_diff_note_on_merge_request) { create(:legacy_diff_note_on_merge_request, project: project, noteable: mr_unassigned, author: author, note: "Hey #{mention}") }
it 'creates a todo for mentioned user on new diff note' do
service.new_note(diff_note_on_merge_request, author)
should_create_todo(user: john_doe, target: mr_unassigned, author: author, action: Todo::MENTIONED, note: diff_note_on_merge_request)
end
2017-08-17 22:00:37 +05:30
it 'creates a directly addressed todo for addressed user on new diff note' do
service.new_note(addressed_diff_note_on_merge_request, author)
should_create_todo(user: john_doe, target: mr_unassigned, author: author, action: Todo::DIRECTLY_ADDRESSED, note: addressed_diff_note_on_merge_request)
end
2016-08-24 12:49:21 +05:30
it 'creates a todo for mentioned user on legacy diff note' do
service.new_note(legacy_diff_note_on_merge_request, author)
should_create_todo(user: john_doe, target: mr_unassigned, author: author, action: Todo::MENTIONED, note: legacy_diff_note_on_merge_request)
end
2016-11-03 12:29:30 +05:30
it 'does not create todo for guests' do
note_on_merge_request = create :note_on_merge_request, project: project, noteable: mr_assigned, note: mentions
service.new_note(note_on_merge_request, author)
should_not_create_todo(user: guest, target: mr_assigned, action: Todo::MENTIONED)
end
2016-08-24 12:49:21 +05:30
end
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
describe '#update_note' do
let(:noteable) { create(:issue, project: project) }
let(:note) { create(:note, project: project, note: mentions, noteable: noteable) }
let(:addressed_note) { create(:note, project: project, note: "#{directly_addressed}", noteable: noteable) }
it 'creates a todo for each valid mentioned user not included in skip_users' do
service.update_note(note, author, skip_users)
should_create_todo(user: member, target: noteable, action: Todo::MENTIONED)
should_create_todo(user: guest, target: noteable, action: Todo::MENTIONED)
should_create_todo(user: john_doe, target: noteable, action: Todo::MENTIONED)
should_create_todo(user: author, target: noteable, action: Todo::MENTIONED)
should_not_create_todo(user: non_member, target: noteable, action: Todo::MENTIONED)
should_not_create_todo(user: skipped, target: noteable, action: Todo::MENTIONED)
end
it 'creates a todo for each valid user not included in skip_users based on the type of mention' do
note.update(note: directly_addressed_and_mentioned)
service.update_note(note, author, skip_users)
should_create_todo(user: member, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: guest, target: noteable, action: Todo::MENTIONED)
should_create_todo(user: admin, target: noteable, action: Todo::MENTIONED)
should_not_create_todo(user: skipped, target: noteable)
end
it 'creates a directly addressed todo for each valid addressed user not included in skip_users' do
service.update_note(addressed_note, author, skip_users)
should_create_todo(user: member, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: guest, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: john_doe, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
should_create_todo(user: author, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: non_member, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
should_not_create_todo(user: skipped, target: noteable, action: Todo::DIRECTLY_ADDRESSED)
end
it 'does not create a todo if user was already mentioned and todo is pending' do
create(:todo, :mentioned, user: member, project: project, target: noteable, author: author)
expect { service.update_note(note, author, skip_users) }.not_to change(member.todos, :count)
end
it 'does not create a todo if user was already mentioned and todo is done' do
create(:todo, :mentioned, :done, user: skipped, project: project, target: noteable, author: author)
expect { service.update_note(note, author, skip_users) }.not_to change(skipped.todos, :count)
end
it 'does not create a directly addressed todo if user was already mentioned or addressed and todo is pending' do
create(:todo, :directly_addressed, user: member, project: project, target: noteable, author: author)
expect { service.update_note(addressed_note, author, skip_users) }.not_to change(member.todos, :count)
end
it 'does not create a directly addressed todo if user was already mentioned or addressed and todo is done' do
create(:todo, :directly_addressed, :done, user: skipped, project: project, target: noteable, author: author)
expect { service.update_note(addressed_note, author, skip_users) }.not_to change(skipped.todos, :count)
end
end
2016-06-22 15:30:34 +05:30
it 'updates cached counts when a todo is created' do
2017-08-17 22:00:37 +05:30
issue = create(:issue, project: project, assignees: [john_doe], author: author, description: mentions)
2016-06-22 15:30:34 +05:30
expect(john_doe.todos_pending_count).to eq(0)
2017-08-17 22:00:37 +05:30
expect(john_doe).to receive(:update_todos_count_cache).and_call_original
2016-06-22 15:30:34 +05:30
service.new_issue(issue, author)
expect(Todo.where(user_id: john_doe.id, state: :pending).count).to eq 1
expect(john_doe.todos_pending_count).to eq(1)
end
2016-09-13 17:45:13 +05:30
describe '#mark_todos_as_done' do
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, project: project, author: author, assignees: [john_doe]) }
let(:another_issue) { create(:issue, project: project, author: author, assignees: [john_doe]) }
2016-09-13 17:45:13 +05:30
it 'marks a relation of todos as done' do
create(:todo, :mentioned, user: john_doe, target: issue, project: project)
todos = TodosFinder.new(john_doe, {}).execute
2017-09-10 17:25:29 +05:30
expect { described_class.new.mark_todos_as_done(todos, john_doe) }
2016-09-13 17:45:13 +05:30
.to change { john_doe.todos.done.count }.from(0).to(1)
end
it 'marks an array of todos as done' do
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
2017-09-10 17:25:29 +05:30
todos = TodosFinder.new(john_doe, {}).execute
expect { described_class.new.mark_todos_as_done(todos, john_doe) }
2016-09-13 17:45:13 +05:30
.to change { todo.reload.state }.from('pending').to('done')
end
2017-08-17 22:00:37 +05:30
it 'returns the ids of updated todos' do # Needed on API
2016-09-13 17:45:13 +05:30
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
2017-09-10 17:25:29 +05:30
todos = TodosFinder.new(john_doe, {}).execute
expect(described_class.new.mark_todos_as_done(todos, john_doe)).to eq([todo.id])
2016-09-13 17:45:13 +05:30
end
2016-09-29 09:46:39 +05:30
context 'when some of the todos are done already' do
2017-08-17 22:00:37 +05:30
let!(:first_todo) { create(:todo, :mentioned, user: john_doe, target: issue, project: project) }
let!(:second_todo) { create(:todo, :mentioned, user: john_doe, target: another_issue, project: project) }
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
it 'returns the ids of those still pending' do
2017-09-10 17:25:29 +05:30
described_class.new.mark_pending_todos_as_done(issue, john_doe)
2016-09-29 09:46:39 +05:30
2017-09-10 17:25:29 +05:30
expect(described_class.new.mark_todos_as_done(Todo.all, john_doe)).to eq([second_todo.id])
2016-09-29 09:46:39 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns an empty array if all are done' do
2017-09-10 17:25:29 +05:30
described_class.new.mark_pending_todos_as_done(issue, john_doe)
described_class.new.mark_pending_todos_as_done(another_issue, john_doe)
2016-09-29 09:46:39 +05:30
2017-09-10 17:25:29 +05:30
expect(described_class.new.mark_todos_as_done(Todo.all, john_doe)).to eq([])
2016-09-29 09:46:39 +05:30
end
end
2017-09-10 17:25:29 +05:30
end
describe '#mark_todos_as_done_by_ids' do
let(:issue) { create(:issue, project: project, author: author, assignees: [john_doe]) }
let(:another_issue) { create(:issue, project: project, author: author, assignees: [john_doe]) }
it 'marks an array of todo ids as done' do
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
another_todo = create(:todo, :mentioned, user: john_doe, target: another_issue, project: project)
expect { described_class.new.mark_todos_as_done_by_ids([todo.id, another_todo.id], john_doe) }
.to change { john_doe.todos.done.count }.from(0).to(2)
end
it 'marks a single todo id as done' do
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
expect { described_class.new.mark_todos_as_done_by_ids(todo.id, john_doe) }
.to change { todo.reload.state }.from('pending').to('done')
end
2016-09-29 09:46:39 +05:30
2017-09-10 17:25:29 +05:30
it 'caches the number of todos of a user', :use_clean_rails_memory_store_caching do
2016-09-13 17:45:13 +05:30
create(:todo, :mentioned, user: john_doe, target: issue, project: project)
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
2017-09-10 17:25:29 +05:30
described_class.new.mark_todos_as_done_by_ids(todo, john_doe)
2016-09-13 17:45:13 +05:30
2018-03-27 19:54:05 +05:30
# Make sure no TodosFinder is inialized to perform counting
expect(TodosFinder).not_to receive(:new)
2016-09-13 17:45:13 +05:30
expect(john_doe.todos_done_count).to eq(1)
expect(john_doe.todos_pending_count).to eq(1)
end
end
2016-04-02 18:10:28 +05:30
def should_create_todo(attributes = {})
attributes.reverse_merge!(
project: project,
author: author,
state: :pending
)
expect(Todo.where(attributes).count).to eq 1
end
def should_not_create_todo(attributes = {})
attributes.reverse_merge!(
project: project,
author: author,
state: :pending
)
expect(Todo.where(attributes).count).to eq 0
end
def should_not_create_any_todo
expect { yield }.not_to change(Todo, :count)
end
end