diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-14 03:17:46 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-14 03:17:46 +0300 |
commit | 270353e1ff556a43333f82f171c3a485958126f0 (patch) | |
tree | c7bb4ac335b1e101b9bf92905ec2e8e170c6696c /spec/services/work_items | |
parent | b2e3da6a38f143a8c782dae4baceae3ed764733d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services/work_items')
-rw-r--r-- | spec/services/work_items/parent_links/create_service_spec.rb | 54 | ||||
-rw-r--r-- | spec/services/work_items/parent_links/destroy_service_spec.rb | 36 |
2 files changed, 84 insertions, 6 deletions
diff --git a/spec/services/work_items/parent_links/create_service_spec.rb b/spec/services/work_items/parent_links/create_service_spec.rb index 4f2be7111ac..41ae6398614 100644 --- a/spec/services/work_items/parent_links/create_service_spec.rb +++ b/spec/services/work_items/parent_links/create_service_spec.rb @@ -118,26 +118,74 @@ RSpec.describe WorkItems::ParentLinks::CreateService, feature_category: :portfol expect(subject[:created_references].map(&:work_item_id)).to match_array([task1.id, task2.id]) end - it 'creates notes', :aggregate_failures do - subject + it 'creates notes and records the events', :aggregate_failures do + expect { subject }.to change(WorkItems::ResourceLinkEvent, :count).by(2) work_item_notes = work_item.notes.last(2) + resource_link_events = WorkItems::ResourceLinkEvent.last(2) expect(work_item_notes.first.note).to eq("added #{task1.to_reference} as child task") expect(work_item_notes.last.note).to eq("added #{task2.to_reference} as child task") expect(task1.notes.last.note).to eq("added #{work_item.to_reference} as parent issue") expect(task2.notes.last.note).to eq("added #{work_item.to_reference} as parent issue") + expect(resource_link_events.first).to have_attributes( + user_id: user.id, + issue_id: work_item.id, + child_work_item_id: task1.id, + action: "add", + system_note_metadata_id: task1.notes.last.system_note_metadata.id + ) + expect(resource_link_events.last).to have_attributes( + user_id: user.id, + issue_id: work_item.id, + child_work_item_id: task2.id, + action: "add", + system_note_metadata_id: task2.notes.last.system_note_metadata.id + ) + end + + context 'when note creation fails for some reason' do + let(:params) { { issuable_references: [task1] } } + + [Note.new, nil].each do |relate_child_note| + it 'still records the link event', :aggregate_failures do + allow_next_instance_of(WorkItems::ParentLinks::CreateService) do |instance| + allow(instance).to receive(:create_notes).and_return(relate_child_note) + end + + expect { subject } + .to change(WorkItems::ResourceLinkEvent, :count).by(1) + .and not_change(Note, :count) + + expect(WorkItems::ResourceLinkEvent.last).to have_attributes( + user_id: user.id, + issue_id: work_item.id, + child_work_item_id: task1.id, + action: "add", + system_note_metadata_id: nil + ) + end + end end context 'when task is already assigned' do let(:params) { { issuable_references: [task, task2] } } it 'creates links only for non related tasks', :aggregate_failures do - expect { subject }.to change(parent_link_class, :count).by(1) + expect { subject } + .to change(parent_link_class, :count).by(1) + .and change(WorkItems::ResourceLinkEvent, :count).by(1) expect(subject[:created_references].map(&:work_item_id)).to match_array([task2.id]) expect(work_item.notes.last.note).to eq("added #{task2.to_reference} as child task") expect(task2.notes.last.note).to eq("added #{work_item.to_reference} as parent issue") expect(task.notes).to be_empty + expect(WorkItems::ResourceLinkEvent.last).to have_attributes( + user_id: user.id, + issue_id: work_item.id, + child_work_item_id: task2.id, + action: "add", + system_note_metadata_id: task2.notes.last.system_note_metadata.id + ) end end diff --git a/spec/services/work_items/parent_links/destroy_service_spec.rb b/spec/services/work_items/parent_links/destroy_service_spec.rb index c77546f6ca1..7e2e3949b73 100644 --- a/spec/services/work_items/parent_links/destroy_service_spec.rb +++ b/spec/services/work_items/parent_links/destroy_service_spec.rb @@ -24,23 +24,53 @@ RSpec.describe WorkItems::ParentLinks::DestroyService, feature_category: :team_p let(:user) { reporter } it 'removes relation and creates notes', :aggregate_failures do - expect { subject }.to change(parent_link_class, :count).by(-1) + expect { subject } + .to change(parent_link_class, :count).by(-1) + .and change(WorkItems::ResourceLinkEvent, :count).by(1) expect(work_item.notes.last.note).to eq("removed child task #{task.to_reference}") expect(task.notes.last.note).to eq("removed parent issue #{work_item.to_reference}") + expect(WorkItems::ResourceLinkEvent.last).to have_attributes( + user_id: user.id, + issue_id: work_item.id, + child_work_item_id: task.id, + action: "remove", + system_note_metadata_id: task.notes.last.system_note_metadata.id + ) end it 'returns success message' do is_expected.to eq(message: 'Relation was removed', status: :success) end + + context 'when note creation fails for some reason' do + [Note.new, nil].each do |unrelate_child_note| + it 'still records the link event', :aggregate_failures do + allow(SystemNoteService).to receive(:unrelate_work_item).and_return(unrelate_child_note) + + expect { subject } + .to change(WorkItems::ResourceLinkEvent, :count).by(1) + .and not_change(Note, :count) + + expect(WorkItems::ResourceLinkEvent.last).to have_attributes( + user_id: user.id, + issue_id: work_item.id, + child_work_item_id: task.id, + action: "remove", + system_note_metadata_id: nil + ) + end + end + end end context 'when user has insufficient permissions' do let(:user) { guest } it 'does not remove relation', :aggregate_failures do - expect { subject }.not_to change(parent_link_class, :count).from(1) - + expect { subject } + .to not_change(parent_link_class, :count).from(1) + .and not_change(WorkItems::ResourceLinkEvent, :count) expect(SystemNoteService).not_to receive(:unrelate_work_item) end |