debian-mirror-gitlab/spec/services/issuable/bulk_update_service_spec.rb

325 lines
11 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Issuable::BulkUpdateService do
2016-09-13 17:45:13 +05:30
let(:user) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, namespace: user.namespace) }
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
def bulk_update(issuables, extra_params = {})
2016-09-13 17:45:13 +05:30
bulk_update_params = extra_params
2017-08-17 22:00:37 +05:30
.reverse_merge(issuable_ids: Array(issuables).map(&:id).join(','))
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
type = Array(issuables).first.model_name.param_key
Issuable::BulkUpdateService.new(project, user, bulk_update_params).execute(type)
2016-09-13 17:45:13 +05:30
end
describe 'close issues' do
let(:issues) { create_list(:issue, 2, project: project) }
2015-04-26 12:48:37 +05:30
it 'succeeds and returns the correct number of issues updated' do
2016-09-13 17:45:13 +05:30
result = bulk_update(issues, state_event: 'close')
2015-04-26 12:48:37 +05:30
expect(result[:success]).to be_truthy
expect(result[:count]).to eq(issues.count)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
it 'closes all the issues passed' do
2016-09-13 17:45:13 +05:30
bulk_update(issues, state_event: 'close')
expect(project.issues.opened).to be_empty
expect(project.issues.closed).not_to be_empty
end
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
describe 'reopen issues' do
let(:issues) { create_list(:closed_issue, 2, project: project) }
2015-04-26 12:48:37 +05:30
it 'succeeds and returns the correct number of issues updated' do
2016-09-13 17:45:13 +05:30
result = bulk_update(issues, state_event: 'reopen')
2015-04-26 12:48:37 +05:30
expect(result[:success]).to be_truthy
expect(result[:count]).to eq(issues.count)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
it 'reopens all the issues passed' do
2016-09-13 17:45:13 +05:30
bulk_update(issues, state_event: 'reopen')
expect(project.issues.closed).to be_empty
expect(project.issues.opened).not_to be_empty
end
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
describe 'updating merge request assignee' do
let(:merge_request) { create(:merge_request, target_project: project, source_project: project, assignee: user) }
2015-04-26 12:48:37 +05:30
context 'when the new assignee ID is a valid user' do
it 'succeeds' do
2017-08-17 22:00:37 +05:30
new_assignee = create(:user)
2018-03-17 18:26:18 +05:30
project.add_developer(new_assignee)
2017-08-17 22:00:37 +05:30
result = bulk_update(merge_request, assignee_id: new_assignee.id)
2016-09-13 17:45:13 +05:30
expect(result[:success]).to be_truthy
expect(result[:count]).to eq(1)
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
it 'updates the assignee to the user ID passed' do
2016-09-13 17:45:13 +05:30
assignee = create(:user)
2018-03-17 18:26:18 +05:30
project.add_developer(assignee)
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
expect { bulk_update(merge_request, assignee_id: assignee.id) }
.to change { merge_request.reload.assignee }.from(user).to(assignee)
end
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
context "when the new assignee ID is #{IssuableFinder::NONE}" do
it 'unassigns the issues' do
2017-08-17 22:00:37 +05:30
expect { bulk_update(merge_request, assignee_id: IssuableFinder::NONE) }
.to change { merge_request.reload.assignee }.to(nil)
end
end
context 'when the new assignee ID is not present' do
it 'does not unassign' do
expect { bulk_update(merge_request, assignee_id: nil) }
.not_to change { merge_request.reload.assignee }
end
end
end
describe 'updating issue assignee' do
let(:issue) { create(:issue, project: project, assignees: [user]) }
context 'when the new assignee ID is a valid user' do
it 'succeeds' do
new_assignee = create(:user)
2018-03-17 18:26:18 +05:30
project.add_developer(new_assignee)
2017-08-17 22:00:37 +05:30
result = bulk_update(issue, assignee_ids: [new_assignee.id])
expect(result[:success]).to be_truthy
expect(result[:count]).to eq(1)
end
it 'updates the assignee to the user ID passed' do
assignee = create(:user)
2018-03-17 18:26:18 +05:30
project.add_developer(assignee)
2017-08-17 22:00:37 +05:30
expect { bulk_update(issue, assignee_ids: [assignee.id]) }
.to change { issue.reload.assignees.first }.from(user).to(assignee)
end
end
context "when the new assignee ID is #{IssuableFinder::NONE}" do
it "unassigns the issues" do
expect { bulk_update(issue, assignee_ids: [IssuableFinder::NONE.to_s]) }
.to change { issue.reload.assignees.count }.from(1).to(0)
end
2015-04-26 12:48:37 +05:30
end
context 'when the new assignee ID is not present' do
it 'does not unassign' do
2017-08-17 22:00:37 +05:30
expect { bulk_update(issue, assignee_ids: []) }
2018-03-17 18:26:18 +05:30
.not_to change { issue.reload.assignees }
end
2015-04-26 12:48:37 +05:30
end
end
describe 'updating milestones' do
2016-09-13 17:45:13 +05:30
let(:issue) { create(:issue, project: project) }
let(:milestone) { create(:milestone, project: project) }
2015-04-26 12:48:37 +05:30
it 'succeeds' do
2016-09-13 17:45:13 +05:30
result = bulk_update(issue, milestone_id: milestone.id)
2015-04-26 12:48:37 +05:30
expect(result[:success]).to be_truthy
expect(result[:count]).to eq(1)
end
2015-04-26 12:48:37 +05:30
it 'updates the issue milestone' do
2016-09-13 17:45:13 +05:30
expect { bulk_update(issue, milestone_id: milestone.id) }
.to change { issue.reload.milestone }.from(nil).to(milestone)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
end
describe 'updating labels' do
def create_issue_with_labels(labels)
2016-09-13 17:45:13 +05:30
create(:labeled_issue, project: project, labels: labels)
end
let(:bug) { create(:label, project: project) }
let(:regression) { create(:label, project: project) }
let(:merge_requests) { create(:label, project: project) }
let(:issue_all_labels) { create_issue_with_labels([bug, regression, merge_requests]) }
let(:issue_bug_and_regression) { create_issue_with_labels([bug, regression]) }
let(:issue_bug_and_merge_requests) { create_issue_with_labels([bug, merge_requests]) }
let(:issue_no_labels) { create(:issue, project: project) }
let(:issues) { [issue_all_labels, issue_bug_and_regression, issue_bug_and_merge_requests, issue_no_labels] }
let(:labels) { [] }
let(:add_labels) { [] }
let(:remove_labels) { [] }
2016-09-13 17:45:13 +05:30
let(:bulk_update_params) do
{
2016-09-13 17:45:13 +05:30
label_ids: labels.map(&:id),
add_label_ids: add_labels.map(&:id),
2017-09-10 17:25:29 +05:30
remove_label_ids: remove_labels.map(&:id)
}
end
2016-09-13 17:45:13 +05:30
before do
bulk_update(issues, bulk_update_params)
end
context 'when label_ids are passed' do
let(:issues) { [issue_all_labels, issue_no_labels] }
let(:labels) { [bug, regression] }
it 'updates the labels of all issues passed to the labels passed' do
2017-08-17 22:00:37 +05:30
expect(issues.map(&:reload).map(&:label_ids)).to all(match_array(labels.map(&:id)))
end
it 'does not update issues not passed in' do
expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
end
context 'when those label IDs are empty' do
let(:labels) { [] }
it 'updates the issues passed to have no labels' do
expect(issues.map(&:reload).map(&:label_ids)).to all(be_empty)
end
end
end
context 'when add_label_ids are passed' do
let(:issues) { [issue_all_labels, issue_bug_and_merge_requests, issue_no_labels] }
let(:add_labels) { [bug, regression, merge_requests] }
it 'adds those label IDs to all issues passed' do
expect(issues.map(&:reload).map(&:label_ids)).to all(include(*add_labels.map(&:id)))
end
it 'does not update issues not passed in' do
expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
end
end
context 'when remove_label_ids are passed' do
let(:issues) { [issue_all_labels, issue_bug_and_merge_requests, issue_no_labels] }
let(:remove_labels) { [bug, regression, merge_requests] }
it 'removes those label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids)).to all(be_empty)
end
it 'does not update issues not passed in' do
expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
end
end
context 'when add_label_ids and remove_label_ids are passed' do
let(:issues) { [issue_all_labels, issue_bug_and_merge_requests, issue_no_labels] }
let(:add_labels) { [bug] }
let(:remove_labels) { [merge_requests] }
it 'adds the label IDs to all issues passed' do
expect(issues.map(&:reload).map(&:label_ids)).to all(include(bug.id))
end
it 'removes the label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id)
end
it 'does not update issues not passed in' do
expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
end
end
context 'when add_label_ids and label_ids are passed' do
let(:issues) { [issue_all_labels, issue_bug_and_regression, issue_bug_and_merge_requests] }
let(:labels) { [merge_requests] }
let(:add_labels) { [regression] }
it 'adds the label IDs to all issues passed' do
expect(issues.map(&:reload).map(&:label_ids)).to all(include(regression.id))
end
it 'ignores the label IDs parameter' do
expect(issues.map(&:reload).map(&:label_ids)).to all(include(bug.id))
end
it 'does not update issues not passed in' do
expect(issue_no_labels.label_ids).to be_empty
end
end
context 'when remove_label_ids and label_ids are passed' do
let(:issues) { [issue_no_labels, issue_bug_and_regression] }
let(:labels) { [merge_requests] }
let(:remove_labels) { [regression] }
2016-09-13 17:45:13 +05:30
it 'removes the label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(regression.id)
end
it 'ignores the label IDs parameter' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id)
end
it 'does not update issues not passed in' do
expect(issue_all_labels.label_ids).to contain_exactly(bug.id, regression.id, merge_requests.id)
end
end
context 'when add_label_ids, remove_label_ids, and label_ids are passed' do
let(:issues) { [issue_bug_and_merge_requests, issue_no_labels] }
let(:labels) { [regression] }
let(:add_labels) { [bug] }
let(:remove_labels) { [merge_requests] }
it 'adds the label IDs to all issues passed' do
expect(issues.map(&:reload).map(&:label_ids)).to all(include(bug.id))
end
it 'removes the label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id)
end
it 'ignores the label IDs parameter' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(regression.id)
end
it 'does not update issues not passed in' do
expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
end
end
end
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
describe 'subscribe to issues' do
let(:issues) { create_list(:issue, 2, project: project) }
2016-08-24 12:49:21 +05:30
it 'subscribes the given user' do
2016-09-13 17:45:13 +05:30
bulk_update(issues, subscription_event: 'subscribe')
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
expect(issues).to all(be_subscribed(user, project))
2016-08-24 12:49:21 +05:30
end
2016-09-13 17:45:13 +05:30
end
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
describe 'unsubscribe from issues' do
let(:issues) do
create_list(:closed_issue, 2, project: project) do |issue|
2017-08-17 22:00:37 +05:30
issue.subscriptions.create(user: user, project: project, subscribed: true)
2016-08-24 12:49:21 +05:30
end
end
it 'unsubscribes the given user' do
2016-09-13 17:45:13 +05:30
bulk_update(issues, subscription_event: 'unsubscribe')
2016-08-24 12:49:21 +05:30
issues.each do |issue|
2017-08-17 22:00:37 +05:30
expect(issue).not_to be_subscribed(user, project)
2016-08-24 12:49:21 +05:30
end
end
end
2015-04-26 12:48:37 +05:30
end