debian-mirror-gitlab/spec/services/timelogs/delete_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
1.9 KiB
Ruby
Raw Normal View History

2022-07-16 23:28:13 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Timelogs::DeleteService, feature_category: :team_planning do
2022-07-16 23:28:13 +05:30
let_it_be(:author) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let_it_be(:issue) { create(:issue, project: project) }
2022-08-27 11:52:29 +05:30
let_it_be(:timelog) { create(:timelog, user: author, issue: issue, time_spent: 1800) }
2022-07-16 23:28:13 +05:30
let(:service) { described_class.new(timelog, user) }
describe '#execute' do
subject { service.execute }
context 'when the timelog exists' do
let(:user) { author }
it 'removes the timelog' do
expect { subject }.to change { Timelog.count }.by(-1)
end
it 'returns the removed timelog' do
2022-08-27 11:52:29 +05:30
is_expected.to be_success
expect(subject.payload[:timelog]).to eq(timelog)
2022-07-16 23:28:13 +05:30
end
end
context 'when the timelog does not exist' do
let(:user) { create(:user) }
let!(:timelog) { nil }
it 'returns an error' do
2022-08-27 11:52:29 +05:30
is_expected.to be_error
2022-07-16 23:28:13 +05:30
expect(subject.message).to eq('Timelog doesn\'t exist or you don\'t have permission to delete it')
expect(subject.http_status).to eq(404)
end
end
context 'when the user does not have permission' do
let(:user) { create(:user) }
it 'returns an error' do
2022-08-27 11:52:29 +05:30
is_expected.to be_error
2022-07-16 23:28:13 +05:30
expect(subject.message).to eq('Timelog doesn\'t exist or you don\'t have permission to delete it')
expect(subject.http_status).to eq(404)
end
end
context 'when the timelog deletion fails' do
let(:user) { author }
2022-08-27 11:52:29 +05:30
let!(:timelog) { create(:timelog, user: author, issue: issue, time_spent: 1800) }
2022-07-16 23:28:13 +05:30
before do
allow(timelog).to receive(:destroy).and_return(false)
end
it 'returns an error' do
2022-08-27 11:52:29 +05:30
is_expected.to be_error
2022-07-16 23:28:13 +05:30
expect(subject.message).to eq('Failed to remove timelog')
expect(subject.http_status).to eq(400)
end
end
end
end