Welcome to mirror list, hosted at ThFree Co, Russian Federation.

delete_service_spec.rb « timelogs « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0543bafcecdfb024393b65d1928e37961230cf9 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Timelogs::DeleteService, feature_category: :team_planning do
  let_it_be(:author) { create(:user) }
  let_it_be(:project) { create(:project, :public) }
  let_it_be(:issue) { create(:issue, project: project) }
  let_it_be(:timelog) { create(:timelog, user: author, issue: issue, time_spent: 1800) }

  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
        is_expected.to be_success
        expect(subject.payload[:timelog]).to eq(timelog)
      end
    end

    context 'when the timelog does not exist' do
      let(:user) { create(:user) }
      let!(:timelog) { nil }

      it 'returns an error' do
        is_expected.to be_error
        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
        is_expected.to be_error
        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 }
      let!(:timelog) { create(:timelog, user: author, issue: issue, time_spent: 1800) }

      before do
        allow(timelog).to receive(:destroy).and_return(false)
      end

      it 'returns an error' do
        is_expected.to be_error
        expect(subject.message).to eq('Failed to remove timelog')
        expect(subject.http_status).to eq(400)
      end
    end
  end
end