summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/repository/protected_branches_spec.rb
blob: 29ea2e69ec78847582cca2e966c831cbd0433f39 (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
module QA
  describe 'branch protection support', :core do
    let(:branch_name) { 'protected-branch' }
    let(:commit_message) { 'Protected push commit message' }
    let(:project) do
      Factory::Resource::Project.fabricate! do |resource|
        resource.name = 'protected-branch-project'
      end
    end

    before do
      Runtime::Browser.visit(:gitlab, Page::Main::Login)
      Page::Main::Login.act { sign_in_using_credentials }
    end

    after do |example|
      # We need to clear localStorage because we're using it for the dropdown,
      # and capybara doesn't do this for us.
      # https://github.com/teamcapybara/capybara/issues/1702
      Capybara.execute_script 'localStorage.clear()'

      # In order to help diagnose a false failure
      # https://gitlab.com/gitlab-org/gitlab-ce/issues/48241
      log_push_output if example.exception
    end

    context 'when developers and maintainers are allowed to push to a protected branch' do
      let!(:protected_branch) { create_protected_branch(allow_to_push: true) }

      it 'user with push rights successfully pushes to the protected branch' do
        expect(protected_branch.name).to have_content(branch_name)
        expect(protected_branch.push_allowance).to have_content('Developers + Maintainers')

        @push = push_new_file(branch_name)

        expect(@push.output).to match(/remote: To create a merge request for protected-branch, visit/)
      end
    end

    context 'when developers and maintainers are not allowed to push to a protected branch' do
      it 'user without push rights fails to push to the protected branch' do
        create_protected_branch(allow_to_push: false)

        @push = push_new_file(branch_name)

        expect(@push.output)
          .to match(/remote\: GitLab\: You are not allowed to push code to protected branches on this project/)
        expect(@push.output)
          .to match(/\[remote rejected\] #{branch_name} -> #{branch_name} \(pre-receive hook declined\)/)
      end
    end

    def create_protected_branch(allow_to_push:)
      Factory::Resource::Branch.fabricate! do |resource|
        resource.branch_name = branch_name
        resource.project = project
        resource.allow_to_push = allow_to_push
        resource.protected = true
      end
    end

    def push_new_file(branch)
      Factory::Repository::ProjectPush.fabricate! do |resource|
        resource.project = project
        resource.file_name = 'new_file.md'
        resource.file_content = '# This is a new file'
        resource.commit_message = 'Add new_file.md'
        resource.branch_name = branch_name
        resource.new_branch = false
      end
    end

    def log_push_output
      if defined?(@push)
        filename = File.join('tmp', "push-output-#{project.name}")
        puts "Exception detected. Push output will be saved to #{filename}"
        IO.binwrite(filename, @push.output)
      end
    end
  end
end