user_access_spec.rb 9.19 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::UserAccess do
4 5
  include ProjectForksHelper

6
  let(:access) { described_class.new(user, project: project) }
7
  let(:project) { create(:project, :repository) }
8 9
  let(:user) { create(:user) }

10
  describe '#can_push_to_branch?' do
11 12
    describe 'push to none protected branch' do
      it 'returns true if user is a master' do
13
        project.add_master(user)
14

15 16 17 18
        expect(access.can_push_to_branch?('random_branch')).to be_truthy
      end

      it 'returns true if user is a developer' do
19
        project.add_developer(user)
20

21 22 23 24
        expect(access.can_push_to_branch?('random_branch')).to be_truthy
      end

      it 'returns false if user is a reporter' do
25
        project.add_reporter(user)
26

27 28 29 30
        expect(access.can_push_to_branch?('random_branch')).to be_falsey
      end
    end

31 32
    describe 'push to empty project' do
      let(:empty_project) { create(:project_empty_repo) }
33
      let(:project_access) { described_class.new(user, project: empty_project) }
34

35 36 37 38 39 40
      it 'returns true for admins' do
        user.update!(admin: true)

        expect(access.can_push_to_branch?('master')).to be_truthy
      end

41
      it 'returns true if user is master' do
42
        empty_project.add_master(user)
43 44 45 46 47

        expect(project_access.can_push_to_branch?('master')).to be_truthy
      end

      it 'returns false if user is developer and project is fully protected' do
48
        empty_project.add_developer(user)
49 50 51 52 53 54
        stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_FULL)

        expect(project_access.can_push_to_branch?('master')).to be_falsey
      end

      it 'returns false if user is developer and it is not allowed to push new commits but can merge into branch' do
55
        empty_project.add_developer(user)
56 57 58 59 60 61
        stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_DEV_CAN_MERGE)

        expect(project_access.can_push_to_branch?('master')).to be_falsey
      end

      it 'returns true if user is developer and project is unprotected' do
62
        empty_project.add_developer(user)
63 64 65 66 67 68
        stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_NONE)

        expect(project_access.can_push_to_branch?('master')).to be_truthy
      end

      it 'returns true if user is developer and project grants developers permission' do
69
        empty_project.add_developer(user)
70 71 72 73 74 75
        stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_DEV_CAN_PUSH)

        expect(project_access.can_push_to_branch?('master')).to be_truthy
      end
    end

76
    describe 'push to protected branch' do
77 78
      let(:branch) { create :protected_branch, project: project, name: "test" }
      let(:not_existing_branch) { create :protected_branch, :developers_can_merge, project: project }
79

80 81 82 83 84 85
      it 'returns true for admins' do
        user.update!(admin: true)

        expect(access.can_push_to_branch?(branch.name)).to be_truthy
      end

86
      it 'returns true if user is a master' do
87
        project.add_master(user)
88

89 90 91 92
        expect(access.can_push_to_branch?(branch.name)).to be_truthy
      end

      it 'returns false if user is a developer' do
93
        project.add_developer(user)
94

95 96 97 98
        expect(access.can_push_to_branch?(branch.name)).to be_falsey
      end

      it 'returns false if user is a reporter' do
99
        project.add_reporter(user)
100

101 102
        expect(access.can_push_to_branch?(branch.name)).to be_falsey
      end
103

104
      it 'returns false if branch does not exist' do
105
        project.add_developer(user)
106

107
        expect(access.can_push_to_branch?(not_existing_branch.name)).to be_falsey
108
      end
109 110 111 112
    end

    describe 'push to protected branch if allowed for developers' do
      before do
113
        @branch = create :protected_branch, :developers_can_push, project: project
114 115 116
      end

      it 'returns true if user is a master' do
117
        project.add_master(user)
118

119 120 121 122
        expect(access.can_push_to_branch?(@branch.name)).to be_truthy
      end

      it 'returns true if user is a developer' do
123
        project.add_developer(user)
124

125 126 127 128
        expect(access.can_push_to_branch?(@branch.name)).to be_truthy
      end

      it 'returns false if user is a reporter' do
129
        project.add_reporter(user)
130

131 132 133 134
        expect(access.can_push_to_branch?(@branch.name)).to be_falsey
      end
    end

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    describe 'allowing pushes to maintainers of forked projects' do
      let(:canonical_project) { create(:project, :public, :repository) }
      let(:project) { fork_project(canonical_project, create(:user), repository: true) }

      before do
        create(
          :merge_request,
          target_project: canonical_project,
          source_project: project,
          source_branch: 'awesome-feature',
          allow_maintainer_to_push: true
        )
      end

      it 'allows users that have push access to the canonical project to push to the MR branch' do
        canonical_project.add_developer(user)

        expect(access.can_push_to_branch?('awesome-feature')).to be_truthy
      end

      it 'does not allow the user to push to other branches' do
        canonical_project.add_developer(user)

        expect(access.can_push_to_branch?('master')).to be_falsey
      end

      it 'does not allow the user to push if he does not have push access to the canonical project' do
        canonical_project.add_guest(user)

        expect(access.can_push_to_branch?('awesome-feature')).to be_falsey
      end
    end

168 169
    describe 'merge to protected branch if allowed for developers' do
      before do
170
        @branch = create :protected_branch, :developers_can_merge, project: project
171 172 173
      end

      it 'returns true if user is a master' do
174
        project.add_master(user)
175

176 177 178 179
        expect(access.can_merge_to_branch?(@branch.name)).to be_truthy
      end

      it 'returns true if user is a developer' do
180
        project.add_developer(user)
181

182 183 184 185
        expect(access.can_merge_to_branch?(@branch.name)).to be_truthy
      end

      it 'returns false if user is a reporter' do
186
        project.add_reporter(user)
187

188 189 190 191
        expect(access.can_merge_to_branch?(@branch.name)).to be_falsey
      end
    end
  end
192

193
  describe '#can_create_tag?' do
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    describe 'push to none protected tag' do
      it 'returns true if user is a master' do
        project.add_user(user, :master)

        expect(access.can_create_tag?('random_tag')).to be_truthy
      end

      it 'returns true if user is a developer' do
        project.add_user(user, :developer)

        expect(access.can_create_tag?('random_tag')).to be_truthy
      end

      it 'returns false if user is a reporter' do
        project.add_user(user, :reporter)

        expect(access.can_create_tag?('random_tag')).to be_falsey
      end
    end

    describe 'push to protected tag' do
      let(:tag) { create(:protected_tag, project: project, name: "test") }
      let(:not_existing_tag) { create :protected_tag, project: project }

      it 'returns true if user is a master' do
        project.add_user(user, :master)

        expect(access.can_create_tag?(tag.name)).to be_truthy
      end

      it 'returns false if user is a developer' do
        project.add_user(user, :developer)

        expect(access.can_create_tag?(tag.name)).to be_falsey
      end

      it 'returns false if user is a reporter' do
        project.add_user(user, :reporter)

        expect(access.can_create_tag?(tag.name)).to be_falsey
      end
    end

    describe 'push to protected tag if allowed for developers' do
      before do
239
        @tag = create(:protected_tag, :developers_can_create, project: project)
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
      end

      it 'returns true if user is a master' do
        project.add_user(user, :master)

        expect(access.can_create_tag?(@tag.name)).to be_truthy
      end

      it 'returns true if user is a developer' do
        project.add_user(user, :developer)

        expect(access.can_create_tag?(@tag.name)).to be_truthy
      end

      it 'returns false if user is a reporter' do
        project.add_user(user, :reporter)

        expect(access.can_create_tag?(@tag.name)).to be_falsey
      end
    end
  end
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

  describe '#can_delete_branch?' do
    describe 'delete unprotected branch' do
      it 'returns true if user is a master' do
        project.add_user(user, :master)

        expect(access.can_delete_branch?('random_branch')).to be_truthy
      end

      it 'returns true if user is a developer' do
        project.add_user(user, :developer)

        expect(access.can_delete_branch?('random_branch')).to be_truthy
      end

      it 'returns false if user is a reporter' do
        project.add_user(user, :reporter)

        expect(access.can_delete_branch?('random_branch')).to be_falsey
      end
    end

    describe 'delete protected branch' do
      let(:branch) { create(:protected_branch, project: project, name: "test") }

      it 'returns true if user is a master' do
        project.add_user(user, :master)

        expect(access.can_delete_branch?(branch.name)).to be_truthy
      end

      it 'returns false if user is a developer' do
        project.add_user(user, :developer)

        expect(access.can_delete_branch?(branch.name)).to be_falsey
      end

      it 'returns false if user is a reporter' do
        project.add_user(user, :reporter)

        expect(access.can_delete_branch?(branch.name)).to be_falsey
      end
    end
  end
305
end