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

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

require 'spec_helper'

RSpec.describe Mutations::Timelogs::Delete do
  include GraphqlHelpers

  let_it_be(:author) { create(:user) }
  let_it_be(:maintainer) { create(:user) }
  let_it_be(:administrator) { create(:user, :admin) }
  let_it_be(:project) { create(:project, :public) }
  let_it_be(:issue) { create(:issue, project: project) }
  let_it_be_with_reload(:timelog) { create(:timelog, user: author, issue: issue, time_spent: 1800) }

  let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }
  let(:timelog_id) { global_id_of(timelog) }
  let(:mutation_arguments) { { id: timelog_id } }

  describe '#resolve' do
    subject(:resolve) do
      mutation.resolve(**mutation_arguments)
    end

    context 'when the timelog id is not valid' do
      let(:current_user) { author }
      let(:timelog_id) { global_id_of(model_name: 'Timelog', id: non_existing_record_id) }

      it 'raises Gitlab::Graphql::Errors::ResourceNotAvailable' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end

    context 'when the current user is not the timelog\'s author, not a maintainer and not an admin' do
      let(:current_user) { create(:user) }

      it 'raises Gitlab::Graphql::Errors::ResourceNotAvailable' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end

    context 'when the current user is the timelog\'s author' do
      let(:current_user) { author }

      it 'deletes the timelog' do
        expect { subject }.to change { Timelog.count }.by(-1)
      end

      it 'returns the deleted timelog' do
        expect(subject[:timelog]).to eq(timelog)
      end

      it 'returns no errors' do
        expect(subject[:errors]).to be_empty
      end
    end

    context 'when the current user is not the timelog\'s author but a maintainer of the project' do
      let(:current_user) { maintainer }

      before do
        project.add_maintainer(maintainer)
      end

      it 'deletes the timelog' do
        expect { subject }.to change { Timelog.count }.by(-1)
      end

      it 'returns the deleted timelog' do
        expect(subject[:timelog]).to eq(timelog)
      end

      it 'returns no errors' do
        expect(subject[:errors]).to be_empty
      end
    end

    context 'when the current user is not the timelog\'s author, not a maintainer but an admin', :enable_admin_mode do
      let(:current_user) { administrator }

      it 'deletes the timelog' do
        expect { subject }.to change { Timelog.count }.by(-1)
      end

      it 'returns the deleted timelog' do
        expect(subject[:timelog]).to eq(timelog)
      end

      it 'returns no errors' do
        expect(subject[:errors]).to be_empty
      end
    end
  end
end