summaryrefslogtreecommitdiff
path: root/spec/policies/timelog_policy_spec.rb
blob: 97e61cfe5cea546154d0f98e28b9f7a8712613f3 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe TimelogPolicy, models: true 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(:user) { nil }
  let(:policy) { described_class.new(user, timelog) }

  describe '#rules' do
    context 'when user is anonymus' do
      it 'prevents adimistration of timelog' do
        expect(policy).to be_disallowed(:admin_timelog)
      end
    end

    context 'when user is the author of the timelog' do
      let(:user) { author }

      it 'allows adimistration of timelog' do
        expect(policy).to be_allowed(:admin_timelog)
      end
    end

    context 'when user is not the author of the timelog but maintainer of the project' do
      let(:user) { create(:user) }

      before do
        project.add_maintainer(user)
      end

      it 'allows adimistration of timelog' do
        expect(policy).to be_allowed(:admin_timelog)
      end
    end

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

      it 'allows adimistration of timelog' do
        expect(policy).to be_allowed(:admin_timelog)
      end
    end

    context 'when user is not the author of the timelog nor a maintainer of the project nor an administrator' do
      let(:user) { create(:user) }

      it 'prevents adimistration of timelog' do
        expect(policy).to be_disallowed(:admin_timelog)
      end
    end
  end
end