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

push_check_spec.rb « checks « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 262438256b4eef04bf3ad4c1698b1f4c4227568f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Checks::PushCheck do
  include_context 'change access checks context'

  describe '#validate!' do
    it 'does not raise any error' do
      expect { subject.validate! }.not_to raise_error
    end

    context 'when the user is not allowed to push to the repo' do
      it 'raises an error' do
        expect(user_access).to receive(:can_do_action?).with(:push_code).and_return(false)
        expect(project).to receive(:branch_allows_collaboration?).with(user_access.user, 'master').and_return(false)

        expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, 'You are not allowed to push code to this project.')
      end
    end

    context 'when using a DeployKeyAccess instance' do
      let(:deploy_key) { create(:deploy_key) }
      let(:user_access) { Gitlab::DeployKeyAccess.new(deploy_key, container: project) }

      context 'when the deploy key cannot push to the targetted branch' do
        it 'raises an error' do
          allow(user_access).to receive(:can_push_to_branch?).and_return(false)

          expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, 'You are not allowed to push code to this project.')
        end
      end

      context 'when the deploy key can push to the targetted branch' do
        it 'is valid' do
          allow(user_access).to receive(:can_push_to_branch?).and_return(true)

          expect { subject.validate! }.not_to raise_error
        end
      end
    end
  end
end