1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
# frozen_string_literal: true
RSpec.shared_examples 'issuable supports timelog creation service' do
let_it_be(:time_spent) { 3600 }
let_it_be(:spent_at) { Time.now }
let_it_be(:summary) { "Test summary" }
let(:service) { described_class.new(issuable, time_spent, spent_at, summary, user) }
shared_examples 'success_response' do
it 'successfully saves the timelog' do
expect(Projects::TriggeredHooks).to receive(:new).with(
issuable.is_a?(Issue) ? :issue_hooks : :merge_request_hooks,
a_hash_including(changes: a_hash_including(total_time_spent: { previous: 0, current: time_spent }))
).and_call_original
is_expected.to be_success
timelog = subject.payload[:timelog]
expect(timelog).to be_persisted
expect(timelog.time_spent).to eq(time_spent)
expect(timelog.spent_at).to eq(spent_at)
expect(timelog.summary).to eq(summary)
expect(timelog.issuable).to eq(issuable)
end
end
context 'when the user does not have permission' do
let(:user) { create(:user) }
it 'returns an error' do
is_expected.to be_error
expect(subject.message).to eq(
"#{issuable.base_class_name} doesn't exist or you don't have permission to add timelog to it.")
expect(subject.http_status).to eq(404)
end
end
context 'when the user has permissions' do
let(:user) { author }
before do
users_container.add_reporter(user)
end
context 'when spent_at is in the future' do
let_it_be(:spent_at) { Time.now + 2.hours }
it 'returns an error' do
is_expected.to be_error
expect(subject.message).to eq("Spent at can't be a future date and time.")
expect(subject.http_status).to eq(404)
end
end
context 'when time_spent is zero' do
let_it_be(:time_spent) { 0 }
it 'returns an error' do
is_expected.to be_error
expect(subject.message).to eq("Time spent can't be zero.")
expect(subject.http_status).to eq(404)
end
end
context 'when time_spent is nil' do
let_it_be(:time_spent) { nil }
it 'returns an error' do
is_expected.to be_error
expect(subject.message).to eq("Time spent can't be blank")
expect(subject.http_status).to eq(404)
end
end
context 'when the timelog save fails' do
before do
allow_next_instance_of(Timelog) do |timelog|
allow(timelog).to receive(:save).and_return(false)
end
end
it 'returns an error' do
is_expected.to be_error
expect(subject.message).to eq('Failed to save timelog')
end
end
context 'when the creation completes successfully' do
it_behaves_like 'success_response'
end
end
end
RSpec.shared_examples 'issuable does not support timelog creation service' do
let_it_be(:time_spent) { 3600 }
let_it_be(:spent_at) { Time.now }
let_it_be(:summary) { "Test summary" }
let(:service) { described_class.new(issuable, time_spent, spent_at, summary, user) }
shared_examples 'error_response' do
it 'returns an error' do
is_expected.to be_error
issuable_type = if issuable.nil?
'Issuable'
else
issuable.base_class_name
end
expect(subject.message).to eq(
"#{issuable_type} doesn't exist or you don't have permission to add timelog to it."
)
expect(subject.http_status).to eq(404)
end
end
context 'when the user does not have permission' do
let(:user) { create(:user) }
it_behaves_like 'error_response'
end
context 'when the user has permissions' do
let(:user) { author }
before do
users_container.add_reporter(user)
end
it_behaves_like 'error_response'
end
end
|