Commit 1a3c1a90 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Extract timeline event tests into shared examples

parent 72201bf6
...@@ -32,7 +32,7 @@ module Mutations ...@@ -32,7 +32,7 @@ module Mutations
end end
def authorize!(object) def authorize!(object)
raise_noteable_not_incident! if object && !object.incident? raise_noteable_not_incident! if object && !object.try(:incident?)
super super
end end
......
...@@ -19,44 +19,32 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::Create do ...@@ -19,44 +19,32 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::Create do
subject(:resolve) { mutation_for(project, current_user).resolve(incident_id: incident.to_global_id, **args) } subject(:resolve) { mutation_for(project, current_user).resolve(incident_id: incident.to_global_id, **args) }
context 'when a user has permissions to create a timeline event' do context 'when a user has permissions to create a timeline event' do
let(:expected_timeline_event) do
instance_double(
'IncidentManagement::TimelineEvent',
note: args[:note],
occurred_at: args[:occurred_at].to_s,
incident: incident,
author: current_user,
promoted_from_note: nil
)
end
before do before do
project.add_developer(current_user) project.add_developer(current_user)
end end
context 'when TimelineEvents::CreateService responds with success' do it_behaves_like 'creating an incident timeline event'
it 'adds timeline event to database' do
expect { resolve }.to change(IncidentManagement::TimelineEvent, :count).by(1)
end
end
context 'when TimelineEvents::CreateService responds with an error' do context 'when TimelineEvents::CreateService responds with an error' do
let(:args) { {} } let(:args) { {} }
it 'returns errors' do it_behaves_like 'responding with an incident timeline errors',
expect(resolve).to eq(timeline_event: nil, errors: ["Occurred at can't be blank, Note can't be blank, and Note html can't be blank"]) errors: ["Occurred at can't be blank, Note can't be blank, and Note html can't be blank"]
end
end
end
context 'when a user has no permissions to create timeline event' do
before do
project.add_guest(current_user)
end
it 'raises an error' do
expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end end
end end
context 'when timeline event feature is not available' do it_behaves_like 'failing to create an incident timeline event'
before do
stub_licensed_features(incident_timeline_events: false)
end
it 'raises an error' do
expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
end end
private private
......
...@@ -9,6 +9,8 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do ...@@ -9,6 +9,8 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do
let_it_be(:comment) { create(:note, project: project, noteable: incident) } let_it_be(:comment) { create(:note, project: project, noteable: incident) }
let_it_be(:issue) { create(:issue, project: project) } let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:issue_comment) { create(:note, project: project, noteable: issue) } let_it_be(:issue_comment) { create(:note, project: project, noteable: issue) }
let_it_be(:alert) { create(:alert_management_alert, project: project) }
let_it_be(:alert_comment) { create(:note, project: project, noteable: alert) }
let(:args) { { note_id: comment.to_global_id.to_s } } let(:args) { { note_id: comment.to_global_id.to_s } }
...@@ -22,25 +24,22 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do ...@@ -22,25 +24,22 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do
subject(:resolve) { mutation_for(project, current_user).resolve(**args) } subject(:resolve) { mutation_for(project, current_user).resolve(**args) }
context 'when a user has permissions to create timeline event' do context 'when a user has permissions to create timeline event' do
before do let(:expected_timeline_event) do
project.add_developer(current_user) instance_double(
'IncidentManagement::TimelineEvent',
note: comment.note,
occurred_at: comment.created_at.to_s,
incident: incident,
author: current_user,
promoted_from_note: comment
)
end end
it 'creates a timeline event' do before do
expect { resolve }.to change(IncidentManagement::TimelineEvent, :count).by(1) project.add_developer(current_user)
end end
it 'responds with a timeline event', :aggregate_failures do it_behaves_like 'creating an incident timeline event'
response = resolve
timeline_event = IncidentManagement::TimelineEvent.last!
expect(response).to match(timeline_event: timeline_event, errors: be_empty)
expect(timeline_event.promoted_from_note).to eq(comment)
expect(timeline_event.note).to eq(comment.note)
expect(timeline_event.occurred_at.to_s).to eq(comment.created_at.to_s)
expect(timeline_event.incident).to eq(incident)
expect(timeline_event.author).to eq(current_user)
end
context 'when TimelineEvents::CreateService responds with an error' do context 'when TimelineEvents::CreateService responds with an error' do
before do before do
...@@ -51,9 +50,7 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do ...@@ -51,9 +50,7 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do
end end
end end
it 'returns errors' do it_behaves_like 'responding with an incident timeline errors', errors: ['Some error']
expect(resolve).to eq(timeline_event: nil, errors: ['Some error'])
end
end end
end end
...@@ -73,25 +70,15 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do ...@@ -73,25 +70,15 @@ RSpec.describe Mutations::IncidentManagement::TimelineEvent::PromoteFromNote do
end end
end end
context 'when a user does not have permissions to create timeline event' do context 'when note belongs to anything else but issuable' do
before do let(:args) { { note_id: alert_comment.to_global_id.to_s } }
project.add_guest(current_user)
end
it 'raises an error' do it 'raises an error' do
expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end end
end end
context 'when timeline event feature is not available' do it_behaves_like 'failing to create an incident timeline event'
before do
stub_licensed_features(incident_timeline_events: false)
end
it 'raises an error' do
expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
end end
private private
......
# frozen_string_literal: true
require 'spec_helper'
# Requres:
# * subject with a 'resolve' name
# * Defined expected timeline event via `let(:expected_timeline_event) { instance_double(...) }`
RSpec.shared_examples 'creating an incident timeline event' do
it 'creates a timeline event' do
expect { resolve }.to change(IncidentManagement::TimelineEvent, :count).by(1)
end
it 'responds with a timeline event', :aggregate_failures do
response = resolve
timeline_event = IncidentManagement::TimelineEvent.last!
expect(response).to match(timeline_event: timeline_event, errors: be_empty)
expect(timeline_event.promoted_from_note).to eq(expected_timeline_event.promoted_from_note)
expect(timeline_event.note).to eq(expected_timeline_event.note)
expect(timeline_event.occurred_at.to_s).to eq(expected_timeline_event.occurred_at)
expect(timeline_event.incident).to eq(expected_timeline_event.incident)
expect(timeline_event.author).to eq(expected_timeline_event.author)
end
end
# Requres
# * subject with a 'resolve' name
# * a user factory with a 'current_user' name
RSpec.shared_examples 'failing to create an incident timeline event' do
context 'when a user has no permissions to create timeline event' do
before do
project.add_guest(current_user)
end
it 'raises an error' do
expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
context 'when timeline event feature is not available' do
before do
stub_licensed_features(incident_timeline_events: false)
end
it 'raises an error' do
expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
end
# Requres:
# * subject with a 'resolve' name
RSpec.shared_examples 'responding with an incident timeline errors' do |errors:|
it 'returns errors' do
expect(resolve).to eq(timeline_event: nil, errors: errors)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment