summaryrefslogtreecommitdiff
path: root/spec/controllers/concerns/checks_collaboration_spec.rb
blob: d7f110e11f3bad732aa8f14cb71d621af60d8eaf (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
require 'spec_helper'

describe ChecksCollaboration do
  include ProjectForksHelper

  let(:helper) do
    fake_class = Class.new(ApplicationController) do
      include ChecksCollaboration
    end

    fake_class.new
  end

  describe '#can_collaborate_with_project?' do
    let(:user) { create(:user) }
    let(:project) { create(:project, :public) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
      allow(helper).to receive(:can?) do |user, ability, subject|
        Ability.allowed?(user, ability, subject)
      end
    end

    it 'is true if the user can push to the project' do
      project.add_developer(user)

      expect(helper.can_collaborate_with_project?(project)).to be_truthy
    end

    it 'is true when the user can push to a branch of the project' do
      fake_access = double('Gitlab::UserAccess')
      expect(fake_access).to receive(:can_push_to_branch?).with('a-branch').and_return(true)
      expect(Gitlab::UserAccess).to receive(:new).with(user, project: project).and_return(fake_access)

      expect(helper.can_collaborate_with_project?(project, ref: 'a-branch')).to be_truthy
    end

    context 'when the user has forked the project' do
      before do
        fork_project(project, user, namespace: user.namespace)
      end

      it 'is true' do
        expect(helper.can_collaborate_with_project?(project)).to be_truthy
      end

      it 'is false when the project is archived' do
        project.archived = true

        expect(helper.can_collaborate_with_project?(project)).to be_falsy
      end
    end
  end
end