summaryrefslogtreecommitdiff
path: root/spec/services/labels/promote_service_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/services/labels/promote_service_spec.rb')
-rw-r--r--spec/services/labels/promote_service_spec.rb206
1 files changed, 108 insertions, 98 deletions
diff --git a/spec/services/labels/promote_service_spec.rb b/spec/services/labels/promote_service_spec.rb
index 7674ec36331..15d53857f33 100644
--- a/spec/services/labels/promote_service_spec.rb
+++ b/spec/services/labels/promote_service_spec.rb
@@ -63,139 +63,149 @@ RSpec.describe Labels::PromoteService do
expect(service.execute(group_label)).to be_falsey
end
- it 'is truthy on success' do
- expect(service.execute(project_label_1_1)).to be_truthy
- end
+ shared_examples 'promoting a project label to a group label' do
+ it 'is truthy on success' do
+ expect(service.execute(project_label_1_1)).to be_truthy
+ end
- it 'recreates the label as a group label' do
- expect { service.execute(project_label_1_1) }
- .to change(project_1.labels, :count).by(-1)
- .and change(group_1.labels, :count).by(1)
- expect(new_label).not_to be_nil
- end
+ it 'removes all project labels with that title within the group' do
+ expect { service.execute(project_label_1_1) }.to change(project_2.labels, :count).by(-1).and \
+ change(project_3.labels, :count).by(-1)
+ end
- it 'copies title, description and color' do
- service.execute(project_label_1_1)
+ it 'keeps users\' subscriptions' do
+ user2 = create(:user)
+ project_label_1_1.subscriptions.create!(user: user, subscribed: true)
+ project_label_2_1.subscriptions.create!(user: user, subscribed: true)
+ project_label_3_2.subscriptions.create!(user: user, subscribed: true)
+ project_label_2_1.subscriptions.create!(user: user2, subscribed: true)
- expect(new_label.title).to eq(promoted_label_name)
- expect(new_label.description).to eq(promoted_description)
- expect(new_label.color).to eq(promoted_color)
- end
+ expect { service.execute(project_label_1_1) }.to change { Subscription.count }.from(4).to(3)
- it 'merges labels with the same name in group' do
- expect { service.execute(project_label_1_1) }.to change(project_2.labels, :count).by(-1).and \
- change(project_3.labels, :count).by(-1)
- end
-
- it 'keeps users\' subscriptions' do
- user2 = create(:user)
- project_label_1_1.subscriptions.create(user: user, subscribed: true)
- project_label_2_1.subscriptions.create(user: user, subscribed: true)
- project_label_3_2.subscriptions.create(user: user, subscribed: true)
- project_label_2_1.subscriptions.create(user: user2, subscribed: true)
+ expect(new_label.subscribed?(user)).to be_truthy
+ expect(new_label.subscribed?(user2)).to be_truthy
+ end
- expect { service.execute(project_label_1_1) }.to change { Subscription.count }.from(4).to(3)
+ it 'recreates priorities' do
+ service.execute(project_label_1_1)
- expect(new_label.subscribed?(user)).to be_truthy
- expect(new_label.subscribed?(user2)).to be_truthy
- end
+ expect(new_label.priority(project_1)).to be_nil
+ expect(new_label.priority(project_2)).to eq(label_2_1_priority)
+ expect(new_label.priority(project_3)).to eq(label_3_1_priority)
+ end
- it 'recreates priorities' do
- service.execute(project_label_1_1)
+ it 'does not touch project out of promoted group' do
+ service.execute(project_label_1_1)
+ project_4_new_label = project_4.labels.find_by(title: promoted_label_name)
- expect(new_label.priority(project_1)).to be_nil
- expect(new_label.priority(project_2)).to eq(label_2_1_priority)
- expect(new_label.priority(project_3)).to eq(label_3_1_priority)
- end
+ expect(project_4_new_label).not_to be_nil
+ expect(project_4_new_label.id).to eq(project_label_4_1.id)
+ end
- it 'does not touch project out of promoted group' do
- service.execute(project_label_1_1)
- project_4_new_label = project_4.labels.find_by(title: promoted_label_name)
+ it 'does not touch out of group priority' do
+ service.execute(project_label_1_1)
- expect(project_4_new_label).not_to be_nil
- expect(project_4_new_label.id).to eq(project_label_4_1.id)
- end
+ expect(new_label.priority(project_4)).to be_nil
+ end
- it 'does not touch out of group priority' do
- service.execute(project_label_1_1)
+ it 'relinks issue with the promoted label' do
+ service.execute(project_label_1_1)
+ issue_label = issue_1_1.labels.find_by(title: promoted_label_name)
- expect(new_label.priority(project_4)).to be_nil
- end
+ expect(issue_label).not_to be_nil
+ expect(issue_label.id).to eq(new_label.id)
+ end
- it 'relinks issue with the promoted label' do
- service.execute(project_label_1_1)
- issue_label = issue_1_1.labels.find_by(title: promoted_label_name)
+ it 'does not remove untouched labels from issue' do
+ expect { service.execute(project_label_1_1) }.not_to change(issue_1_1.labels, :count)
+ end
- expect(issue_label).not_to be_nil
- expect(issue_label.id).to eq(new_label.id)
- end
+ it 'does not relink untouched label in issue' do
+ service.execute(project_label_1_1)
+ issue_label = issue_1_2.labels.find_by(title: untouched_label_name)
- it 'does not remove untouched labels from issue' do
- expect { service.execute(project_label_1_1) }.not_to change(issue_1_1.labels, :count)
- end
+ expect(issue_label).not_to be_nil
+ expect(issue_label.id).to eq(project_label_1_2.id)
+ end
- it 'does not relink untouched label in issue' do
- service.execute(project_label_1_1)
- issue_label = issue_1_2.labels.find_by(title: untouched_label_name)
+ it 'relinks issues with merged labels' do
+ service.execute(project_label_1_1)
+ issue_label = issue_2_1.labels.find_by(title: promoted_label_name)
- expect(issue_label).not_to be_nil
- expect(issue_label.id).to eq(project_label_1_2.id)
- end
+ expect(issue_label).not_to be_nil
+ expect(issue_label.id).to eq(new_label.id)
+ end
- it 'relinks issues with merged labels' do
- service.execute(project_label_1_1)
- issue_label = issue_2_1.labels.find_by(title: promoted_label_name)
+ it 'does not relink issues from other group' do
+ service.execute(project_label_1_1)
+ issue_label = issue_4_1.labels.find_by(title: promoted_label_name)
- expect(issue_label).not_to be_nil
- expect(issue_label.id).to eq(new_label.id)
- end
+ expect(issue_label).not_to be_nil
+ expect(issue_label.id).to eq(project_label_4_1.id)
+ end
- it 'does not relink issues from other group' do
- service.execute(project_label_1_1)
- issue_label = issue_4_1.labels.find_by(title: promoted_label_name)
+ it 'updates merge request' do
+ service.execute(project_label_1_1)
+ merge_label = merge_3_1.labels.find_by(title: promoted_label_name)
- expect(issue_label).not_to be_nil
- expect(issue_label.id).to eq(project_label_4_1.id)
- end
+ expect(merge_label).not_to be_nil
+ expect(merge_label.id).to eq(new_label.id)
+ end
- it 'updates merge request' do
- service.execute(project_label_1_1)
- merge_label = merge_3_1.labels.find_by(title: promoted_label_name)
+ it 'updates board lists' do
+ service.execute(project_label_1_1)
+ list = issue_board_2_1.lists.find_by(label: new_label)
- expect(merge_label).not_to be_nil
- expect(merge_label.id).to eq(new_label.id)
- end
+ expect(list).not_to be_nil
+ end
- it 'updates board lists' do
- service.execute(project_label_1_1)
- list = issue_board_2_1.lists.find_by(label: new_label)
+ # In case someone adds a new relation to Label.rb and forgets to relink it
+ # and the database doesn't have foreign key constraints
+ it 'relinks all relations' do
+ service.execute(project_label_1_1)
- expect(list).not_to be_nil
+ Label.reflect_on_all_associations.each do |association|
+ expect(project_label_1_1.send(association.name).any?).to be_falsey
+ end
+ end
end
- # In case someone adds a new relation to Label.rb and forgets to relink it
- # and the database doesn't have foreign key constraints
- it 'relinks all relations' do
- service.execute(project_label_1_1)
+ context 'if there is an existing identical group label' do
+ let!(:existing_group_label) { create(:group_label, group: group_1, title: project_label_1_1.title ) }
+
+ it 'uses the existing group label' do
+ expect { service.execute(project_label_1_1) }
+ .to change(project_1.labels, :count).by(-1)
+ .and not_change(group_1.labels, :count)
+ expect(new_label).not_to be_nil
+ end
- Label.reflect_on_all_associations.each do |association|
- expect(project_label_1_1.send(association.name).any?).to be_falsey
+ it 'does not create a new group label clone' do
+ expect { service.execute(project_label_1_1) }.not_to change { GroupLabel.maximum(:id) }
end
+
+ it_behaves_like 'promoting a project label to a group label'
end
- context 'with invalid group label' do
- before do
- allow(service).to receive(:clone_label_to_group_label).and_wrap_original do |m, *args|
- label = m.call(*args)
- allow(label).to receive(:valid?).and_return(false)
+ context 'if there is no existing identical group label' do
+ let(:existing_group_label) { nil }
- label
- end
+ it 'recreates the label as a group label' do
+ expect { service.execute(project_label_1_1) }
+ .to change(project_1.labels, :count).by(-1)
+ .and change(group_1.labels, :count).by(1)
+ expect(new_label).not_to be_nil
end
- it 'raises an exception' do
- expect { service.execute(project_label_1_1) }.to raise_error(ActiveRecord::RecordInvalid)
+ it 'copies title, description and color to cloned group label' do
+ service.execute(project_label_1_1)
+
+ expect(new_label.title).to eq(promoted_label_name)
+ expect(new_label.description).to eq(promoted_description)
+ expect(new_label.color).to eq(promoted_color)
end
+
+ it_behaves_like 'promoting a project label to a group label'
end
end
end