Commit da8b6c25 authored by Sean Arnold's avatar Sean Arnold Committed by syasonik

Add read service and specs

Adjust factory
parent 326e793d
# frozen_string_literal: true
module IncidentManagement
module OncallShifts
class ReadService
# @param rotation [IncidentManagement::OncallRotation]
# @param current_user [User]
# @param params [Hash<Symbol,Any>]
# @option params - starts_at [Time]
# @option params - ends_at [Time]
def initialize(rotation, current_user, starts_at:, ends_at:)
@rotation = rotation
@current_user = current_user
@starts_at = starts_at
@ends_at = ends_at
end
def execute
return error_no_license unless available?
return error_no_permissions unless allowed?
success(
::IncidentManagement::OncallShiftGenerator
.new(rotation)
.for_timeframe(starts_at: starts_at, ends_at: ends_at)
)
end
private
attr_reader :rotation, :current_user, :starts_at, :ends_at
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(rotation.project)
end
def allowed?
Ability.allowed?(current_user, :read_incident_management_oncall_schedule, rotation)
end
def error(message)
ServiceResponse.error(message: message)
end
def success(shifts)
ServiceResponse.success(payload: { shifts: shifts })
end
def error_no_permissions
error(_('You have insufficient permissions to view shifts for this rotation'))
end
def error_no_license
error(_('Your license does not support on-call rotations'))
end
end
end
end
......@@ -7,5 +7,13 @@ FactoryBot.define do
starts_at { Time.current }
length { 5 }
length_unit { :days }
trait :with_participant do
after(:create) do |rotation|
user = create(:user)
rotation.project.add_reporter(user)
create(:incident_management_oncall_participant, rotation: rotation, user: user)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::IncidentManagement::OncallShifts::ReadService do
let_it_be_with_refind(:rotation) { create(:incident_management_oncall_rotation, :with_participant) }
let_it_be(:project) { rotation.project }
let_it_be(:user_with_permissions) { create(:user) }
let_it_be(:user_without_permissions) { create(:user) }
let_it_be(:current_user) { user_with_permissions }
let(:params) { { starts_at: rotation.starts_at + 15.minutes, ends_at: rotation.starts_at + 3.weeks } }
let(:service) { described_class.new(rotation, current_user, params) }
before_all do
project.add_reporter(user_with_permissions)
end
before do
stub_licensed_features(oncall_schedules: true)
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 the current_user is anonymous' do
let(:current_user) { nil }
it_behaves_like 'error response', 'You have insufficient permissions to view shifts for this rotation'
end
context 'when the current_user does not have permissions to create on-call schedules' do
let(:current_user) { user_without_permissions }
it_behaves_like 'error response', 'You have insufficient permissions to view shifts for this rotation'
end
context 'when feature is not available' do
before do
stub_licensed_features(oncall_schedules: false)
end
it_behaves_like 'error response', 'Your license does not support on-call rotations'
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(oncall_schedules_mvc: false)
end
it_behaves_like 'error response', 'Your license does not support on-call rotations'
end
context 'with valid params' do
it 'successfully returns a sorted collection of IncidentManagement::OncallShifts' do
expect(execute).to be_success
shifts = execute.payload[:shifts]
expect(shifts).to all( be_a(::IncidentManagement::OncallShift) )
expect(shifts).to all( be_valid )
expect(shifts.sort_by(&:starts_at)).to eq(shifts)
expect(shifts.first.starts_at).to be <= params[:starts_at]
expect(shifts.last.ends_at).to be >= params[:ends_at]
end
end
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