2018-08-01 08:58:49 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe ResourceEvents::ChangeLabelsService do
|
2020-01-10 09:07:49 +00:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:author) { create(:user) }
|
2022-08-02 15:09:10 +00:00
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
|
|
|
let_it_be(:incident) { create(:incident, project: project) }
|
2021-06-29 03:07:32 +00:00
|
|
|
|
2022-08-02 15:09:10 +00:00
|
|
|
let(:resource) { issue }
|
2018-08-01 08:58:49 +00:00
|
|
|
|
2022-08-02 15:09:10 +00:00
|
|
|
describe '#execute' do
|
|
|
|
shared_examples 'creating timeline events' do
|
|
|
|
context 'when resource is not an incident' do
|
|
|
|
let(:resource) { issue }
|
|
|
|
|
|
|
|
it 'does not call create timeline events service' do
|
|
|
|
expect(IncidentManagement::TimelineEvents::CreateService).not_to receive(:change_labels)
|
|
|
|
|
|
|
|
change_labels
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when resource is an incident' do
|
|
|
|
let(:resource) { incident }
|
|
|
|
|
|
|
|
it 'calls create timeline events service with correct attributes' do
|
|
|
|
expect(IncidentManagement::TimelineEvents::CreateService)
|
|
|
|
.to receive(:change_labels)
|
|
|
|
.with(resource, author, added_labels: added, removed_labels: removed)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
change_labels
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:change_labels) do
|
|
|
|
described_class.new(resource, author).execute(added_labels: added, removed_labels: removed)
|
|
|
|
end
|
2018-08-01 08:58:49 +00:00
|
|
|
|
2020-12-10 12:09:43 +00:00
|
|
|
let_it_be(:labels) { create_list(:label, 2, project: project) }
|
2018-08-01 08:58:49 +00:00
|
|
|
|
|
|
|
def expect_label_event(event, label, action)
|
|
|
|
expect(event.user).to eq(author)
|
|
|
|
expect(event.label).to eq(label)
|
|
|
|
expect(event.action).to eq(action)
|
|
|
|
end
|
|
|
|
|
2018-09-07 13:39:20 +00:00
|
|
|
it 'expires resource note etag cache' do
|
2022-08-02 15:09:10 +00:00
|
|
|
expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(
|
|
|
|
"/#{resource.project.namespace.to_param}/#{resource.project.to_param}/noteable/issue/#{resource.id}/notes"
|
|
|
|
)
|
2018-09-07 13:39:20 +00:00
|
|
|
|
|
|
|
described_class.new(resource, author).execute(added_labels: [labels[0]])
|
|
|
|
end
|
|
|
|
|
2018-08-01 08:58:49 +00:00
|
|
|
context 'when adding a label' do
|
|
|
|
let(:added) { [labels[0]] }
|
|
|
|
let(:removed) { [] }
|
|
|
|
|
|
|
|
it 'creates new label event' do
|
2022-08-02 15:09:10 +00:00
|
|
|
expect { change_labels }.to change { resource.resource_label_events.count }.from(0).to(1)
|
2018-08-01 08:58:49 +00:00
|
|
|
|
|
|
|
expect_label_event(resource.resource_label_events.first, labels[0], 'add')
|
|
|
|
end
|
2022-08-02 15:09:10 +00:00
|
|
|
|
|
|
|
it_behaves_like 'creating timeline events'
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when removing a label' do
|
|
|
|
let(:added) { [] }
|
|
|
|
let(:removed) { [labels[1]] }
|
|
|
|
|
|
|
|
it 'creates new label event' do
|
2022-08-02 15:09:10 +00:00
|
|
|
expect { change_labels }.to change { resource.resource_label_events.count }.from(0).to(1)
|
2018-08-01 08:58:49 +00:00
|
|
|
|
|
|
|
expect_label_event(resource.resource_label_events.first, labels[1], 'remove')
|
|
|
|
end
|
2022-08-02 15:09:10 +00:00
|
|
|
|
|
|
|
it_behaves_like 'creating timeline events'
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when both adding and removing labels' do
|
|
|
|
let(:added) { [labels[0]] }
|
|
|
|
let(:removed) { [labels[1]] }
|
|
|
|
|
|
|
|
it 'creates all label events in a single query' do
|
2021-11-15 15:10:57 +00:00
|
|
|
expect(ApplicationRecord).to receive(:legacy_bulk_insert).once.and_call_original
|
2022-08-02 15:09:10 +00:00
|
|
|
expect { change_labels }.to change { resource.resource_label_events.count }.from(0).to(2)
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
2022-08-02 15:09:10 +00:00
|
|
|
|
|
|
|
it_behaves_like 'creating timeline events'
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
2020-12-10 12:09:43 +00:00
|
|
|
|
|
|
|
describe 'usage data' do
|
|
|
|
let(:added) { [labels[0]] }
|
|
|
|
let(:removed) { [labels[1]] }
|
|
|
|
|
|
|
|
context 'when resource is an issue' do
|
|
|
|
it 'tracks changed labels' do
|
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_label_changed_action)
|
|
|
|
|
2022-08-02 15:09:10 +00:00
|
|
|
change_labels
|
2020-12-10 12:09:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when resource is a merge request' do
|
|
|
|
let(:resource) { create(:merge_request, source_project: project) }
|
|
|
|
|
|
|
|
it 'does not track changed labels' do
|
2022-08-02 15:09:10 +00:00
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter)
|
|
|
|
.not_to receive(:track_issue_label_changed_action)
|
2020-12-10 12:09:43 +00:00
|
|
|
|
2022-08-02 15:09:10 +00:00
|
|
|
change_labels
|
2020-12-10 12:09:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
|
|
|
end
|