diff options
Diffstat (limited to 'spec/services/incident_management/timeline_events')
3 files changed, 345 insertions, 0 deletions
diff --git a/spec/services/incident_management/timeline_events/create_service_spec.rb b/spec/services/incident_management/timeline_events/create_service_spec.rb new file mode 100644 index 00000000000..38ce15e74f1 --- /dev/null +++ b/spec/services/incident_management/timeline_events/create_service_spec.rb @@ -0,0 +1,117 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe IncidentManagement::TimelineEvents::CreateService do + let_it_be(:user_with_permissions) { create(:user) } + let_it_be(:user_without_permissions) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be_with_refind(:incident) { create(:incident, project: project) } + let_it_be(:comment) { create(:note, project: project, noteable: incident) } + + let(:args) do + { + note: 'note', + occurred_at: Time.current, + action: 'new comment', + promoted_from_note: comment + } + end + + let(:current_user) { user_with_permissions } + let(:service) { described_class.new(incident, current_user, args) } + + before_all do + project.add_developer(user_with_permissions) + project.add_reporter(user_without_permissions) + end + + describe '#execute' do + shared_examples 'error response' do |message| + it 'has an informative message' do + expect(execute).to be_error + expect(execute.message).to eq(message) + end + end + + shared_examples 'success response' do + it 'has timeline event', :aggregate_failures do + expect(execute).to be_success + + result = execute.payload[:timeline_event] + expect(result).to be_a(::IncidentManagement::TimelineEvent) + expect(result.author).to eq(current_user) + expect(result.incident).to eq(incident) + expect(result.project).to eq(project) + expect(result.note).to eq(args[:note]) + expect(result.promoted_from_note).to eq(comment) + end + end + + subject(:execute) { service.execute } + + context 'when current user is blank' do + let(:current_user) { nil } + + it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident' + end + + context 'when user does not have permissions to create timeline events' do + let(:current_user) { user_without_permissions } + + it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident' + end + + context 'when error occurs during creation' do + let(:args) { {} } + + it_behaves_like 'error response', "Occurred at can't be blank, Note can't be blank, and Note html can't be blank" + end + + context 'with default action' do + let(:args) { { note: 'note', occurred_at: Time.current, promoted_from_note: comment } } + + it_behaves_like 'success response' + + it 'matches the default action', :aggregate_failures do + result = execute.payload[:timeline_event] + + expect(result.action).to eq(IncidentManagement::TimelineEvents::DEFAULT_ACTION) + end + end + + context 'with non_default action' do + it_behaves_like 'success response' + + it 'matches the action from arguments', :aggregate_failures do + result = execute.payload[:timeline_event] + + expect(result.action).to eq(args[:action]) + end + end + + it 'successfully creates a database record', :aggregate_failures do + expect { execute }.to change { ::IncidentManagement::TimelineEvent.count }.by(1) + end + + context 'when incident_timeline feature flag is enabled' do + before do + stub_feature_flags(incident_timeline: project) + end + + it 'creates a system note' do + expect { execute }.to change { incident.notes.reload.count }.by(1) + end + end + + context 'when incident_timeline feature flag is disabled' do + before do + stub_feature_flags(incident_timeline: false) + end + + it 'does not create a system note' do + expect { execute }.not_to change { incident.notes.reload.count } + end + end + end +end diff --git a/spec/services/incident_management/timeline_events/destroy_service_spec.rb b/spec/services/incident_management/timeline_events/destroy_service_spec.rb new file mode 100644 index 00000000000..01daee2b749 --- /dev/null +++ b/spec/services/incident_management/timeline_events/destroy_service_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe IncidentManagement::TimelineEvents::DestroyService do + let_it_be(:user_with_permissions) { create(:user) } + let_it_be(:user_without_permissions) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be_with_refind(:incident) { create(:incident, project: project) } + + let!(:timeline_event) { create(:incident_management_timeline_event, incident: incident, project: project) } + let(:current_user) { user_with_permissions } + let(:params) { {} } + let(:service) { described_class.new(timeline_event, current_user) } + + before_all do + project.add_developer(user_with_permissions) + project.add_reporter(user_without_permissions) + end + + describe '#execute' do + shared_examples 'error response' do |message| + it 'has an informative message' do + expect(execute).to be_error + expect(execute.message).to eq(message) + end + end + + subject(:execute) { service.execute } + + context 'when current user is anonymous' do + let(:current_user) { nil } + + it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident' + end + + context 'when user does not have permissions to remove timeline events' do + let(:current_user) { user_without_permissions } + + it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident' + end + + context 'when an error occurs during removal' do + before do + allow(timeline_event).to receive(:destroy).and_return(false) + timeline_event.errors.add(:note, 'cannot be removed') + end + + it_behaves_like 'error response', 'Note cannot be removed' + end + + it 'successfully returns the timeline event', :aggregate_failures do + expect(execute).to be_success + + result = execute.payload[:timeline_event] + expect(result).to be_a(::IncidentManagement::TimelineEvent) + expect(result.id).to eq(timeline_event.id) + end + + context 'when incident_timeline feature flag is enabled' do + before do + stub_feature_flags(incident_timeline: project) + end + + it 'creates a system note' do + expect { execute }.to change { incident.notes.reload.count }.by(1) + end + end + + context 'when incident_timeline feature flag is disabled' do + before do + stub_feature_flags(incident_timeline: false) + end + + it 'does not create a system note' do + expect { execute }.not_to change { incident.notes.reload.count } + end + end + end +end diff --git a/spec/services/incident_management/timeline_events/update_service_spec.rb b/spec/services/incident_management/timeline_events/update_service_spec.rb new file mode 100644 index 00000000000..8bc0e5ce0ed --- /dev/null +++ b/spec/services/incident_management/timeline_events/update_service_spec.rb @@ -0,0 +1,148 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe IncidentManagement::TimelineEvents::UpdateService do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:incident) { create(:incident, project: project) } + + let!(:timeline_event) { create(:incident_management_timeline_event, project: project, incident: incident) } + let(:occurred_at) { 1.minute.ago } + let(:params) { { note: 'Updated note', occurred_at: occurred_at } } + + before do + stub_feature_flags(incident_timeline: project) + end + + describe '#execute' do + shared_examples 'successful response' do + it 'responds with success', :aggregate_failures do + expect(execute).to be_success + expect(execute.payload).to eq(timeline_event: timeline_event.reload) + end + end + + shared_examples 'error response' do |message| + it 'has an informative message' do + expect(execute).to be_error + expect(execute.message).to eq(message) + end + end + + shared_examples 'passing the correct was_changed value' do |was_changed| + it 'passes the correct was_changed value into SysteNoteService.edit_timeline_event' do + expect(SystemNoteService) + .to receive(:edit_timeline_event) + .with(timeline_event, user, was_changed: was_changed) + .and_call_original + + execute + end + end + + subject(:execute) { described_class.new(timeline_event, user, params).execute } + + context 'when user has permissions' do + before do + project.add_developer(user) + end + + it_behaves_like 'successful response' + + it 'updates attributes' do + expect { execute }.to change { timeline_event.note }.to(params[:note]) + .and change { timeline_event.occurred_at }.to(params[:occurred_at]) + end + + it 'creates a system note' do + expect { execute }.to change { incident.notes.reload.count }.by(1) + end + + it_behaves_like 'passing the correct was_changed value', :occurred_at_and_note + + context 'when incident_timeline feature flag is disabled' do + before do + stub_feature_flags(incident_timeline: false) + end + + it 'does not add a system note' do + expect { execute }.not_to change { incident.notes } + end + end + + context 'when note is nil' do + let(:params) { { occurred_at: occurred_at } } + + it_behaves_like 'successful response' + it_behaves_like 'passing the correct was_changed value', :occurred_at + + it 'does not update the note' do + expect { execute }.not_to change { timeline_event.reload.note } + end + + it 'updates occurred_at' do + expect { execute }.to change { timeline_event.occurred_at }.to(params[:occurred_at]) + end + end + + context 'when note is blank' do + let(:params) { { note: '', occurred_at: occurred_at } } + + it_behaves_like 'successful response' + it_behaves_like 'passing the correct was_changed value', :occurred_at + + it 'does not update the note' do + expect { execute }.not_to change { timeline_event.reload.note } + end + + it 'updates occurred_at' do + expect { execute }.to change { timeline_event.occurred_at }.to(params[:occurred_at]) + end + end + + context 'when occurred_at is nil' do + let(:params) { { note: 'Updated note' } } + + it_behaves_like 'successful response' + it_behaves_like 'passing the correct was_changed value', :note + + it 'updates the note' do + expect { execute }.to change { timeline_event.note }.to(params[:note]) + end + + it 'does not update occurred_at' do + expect { execute }.not_to change { timeline_event.reload.occurred_at } + end + end + + context 'when both occurred_at and note is nil' do + let(:params) { {} } + + it_behaves_like 'successful response' + + it 'does not update the note' do + expect { execute }.not_to change { timeline_event.note } + end + + it 'does not update occurred_at' do + expect { execute }.not_to change { timeline_event.reload.occurred_at } + end + + it 'does not call SysteNoteService.edit_timeline_event' do + expect(SystemNoteService).not_to receive(:edit_timeline_event) + + execute + end + end + end + + context 'when user does not have permissions' do + before do + project.add_reporter(user) + end + + it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident' + end + end +end |