summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/user_access_snippet_spec.rb
blob: 916e920e2acbbf853a1b427c8bfebd1ce371214b (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
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
140
141
142
143
144
145
146
147
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::UserAccessSnippet do
  subject(:access) { described_class.new(user, snippet: snippet) }

  let_it_be(:project) { create(:project, :private) }
  let_it_be(:snippet) { create(:project_snippet, :private, project: project) }
  let_it_be(:migration_bot) { User.migration_bot }

  let(:user) { create(:user) }

  describe '#can_do_action?' do
    before do
      allow(Ability).to receive(:allowed?).and_call_original
      allow(Ability).to receive(:allowed?).with(user, :ability, snippet).and_return(:foo)
    end

    context 'when can access_git' do
      it 'calls Ability#allowed? and returns its result' do
        expect(access.can_do_action?(:ability)).to eq(:foo)
      end
    end

    context 'when can not access_git' do
      it 'disallows access' do
        expect(Ability).to receive(:allowed?).with(user, :access_git, :global).and_return(false)

        expect(access.can_do_action?(:ability)).to eq(false)
      end
    end

    context 'when user is nil' do
      let(:user) { nil }

      it 'disallows access' do
        expect(access.can_do_action?(:ability)).to eq(false)
      end
    end

    context 'when user is migration bot' do
      let(:user) { migration_bot }

      it 'allows access' do
        expect(access.can_do_action?(:ability)).to eq(true)
      end
    end
  end

  describe '#can_push_to_branch?' do
    include UserHelpers

    [:anonymous, :non_member, :guest, :reporter, :maintainer, :admin, :author].each do |membership|
      context membership.to_s do
        let(:user) do
          membership == :author ? snippet.author : create_user_from_membership(project, membership)
        end

        context 'when can access_git' do
          it 'respects accessibility' do
            expected_result = Ability.allowed?(user, :update_snippet, snippet)

            expect(access.can_push_to_branch?('random_branch')).to eq(expected_result)
          end
        end

        context 'when can not access_git' do
          it 'disallows access' do
            expect(Ability).to receive(:allowed?).with(user, :access_git, :global).and_return(false) if user

            expect(access.can_push_to_branch?('random_branch')).to eq(false)
          end
        end
      end
    end

    context 'when user is migration bot' do
      let(:user) { migration_bot }

      it 'allows access' do
        allow(Ability).to receive(:allowed?).and_return(false)

        expect(access.can_push_to_branch?('random_branch')).to eq(true)
      end
    end

    context 'when snippet is nil' do
      let(:user) { create_user_from_membership(project, :admin) }
      let(:snippet) { nil }

      it 'disallows access' do
        expect(access.can_push_to_branch?('random_branch')).to eq(false)
      end

      context 'when user is migration bot' do
        let(:user) { migration_bot }

        it 'disallows access' do
          expect(access.can_push_to_branch?('random_branch')).to eq(false)
        end
      end
    end
  end

  describe '#can_create_tag?' do
    it 'returns false' do
      expect(access.can_create_tag?('random_tag')).to be_falsey
    end

    context 'when user is migration bot' do
      let(:user) { migration_bot }

      it 'returns false' do
        expect(access.can_create_tag?('random_tag')).to be_falsey
      end
    end
  end

  describe '#can_delete_branch?' do
    it 'returns false' do
      expect(access.can_delete_branch?('random_branch')).to be_falsey
    end

    context 'when user is migration bot' do
      let(:user) { migration_bot }

      it 'returns false' do
        expect(access.can_delete_branch?('random_branch')).to be_falsey
      end
    end
  end

  describe '#can_merge_to_branch?' do
    it 'returns false' do
      expect(access.can_merge_to_branch?('random_branch')).to be_falsey
    end

    context 'when user is migration bot' do
      let(:user) { migration_bot }

      it 'returns false' do
        expect(access.can_merge_to_branch?('random_branch')).to be_falsey
      end
    end
  end
end